Class: Charming::Markdown::Renderer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_urlObject (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

#contentObject (readonly)

Returns the value of attribute content.



12
13
14
# File 'lib/charming/presentation/markdown/renderer.rb', line 12

def content
  @content
end

#styleObject (readonly)

Returns the value of attribute style.



12
13
14
# File 'lib/charming/presentation/markdown/renderer.rb', line 12

def style
  @style
end

#syntax_highlightingObject (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

#themeObject (readonly)

Returns the value of attribute theme.



12
13
14
# File 'lib/charming/presentation/markdown/renderer.rb', line 12

def theme
  @theme
end

#widthObject (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

#renderObject



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