Class: BlogTools::Commands::Generate
- Inherits:
-
Thor
- Object
- Thor
- BlogTools::Commands::Generate
- Defined in:
- lib/blog-tools/commands/generate.rb
Overview
Generate handles all of the subcommands related to generating posts
Instance Method Summary collapse
Instance Method Details
#post(title) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/blog-tools/commands/generate.rb', line 17 def post(title) config = File.exist?(Storage::CONFIG_FILE) ? YAML.load_file(Storage::CONFIG_FILE) : {} template_file = [:template] || config['default_template'] || 'post.md' template_path = File.(File.join(Storage::TEMPLATES_DIR + template_file)) return puts("[!] Template file not found: #{template_path}") unless File.exist?(template_path) template = File.read(template_path) renderer = ERB.new(template) result = renderer.result_with_hash( title: title, date: Date.today.to_s, author: [:author] || config['author'] || ENV['USER'] || 'unknown', tags: [:tags] || config['tags'] || [], content: [:content] ? File.read(File.([:content])) : '' ) dir_path = "#{[:output]}/" output_filename = [:output] ? "#{dir_path}#{title}.md" : "#{title.downcase.strip.gsub(/\s+/, '_')}.md" File.write(output_filename, result) puts "[✓] Post generated at #{output_filename}" end |