Class: RubyRich::Markdown::TerminalConverter

Inherits:
Kramdown::Converter::Base
  • Object
show all
Defined in:
lib/ruby_rich/markdown.rb

Defined Under Namespace

Modules: LatexConverter, MermaidRenderer

Instance Method Summary collapse

Constructor Details

#initialize(root, options) ⇒ TerminalConverter

Returns a new instance of TerminalConverter.



58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_rich/markdown.rb', line 58

def initialize(root, options)
  super
  @width = options[:width] || 80
  @indent_str = options[:indent] || '  '
  @table_border_style = options[:table_border_style] || :simple
  @theme = (options[:theme] || MarkdownTheme).freeze
  # 用于有序列表编号
  @list_counters = []
  @list_types = []
end

Instance Method Details

#convert(el, _indent = 0) ⇒ Object

主分发方法 — 根据 AST 元素类型路由到对应处理方法



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ruby_rich/markdown.rb', line 86

def convert(el, _indent = 0)
  case el.type
  when :root         then convert_children(el)
  when :blank        then "\n"
  when :text         then el.value
  when :p            then convert_p(el)
  when :header       then convert_header(el)
  when :codeblock    then convert_codeblock(el)
  when :codespan     then convert_codespan(el)
  when :blockquote   then convert_blockquote(el)
  when :ul           then convert_list(el)
  when :ol           then convert_list(el)
  when :li           then convert_li(el)
  when :em           then convert_em(el)
  when :strong       then convert_strong(el)
  when :em_strong    then convert_em_strong(el)
  when :a            then convert_link(el)
  when :img          then convert_image(el)
  when :hr           then convert_hr(el)
  when :br           then "\n"
  when :table        then convert_table(el)
  when :thead        then convert_children(el)
  when :tbody        then convert_children(el)
  when :tr           then convert_table_row(el)
  when :th           then convert_table_cell(el)
  when :td           then convert_table_cell(el)
  when :html_element then convert_html_element(el)
  when :html_entity  then convert_html_entity(el)
  when :smart_quote  then convert_smart_quote(el)
  when :entity       then el.value.to_s
  when :raw          then convert_raw(el)
  when :comment      then ''
  when :footnote     then convert_footnote(el)
  when :dl           then convert_definition_list(el)
  when :dt           then convert_definition_term(el)
  when :dd           then convert_definition_desc(el)
  when :abbreviation then convert_abbreviation(el)
  when :math         then convert_math(el)
  else
    # 未知类型,递归处理子元素
    if el.children && !el.children.empty?
      convert_children(el)
    else
      el.value || el.text || ''
    end
  end
end

#tbg(key) ⇒ Object



75
76
77
78
# File 'lib/ruby_rich/markdown.rb', line 75

def tbg(key)
  color, bright = @theme[key]
  AnsiCode.background(color, bright)
end

#tc(key) ⇒ Object

Shortcut: AnsiCode.color with theme lookup.



70
71
72
73
# File 'lib/ruby_rich/markdown.rb', line 70

def tc(key)
  color, bright = @theme[key]
  AnsiCode.color(color, bright)
end

#tfont(key, **overrides) ⇒ Object



80
81
82
83
# File 'lib/ruby_rich/markdown.rb', line 80

def tfont(key, **overrides)
  color, bright = @theme[key]
  AnsiCode.font(color, font_bright: bright, **overrides)
end