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

#contentObject (readonly)

Returns the value of attribute content.



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

def content
  @content
end

Returns the value of attribute hyperlinks.



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

def hyperlinks
  @hyperlinks
end

#styleObject (readonly)

Returns the value of attribute style.



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

def style
  @style
end

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

#themeObject (readonly)

Returns the value of attribute theme.



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

def theme
  @theme
end

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

#renderObject



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