Class: RubyRich::Markdown::TerminalRenderer

Inherits:
Redcarpet::Render::Base
  • Object
show all
Defined in:
lib/ruby_rich/markdown.rb

Overview

简化的 Markdown 渲染器,将 Markdown 转换为带 ANSI 颜色的终端输出

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TerminalRenderer

Returns a new instance of TerminalRenderer.



7
8
9
10
11
12
13
14
15
16
# File 'lib/ruby_rich/markdown.rb', line 7

def initialize(options = {})
  @options = {
    width: 80,
    indent: '  '
  }.merge(options)
  super()
  
  # 表格状态
  reset_table_state
end

Instance Method Details

#block_code(code, language) ⇒ Object

代码块



47
48
49
50
# File 'lib/ruby_rich/markdown.rb', line 47

def block_code(code, language)
  # 简单的代码格式,不使用语法高亮避免循环依赖
  "\e[100m\e[37m" + indent_lines(code.strip) + "\e[0m\n\n"
end

#block_quote(quote) ⇒ Object

引用



58
59
60
61
62
# File 'lib/ruby_rich/markdown.rb', line 58

def block_quote(quote)
  lines = quote.strip.split("\n")
  quoted_lines = lines.map { |line| "\e[90m│ \e[37m#{line.strip}" }
  quoted_lines.join("\n") + "\e[0m\n\n"
end

#codespan(code) ⇒ Object

行内代码



53
54
55
# File 'lib/ruby_rich/markdown.rb', line 53

def codespan(code)
  "\e[47m\e[30m #{code} \e[0m"
end

#double_emphasis(text) ⇒ Object

加粗



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

def double_emphasis(text)
  "\e[1m#{text}\e[22m"
end

#emphasis(text) ⇒ Object

强调



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

def emphasis(text)
  "\e[3m#{text}\e[23m"
end

#header(text, level) ⇒ Object

标题



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_rich/markdown.rb', line 33

def header(text, level)
  case level
  when 1
    "\e[1m\e[96m#{text}\e[0m\n" + "\e[96m#{'=' * text.length}\e[0m\n\n"
  when 2
    "\e[1m\e[94m#{text}\e[0m\n" + "\e[94m#{'-' * text.length}\e[0m\n\n"
  when 3
    "\e[1m\e[93m### #{text}\e[0m\n\n"
  else
    "\e[1m\e[90m#{'#' * level} #{text}\e[0m\n\n"
  end
end

#hruleObject

水平线



109
110
111
# File 'lib/ruby_rich/markdown.rb', line 109

def hrule
  "\e[90m" + "─" * @options[:width] + "\e[0m\n\n"
end

#image(link, title, alt_text) ⇒ Object

图片



100
101
102
103
104
105
106
# File 'lib/ruby_rich/markdown.rb', line 100

def image(link, title, alt_text)
  if title && !title.empty?
    "\e[95m[Image: #{alt_text}]\e[0m \e[90m(#{link} - #{title})\e[0m"
  else
    "\e[95m[Image: #{alt_text}]\e[0m \e[90m(#{link})\e[0m"
  end
end

#linebreakObject

换行



178
179
180
# File 'lib/ruby_rich/markdown.rb', line 178

def linebreak
  "\n"
end

链接



91
92
93
94
95
96
97
# File 'lib/ruby_rich/markdown.rb', line 91

def link(link, title, content)
  if title && !title.empty?
    "\e[94m\e[4m#{content}\e[24m\e[0m \e[90m(#{link} - #{title})\e[0m"
  else
    "\e[94m\e[4m#{content}\e[24m\e[0m \e[90m(#{link})\e[0m"
  end
end

#list(contents, list_type) ⇒ Object

无序列表



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

def list(contents, list_type)
  contents + "\n"
end

#list_item(text, list_type) ⇒ Object

列表项



65
66
67
68
# File 'lib/ruby_rich/markdown.rb', line 65

def list_item(text, list_type)
  marker = list_type == :ordered ? '1.' : '•'
  "\e[96m#{marker}\e[0m #{text.strip}\n"
end

#paragraph(text) ⇒ Object

段落



28
29
30
# File 'lib/ruby_rich/markdown.rb', line 28

def paragraph(text)
  wrap_text(text) + "\n\n"
end

#reset_table_stateObject



18
19
20
21
22
23
24
25
# File 'lib/ruby_rich/markdown.rb', line 18

def reset_table_state
  @table_state = {
    in_table: false,
    headers: [],
    current_row: [],
    rows: []
  }
end

#strikethrough(text) ⇒ Object

删除线



86
87
88
# File 'lib/ruby_rich/markdown.rb', line 86

def strikethrough(text)
  "\e[9m#{text}\e[29m"
end

#table(header, body) ⇒ Object

表格渲染 - 智能分割方法



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ruby_rich/markdown.rb', line 114

def table(header, body)
  return "" if header.nil? && body.nil?
  
  begin
    # 尝试智能分割表格内容
    headers = []
    rows = []
    
    if header && !header.strip.empty?
      # 从header中提取列标题
      header_content = header.strip
      # 尝试按常见模式分割(大写字母开头的单词)
      headers = split_table_content_intelligently(header_content)
    end
    
    if body && !body.strip.empty?
      body_lines = body.strip.split("\n").reject(&:empty?)
      body_lines.each do |line|
        row_data = split_table_content_intelligently(line.strip)
        rows << row_data unless row_data.all?(&:empty?)
      end
    end
    
    # 如果成功解析出了数据,使用RubyRich表格
    if !headers.empty? && !rows.empty?
      table = RubyRich::Table.new(
        headers: headers,
        border_style: @options[:table_border_style] || :simple
      )
      
      rows.each do |row|
        # 确保行长度与标题长度一致
        padded_row = row + Array.new([0, headers.length - row.length].max, "")
        table.add_row(padded_row[0...headers.length])
      end
      
      return table.render + "\n\n"
    end
    
  rescue => e
    # 如果出错,继续使用fallback
  end
  
  # Fallback: 简单显示
  result = "\n"
  if header && !header.strip.empty?
    result += "#{header.strip}\n"
    result += "-" * [header.strip.length, 20].min + "\n"
  end
  if body && !body.strip.empty?
    result += body.strip + "\n"
  end
  result + "\n"
end

#table_cell(content, alignment) ⇒ Object



173
174
175
# File 'lib/ruby_rich/markdown.rb', line 173

def table_cell(content, alignment)
  content
end

#table_row(content) ⇒ Object



169
170
171
# File 'lib/ruby_rich/markdown.rb', line 169

def table_row(content)
  content + "\n"
end