Class: BlogTools::Commands::Generate

Inherits:
Thor
  • Object
show all
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 = options[:template] || config['default_template'] || 'post.md'
  template_path = File.expand_path(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: options[:author] || config['author'] || ENV['USER'] || 'unknown',
    tags: options[:tags] || config['tags'] || [],
    content: options[:content] ? File.read(File.expand_path(options[:content])) : ''
  )

  dir_path = "#{options[:output]}/"
  output_filename = options[:output] ? "#{dir_path}#{title}.md" : "#{title.downcase.strip.gsub(/\s+/, '_')}.md"
  File.write(output_filename, result)

  puts "[✓] Post generated at #{output_filename}"
end