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

.admonition(lines, type: 'INFO') ⇒ Object

type: NOTE CAUTION WARNING IMPORTANT TIP INFO



35
36
37
# File 'lib/aspera/markdown.rb', line 35

def admonition(lines, type: 'INFO')
  "> [!{type}]\n#{lines.map{ |l| "> #{l}"}.join("\n")}\n\n"
end

.code(lines, type: 'shell') ⇒ Object



39
40
41
# File 'lib/aspera/markdown.rb', line 39

def code(lines, type: 'shell')
  "```#{type}\n#{lines.join("\n")}\n```\n\n"
end

.heading(title, level: 1) ⇒ Object



30
31
32
# File 'lib/aspera/markdown.rb', line 30

def heading(title, level: 1)
  "#{'#' * level} #{title}\n\n"
end

.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

.paragraph(text) ⇒ Object



43
44
45
# File 'lib/aspera/markdown.rb', line 43

def paragraph(text)
  "#{text}\n\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