Class: MarkMaker::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/mark_maker/generator.rb

Overview

Generator is the workhorse of the MarkMaker utility. It provides line by line generation of markdown output.

Instance Method Summary collapse

Instance Method Details

#block_quote(*content) ⇒ Object



142
143
144
# File 'lib/mark_maker/generator.rb', line 142

def block_quote(*content)
  content.map { |c| "#{BLOCK_QUOTE} #{c}" }
end

#bullet(content) ⇒ Object



57
58
59
# File 'lib/mark_maker/generator.rb', line 57

def bullet(content)
  " - #{content}"
end

#bullets(*content) ⇒ Object



61
62
63
# File 'lib/mark_maker/generator.rb', line 61

def bullets(*content)
  content.map { |li| bullet(li) }
end

#center_justify(fill, *content) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/mark_maker/generator.rb', line 173

def center_justify(fill, *content)
  width = column_width(*content)

  content.map do |c|
    if justification?(c)
      # special case here, as justification must be filled from
      # the middle out in order to meet the markdown spec requirements
      # that will trigger proper justification
      f = []
      f << c
      f << 'a' * width
      fill_justify(justified_fill(c, fill), *f)[0]
    else
      left, right = centered_margins(width, c)
      justified_fill(c, fill) * left + c + justified_fill(c, fill) * right
    end
  end
end

#centered_margins(width, content) ⇒ Object



207
208
209
210
211
212
213
214
215
# File 'lib/mark_maker/generator.rb', line 207

def centered_margins(width, content)
  space = width - content.length
  base_margin = space / 2
  remainder_margin = width - content.length - base_margin * 2
  [
    base_margin,
    base_margin + remainder_margin
  ]
end

#code(content) ⇒ Object



78
79
80
# File 'lib/mark_maker/generator.rb', line 78

def code(content)
  "    #{content}"
end

#code_block(*content) ⇒ Object



86
87
88
# File 'lib/mark_maker/generator.rb', line 86

def code_block(*content)
  content.map { |c| code(c) }
end

#code_span(content) ⇒ Object



82
83
84
# File 'lib/mark_maker/generator.rb', line 82

def code_span(content)
  "#{CODE_TIC}#{content}#{CODE_TIC}"
end

#column_width(*content) ⇒ Object



202
203
204
205
# File 'lib/mark_maker/generator.rb', line 202

def column_width(*content)
  longest = content.reduce { |a, e| a.length > e.length ? a : e }
  longest.length
end

#emphasis(content) ⇒ Object



105
106
107
# File 'lib/mark_maker/generator.rb', line 105

def emphasis(content)
  "#{EMPHASIS}#{content}#{EMPHASIS}"
end

#fenced_code_block(*content) ⇒ Object

creates a github flavored markdown fenced code block



91
92
93
94
95
# File 'lib/mark_maker/generator.rb', line 91

def fenced_code_block(*content)
  block = *FENCE
  block.push(*content)
  block << FENCE
end

#fenced_code_language(lang, *content) ⇒ Object

creates a github flavored markdown fenced code block that specifies the language for syntax highlighting purposes



99
100
101
102
103
# File 'lib/mark_maker/generator.rb', line 99

def fenced_code_language(lang, *content)
  block = *FENCE + lang
  block.push(*content)
  block << FENCE
end

#fill_justify(fill, *content) ⇒ Object



192
193
194
195
196
197
198
199
200
# File 'lib/mark_maker/generator.rb', line 192

def fill_justify(fill, *content)
  width = column_width(*content)
  content.map do |c|
    # split the cell in two and then add the fill character
    # to the end of the first half of the cell to reach the
    # justified width
    c.insert(c.length / 2, fill * (width - c.length))
  end
end

#header1(title) ⇒ Object



45
46
47
# File 'lib/mark_maker/generator.rb', line 45

def header1(title)
  "#{title}\n#{line_for('=', title)}"
end

#header2(title) ⇒ Object



49
50
51
# File 'lib/mark_maker/generator.rb', line 49

def header2(title)
  "#{title}\n#{line_for('-', title)}"
end

#header3(title) ⇒ Object



53
54
55
# File 'lib/mark_maker/generator.rb', line 53

def header3(title)
  "### #{title}"
end

#justification?(cell) ⇒ Boolean

detect if the given table cell content is a justification indicator

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/mark_maker/generator.rb', line 33

def justification?(cell)
  cell =~ MarkMaker::RIGHT_JUSTIFY ||
    cell =~ MarkMaker::LEFT_JUSTIFY ||
    cell =~ MarkMaker::CENTER_JUSTIFY
end

#justified_fill(c, fill) ⇒ Object

Inspect the cell contents and return a justification indicator as the fill element if the cell is a justification directive.



41
42
43
# File 'lib/mark_maker/generator.rb', line 41

def justified_fill(c, fill)
  justification?(c) ? '-' : fill
end

#justify(*content) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/mark_maker/generator.rb', line 146

def justify(*content)
  # check for a justification marker in the second row
  case content[1]
  when MarkMaker::RIGHT_JUSTIFY
    right_justify(' ', *content)
  when MarkMaker::LEFT_JUSTIFY
    left_justify(' ', *content)
  when MarkMaker::CENTER_JUSTIFY
    center_justify(' ', *content)
  else
    # no justification indicator was found, use a default
    left_justify(' ', *content)
  end
end

#left_justify(fill, *content) ⇒ Object



161
162
163
164
165
# File 'lib/mark_maker/generator.rb', line 161

def left_justify(fill, *content)
  width = column_width(*content)

  content.map { |c| c + justified_fill(c, fill) * (width - c.length) }
end

#line_for(underscore, content) ⇒ Object



5
6
7
# File 'lib/mark_maker/generator.rb', line 5

def line_for(underscore, content)
  underscore * content.size
end

#line_for_centerObject



15
16
# File 'lib/mark_maker/generator.rb', line 15

def line_for_center
end

#line_for_leftObject



9
10
# File 'lib/mark_maker/generator.rb', line 9

def line_for_left
end

#line_for_rightObject



12
13
# File 'lib/mark_maker/generator.rb', line 12

def line_for_right
end


74
75
76
# File 'lib/mark_maker/generator.rb', line 74

def link(label, url)
  "[#{label}](#{url})"
end

#number(content, number = 1) ⇒ Object



65
66
67
# File 'lib/mark_maker/generator.rb', line 65

def number(content, number = 1)
  " #{number}. #{content}"
end

#numbers(*content) ⇒ Object



69
70
71
72
# File 'lib/mark_maker/generator.rb', line 69

def numbers(*content)
  current_number = 0
  content.map { |li| number(li, current_number += 1) }
end

#right_justify(fill, *content) ⇒ Object



167
168
169
170
171
# File 'lib/mark_maker/generator.rb', line 167

def right_justify(fill, *content)
  width = column_width(*content)

  content.map { |c| justified_fill(c, fill) * (width - c.length) + c }
end

#strong(content) ⇒ Object



109
110
111
# File 'lib/mark_maker/generator.rb', line 109

def strong(content)
  EMPHASIS * 2 + content + EMPHASIS * 2
end

#table(*content) ⇒ Object

Table will treat the first line of content as the table header. It will also assess each ‘column’ and derive the width from the largest cell value, including the header. Justification can be passed in optionally.



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/mark_maker/generator.rb', line 129

def table(*content)
  columns = content.transpose
  justified = columns.map { |c| justify(*c) }
  content = justified.transpose
  table = []
  # if content.size >= 1
  #   header, separator = table_header(*content[0])
  #   table << header << separator
  # end
  content[0, content.size].each { |c| table << table_row(*c) }
  table.map { |t| t + "\n" }
end

#table_header(*content) ⇒ Object



113
114
115
116
117
118
# File 'lib/mark_maker/generator.rb', line 113

def table_header(*content)
  [
    content.inject("|") { |a, e| a + "#{e}|" },
    content.inject("|") { |a, e| a + "-" * e.size + "|" }
  ]
end

#table_row(*content) ⇒ Object



120
121
122
# File 'lib/mark_maker/generator.rb', line 120

def table_row(*content)
  content.inject("|") { |a, e| a << e << "|" }
end