Class: Mkmatter::App::Classes::NewContent

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/mkmatter/cli/subs/new.rb

Overview

Generate ‘New’ Content

Constant Summary collapse

HILINE =
HighLine.new($stdin, $stderr, 40)

Instance Method Summary collapse

Instance Method Details

#pageObject



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
59
60
61
62
63
64
65
66
67
68
# File 'lib/mkmatter/cli/subs/new.rb', line 21

def page
  if options[:file]
    if Mkmatter::Methods.check_if_jekyll
      @questions = Mkmatter::Questions::Page.new(HILINE).ask
      answers    = Mkmatter::Answers.new(@questions, options.fetch(:publish, nil))
      if options.fetch(:index, nil)
        filename = 'index.' + answers.file_format.downcase
      else
        filename   = answers.title.to_slug + '.' + answers.file_format.downcase
      end

      path       = Pathname("./#{filename}").realdirpath
      if HILINE.agree('Would you like to put this page into a subdirectory?', true)
        HILINE.say("What path? (directories will be created if they don't exist) ")
        HILINE.say("Don't use a path starting with a slash, just put a relative path.")
        HILINE.say('good => path/to/dir ‖ bad => /root/paths/are/bad/mmkay')
        folder = HILINE.ask('? ') do |q|
          q.confirm  = true
          q.default  = '.'
          q.validate = /^[^\/].*$/
        end
        folder = Pathname(folder)
        begin
          FileUtils.mkdir_p(File.join(Mkmatter::Methods.get_jekyll_root, folder))
        rescue Errno::EEXIST
          HILINE.say("<%= Paint['Error', 'red', :bold] %>: Insufficient Permissions")
          exit 1
        end
        path = Pathname(folder).realdirpath.join(filename)
      end
      File.open(path.to_path, 'a') do |fd|
        answers.to_h = {:layout => 'page'}
        fd.puts answers.to_h.stringify_keys.to_yaml(indentation: 2)
        fd.puts '---'
      end
      Mkmatter::Methods.launch_editor(options[:editor], path)
    else
      $stderr.puts "Not in a Jekyll directory. (no '_config.yml' in any parent directory)"
      exit 1
    end
  else
    @questions = Mkmatter::Questions::Page.new(HILINE).ask
    answers = Mkmatter::Answers.new(@questions, options.fetch(:publish, nil))
    puts ''
    puts answers.to_h.stringify_keys.to_yaml(indentation: 2)
    puts '---'
  end
end

#postObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/mkmatter/cli/subs/new.rb', line 77

def post
  if options[:draft] and options[:file]
    if Mkmatter::Methods.check_if_jekyll
      @questions  = Mkmatter::Questions::Post.new(HILINE).ask
      answers     = Mkmatter::Answers.new(@questions, options[:publish])
      file_folder = '_drafts'
      filename    = [].concat([answers.slug_date, '-', answers.title.to_slug, '.', answers.file_format.downcase]).join

      path = Pathname("./#{file_folder}/#{filename}").realdirpath
      if HILINE.agree('Would you like to put this page into a subdirectory?', true)
        HILINE.say("What path? (directories will be created if they don't exist)")
        HILINE.say("Don't use a path starting with a slash, just put a relative path.")
        HILINE.say("<% Paint['Good', 'green', :bold] %>: path/to/dir ‖ <%= color('Bad', 'red', :bold) %>: /root/paths/are/bad/mmkay")
        folder = HILINE.ask('? ') do |q|
          q.confirm  = true
          q.default  = '.'
          q.validate = /^[^\/].*$/
        end
        folder = Pathname(folder)
        begin
          FileUtils.mkdir_p(File.join(Mkmatter::Methods.get_jekyll_root, folder))
        rescue Errno::EEXIST
          HILINE.say("<% Paint['Error', 'red', :bold] %>:Insufficient Permissions")
          exit 1
        end
        path = Pathname(folder).realdirpath.join(filename)
      end
      File.open(path.to_path, 'a') do |fd|
        answers.to_h = {:layout => 'post'}
        fd.puts answers.to_h.stringify_keys.to_yaml(indentation: 2)
        fd.puts '---'
      end
      Mkmatter::Methods.launch_editor(options[:editor], path)
    else
      puts "Not in a Jekyll directory. (no '_config.yml' in any parent directory)"
      exit 1
    end
  elsif options[:file] and options[:draft].nil? or options[:draft] == false

    if Mkmatter::Methods.check_if_jekyll
      @questions  = Mkmatter::Questions::Post.new(HILINE).ask
      answers     = Mkmatter::Answers.new(@questions, options[:publish])
      file_folder = '_posts'
      filename    = [].concat([answers.slug_date, '-', answers.title.to_slug, '.', answers.file_format.downcase]).join('')
      path        = Pathname("./#{file_folder}/#{filename}").realdirpath
      if HILINE.agree('Would you like to put this post into a subdirectory?', true)
        HILINE.say('What path?')
        HILINE.say('----------------')
        HILINE.say("Don't use a path starting with a slash, just put a relative path.")
        HILINE.say("If you enter a path you don't like, you will have manually remove it if you confirm it.")
        HILINE.say("<% Paint['Good', 'green', :bold] %>: path/to/dir ‖ <% Paint['Bad', :red, :bold] %>: /root/paths/are/bad/mmkay")
        folder = HILINE.ask('? ') do |q|
          q.confirm  = true
          q.default  = '.'
          q.validate = /^[^\/].*$/
        end
        folder = Pathname("#{file_folder}/#{folder}")
        begin
          FileUtils.mkdir_p(File.join(Mkmatter::Methods.get_jekyll_root, folder))
        rescue Errno::EEXIST
          HILINE.say("<% Paint['Error', 'red', :bold] %>:Insufficient Permissions")
          exit 1
        end
        path = Pathname(folder).realdirpath.join(filename)
      end
      File.open(path.to_path, 'a') do |fd|
        answers.to_h = {:layout => 'post'}
        fd.puts answers.to_h.stringify_keys.to_yaml(indentation: 2)
        fd.puts '---'
      end

      Mkmatter::Methods.launch_editor(options[:editor], path)
    else
      puts "Not in a Jekyll directory. (no '_config.yml' in any parent directory)"
      exit 1
    end
  elsif options[:draft].nil? and options[:file].nil?
    @questions = Mkmatter::Questions::Post.new(HILINE).ask
    answers    = Mkmatter::Answers.new(@questions, options[:publish])
    puts ''
    puts answers.to_h.stringify_keys.to_yaml(indentation: 2)
    puts '---'

  end
end