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



98
99
100
# File 'lib/mark_maker/generator.rb', line 98

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

#bullets(*content) ⇒ Object



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

def bullets(*content)
  content.map(&:bullet)
end

#center_justify(fill, *content) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/mark_maker/generator.rb', line 129

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



162
163
164
165
166
167
168
169
170
# File 'lib/mark_maker/generator.rb', line 162

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_block(*content) ⇒ Object



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

def code_block(*content)
  content.map(&:code)
end

#column_width(*content) ⇒ Object



158
159
160
# File 'lib/mark_maker/generator.rb', line 158

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

#fenced_code_block(*content) ⇒ Object

creates a github flavored markdown fenced code block



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

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



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

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

#fill_justify(fill, *content) ⇒ Object

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



151
152
153
154
155
156
# File 'lib/mark_maker/generator.rb', line 151

def fill_justify(fill, *content)
  width = column_width(*content)
  content.map do |c|
    c.insert(c.length / 2, fill * (width - c.length))
  end
end

#image(alt, path, title = "") ⇒ Object



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

def image(alt, path, title = "")
  %Q(![#{alt}](#{path} "#{title}"))
end

#justification?(cell) ⇒ Boolean

detect if the given table cell content is a justification indicator

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/mark_maker/generator.rb', line 22

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.



30
31
32
# File 'lib/mark_maker/generator.rb', line 30

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

#justify(*content) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mark_maker/generator.rb', line 102

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



117
118
119
120
121
# File 'lib/mark_maker/generator.rb', line 117

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

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


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

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

#numbers(*content) ⇒ Object



38
39
40
# File 'lib/mark_maker/generator.rb', line 38

def numbers(*content)
  content.each.with_index(1).map { |l, i| l.number(i) }
end

#right_justify(fill, *content) ⇒ Object



123
124
125
126
127
# File 'lib/mark_maker/generator.rb', line 123

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

  content.map { |c| justified_fill(c, fill) * (width - c.length) + c }
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.



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mark_maker/generator.rb', line 85

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



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

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

#table_row(*content) ⇒ Object



76
77
78
# File 'lib/mark_maker/generator.rb', line 76

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