Class: Rubino::UI::MarkdownRenderer
- Inherits:
-
Object
- Object
- Rubino::UI::MarkdownRenderer
- 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
-
#initialize(width: nil) ⇒ MarkdownRenderer
constructor
A new instance of MarkdownRenderer.
- #render(text) ⇒ Object
Constructor Details
#initialize(width: nil) ⇒ MarkdownRenderer
Returns a new instance of MarkdownRenderer.
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 |