Class: Mkmatter::Questions

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

Constant Summary collapse

@@hl =
HighLine.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQuestions

Returns a new instance of Questions.



38
39
40
# File 'lib/mkmatter/questions.rb', line 38

def initialize
  @answers = {}
end

Instance Attribute Details

#answersObject (readonly)

Returns the value of attribute answers.



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

def answers
  @answers
end

Instance Method Details

#ask(type, include_post_qs) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mkmatter/questions.rb', line 11

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}_/, '').to_sym] = method(m).call
  end
  @answers
end

#get_001_titleObject



42
43
44
45
46
47
48
49
50
# File 'lib/mkmatter/questions.rb', line 42

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

#get_002_tagsArray

Returns:

  • (Array)


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

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

#get_003_categoriesArray

Returns:

  • (Array)


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

def get_003_categories
  @@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)


68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mkmatter/questions.rb', line 68

def get_004_file_format
  @@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)


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

def get_005_extra_fields
  fields = {}
  custom_fields = []
  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
  unless 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_summaryObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mkmatter/questions.rb', line 114

def get_006_summary
  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") if summary
end