Class: Charming::Markdown::Renderer
- Inherits:
-
Object
- Object
- Charming::Markdown::Renderer
- Defined in:
- lib/charming/presentation/markdown/renderer.rb
Overview
Renderer parses CommonMark/GFM with Commonmarker and renders it as ANSI text.
Constant Summary collapse
- DEFAULT_RULE_WIDTH =
40
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#hyperlinks ⇒ Object
readonly
Returns the value of attribute hyperlinks.
-
#style ⇒ Object
readonly
Returns the value of attribute style.
-
#syntax_highlighting ⇒ Object
readonly
Returns the value of attribute syntax_highlighting.
-
#theme ⇒ Object
readonly
Returns the value of attribute theme.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
-
#initialize(content:, width: nil, theme: UI::Theme.default, syntax_highlighting: true, style: :dark, base_url: nil, hyperlinks: false) ⇒ Renderer
constructor
hyperlinks (default false) wraps links in OSC 8 escape sequences so modern terminals make them clickable; the ‘ <url>` suffix is omitted since the target is embedded in the escape.
- #render ⇒ Object
- #render_blocks(elements, context:) ⇒ Object
- #render_inlines(elements, context:) ⇒ Object
- #style_for(name, fallback:) ⇒ Object
- #theme_style(name) ⇒ Object
- #wrap(value, width:) ⇒ Object
Constructor Details
#initialize(content:, width: nil, theme: UI::Theme.default, syntax_highlighting: true, style: :dark, base_url: nil, hyperlinks: false) ⇒ Renderer
hyperlinks (default false) wraps links in OSC 8 escape sequences so modern terminals make them clickable; the ‘ <url>` suffix is omitted since the target is embedded in the escape.
16 17 18 19 20 21 22 23 24 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 16 def initialize(content:, width: nil, theme: UI::Theme.default, syntax_highlighting: true, style: :dark, base_url: nil, hyperlinks: false) @content = content @width = width @theme = theme || UI::Theme.default @syntax_highlighting = syntax_highlighting @style = StyleConfig.from(style) @base_url = base_url @hyperlinks = hyperlinks end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
11 12 13 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 11 def base_url @base_url end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
11 12 13 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 11 def content @content end |
#hyperlinks ⇒ Object (readonly)
Returns the value of attribute hyperlinks.
11 12 13 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 11 def hyperlinks @hyperlinks end |
#style ⇒ Object (readonly)
Returns the value of attribute style.
11 12 13 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 11 def style @style end |
#syntax_highlighting ⇒ Object (readonly)
Returns the value of attribute syntax_highlighting.
11 12 13 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 11 def syntax_highlighting @syntax_highlighting end |
#theme ⇒ Object (readonly)
Returns the value of attribute theme.
11 12 13 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 11 def theme @theme end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
11 12 13 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 11 def width @width end |
Instance Method Details
#render ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 26 def render context = RenderContext.from( width: width, style: style, base_url: base_url, source_lines: content.to_s.lines(chomp: true) ) render_document(parse_document, context: context) end |
#render_blocks(elements, context:) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 36 def render_blocks(elements, context:) elements.filter_map do |element| rendered = render_block(element, context: context) rendered unless rendered.to_s.empty? end.join("\n\n") end |
#render_inlines(elements, context:) ⇒ Object
43 44 45 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 43 def render_inlines(elements, context:) elements.map { |element| render_inline(element, context: context) }.join end |
#style_for(name, fallback:) ⇒ Object
51 52 53 54 55 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 51 def style_for(name, fallback:) return theme.public_send(name) if theme.respond_to?(name) fallback end |
#theme_style(name) ⇒ Object
57 58 59 60 61 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 57 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
47 48 49 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 47 def wrap(value, width:) TextWrapper.new(width: width).wrap(value) end |