diff --git a/config/config.json b/config/config.json new file mode 100644 index 0000000..2d6e484 --- /dev/null +++ b/config/config.json @@ -0,0 +1,45 @@ +{ + "buildCommand": "npx @11ty/eleventy", + "postTypes": [ + { + "name": "Now Burning", + "contentEnabled": true, + "frontMatter": [ + { + "name": "title" + }, + { + "name": "manufacturer" + }, + { + "name": "date" + }, + { + "name": "time" + } + ], + "postDirectory": "/home/n_u/Repos/nathanupchurch.com/content/now_burning/" + }, + { + "name": "Now Listening", + "contentEnabled": true, + "frontMatter": [ + { + "name": "title" + }, + { + "name": "artist" + }, + { + "name": "date" + }, + { + "name": "time" + } + ], + "postDirectory": "/home/n_u/Repos/nathanupchurch.com/content/now_listening/" + } + ], + "uploadCommand": "update-website", + "siteDirectory": "/home/n_u/Repos/nathanupchurch.com/" +} diff --git a/lib/ensure_dir_exists.rb b/lib/ensure_dir_exists.rb new file mode 100644 index 0000000..ddc49ac --- /dev/null +++ b/lib/ensure_dir_exists.rb @@ -0,0 +1,6 @@ +def ensure_dir_exists(directory_path) + unless Dir.exist?(directory_path) + FileUtils.mkdir_p(directory_path) + spawn_toast 'Directory Created', %(Poaster created #{directory_path}.), 10 + end +end diff --git a/lib/spawn_input_box.rb b/lib/spawn_input_box.rb new file mode 100644 index 0000000..421bd7f --- /dev/null +++ b/lib/spawn_input_box.rb @@ -0,0 +1,3 @@ +def spawn_input_box(title, text) + `kdialog --title "#{title}" --inputbox "#{text}"` +end diff --git a/lib/spawn_radio_list.rb b/lib/spawn_radio_list.rb new file mode 100644 index 0000000..3321079 --- /dev/null +++ b/lib/spawn_radio_list.rb @@ -0,0 +1,7 @@ +def spawn_radio_list(title, text, options_arr) + command = %(kdialog --title "#{title}" --radiolist "#{text}") + options_arr.each_with_index do |option, i| + command += %( #{i} "#{option}" off) + end + `#{command}` +end diff --git a/lib/spawn_toast.rb b/lib/spawn_toast.rb new file mode 100644 index 0000000..3479291 --- /dev/null +++ b/lib/spawn_toast.rb @@ -0,0 +1,3 @@ +def spawn_toast(title, text, seconds) + `kdialog --title "#{title}" --passivepopup "#{text}" #{seconds}` +end diff --git a/lib/write_file.rb b/lib/write_file.rb new file mode 100644 index 0000000..8e398ad --- /dev/null +++ b/lib/write_file.rb @@ -0,0 +1,5 @@ +def write_file(directory, name, extension, content) + post_file = File.new(%(#{directory}/#{name}.#{extension}), 'w+') + post_file.syswrite(content) + post_file.close +end diff --git a/poaster.rb b/poaster.rb index f03594e..3bd9659 100644 --- a/poaster.rb +++ b/poaster.rb @@ -1,16 +1,49 @@ require 'json' +require 'fileutils' +require './lib/spawn_input_box' +require './lib/spawn_radio_list' +require './lib/spawn_toast' +require './lib/ensure_dir_exists' +require './lib/write_file' -config_file = File.read('./poasterConfig.json') -config_data = JSON.parse(config_file) +config_data = JSON.parse(File.read('./config/config.json')) +dialog_title_prefix = 'Poaster' -post_type_radiolist_prefix = %Q[kdialog --title "Poaster" --radiolist "Select a post type:"] - -def build_radiolist_command(prefix_string, config_data) - command_string = prefix_string - config_data['postTypes'].each_with_index do |type, i| - command_string += %Q[ #{i+1} "#{type['name']}" off] - end - return command_string +# Populate types_arr with post types +post_types_arr = [] +config_data['postTypes'].each do |type| + post_types_arr.push(type['name']) end -`#{build_radiolist_command(post_type_radiolist_prefix, config_data)}` +# Display post list dialog to user +post_type = config_data['postTypes'][Integer(spawn_radio_list(dialog_title_prefix, 'Select a post type:', post_types_arr))] + +# Collect post ID from user +post_id = spawn_input_box %(#{dialog_title_prefix} - Enter Post ID), 'Enter post ID:' + +# Collect frontmatter from user +frontmatter = [] +post_type['frontMatter'].each do |item| + frontmatter.push({ item['name'] => spawn_input_box(%(#{dialog_title_prefix} - Enter Frontmatter'), %(Enter post #{item['name']}:)) }) +end + +# Collect post content from user +post_content = spawn_input_box %(#{dialog_title_prefix} - Enter Content), 'Enter post content:' + +# Make sure the output folder exists +post_directory = post_type['postDirectory'] +ensure_dir_exists(post_directory) + +# Create post string +post = '---' +frontmatter.each do |item| + post += %(#{item.keys[0]}: #{item[item.keys[0]]}) +end +post += %(---\n#{post_content}) + +# Write post string to file and notify user +post_file_name = %(#{post_type['name']}_#{post_id.chomp}) +post_extension = '.md' + +write_file post_directory, post_file_name, post_extension, post +spawn_toast 'File Created', %(Poaster created #{post_file_name}#{post_extension} at #{post_directory}.), 10 diff --git a/poasterConfig.json b/poasterConfig.json deleted file mode 100644 index 841d1a0..0000000 --- a/poasterConfig.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "buildCommand": "npx @11ty/eleventy", - "postTypes": [ - { - "name": "Now Burning", - "contentEnabled": true, - "frontMatter": [ - { - "name": "Title", - "allowEmpty": false, - "inputType": "text" - }, - { - "name": "Date", - "allowEmpty": false, - "inputType": "date" - } - ], - "titlePrefix": "Now Burning: ", - "titleSuffix": "" - } - ], - "uploadCommand": "update-website", - "workingDirectory": "/home/n_u/Repos/nathanupchurch.com/" -}