Class: Charming::Markdown::Renderer
- Inherits:
-
Object
- Object
- Charming::Markdown::Renderer
- Defined in:
- lib/charming/presentation/markdown/renderer.rb
Overview
Renderer is the top-level Markdown-to-ANSI renderer. Parses the content with Kramdown, then walks the document’s block and inline trees to produce styled terminal output. Code blocks are highlighted via Rouge when ‘syntax_highlighting` is enabled.
Constant Summary collapse
- DEFAULT_RULE_WIDTH =
Wrap width used by ‘render_rule` when no width is otherwise specified.
40
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
The Markdown source, configured wrap width, theme, and syntax-highlighting flag.
-
#syntax_highlighting ⇒ Object
readonly
The Markdown source, configured wrap width, theme, and syntax-highlighting flag.
-
#theme ⇒ Object
readonly
The Markdown source, configured wrap width, theme, and syntax-highlighting flag.
-
#width ⇒ Object
readonly
The Markdown source, configured wrap width, theme, and syntax-highlighting flag.
Instance Method Summary collapse
-
#initialize(content:, width: nil, theme: UI::Theme.default, syntax_highlighting: true) ⇒ Renderer
constructor
content is the Markdown source string.
-
#render ⇒ Object
Parses the content and returns the fully-rendered Markdown as a single string.
-
#render_blocks(elements, list_depth: 0, width: @width) ⇒ Object
Renders a list of Kramdown block elements into a string, joined by blank lines.
-
#render_inlines(elements, width: @width) ⇒ Object
Renders a list of Kramdown inline elements into a single concatenated string.
-
#style_for(name, fallback:) ⇒ Object
Returns the theme’s style for name if the theme defines it, otherwise returns fallback.
-
#theme_style(name) ⇒ Object
Returns the theme’s style for name, building a one-token default theme when the active theme doesn’t define it.
-
#wrap(value, width:) ⇒ Object
Word-wraps value to width display columns (when width is given), preserving any ANSI styling on each line.
Constructor Details
#initialize(content:, width: nil, theme: UI::Theme.default, syntax_highlighting: true) ⇒ Renderer
content is the Markdown source string. width optionally wraps paragraphs to that many display columns. theme is the Charming theme used to style blocks/inlines. syntax_highlighting enables Rouge-backed code block highlighting (default true).
21 22 23 24 25 26 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 21 def initialize(content:, width: nil, theme: UI::Theme.default, syntax_highlighting: true) @content = content @width = width @theme = theme || UI::Theme.default @syntax_highlighting = syntax_highlighting end |
Instance Attribute Details
#content ⇒ Object (readonly)
The Markdown source, configured wrap width, theme, and syntax-highlighting flag.
16 17 18 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 16 def content @content end |
#syntax_highlighting ⇒ Object (readonly)
The Markdown source, configured wrap width, theme, and syntax-highlighting flag.
16 17 18 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 16 def syntax_highlighting @syntax_highlighting end |
#theme ⇒ Object (readonly)
The Markdown source, configured wrap width, theme, and syntax-highlighting flag.
16 17 18 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 16 def theme @theme end |
#width ⇒ Object (readonly)
The Markdown source, configured wrap width, theme, and syntax-highlighting flag.
16 17 18 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 16 def width @width end |
Instance Method Details
#render ⇒ Object
Parses the content and returns the fully-rendered Markdown as a single string.
29 30 31 32 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 29 def render document = Kramdown::Document.new(content.to_s) render_blocks(document.root.children) end |
#render_blocks(elements, list_depth: 0, width: @width) ⇒ Object
Renders a list of Kramdown block elements into a string, joined by blank lines. list_depth is forwarded to the render context for list indentation. width defaults to the renderer’s configured width.
37 38 39 40 41 42 43 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 37 def render_blocks(elements, list_depth: 0, width: @width) context = RenderContext.from(width: width, list_depth: list_depth) elements.filter_map do |element| rendered = block_renderer.render(element, context: context) rendered unless rendered.to_s.empty? end.join("\n\n") end |
#render_inlines(elements, width: @width) ⇒ Object
Renders a list of Kramdown inline elements into a single concatenated string. width defaults to the renderer’s configured width.
47 48 49 50 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 47 def render_inlines(elements, width: @width) context = RenderContext.from(width: width) elements.map { |element| inline_renderer.render(element, context: context) }.join end |
#style_for(name, fallback:) ⇒ Object
Returns the theme’s style for name if the theme defines it, otherwise returns fallback. Lets views override markdown-specific theme tokens.
62 63 64 65 66 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 62 def style_for(name, fallback:) return theme.public_send(name) if theme.respond_to?(name) fallback end |
#theme_style(name) ⇒ Object
Returns the theme’s style for name, building a one-token default theme when the active theme doesn’t define it. Used as a final fallback for markdown styling.
70 71 72 73 74 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 70 def theme_style(name) return theme.public_send(name) if theme.respond_to?(name) UI::Theme::DEFAULT_TOKENS.fetch(name).then { |token| UI::Theme.new(name => token).public_send(name) } end |
#wrap(value, width:) ⇒ Object
Word-wraps value to width display columns (when width is given), preserving any ANSI styling on each line. Returns value unchanged when width is nil.
54 55 56 57 58 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 54 def wrap(value, width:) return value unless width value.to_s.lines(chomp: true).map { |line| wrap_line(line, width) }.join("\n") end |