Module: Polyrun::Export::Markdown

Defined in:
lib/polyrun/export/markdown.rb

Overview

GitHub-flavored markdown tables for report exports.

Class Method Summary collapse

Class Method Details

.document(title, sections) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/polyrun/export/markdown.rb', line 27

def document(title, sections)
  lines = [heading(1, title), ""]
  sections.each do |section|
    heading_text = section[:heading] || section["heading"]
    lines << heading(2, heading_text) if heading_text && !heading_text.to_s.empty?

    body = section[:body] || section["body"]
    lines << body if body && !body.to_s.empty?

    table_headers = section[:headers] || section["headers"]
    table_rows = section[:rows] || section["rows"]
    if table_headers && !table_headers.empty?
      lines << table(table_headers, table_rows || [])
    end
    lines << ""
  end
  lines.join("\n")
end

.escape_cell(value) ⇒ Object



7
8
9
# File 'lib/polyrun/export/markdown.rb', line 7

def escape_cell(value)
  value.nil? ? "" : value.to_s.gsub("|", "\\|").tr("\n", " ")
end

.heading(level, text) ⇒ Object



11
12
13
14
# File 'lib/polyrun/export/markdown.rb', line 11

def heading(level, text)
  level = level.to_i.clamp(1, 6)
  "#{"#" * level} #{text}\n"
end

.table(headers, rows) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/polyrun/export/markdown.rb', line 16

def table(headers, rows)
  return "" if headers.nil? || headers.empty?

  header_line = "| #{headers.map { |header| escape_cell(header) }.join(" | ")} |"
  separator_line = "| #{headers.map { "---" }.join(" | ")} |"
  body_lines = rows.map do |row|
    "| #{Array(row).map { |cell| escape_cell(cell) }.join(" | ")} |"
  end
  ([header_line, separator_line] + body_lines).join("\n") + "\n"
end