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.
-
#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) ⇒ Renderer
constructor
A new instance of Renderer.
- #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) ⇒ Renderer
Returns a new instance of Renderer.
14 15 16 17 18 19 20 21 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 14 def initialize(content:, width: nil, theme: UI::Theme.default, syntax_highlighting: true, style: :dark, base_url: nil) @content = content @width = width @theme = theme || UI::Theme.default @syntax_highlighting = syntax_highlighting @style = StyleConfig.from(style) @base_url = base_url end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
12 13 14 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 12 def base_url @base_url end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
12 13 14 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 12 def content @content end |
#style ⇒ Object (readonly)
Returns the value of attribute style.
12 13 14 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 12 def style @style end |
#syntax_highlighting ⇒ Object (readonly)
Returns the value of attribute syntax_highlighting.
12 13 14 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 12 def syntax_highlighting @syntax_highlighting end |
#theme ⇒ Object (readonly)
Returns the value of attribute theme.
12 13 14 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 12 def theme @theme end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
12 13 14 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 12 def width @width end |
Instance Method Details
#render ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 23 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
33 34 35 36 37 38 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 33 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
40 41 42 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 40 def render_inlines(elements, context:) elements.map { |element| render_inline(element, context: context) }.join end |
#style_for(name, fallback:) ⇒ Object
50 51 52 53 54 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 50 def style_for(name, fallback:) return theme.public_send(name) if theme.respond_to?(name) fallback end |
#theme_style(name) ⇒ Object
56 57 58 59 60 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 56 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
44 45 46 47 48 |
# File 'lib/charming/presentation/markdown/renderer.rb', line 44 def wrap(value, width:) return value unless width value.to_s.lines(chomp: true).map { |line| wrap_line(line, width) }.join("\n") end |