Poaster/poaster.rb
2025-05-15 11:44:02 -05:00

50 lines
1.6 KiB
Ruby

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_data = JSON.parse(File.read('./config/config.json'))
dialog_title_prefix = 'Poaster'
# Populate types_arr with post types
post_types_arr = []
config_data['postTypes'].each do |type|
post_types_arr.push(type['name'])
end
# 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