class BBS::Markdown::Table
A GFM table: a header row, a delimiter row that sets the alignments, and the body rows, drawn as box-drawn ANSI lines.
Public Class Methods
Source
# File lib/bbs/markdown/table.rb, line 18 def self.cells(line) s = line.strip s = s[1..] if s.start_with?('|') s = s[0..-2] if s.end_with?('|') s.split('|').map(&:strip) end
The trimmed cells of a | a | b | row; the leading and the trailing pipe may be missing.
Source
# File lib/bbs/markdown/table.rb, line 9 def self.delimiter?(line) return false unless line.include?('-') cells = cells(line) !cells.empty? && cells.all? { |c| c.match?(DELIMITER_CELL) } end
Whether line is a table delimiter row, e.g. |---|:--:|---:|.
# File lib/bbs/markdown/table.rb, line 27 def initialize(header, delimiter, rows, styles) @styles = styles @ncols = Table.cells(header).size @aligns = aligns(delimiter) @rows = [header, *rows].map do |line| cells = Table.cells(line) Array.new(@ncols) { |c| Inline.style(cells[c].to_s, styles) } end end
The table of the header line, the delimiter line and the body rows (lines), its cells styled inline with styles.
Public Instance Methods
Source
# File lib/bbs/markdown/table.rb, line 39 def lines(width) return [] if @ncols.zero? @widths = shrink(natural_widths, [width - (@ncols * 3) - 1, @ncols].max) [border('┌', '┬', '┐'), row(@rows.first, header: true), border('├', '┼', '┤'), *@rows.drop(1).map { |r| row(r) }, border('└', '┴', '┘')] end
The lines of the table, fitting width: the widest columns shrink when it overflows. Empty for a table without columns.