class MarkMaker::Generator

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

Public Instance Methods

block_quote(*content) click to toggle source
# File lib/mark_maker/generator.rb, line 98
def block_quote(*content)
  content.map { |c| "#{BLOCK_QUOTE} #{c}" }
end
bullets(*content) click to toggle source
# File lib/mark_maker/generator.rb, line 34
def bullets(*content)
  content.map(&:bullet)
end
center_justify(fill, *content) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/mark_maker/generator.rb, line 50
def code_block(*content)
  content.map(&:code)
end
column_width(*content) click to toggle source
# 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) click to toggle source

creates a github flavored markdown fenced code block

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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 = "") click to toggle source
# File lib/mark_maker/generator.rb, line 46
def image(alt, path, title = "")
  %Q(![#{alt}](#{path} "#{title}"))
end
justification?(cell) click to toggle source

detect if the given table cell content is a justification indicator

# 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) click to toggle source

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

# File lib/mark_maker/generator.rb, line 30
def justified_fill(c, fill)
  justification?(c) ? '-' : fill
end
justify(*content) click to toggle source
# 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) click to toggle source
# 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
numbers(*content) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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.

# 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) click to toggle source
# 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) click to toggle source
# File lib/mark_maker/generator.rb, line 76
def table_row(*content)
  content.inject("|") { |a, e| a << e << "|" }
end