Class: NewPage::CreateNewPage

Inherits:
Jekyll::Command
  • Object
show all
Defined in:
lib/jekyll_new_page.rb

Class Method Summary collapse

Class Method Details

.init_with_program(c) ⇒ Object



16
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jekyll_new_page.rb', line 16

def init_with_program(c)
  c.command("create-page".to_sym) do |prog|
    prog.syntax "create-page [options] <Template name>"
    prog.description "create a new page, based on a liquid template." 

    prog.option 'output', '-o', '--output PATH', 'Path where to create the new document.' do |out| 
      return Pathname.new(out)
    end

    prog.option 'path', '--path [PATH]', 'Path where to locate the template.'
    prog.option 'name', '-n', '--name NAME', 'Name of the document'
    prog.option 'dir', '-d', '--create-subdir', 'Create a subdir to store the new page'

    prog.action do |args, options|
      options.transform_keys! { |old_key| old_key.to_sym }

      raise ArgumentError("Need to specify a template name") if args.size > 1
      raise ArgumentError("No output path specified.") if options[:output].nil?
      raise ArgumentError("No page name specified.") if options[:name].nil?

      options[:dir] = false unless options.include? :dir

      [:output, :path].each do |key|
        options[key] = Pathname.new(options[key]) unless options[key].nil?
      end

      options[:template_name] = args[0] || '*'

      options[:history_file] = options[:output] / Pathname.new('.history.txt')
      context = InputableContext.new(outer_scope: {name: options[:name]}, reader: NewPage::ReadlineInputHandler, completion_handler: NewPage::HistoryHandler, **options)

      begin
        process = ProcessTemplate.new(context: context,  **options)

        process.render(context)

        exec('nvim', "#{process.output_file}")
      rescue ArgumentError => error
        print error.message
      end
    end
  end
end