Class: Kramdown::ANSI

Inherits:
Converter::Base
  • Object
show all
Includes:
Width, Term::ANSIColor
Defined in:
lib/kramdown/ansi.rb,
lib/kramdown/ansi.rb

Defined Under Namespace

Modules: Pager, Width

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Width

truncate, width, wrap

Constructor Details

#initialize(root, options) ⇒ ANSI

Returns a new instance of ANSI.



34
35
36
# File 'lib/kramdown/ansi.rb', line 34

def initialize(root, options)
  super
end

Class Method Details

.parse(source) ⇒ Object



28
29
30
31
32
# File 'lib/kramdown/ansi.rb', line 28

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



38
39
40
# File 'lib/kramdown/ansi.rb', line 38

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

#convert_a(el, opts) ⇒ Object



88
89
90
91
# File 'lib/kramdown/ansi.rb', line 88

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

#convert_blank(_el, opts) ⇒ Object



62
63
64
# File 'lib/kramdown/ansi.rb', line 62

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

#convert_blockquote(el, opts) ⇒ Object



101
102
103
# File 'lib/kramdown/ansi.rb', line 101

def convert_blockquote(el, opts)
  newline ?

#convert_br(_el, opts) ⇒ Object



211
212
213
# File 'lib/kramdown/ansi.rb', line 211

def convert_br(_el, opts)
  ''
end

#convert_codeblock(el, _opts) ⇒ Object



97
98
99
# File 'lib/kramdown/ansi.rb', line 97

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

#convert_codespan(el, _opts) ⇒ Object



93
94
95
# File 'lib/kramdown/ansi.rb', line 93

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

#convert_em(el, opts) ⇒ Object



84
85
86
# File 'lib/kramdown/ansi.rb', line 84

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

#convert_entity(el, _opts) ⇒ Object



199
200
201
# File 'lib/kramdown/ansi.rb', line 199

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

#convert_header(el, opts) ⇒ Object



70
71
72
# File 'lib/kramdown/ansi.rb', line 70

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

#convert_hr(_el, _opts) ⇒ Object



105
106
107
# File 'lib/kramdown/ansi.rb', line 105

def convert_hr(_el, _opts)
  newline ?

#convert_html_element(el, opts) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/kramdown/ansi.rb', line 141

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



109
110
111
112
113
114
115
# File 'lib/kramdown/ansi.rb', line 109

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



135
136
137
138
139
# File 'lib/kramdown/ansi.rb', line 135

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



126
127
128
129
130
131
132
133
# File 'lib/kramdown/ansi.rb', line 126

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



74
75
76
77
78
# File 'lib/kramdown/ansi.rb', line 74

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



58
59
60
# File 'lib/kramdown/ansi.rb', line 58

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

#convert_smart_quote(el, _opts) ⇒ Object



215
216
217
# File 'lib/kramdown/ansi.rb', line 215

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

#convert_strong(el, opts) ⇒ Object



80
81
82
# File 'lib/kramdown/ansi.rb', line 80

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

#convert_table(el, opts) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/kramdown/ansi.rb', line 151

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



173
174
175
176
# File 'lib/kramdown/ansi.rb', line 173

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

#convert_td(el, opts) ⇒ Object



195
196
197
# File 'lib/kramdown/ansi.rb', line 195

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

#convert_text(el, _opts) ⇒ Object



66
67
68
# File 'lib/kramdown/ansi.rb', line 66

def convert_text(el, _opts)
  el.value
end

#convert_tfoot(el, opts) ⇒ Object



178
179
180
# File 'lib/kramdown/ansi.rb', line 178

def convert_tfoot(el, opts)
  ''
end

#convert_thead(el, opts) ⇒ Object



166
167
168
169
170
171
# File 'lib/kramdown/ansi.rb', line 166

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



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/kramdown/ansi.rb', line 182

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



117
118
119
120
121
122
123
124
# File 'lib/kramdown/ansi.rb', line 117

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



203
204
205
# File 'lib/kramdown/ansi.rb', line 203

def convert_xml_comment(*)
  ''
end

#convert_xml_piObject



207
208
209
# File 'lib/kramdown/ansi.rb', line 207

def convert_xml_pi(*)
  ''
end

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kramdown/ansi.rb', line 42

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



219
220
221
# File 'lib/kramdown/ansi.rb', line 219

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