Class: Aspera::Markdown

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/markdown.rb

Overview

Formatting for Markdown

Constant Summary collapse

FORMATS =

Matches: bold, ‘code`, or an HTML entity (&, ©, 💩)

/(?:\*\*(?<bold>[^*]+?)\*\*)|(?:`(?<code>[^`]+)`)|&(?<entity>(?:[A-Za-z][A-Za-z0-9]{1,31}|#\d{1,7}|#x[0-9A-Fa-f]{1,6}));/m
HTML_BREAK =
'<br/>'

Class Method Summary collapse

Class Method Details

.list(items) ⇒ Object

Generate markdown list from the provided list



26
27
28
# File 'lib/aspera/markdown.rb', line 26

def list(items)
  items.map{ |i| "- #{i}"}.join("\n")
end

.table(table) ⇒ Object

Generate markdown from the provided 2D table



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/aspera/markdown.rb', line 12

def table(table)
  # get max width of each columns
  col_widths = table.transpose.map do |col|
    [col.flat_map{ |c| c.to_s.delete('`').split(HTML_BREAK).map(&:size)}.max, 80].min
  end
  headings = table.shift
  table.unshift(col_widths.map{ |col_width| '-' * col_width})
  table.unshift(headings)
  lines = table.map{ |line| "| #{line.map{ |i| i.to_s.gsub('\\', '\\\\').gsub('|', '\|')}.join(' | ')} |\n"}
  lines[1] = lines[1].tr(' ', '-')
  return lines.join.chomp
end