Class: Charming::Presentation::Markdown::Renderer
- Inherits:
-
Object
- Object
- Charming::Presentation::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).
22 23 24 25 26 27 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 22 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.
17 18 19 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 17 def content @content end |
#syntax_highlighting ⇒ Object (readonly)
The Markdown source, configured wrap width, theme, and syntax-highlighting flag.
17 18 19 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 17 def syntax_highlighting @syntax_highlighting end |
#theme ⇒ Object (readonly)
The Markdown source, configured wrap width, theme, and syntax-highlighting flag.
17 18 19 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 17 def theme @theme end |
#width ⇒ Object (readonly)
The Markdown source, configured wrap width, theme, and syntax-highlighting flag.
17 18 19 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 17 def width @width end |
Instance Method Details
#render ⇒ Object
Parses the content and returns the fully-rendered Markdown as a single string.
30 31 32 33 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 30 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.
38 39 40 41 42 43 44 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 38 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.
48 49 50 51 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 48 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.
63 64 65 66 67 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 63 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.
71 72 73 74 75 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 71 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.
55 56 57 58 59 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 55 def wrap(value, width:) return value unless width value.to_s.lines(chomp: true).map { |line| wrap_line(line, width) }.join("\n") end |