Class: Ollama::Utils::ANSIMarkdown

Inherits:
Kramdown::Converter::Base
  • Object
show all
Includes:
Width, Term::ANSIColor
Defined in:
lib/ollama/utils/ansi_markdown.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Width

truncate, width, wrap

Constructor Details

#initialize(root, options) ⇒ ANSIMarkdown

Returns a new instance of ANSIMarkdown.



27
28
29
# File 'lib/ollama/utils/ansi_markdown.rb', line 27

def initialize(root, options)
  super
end

Class Method Details

.parse(source) ⇒ Object



21
22
23
24
25
# File 'lib/ollama/utils/ansi_markdown.rb', line 21

def self.parse(source)
  @doc = Kramdown::Document.new(
    source, input: :mygfm, auto_ids: false, entity_output: :as_char
  ).to_ansi
end

Instance Method Details

#convert(el, opts = {}) ⇒ Object



31
32
33
# File 'lib/ollama/utils/ansi_markdown.rb', line 31

def convert(el, opts = {})
  send("convert_#{el.type}", el, opts)
end

#convert_a(el, opts) ⇒ Object



81
82
83
84
# File 'lib/ollama/utils/ansi_markdown.rb', line 81

def convert_a(el, opts)
  url = el.attr['href']
  hyperlink(url) { inner(el, opts) }
end

#convert_blank(_el, opts) ⇒ Object



55
56
57
# File 'lib/ollama/utils/ansi_markdown.rb', line 55

def convert_blank(_el, opts)
  opts[:result] =~ /\n\n\Z|\A\Z/ ? "" : "\n"
end

#convert_blockquote(el, opts) ⇒ Object



94
95
96
# File 'lib/ollama/utils/ansi_markdown.rb', line 94

def convert_blockquote(el, opts)
  newline ?“ + inner(el, opts).sub(/\n+\z/, '') + ?”
end

#convert_br(_el, opts) ⇒ Object



204
205
206
# File 'lib/ollama/utils/ansi_markdown.rb', line 204

def convert_br(_el, opts)
  ''
end

#convert_codeblock(el, _opts) ⇒ Object



90
91
92
# File 'lib/ollama/utils/ansi_markdown.rb', line 90

def convert_codeblock(el, _opts)
  blue { el.value }
end

#convert_codespan(el, _opts) ⇒ Object



86
87
88
# File 'lib/ollama/utils/ansi_markdown.rb', line 86

def convert_codespan(el, _opts)
  blue { el.value }
end

#convert_em(el, opts) ⇒ Object



77
78
79
# File 'lib/ollama/utils/ansi_markdown.rb', line 77

def convert_em(el, opts)
  italic { inner(el, opts) }
end

#convert_entity(el, _opts) ⇒ Object



192
193
194
# File 'lib/ollama/utils/ansi_markdown.rb', line 192

def convert_entity(el, _opts)
  el.value.char
end

#convert_header(el, opts) ⇒ Object



63
64
65
# File 'lib/ollama/utils/ansi_markdown.rb', line 63

def convert_header(el, opts)
  newline bold { underline { inner(el, opts) } }
end

#convert_hr(_el, _opts) ⇒ Object



98
99
100
# File 'lib/ollama/utils/ansi_markdown.rb', line 98

def convert_hr(_el, _opts)
  newline ?─ * width(percentage: 100)
end

#convert_html_element(el, opts) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/ollama/utils/ansi_markdown.rb', line 134

def convert_html_element(el, opts)
  if el.value == 'i' || el.value == 'em'
    italic { inner(el, opts) }
  elsif el.value == 'b' || el.value == 'strong'
    bold { inner(el, opts) }
  else
    ''
  end
end

#convert_img(el, _opts) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/ollama/utils/ansi_markdown.rb', line 102

def convert_img(el, _opts)
  url = el.attr['src']
  alt = el.attr['alt']
  alt.strip.size == 0 and alt = url
  alt = '🖼 ' + alt
  hyperlink(url) { alt }
end

#convert_li(el, opts) ⇒ Object



128
129
130
131
132
# File 'lib/ollama/utils/ansi_markdown.rb', line 128

def convert_li(el, opts)
  opts = opts.dup
  opts[:list_indent] = 2 + opts[:list_indent].to_i
  newline inner(el, opts).sub(/\n+\Z/, '')
end

#convert_ol(el, opts) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/ollama/utils/ansi_markdown.rb', line 119

def convert_ol(el, opts)
  list_indent = opts[:list_indent].to_i
  inner(el, opts) { |_inner_el, index, content|
    result = '%u. %s' % [ index + 1, content ]
    result = newline(result, count: index <= el.children.size - 1 ? 1 : 2)
    result.gsub(/^/, ' ' * list_indent)
  }
end

#convert_p(el, opts) ⇒ Object



67
68
69
70
71
# File 'lib/ollama/utils/ansi_markdown.rb', line 67

def convert_p(el, opts)
  length = width(percentage: 90) - opts[:list_indent].to_i
  length < 0 and return ''
  newline wrap(inner(el, opts), length:)
end

#convert_root(el, opts) ⇒ Object



51
52
53
# File 'lib/ollama/utils/ansi_markdown.rb', line 51

def convert_root(el, opts)
  inner(el, opts)
end

#convert_smart_quote(el, _opts) ⇒ Object



208
209
210
# File 'lib/ollama/utils/ansi_markdown.rb', line 208

def convert_smart_quote(el, _opts)
  el.value.to_s =~ /[rl]dquo/ ? "\"" : "'"
end

#convert_strong(el, opts) ⇒ Object



73
74
75
# File 'lib/ollama/utils/ansi_markdown.rb', line 73

def convert_strong(el, opts)
  bold { inner(el, opts) }
end

#convert_table(el, opts) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ollama/utils/ansi_markdown.rb', line 144

def convert_table(el, opts)
  table = Terminal::Table.new
  table.style = {
    all_separators: true,
    border: :unicode_round,
  }
  opts[:table] = table
  inner(el, opts)
  el.options[:alignment].each_with_index do |a, i|
    a == :default and next
    opts[:table].align_column(i, a)
  end
  newline table.to_s
end

#convert_tbody(el, opts) ⇒ Object



166
167
168
169
# File 'lib/ollama/utils/ansi_markdown.rb', line 166

def convert_tbody(el, opts)
  res = +''
  res << inner(el, opts)
end

#convert_td(el, opts) ⇒ Object



188
189
190
# File 'lib/ollama/utils/ansi_markdown.rb', line 188

def convert_td(el, opts)
  inner(el, opts)
end

#convert_text(el, _opts) ⇒ Object



59
60
61
# File 'lib/ollama/utils/ansi_markdown.rb', line 59

def convert_text(el, _opts)
  el.value
end

#convert_tfoot(el, opts) ⇒ Object



171
172
173
# File 'lib/ollama/utils/ansi_markdown.rb', line 171

def convert_tfoot(el, opts)
  ''
end

#convert_thead(el, opts) ⇒ Object



159
160
161
162
163
164
# File 'lib/ollama/utils/ansi_markdown.rb', line 159

def convert_thead(el, opts)
  rows = inner(el, opts)
  rows = rows.split(/\s*\|\s*/)[1..].map(&:strip)
  opts[:table].headings = rows
  ''
end

#convert_tr(el, opts) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/ollama/utils/ansi_markdown.rb', line 175

def convert_tr(el, opts)
  return '' if el.children.empty?
  full_width = width(percentage: 90)
  cols = el.children.map { |c| convert(c, opts).strip }
  row_size = cols.sum(&:size)
  return '' if row_size.zero?
  opts[:table] << cols.map { |c|
    length = (full_width * (c.size / row_size.to_f)).floor
    wrap(c, length:)
  }
  ''
end

#convert_ul(el, opts) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/ollama/utils/ansi_markdown.rb', line 110

def convert_ul(el, opts)
  list_indent = opts[:list_indent].to_i
  inner(el, opts) { |_inner_el, index, content|
    result = '· %s' % content
    result = newline(result, count: index <= el.children.size - 1 ? 1 : 2)
    result.gsub(/^/, ' ' * list_indent)
  }
end

#convert_xml_commentObject



196
197
198
# File 'lib/ollama/utils/ansi_markdown.rb', line 196

def convert_xml_comment(*)
  ''
end

#convert_xml_piObject



200
201
202
# File 'lib/ollama/utils/ansi_markdown.rb', line 200

def convert_xml_pi(*)
  ''
end

#inner(el, opts, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ollama/utils/ansi_markdown.rb', line 35

def inner(el, opts, &block)
  result = +''
  options = opts.dup.merge(parent: el)
  el.children.each_with_index do |inner_el, index|
    options[:index] = index
    options[:result] = result
    begin
      content = send("convert_#{inner_el.type}", inner_el, options)
      result << (block&.(inner_el, index, content) || content)
    rescue NameError => e
      warning "Caught #{e.class} for #{inner_el.type}"
    end
  end
  result
end

#newline(text, count: 1) ⇒ Object



212
213
214
# File 'lib/ollama/utils/ansi_markdown.rb', line 212

def newline(text, count: 1)
  text.gsub(/\n*\z/, ?\n * count)
end