Class: Rubino::UI::MarkdownRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/rubino/ui/markdown_renderer.rb

Overview

Renders a markdown string into a list of styled token-lines.

Output shape:

render(text) -> [LineTokens, LineTokens, ...]
LineTokens   = [[String, StyleHash], ...]
StyleHash    = { fg:, bg:, modifiers: [...] } (any subset; nil ≈ default)

The caller turns these into ANSI-colored strings via Pastel. Keeping the output as plain Ruby data lets the renderer be tested without a real terminal.

Coverage: headings 1-3, paragraphs, bold, italic, ‘inline code`, “`fenced“` code blocks, ordered/unordered lists (one level), block quotes, [links](url), horizontal rules. Anything unrecognized falls back to its raw text content, never blowing up.

Constant Summary collapse

MIN_TABLE_WIDTH =

Smallest width we’ll ask TTY::Table to fit into. Below this, resize tends to raise (a column needs at least ~2 cols + borders); we clamp up to keep the headless/extreme-narrow paths from blowing up.

20
DEFAULT_WIDTH =
80

Instance Method Summary collapse

Constructor Details

#initialize(width: nil) ⇒ MarkdownRenderer

Returns a new instance of MarkdownRenderer.

Parameters:

  • width (Integer, nil) (defaults to: nil)

    the column budget tables must fit into. When nil we detect the terminal width (IO.console winsize), falling back to 80 so the renderer still works headless / without a real terminal.



46
47
48
# File 'lib/rubino/ui/markdown_renderer.rb', line 46

def initialize(width: nil)
  @width = width || detect_width
end

Instance Method Details

#render(text) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/rubino/ui/markdown_renderer.rb', line 50

def render(text)
  return [] if text.nil? || text.to_s.strip.empty?

  doc = Kramdown::Document.new(normalize(text.to_s), input: "GFM", auto_ids: false, hard_wrap: false)
  block_lines(doc.root).reject { |line| line == :drop }
rescue StandardError
  # Parser failure -> degrade to plain text rather than break the UI.
  text.to_s.split("\n", -1).map { |l| [[l, nil]] }
end