Class: Mkmatter::Questions

Inherits:
Object
  • Object
show all
Defined in:
lib/mkmatter/questions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#answersObject (readonly)

Returns the value of attribute answers.



6
7
8
# File 'lib/mkmatter/questions.rb', line 6

def answers
  @answers
end

Instance Method Details

#ask(type, include_post_qs) ⇒ Object

def self.ask(cls)

known_questions = const_get(cls).methods.sort.delete_if { |m| m.to_s !~ /^get_.*$/ }
known_questions.each do |m|
  @answers[:layout] = cls.to_s.lower
  @answers[m.to_s.gsub(/^get_[0-9]{3}_/, "")] = method(m).call
end
@answers

end



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
# File 'lib/mkmatter/questions.rb', line 17

def ask(type, include_post_qs)
  known_questions = methods.sort.delete_if { |m| m.to_s !~ /^get_.*$/ }
  if type != "post"
    if include_post_qs
    else
      post_only_questions = [
        :get_002_tags,
        :get_003_categories,
        :get_006_summary,
      ]
      2.times do
        for q in known_questions
          if post_only_questions.include?(q)
            known_questions.delete(q)
          end
        end
      end
    end
    # puts known_questions
  end
  known_questions.each do |m|
    @answers[:layout] = type
    @answers[m.to_s.gsub(/^get_[0-9]{3}_/, "")] = method(m).call
  end
  @answers
end

#get_001_titleObject



50
51
52
53
54
55
56
57
58
# File 'lib/mkmatter/questions.rb', line 50

def get_001_title
  hl = @hl
  title = hl.ask "Title: "
  if hl.agree("Would you like it 'titleized' (Title instead of title)? ", true)
    title.titleize
  else
    title
  end
end

#get_002_tagsArray

Returns:

  • (Array)


61
62
63
64
65
66
# File 'lib/mkmatter/questions.rb', line 61

def get_002_tags
  hl = @hl
  hl.ask("Tags? (write one on each line, then press '.' then press 'Enter')") do |q|
    q.gather = '.'
  end
end

#get_003_categoriesArray

Returns:

  • (Array)


69
70
71
72
73
74
# File 'lib/mkmatter/questions.rb', line 69

def get_003_categories
  hl = @hl
  hl.ask("Categories? (write one on each line, then press '.' then press 'Enter')") do |q|
    q.gather = '.'
  end
end

#get_004_file_formatString

Returns:

  • (String)


77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mkmatter/questions.rb', line 77

def get_004_file_format
  hl = @hl
  hl.choose do |menu|
    menu.header = "Choose whether you want HTML or Markdown"
    menu.choice "html" do
      return "html"
    end
    menu.choice "md" do
      return "md"
    end
    menu.prompt = "? "
  end
end

#get_005_extra_fieldsString

Returns:

  • (String)


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
# File 'lib/mkmatter/questions.rb', line 92

def get_005_extra_fields
  hl = @hl
  fields = {}
  custom_fields = nil
  cfh = nil
  if hl.agree("Do you want to add custom fields? ", true)
    hl.say(<<~EXTRA_FIELDS)
    These fields will be usable as {{LAYOUT_TYPE.FIELD}} in pages/posts etc.
    Your fields should be inputted as FIELD=>TEXT HERE
    Type 'EOL' on a new line then press Enter when you are done.
    <%= color('NOTE', :bold, RED) %>: Input is <%= color('NOT', :bold, RED) %> evaluated!
    EXTRA_FIELDS
    custom_fields = hl.ask("Fields?") do |q|
      q.gather = '.'
    end
  end
  if !custom_fields.empty?
    custom_fields.each do |field|
      fields.store(field.to_sym, 'nil')
    end
  end        
  if custom_fields.empty?
    hl.say("No extra fields were added.")
    return
  else
    hl.say("#{fields} #{fields.class}")
    cfh = hl.ask("Value of field '<%= key %>'?") do |q|
      q.gather = fields
    end
  end
  cfh
end

#get_006_summaryOpenStruct

Returns:

  • (OpenStruct)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/mkmatter/questions.rb', line 126

def get_006_summary
  hl = @hl
  summary = nil
  summary_if = hl.agree("Summary? ", true)
  if summary_if
    summary = hl.ask(<<~SUMMARYDOC) do |q|
      Input a summary of the post.
      This will be outputted as a summary in the front matter.
      This is useful for a post that is long and you want to
      show a summary of the post.
    SUMMARYDOC
      q.gather = "."
    end
  end
  summary.join("\n")
end