Module: LLM::Repl::Markdown::Table

Included in:
LLM::Repl::Markdown
Defined in:
lib/llm/repl/markdown/table.rb

Overview

Renders Kramdown :table nodes as aligned columns.

Instance Method Summary collapse

Instance Method Details

#walk_table(node, attrs) ⇒ Object

Renders a table node by collecting all cells first to compute column widths, then emitting each row with padded text.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/llm/repl/markdown/table.rb', line 11

def walk_table(node, attrs)
  rows = collect_rows(node, attrs)
  return if rows.empty?
  widths = column_widths(rows)
  rows.each do |row|
    emit("| ", attrs)
    row.each_with_index do |chunks, i|
      text = chunks.map { _1[:text] }.join
      width = widths[i]
      chunks.each { |c| emit(c[:text].ljust(width), c[:attrs]) }
      emit(" | ", attrs) unless i == row.size - 1
    end
    emit(" |", attrs)
    emit("\n", attrs)
  end
  emit("\n", attrs)
end