mdless 2.1.44 → 2.1.46

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,7 +24,7 @@ module Redcarpet
24
24
  end
25
25
 
26
26
  def xc
27
- x + color('text')
27
+ x + color("text")
28
28
  end
29
29
 
30
30
  def x
@@ -36,16 +36,16 @@ module Redcarpet
36
36
  input.split(/\n/).map do |line|
37
37
  if first
38
38
  if line =~ /^\+-+/
39
- line.gsub!(/^/, color('table border'))
39
+ line.gsub!(/^/, color("table border"))
40
40
  else
41
41
  first = false
42
- line.gsub!(/\|/, "#{color('table border')}|#{color('table header')}")
42
+ line.gsub!(/\|/, "#{color("table border")}|#{color("table header")}")
43
43
  end
44
44
  elsif line.strip =~ /^[|:\- +]+$/
45
- line.gsub!(/^(.*)$/, "#{color('table border')}\\1#{color('table color')}")
46
- line.gsub!(/([:\-+]+)/, "#{color('table divider')}\\1#{color('table border')}")
45
+ line.gsub!(/^(.*)$/, "#{color("table border")}\\1#{color("table color")}")
46
+ line.gsub!(/([:\-+]+)/, "#{color("table divider")}\\1#{color("table border")}")
47
47
  else
48
- line.gsub!(/\|/, "#{color('table border')}|#{color('table color')}")
48
+ line.gsub!(/\|/, "#{color("table border")}|#{color("table color")}")
49
49
  end
50
50
  end.join("\n")
51
51
  end
@@ -60,7 +60,7 @@ module Redcarpet
60
60
 
61
61
  def code_bg(input, width)
62
62
  input.split(/\n/).map do |line|
63
- tail = line.uncolor.length < width ? "\u00A0" * (width - line.uncolor.length) : ''
63
+ tail = line.uncolor.length < width ? "\u00A0" * (width - line.uncolor.length) : ""
64
64
  "#{x}#{line}#{tail}#{x}"
65
65
  end.join("\n")
66
66
  end
@@ -96,41 +96,40 @@ module Redcarpet
96
96
  # end.join("\n").blackout(MDLess.theme['code_block']['bg']) + "#{xc}\n"
97
97
  # end
98
98
 
99
- if MDLess.options[:syntax_higlight] && !exec_available('pygmentize')
100
- MDLess.log.error('Syntax highlighting requested by pygmentize is not available')
99
+ if MDLess.options[:syntax_higlight] && !exec_available("pygmentize")
100
+ MDLess.log.error("Syntax highlighting requested by pygmentize is not available")
101
101
  MDLess.options[:syntax_higlight] = false
102
102
  end
103
103
 
104
104
  if MDLess.options[:syntax_higlight]
105
- pyg = TTY::Which.which('pygmentize')
106
- lexer = language&.valid_lexer? ? "-l #{language}" : '-g'
105
+ pyg = TTY::Which.which("pygmentize")
106
+ lexer = language&.valid_lexer? ? "-l #{language}" : "-g"
107
107
  begin
108
- pygments_theme = MDLess.options[:pygments_theme] || MDLess.theme['code_block']['pygments_theme']
108
+ pygments_theme = MDLess.options[:pygments_theme] || MDLess.theme["code_block"]["pygments_theme"]
109
109
 
110
110
  unless pygments_theme.valid_pygments_theme?
111
111
  MDLess.log.error("Invalid Pygments theme #{pygments_theme}, defaulting to 'default' for highlighting")
112
- pygments_theme = 'default'
112
+ pygments_theme = "default"
113
113
  end
114
114
 
115
115
  cmd = [
116
116
  "#{pyg} -f terminal256",
117
117
  "-O style=#{pygments_theme}",
118
118
  lexer,
119
- '2> /dev/null'
120
- ].join(' ')
119
+ "2> /dev/null",
120
+ ].join(" ")
121
121
  hilite, s = Open3.capture2(cmd,
122
122
  stdin_data: code_block)
123
123
 
124
124
  if s.success?
125
125
  hilite = xc + hilite.split(/\n/).map do |l|
126
126
  [
127
- color('code_block marker'),
128
- MDLess.theme['code_block']['character'],
129
- "#{color('code_block bg')}#{l}#{xc}"
127
+ color("code_block marker"),
128
+ MDLess.theme["code_block"]["character"],
129
+ "#{color("code_block bg")}#{l}#{xc}",
130
130
  ].join
131
- end.join("\n").blackout(MDLess.theme['code_block']['bg']) + "#{xc}\n"
131
+ end.join("\n").blackout(MDLess.theme["code_block"]["bg"]) + "#{xc}\n"
132
132
  end
133
-
134
133
  rescue StandardError => e
135
134
  MDLess.log.error(e)
136
135
  hilite = code_block
@@ -138,32 +137,32 @@ module Redcarpet
138
137
  else
139
138
  hilite = code_block.split(/\n/).map do |line|
140
139
  [
141
- color('code_block marker'),
142
- MDLess.theme['code_block']['character'],
143
- color('code_block color'),
140
+ color("code_block marker"),
141
+ MDLess.theme["code_block"]["character"],
142
+ color("code_block color"),
144
143
  line,
145
- xc
144
+ xc,
146
145
  ].join
147
- end.join("\n").blackout(MDLess.theme['code_block']['bg']) + "#{xc}\n"
146
+ end.join("\n").blackout(MDLess.theme["code_block"]["bg"]) + "#{xc}\n"
148
147
  end
149
148
 
150
149
  top_border = if language.nil? || language.empty?
151
- '-' * longest_line
152
- else
153
- "--[ #{language} ]#{'-' * (longest_line - 6 - language.length)}"
154
- end
150
+ "-" * longest_line
151
+ else
152
+ "--[ #{language} ]#{"-" * (longest_line - 6 - language.length)}"
153
+ end
155
154
  [
156
155
  xc,
157
- color('code_block border'),
156
+ color("code_block border"),
158
157
  top_border,
159
158
  xc,
160
159
  "\n",
161
- color('code_block color'),
160
+ color("code_block color"),
162
161
  code_bg(hilite.chomp, longest_line),
163
162
  "\n",
164
- color('code_block border'),
165
- '-' * longest_line,
166
- xc
163
+ color("code_block border"),
164
+ "-" * longest_line,
165
+ xc,
167
166
  ].join
168
167
  end
169
168
 
@@ -200,56 +199,56 @@ module Redcarpet
200
199
 
201
200
  def block_quote(quote)
202
201
  ret = "\n\n"
203
- quote.strip.wrap(MDLess.cols, color('blockquote color')).split(/\n/).each do |line|
202
+ quote.strip.wrap(MDLess.cols, color("blockquote color")).split(/\n/).each do |line|
204
203
  ret += [
205
- color('blockquote marker color'),
206
- MDLess.theme['blockquote']['marker']['character'],
207
- color('blockquote color'),
208
- ' ',
204
+ color("blockquote marker color"),
205
+ MDLess.theme["blockquote"]["marker"]["character"],
206
+ color("blockquote color"),
207
+ " ",
209
208
  line,
210
- "\n"
211
- ].join('')
209
+ "\n",
210
+ ].join("")
212
211
  end
213
212
  "#{ret}\n\n"
214
213
  end
215
214
 
216
215
  def block_html(raw_html)
217
- "#{color('html color')}#{color_tags(raw_html)}#{xc}"
216
+ "#{color("html color")}#{color_tags(raw_html)}#{xc}"
218
217
  end
219
218
 
220
219
  def header(text, header_level)
221
- pad = ''
222
- ansi = ''
220
+ pad = ""
221
+ ansi = ""
223
222
  text.clean_header_ids!
224
- uncolored = text.uncolor.gsub(/<<(pre|post)\d+>>/, '')
223
+ uncolored = text.uncolor.gsub(/<<(pre|post)\d+>>/, "")
225
224
  uncolored.sub!(/\[(.*?)\]\(.*?\)/, '[\1][xxx]') if MDLess.options[:links] != :inline
226
225
 
227
226
  text_length = uncolored.length
228
227
  case header_level
229
228
  when 1
230
- ansi = color('h1 color')
231
- pad = color('h1 pad')
232
- char = MDLess.theme['h1']['pad_char'] || '='
229
+ ansi = color("h1 color")
230
+ pad = color("h1 pad")
231
+ char = MDLess.theme["h1"]["pad_char"] || "="
233
232
  pad += text_length + 2 > MDLess.cols ? char * text_length : char * (MDLess.cols - (text_length + 1))
234
233
  when 2
235
- ansi = color('h2 color')
236
- pad = color('h2 pad')
237
- char = MDLess.theme['h2']['pad_char'] || '-'
234
+ ansi = color("h2 color")
235
+ pad = color("h2 pad")
236
+ char = MDLess.theme["h2"]["pad_char"] || "-"
238
237
  pad += text_length + 2 > MDLess.cols ? char * text_length : char * (MDLess.cols - (text_length + 1))
239
238
  when 3
240
- ansi = color('h3 color')
239
+ ansi = color("h3 color")
241
240
  when 4
242
- ansi = color('h4 color')
241
+ ansi = color("h4 color")
243
242
  when 5
244
- ansi = color('h5 color')
243
+ ansi = color("h5 color")
245
244
  else
246
- ansi = color('h6 color')
245
+ ansi = color("h6 color")
247
246
  end
248
247
 
249
248
  # If we're in iTerm and not paginating, add
250
249
  # iTerm Marks for navigation on h1-3
251
250
  if header_level < 4 &&
252
- ENV['TERM_PROGRAM'] =~ /^iterm/i &&
251
+ ENV["TERM_PROGRAM"] =~ /^iterm/i &&
253
252
  MDLess.options[:pager] == false
254
253
  ansi = "\e]1337;SetMark\a#{ansi}"
255
254
  end
@@ -258,20 +257,20 @@ module Redcarpet
258
257
  end
259
258
 
260
259
  def hrule()
261
- "\n\n#{color('hr color')}#{'_' * MDLess.cols}#{xc}\n\n"
260
+ "\n\n#{color("hr color")}#{"_" * MDLess.cols}#{xc}\n\n"
262
261
  end
263
262
 
264
263
  def paragraph(text)
265
264
  text.scrub!
266
265
  out = if MDLess.options[:preserve_linebreaks]
267
- "#{xc}#{text.gsub(/ +/, ' ').strip}#{xc}#{x}\n\n"
268
- else
269
- if text.uncolor =~ / {2,}$/ || text.uncolor =~ /^%/
270
- "#{xc}#{text.gsub(/ +/, ' ').strip}#{xc}#{x}\n"
271
- else
272
- "#{xc}#{text.gsub(/ +/, ' ').gsub(/\n+(?![:-])/, ' ').strip}#{xc}#{x}\n\n"
273
- end
274
- end
266
+ "#{xc}#{text.gsub(/ +/, " ").strip}#{xc}#{x}\n\n"
267
+ else
268
+ if text.uncolor =~ / {2,}$/ || text.uncolor =~ /^%/
269
+ "#{xc}#{text.gsub(/ +/, " ").strip}#{xc}#{x}\n"
270
+ else
271
+ "#{xc}#{text.gsub(/ +/, " ").gsub(/\n+(?![:-])/, " ").strip}#{xc}#{x}\n\n"
272
+ end
273
+ end
275
274
  if MDLess.options[:at_tags] || MDLess.options[:taskpaper]
276
275
  highlight_tags(out)
277
276
  else
@@ -280,7 +279,7 @@ module Redcarpet
280
279
  end
281
280
 
282
281
  def uncolor_grafs(text)
283
- text.gsub(/#{Regexp.escape(color('text'))}/, color('list color'))
282
+ text.gsub(/#{Regexp.escape(color("text"))}/, color("list color"))
284
283
  end
285
284
 
286
285
  @table_cols = nil
@@ -289,15 +288,15 @@ module Redcarpet
289
288
  @header_row.map do |alignment|
290
289
  case alignment
291
290
  when :left
292
- '|:---'
291
+ "|:---"
293
292
  when :right
294
- '|---:'
293
+ "|---:"
295
294
  when :center
296
- '|:--:'
295
+ "|:--:"
297
296
  else
298
- '|----'
297
+ "|----"
299
298
  end
300
- end.join('') + '|'
299
+ end.join("") + "|"
301
300
  end
302
301
 
303
302
  def table(header, body)
@@ -305,8 +304,8 @@ module Redcarpet
305
304
  "#{header}",
306
305
  table_header_row,
307
306
  "|\n",
308
- "#{body}\n\n"
309
- ].join(''))
307
+ "#{body}\n\n",
308
+ ].join(""))
310
309
  @header_row = []
311
310
  res = formatted.to_md
312
311
  "#{color_table(res)}\n\n"
@@ -329,71 +328,71 @@ module Redcarpet
329
328
  def autolink(link, _)
330
329
  [
331
330
  pre_element,
332
- color('link brackets'),
333
- '<',
334
- color('link url'),
331
+ color("link brackets"),
332
+ "<",
333
+ color("link url"),
335
334
  link,
336
- color('link brackets'),
337
- '>',
335
+ color("link brackets"),
336
+ ">",
338
337
  xc,
339
- post_element
340
- ].join('')
338
+ post_element,
339
+ ].join("")
341
340
  end
342
341
 
343
342
  def codespan(code)
344
343
  out = [
345
344
  pre_element,
346
- color('code_span marker'),
347
- MDLess.theme['code_span']['character'],
348
- color('code_span color'),
345
+ color("code_span marker"),
346
+ MDLess.theme["code_span"]["character"],
347
+ color("code_span color"),
349
348
  code,
350
- color('code_span marker'),
351
- MDLess.theme['code_span']['character'],
349
+ color("code_span marker"),
350
+ MDLess.theme["code_span"]["character"],
352
351
  xc,
353
- post_element
352
+ post_element,
354
353
  ].join
355
354
  end
356
355
 
357
356
  def double_emphasis(text)
358
357
  [
359
358
  pre_element,
360
- color('emphasis bold'),
361
- MDLess.theme['emphasis']['bold_character'],
359
+ color("emphasis bold"),
360
+ MDLess.theme["emphasis"]["bold_character"],
362
361
  text,
363
- MDLess.theme['emphasis']['bold_character'],
362
+ MDLess.theme["emphasis"]["bold_character"],
364
363
  xc,
365
- post_element
364
+ post_element,
366
365
  ].join
367
366
  end
368
367
 
369
368
  def emphasis(text)
370
369
  [
371
370
  pre_element,
372
- color('emphasis italic'),
373
- MDLess.theme['emphasis']['italic_character'],
371
+ color("emphasis italic"),
372
+ MDLess.theme["emphasis"]["italic_character"],
374
373
  text,
375
- MDLess.theme['emphasis']['italic_character'],
374
+ MDLess.theme["emphasis"]["italic_character"],
376
375
  xc,
377
- post_element
376
+ post_element,
378
377
  ].join
379
378
  end
380
379
 
381
380
  def triple_emphasis(text)
382
381
  [
383
382
  pre_element,
384
- color('emphasis bold-italic'),
385
- MDLess.theme['emphasis']['italic_character'],
386
- MDLess.theme['emphasis']['bold_character'],
383
+ color("emphasis bold-italic"),
384
+ MDLess.theme["emphasis"]["italic_character"],
385
+ MDLess.theme["emphasis"]["bold_character"],
387
386
  text,
388
- MDLess.theme['emphasis']['bold_character'],
389
- MDLess.theme['emphasis']['italic_character'],
387
+ MDLess.theme["emphasis"]["bold_character"],
388
+ MDLess.theme["emphasis"]["italic_character"],
390
389
  xc,
391
- post_element
390
+ post_element,
392
391
  ].join
393
392
  end
394
393
 
395
394
  def highlight(text)
396
- "#{pre_element}#{color('highlight')}#{text}#{xc}#{post_element}"
395
+ "#{pre_element}#{color("highlight")}#{text}#{xc}#{post_element}"
397
396
  end
398
397
 
399
398
  def image(link, title, alt_text)
@@ -407,35 +406,35 @@ module Redcarpet
407
406
  def color_link(link, title, content)
408
407
  [
409
408
  pre_element,
410
- color('link brackets'),
411
- '[',
412
- color('link text'),
409
+ color("link brackets"),
410
+ "[",
411
+ color("link text"),
413
412
  content,
414
- color('link brackets'),
415
- '](',
416
- color('link url'),
413
+ color("link brackets"),
414
+ "](",
415
+ color("link url"),
417
416
  link,
418
- title.nil? ? '' : %( "#{title}"),
419
- color('link brackets'),
420
- ')',
417
+ title.nil? ? "" : %( "#{title}"),
418
+ color("link brackets"),
419
+ ")",
421
420
  xc,
422
- post_element
421
+ post_element,
423
422
  ].join
424
423
  end
425
424
 
426
425
  def color_image_tag(link, title, alt_text)
427
426
  image = [
428
- color('image brackets'),
429
- '[',
430
- color('image title'),
427
+ color("image brackets"),
428
+ "[",
429
+ color("image title"),
431
430
  alt_text,
432
- color('image brackets'),
433
- '](',
434
- color('image url'),
431
+ color("image brackets"),
432
+ "](",
433
+ color("image url"),
435
434
  link,
436
- title.nil? ? '' : %( "#{title}"),
437
- color('image brackets'),
438
- ')'
435
+ title.nil? ? "" : %( "#{title}"),
436
+ color("image brackets"),
437
+ ")",
439
438
  ].join
440
439
 
441
440
  @@links << {
@@ -443,67 +442,67 @@ module Redcarpet
443
442
  url: link,
444
443
  title: title,
445
444
  content: alt_text,
446
- image: true
445
+ image: true,
447
446
  }
448
447
 
449
448
  [
450
- color('image bang'),
451
- '!',
449
+ color("image bang"),
450
+ "!",
452
451
  image,
453
- xc
452
+ xc,
454
453
  ].join
455
454
  end
456
455
 
457
456
  def color_link_reference(link, idx, content)
458
457
  [
459
458
  pre_element,
460
- color('link brackets'),
461
- '[',
462
- color('link text'),
459
+ color("link brackets"),
460
+ "[",
461
+ color("link text"),
463
462
  content,
464
- color('link brackets'),
465
- '][',
466
- color('link url'),
463
+ color("link brackets"),
464
+ "][",
465
+ color("link url"),
467
466
  idx,
468
- color('link brackets'),
469
- ']',
467
+ color("link brackets"),
468
+ "]",
470
469
  xc,
471
- post_element
470
+ post_element,
472
471
  ].join
473
472
  end
474
473
 
475
474
  def color_reference_link(link, title, content, image: false)
476
475
  [
477
- color('link brackets'),
478
- '[',
479
- color('link text'),
476
+ color("link brackets"),
477
+ "[",
478
+ color("link text"),
480
479
  content,
481
- color('link brackets'),
482
- ']:',
483
- color('text'),
484
- ' ',
485
- image ? color('image url') : color('link url'),
480
+ color("link brackets"),
481
+ "]:",
482
+ color("text"),
483
+ " ",
484
+ image ? color("image url") : color("link url"),
486
485
  link,
487
- title.nil? ? '' : %( "#{title}"),
488
- xc
486
+ title.nil? ? "" : %( "#{title}"),
487
+ xc,
489
488
  ].join
490
489
  end
491
490
 
492
491
  def color_image_reference(idx, content)
493
492
  [
494
493
  pre_element,
495
- color('image brackets'),
496
- '[',
497
- color('image title'),
494
+ color("image brackets"),
495
+ "[",
496
+ color("image title"),
498
497
  content,
499
- color('image brackets'),
500
- '][',
501
- color('link url'),
498
+ color("image brackets"),
499
+ "][",
500
+ color("link url"),
502
501
  idx,
503
- color('image brackets'),
504
- ']',
502
+ color("image brackets"),
503
+ "]",
505
504
  xc,
506
- post_element
505
+ post_element,
507
506
  ].join
508
507
  end
509
508
 
@@ -513,25 +512,25 @@ module Redcarpet
513
512
  link: res,
514
513
  url: link,
515
514
  title: title,
516
- content: content
515
+ content: content,
517
516
  }
518
517
  res
519
518
  end
520
519
 
521
520
  def color_tags(html)
522
- html.gsub(%r{((?!<)</?\w+( [^>]+)?>)}, "#{color('html brackets')}\\1#{xc}")
521
+ html.gsub(%r{((?!<)</?\w+( [^>]+)?>)}, "#{color("html brackets")}\\1#{xc}")
523
522
  end
524
523
 
525
524
  def raw_html(raw_html)
526
- "#{pre_element}#{color('html color')}#{color_tags(raw_html)}#{xc}#{post_element}"
525
+ "#{pre_element}#{color("html color")}#{color_tags(raw_html)}#{xc}#{post_element}"
527
526
  end
528
527
 
529
528
  def strikethrough(text)
530
- "#{pre_element}#{color('strikethrough')}#{text}#{xc}#{post_element}"
529
+ "#{pre_element}#{color("strikethrough")}#{text}#{xc}#{post_element}"
531
530
  end
532
531
 
533
532
  def superscript(text)
534
- "#{pre_element}#{color('super')}^#{text}#{xc}#{post_element}"
533
+ "#{pre_element}#{color("super")}^#{text}#{xc}#{post_element}"
535
534
  end
536
535
 
537
536
  def footnotes(text)
@@ -547,20 +546,20 @@ module Redcarpet
547
546
  def color_footnote_def(idx)
548
547
  text = @@footnotes[idx]
549
548
  [
550
- color('footnote brackets'),
549
+ color("footnote brackets"),
551
550
  "[",
552
- color('footnote caret'),
551
+ color("footnote caret"),
553
552
  "^",
554
- color('footnote title'),
553
+ color("footnote title"),
555
554
  idx,
556
- color('footnote brackets'),
555
+ color("footnote brackets"),
557
556
  "]:",
558
- color('footnote note'),
559
- ' ',
557
+ color("footnote note"),
558
+ " ",
560
559
  text.uncolor.strip,
561
560
  xc,
562
- "\n"
563
- ].join('')
561
+ "\n",
562
+ ].join("")
564
563
  end
565
564
 
566
565
  def footnote_def(text, idx)
@@ -570,18 +569,18 @@ module Redcarpet
570
569
  def footnote_ref(text)
571
570
  [
572
571
  pre_element,
573
- color('footnote title'),
572
+ color("footnote title"),
574
573
  "[^#{text}]",
575
574
  xc,
576
- post_element
577
- ].join('')
575
+ post_element,
576
+ ].join("")
578
577
  end
579
578
 
580
579
  def insert_footnotes(input)
581
580
  input.split(/\n/).map do |line|
582
581
  notes = line.to_enum(:scan, /\[\^(?<ref>\d+)\]/).map { Regexp.last_match }
583
582
  if notes.count.positive?
584
- footnotes = notes.map { |n| color_footnote_def(n['ref'].to_i) }.join("\n")
583
+ footnotes = notes.map { |n| color_footnote_def(n["ref"].to_i) }.join("\n")
585
584
  "#{line}\n\n#{footnotes}\n\n\n"
586
585
  else
587
586
  line
@@ -609,7 +608,7 @@ module Redcarpet
609
608
 
610
609
  lines = input.split(/\n/)
611
610
  line1 = lines.shift
612
- pre = ' '
611
+ pre = " "
613
612
 
614
613
  body = lines.map { |l| "#{pre}#{l.rstrip}" }.join("\n")
615
614
  "#{line1}\n#{body}"
@@ -617,26 +616,26 @@ module Redcarpet
617
616
 
618
617
  def color_list_item(indent, content, type, counter)
619
618
  out = case type
620
- when :unordered
621
- [
622
- ' ' * indent,
623
- color('list bullet'),
624
- MDLess.theme['list']['ul_char'].strip,
625
- ' ',
626
- color('list color'),
627
- indent_lines(content).strip,
628
- xc
629
- ].join
630
- when :ordered
631
- [
632
- ' ' * indent,
633
- color('list number'),
634
- "#{counter}. ",
635
- color('list color'),
636
- indent_lines(content).strip,
637
- xc
638
- ].join
639
- end
619
+ when :unordered
620
+ [
621
+ " " * indent,
622
+ color("list bullet"),
623
+ MDLess.theme["list"]["ul_char"].strip,
624
+ " ",
625
+ color("list color"),
626
+ indent_lines(content).strip,
627
+ xc,
628
+ ].join
629
+ when :ordered
630
+ [
631
+ " " * indent,
632
+ color("list number"),
633
+ "#{counter}. ",
634
+ color("list color"),
635
+ indent_lines(content).strip,
636
+ xc,
637
+ ].join
638
+ end
640
639
  if MDLess.options[:at_tags] || MDLess.options[:taskpaper]
641
640
  color_tags(out)
642
641
  else
@@ -657,7 +656,7 @@ module Redcarpet
657
656
  def nest_lists(input, indent = 0)
658
657
  input.gsub!(%r{<<list(?<id>\d+)-(?<type>.*?)>>(?<content>.*?)<</list\k<id>>>}m) do
659
658
  m = Regexp.last_match
660
- lines = m['content'].strip.split(/\n/)
659
+ lines = m["content"].strip.split(/\n/)
661
660
 
662
661
  list = nest_lists(lines.map do |l|
663
662
  outdent = l.scan(%r{<</list\d+>>}).count
@@ -667,18 +666,18 @@ module Redcarpet
667
666
  end.join("\n"), indent)
668
667
  next if list.nil?
669
668
 
670
- "<<main#{m['id']}>>#{list}<</main#{m['id']}>>\n\n"
669
+ "<<main#{m["id"]}>>#{list}<</main#{m["id"]}>>\n\n"
671
670
  end
672
671
 
673
672
  input.gsub(/^(?<indent> +)<<main(?<id>\d+)>>(?<content>.*?)<<\/main\k<id>>>/m) do
674
673
  m = Regexp.last_match
675
- "#{m['indent']}#{m['content']}"
674
+ "#{m["indent"]}#{m["content"]}"
676
675
  end
677
676
  end
678
677
 
679
678
  def normalize_indentation(line)
680
679
  line.gsub(/^([ \t]+)/) do |pre|
681
- pre.gsub(/\t/, ' ')
680
+ pre.gsub(/\t/, " ")
682
681
  end
683
682
  end
684
683
 
@@ -686,8 +685,8 @@ module Redcarpet
686
685
  content.gsub(%r{^(?<indent> *)<<listitem(?<id>\d+)-(?<type>(?:un)?ordered)>>(?<content>.*?)<</listitem\k<id>>>}m) do
687
686
  m = Regexp.last_match
688
687
 
689
- indent = m['indent'].length
690
- if m['type'].to_sym == :ordered
688
+ indent = m["indent"].length
689
+ if m["type"].to_sym == :ordered
691
690
  if indent == last_indent
692
691
  levels[indent] ||= 0
693
692
  levels[indent] += 1
@@ -701,15 +700,15 @@ module Redcarpet
701
700
  end
702
701
  end
703
702
 
704
- content = m['content'] =~/<<listitem/ ? fix_items(m['content'], indent, levels) : m['content']
705
- color_list_item(indent, uncolor_grafs(content), m['type'].to_sym, levels[indent])
703
+ content = m["content"] =~ /<<listitem/ ? fix_items(m["content"], indent, levels) : m["content"]
704
+ color_list_item(indent, uncolor_grafs(content), m["type"].to_sym, levels[indent])
706
705
  end
707
706
  end
708
707
 
709
708
  def fix_list_items(input)
710
709
  input.gsub(%r{<<main(?<id>\d+)>>(?<content>.*?)<</main\k<id>>>}m) do
711
710
  m = Regexp.last_match
712
- fix_items(m['content'])
711
+ fix_items(m["content"])
713
712
  end
714
713
  end
715
714
 
@@ -732,9 +731,9 @@ module Redcarpet
732
731
  title = h[2]
733
732
  end
734
733
  @headers << [
735
- '#' * hlevel,
734
+ "#" * hlevel,
736
735
  title,
737
- h[0]
736
+ h[0],
738
737
  ]
739
738
  end
740
739
  end
@@ -748,16 +747,16 @@ module Redcarpet
748
747
  MDLess.meta = {}
749
748
  first_line = input.split("\n").first
750
749
  if first_line =~ /(?i-m)^---[ \t]*?$/
751
- MDLess.log.info('Found YAML')
750
+ MDLess.log.info("Found YAML")
752
751
  # YAML
753
752
  in_yaml = true
754
753
  input.sub!(/(?i-m)^---[ \t]*\n(?<content>(?:[\s\S]*?))\n[-.]{3}[ \t]*\n/m) do
755
754
  m = Regexp.last_match
756
- MDLess.log.info('Processing YAML header')
755
+ MDLess.log.info("Processing YAML header")
757
756
  begin
758
- MDLess.meta = YAML.load(m['content']).each_with_object({}) { |(k, v), h| h[k.downcase] = v }
757
+ MDLess.meta = YAML.load(m["content"]).each_with_object({}) { |(k, v), h| h[k.downcase] = v }
759
758
  rescue Psych::DisallowedClass => e
760
- @log.error('Error reading YAML header')
759
+ @log.error("Error reading YAML header")
761
760
  @log.error(e)
762
761
  MDLess.meta = {}
763
762
  rescue StandardError => e
@@ -769,10 +768,10 @@ module Redcarpet
769
768
  longest = longest < MDLess.cols ? longest + 1 : MDLess.cols
770
769
  lines.map do |line|
771
770
  if line =~ /^[-.]{3}\s*$/
772
- line = "#{color('metadata marker')}#{'%' * longest}"
771
+ line = "#{color("metadata marker")}#{"%" * longest}"
773
772
  else
774
773
  line.sub!(/^(.*?:)[ \t]+(\S)/, '\1 \2')
775
- line = "#{color('metadata marker')}% #{color('metadata color')}#{line}"
774
+ line = "#{color("metadata marker")}% #{color("metadata color")}#{line}"
776
775
  end
777
776
  line += "\u00A0" * (longest - line.uncolor.strip.length) if (longest - line.uncolor.strip.length).positive?
778
777
  line + xc
@@ -781,7 +780,7 @@ module Redcarpet
781
780
  end
782
781
 
783
782
  if !in_yaml && first_line =~ /(?i-m)^[\w ]+:\s+\S+/
784
- MDLess.log.info('Found MMD Headers')
783
+ MDLess.log.info("Found MMD Headers")
785
784
  input.sub!(/(?i-m)^([\S ]+:[\s\S]*?)+(?=\n *\n)/) do |mmd|
786
785
  lines = mmd.split(/\n/)
787
786
  return mmd if lines.count > 20
@@ -793,11 +792,11 @@ module Redcarpet
793
792
  line.sub!(/^(.*?:)[ \t]+(\S)/, '\1 \2')
794
793
  parts = line.match(/^[ \t]*(\S.*?):[ \t]+(\S.*?)$/)
795
794
  if parts
796
- key = parts[1].gsub(/[^a-z0-9\-_]/i, '')
795
+ key = parts[1].gsub(/[^a-z0-9\-_]/i, "")
797
796
  value = parts[2].strip
798
797
  MDLess.meta[key] = value
799
798
  end
800
- line = "#{color('metadata marker')}%#{color('metadata color')}#{line}"
799
+ line = "#{color("metadata marker")}%#{color("metadata color")}#{line}"
801
800
  line += "\u00A0" * (longest - line.uncolor.strip.length) if (longest - line.uncolor.strip.length).positive?
802
801
  line + xc
803
802
  end.join("\n") + "#{xc}\n"
@@ -808,15 +807,15 @@ module Redcarpet
808
807
  end
809
808
 
810
809
  def mmd_transclude(input)
811
- return input unless MDLess.file || MDLess.meta.key?('transcludebase')
810
+ return input unless MDLess.file || MDLess.meta.key?("transcludebase")
812
811
 
813
812
  input.gsub(/^{{(.*?)}}/) do |m|
814
813
  filename = Regexp.last_match(1).strip
815
- file = if MDLess.meta.key?('transcludebase')
816
- File.join(File.expand_path(MDLess.meta['transcludebase']), filename)
817
- else
818
- File.join(File.dirname(MDLess.file), filename)
819
- end
814
+ file = if MDLess.meta.key?("transcludebase")
815
+ File.join(File.expand_path(MDLess.meta["transcludebase"]), filename)
816
+ else
817
+ File.join(File.dirname(MDLess.file), filename)
818
+ end
820
819
  File.exist?(file) ? "\n\n#{mmd_transclude(IO.read(file).remove_meta)}\n\n" : m
821
820
  end
822
821
  end
@@ -851,7 +850,7 @@ module Redcarpet
851
850
 
852
851
  if MDLess.options[:section]
853
852
  new_content = []
854
- MDLess.log.info("Matching section(s) #{MDLess.options[:section].join(', ')}")
853
+ MDLess.log.info("Matching section(s) #{MDLess.options[:section].join(", ")}")
855
854
  MDLess.options[:section].each do |sect|
856
855
  comparison = MDLess.options[:section][0].is_a?(String) ? :regex : :numeric
857
856
 
@@ -888,7 +887,7 @@ module Redcarpet
888
887
  # definition lists
889
888
  input.gsub!(/(?mi)(?<=\n|\A)(?<term>(?!<:)[^\n]+)(?<def>(\n+: [^\n]+)+)/) do
890
889
  m = Regexp.last_match
891
- "#{color('dd term')}#{m['term']}#{xc}#{color('dd color')}#{color_dd_def(m['def'])}"
890
+ "#{color("dd term")}#{m["term"]}#{xc}#{color("dd color")}#{color_dd_def(m["def"])}"
892
891
  end
893
892
 
894
893
  input
@@ -898,11 +897,11 @@ module Redcarpet
898
897
  input.gsub(/(?<=\n|\A)(?::)\s+(.*)/) do
899
898
  m = Regexp.last_match
900
899
  [
901
- color('dd marker'),
900
+ color("dd marker"),
902
901
  ": ",
903
- color('dd color'),
902
+ color("dd color"),
904
903
  m[1],
905
- xc
904
+ xc,
906
905
  ].join
907
906
  end
908
907
  end
@@ -910,7 +909,7 @@ module Redcarpet
910
909
  def color_links(input)
911
910
  input.gsub(/(?mi)(?<!\\e)\[(?<text>[^\[]+)\]\((?<url>\S+)(?: +"(?<title>.*?)")? *\)/) do
912
911
  m = Regexp.last_match
913
- color_link(m['url'].uncolor, m['title']&.uncolor, m['text'].uncolor)
912
+ color_link(m["url"].uncolor, m["title"]&.uncolor, m["text"].uncolor)
914
913
  end
915
914
  end
916
915
 
@@ -924,13 +923,26 @@ module Redcarpet
924
923
  links_added = false
925
924
 
926
925
  @@links.each do |link|
927
- if graf =~ /#{Regexp.escape(link[:link].gsub(/\n/, ' '))}/
926
+ if graf =~ /#{Regexp.escape(link[:link].gsub(/\n/, " "))}/
927
+ table = graf.uncolor =~ /^ *\|/
928
928
  url = link[:url].uncolor
929
929
  content = link[:content]
930
930
  title = link[:title]&.uncolor
931
931
  image = link.key?(:image) && link[:image] ? true : false
932
932
  colored_link = image ? color_image_reference(counter, content) : color_link_reference(url, counter, content)
933
- graf.gsub!(/#{Regexp.escape(link[:link].gsub(/\n/, ' '))}/, colored_link)
933
+ if table
934
+ diff = link[:link].gsub(/\n/, " ").uncolor.length - colored_link.uncolor.gsub(/\n/, " ").length
935
+ graf.gsub!(/(?<=\|)([^\|]*?)#{Regexp.escape(link[:link].gsub(/\n/, " "))}(.*?)(?=\|)/) do
936
+ before = Regexp.last_match(1)
937
+ after = Regexp.last_match(2)
938
+ surround = "#{before}#{after}"
939
+ puts "**#{surround}**"
940
+ diff += 2 if surround.uncolor.gsub(/#{CLIMarkdown::MDTableCleanup::PAD_CHAR}*/, "").strip.length.zero?
941
+ "#{before}#{colored_link}#{after}#{" " * diff}"
942
+ end
943
+ else
944
+ graf.gsub!(/#{Regexp.escape(link[:link].gsub(/\n/, " "))}/, colored_link)
945
+ end
934
946
  if MDLess.options[:links] == :paragraph
935
947
  if links_added
936
948
  graf += "\n#{color_reference_link(url, title, counter, image: image)}"
@@ -957,56 +969,56 @@ module Redcarpet
957
969
  def fix_colors(input)
958
970
  input.gsub(/<<pre(?<id>\d+)>>(?<content>.*?)<<post\k<id>>>/m) do
959
971
  m = Regexp.last_match
960
- pre = m.pre_match.gsub(/<<pre(?<id>\d+)>>.*?<<post\k<id>>>/m, '')
972
+ pre = m.pre_match.gsub(/<<pre(?<id>\d+)>>.*?<<post\k<id>>>/m, "")
961
973
  last_color = pre.last_color_code
962
974
 
963
- "#{fix_colors(m['content'])}#{last_color}"
964
- end.gsub(/<<(pre|post)\d+>>/, '')
975
+ "#{fix_colors(m["content"])}#{last_color}"
976
+ end.gsub(/<<(pre|post)\d+>>/, "")
965
977
  end
966
978
 
967
979
  def render_images(input)
968
980
  input.gsub(%r{<<img>>(.*?)<</img>>}) do
969
981
  link, title, alt_text = Regexp.last_match(1).split(/\|\|/)
970
982
 
971
- if (exec_available('imgcat') || exec_available('chafa')) && MDLess.options[:local_images]
972
- if exec_available('imgcat')
973
- MDLess.log.info('Using imgcat for image rendering')
974
- elsif exec_available('chafa')
975
- MDLess.log.info('Using chafa for image rendering')
983
+ if (exec_available("imgcat") || exec_available("chafa")) && MDLess.options[:local_images]
984
+ if exec_available("imgcat")
985
+ MDLess.log.info("Using imgcat for image rendering")
986
+ elsif exec_available("chafa")
987
+ MDLess.log.info("Using chafa for image rendering")
976
988
  end
977
989
  img_path = link
978
990
  if img_path =~ /^http/ && MDLess.options[:remote_images]
979
- if exec_available('imgcat')
980
- MDLess.log.info('Using imgcat for image rendering')
991
+ if exec_available("imgcat")
992
+ MDLess.log.info("Using imgcat for image rendering")
981
993
  begin
982
994
  res, s = Open3.capture2(%(curl -sS "#{img_path}" 2> /dev/null | imgcat))
983
995
 
984
996
  if s.success?
985
- pre = !alt_text.nil? ? " #{c(%i[d blue])}[#{alt_text.strip}]\n" : ''
986
- post = !title.nil? ? "\n #{c(%i[b blue])}-- #{title} --" : ''
997
+ pre = !alt_text.nil? ? " #{c(%i[d blue])}[#{alt_text.strip}]\n" : ""
998
+ post = !title.nil? ? "\n #{c(%i[b blue])}-- #{title} --" : ""
987
999
  result = pre + res + post
988
1000
  end
989
1001
  rescue StandardError => e
990
1002
  MDLess.log.error(e)
991
1003
  end
992
- elsif exec_available('chafa')
993
- MDLess.log.info('Using chafa for image rendering')
994
- term = '-f sixels'
995
- term = ENV['TERMINAL_PROGRAM'] =~ /iterm/i ? '-f iterm' : term
996
- term = ENV['TERMINAL_PROGRAM'] =~ /kitty/i ? '-f kitty' : term
997
- FileUtils.rm_r '.mdless_tmp', force: true if File.directory?('.mdless_tmp')
998
- Dir.mkdir('.mdless_tmp')
999
- Dir.chdir('.mdless_tmp')
1004
+ elsif exec_available("chafa")
1005
+ MDLess.log.info("Using chafa for image rendering")
1006
+ term = "-f sixels"
1007
+ term = ENV["TERMINAL_PROGRAM"] =~ /iterm/i ? "-f iterm" : term
1008
+ term = ENV["TERMINAL_PROGRAM"] =~ /kitty/i ? "-f kitty" : term
1009
+ FileUtils.rm_r ".mdless_tmp", force: true if File.directory?(".mdless_tmp")
1010
+ Dir.mkdir(".mdless_tmp")
1011
+ Dir.chdir(".mdless_tmp")
1000
1012
  `curl -SsO #{img_path} 2> /dev/null`
1001
1013
  tmp_img = File.basename(img_path)
1002
1014
  img = `chafa #{term} "#{tmp_img}"`
1003
- pre = alt_text ? " #{c(%i[d blue])}[#{alt_text.strip}]\n" : ''
1004
- post = title ? "\n #{c(%i[b blue])}-- #{title} --" : ''
1015
+ pre = alt_text ? " #{c(%i[d blue])}[#{alt_text.strip}]\n" : ""
1016
+ post = title ? "\n #{c(%i[b blue])}-- #{title} --" : ""
1005
1017
  result = pre + img + post
1006
- Dir.chdir('..')
1007
- FileUtils.rm_r '.mdless_tmp', force: true
1018
+ Dir.chdir("..")
1019
+ FileUtils.rm_r ".mdless_tmp", force: true
1008
1020
  else
1009
- MDLess.log.warn('No viewer for remote images')
1021
+ MDLess.log.warn("No viewer for remote images")
1010
1022
  end
1011
1023
  else
1012
1024
  if img_path =~ %r{^[~/]}
@@ -1016,14 +1028,14 @@ module Redcarpet
1016
1028
  img_path = File.join(base, img_path)
1017
1029
  end
1018
1030
  if File.exist?(img_path)
1019
- pre = !alt_text.nil? ? " #{c(%i[d blue])}[#{alt_text.strip}]\n" : ''
1020
- post = !title.nil? ? "\n #{c(%i[b blue])}-- #{title} --" : ''
1021
- if exec_available('imgcat')
1031
+ pre = !alt_text.nil? ? " #{c(%i[d blue])}[#{alt_text.strip}]\n" : ""
1032
+ post = !title.nil? ? "\n #{c(%i[b blue])}-- #{title} --" : ""
1033
+ if exec_available("imgcat")
1022
1034
  img = `imgcat "#{img_path}"`
1023
- elsif exec_available('chafa')
1024
- term = '-f sixels'
1025
- term = ENV['TERMINAL_PROGRAM'] =~ /iterm/i ? '-f iterm' : term
1026
- term = ENV['TERMINAL_PROGRAM'] =~ /kitty/i ? '-f kitty' : term
1035
+ elsif exec_available("chafa")
1036
+ term = "-f sixels"
1037
+ term = ENV["TERMINAL_PROGRAM"] =~ /iterm/i ? "-f iterm" : term
1038
+ term = ENV["TERMINAL_PROGRAM"] =~ /kitty/i ? "-f kitty" : term
1027
1039
  img = `chafa #{term} "#{img_path}"`
1028
1040
  end
1029
1041
  result = pre + img + post
@@ -1050,36 +1062,36 @@ module Redcarpet
1050
1062
  end
1051
1063
  [
1052
1064
  pre_element,
1053
- color('math brackets'),
1065
+ color("math brackets"),
1054
1066
  brackets[0],
1055
1067
  xc,
1056
- color('math equation'),
1068
+ color("math equation"),
1057
1069
  equat,
1058
- color('math brackets'),
1070
+ color("math brackets"),
1059
1071
  brackets[1],
1060
1072
  xc,
1061
- post_element
1073
+ post_element,
1062
1074
  ].join
1063
1075
  end
1064
1076
  end
1065
1077
 
1066
1078
  def highlight_tags(input)
1067
- tag_color = color('at_tags tag')
1068
- value_color = color('at_tags value')
1079
+ tag_color = color("at_tags tag")
1080
+ value_color = color("at_tags value")
1069
1081
  input.gsub(/(?<pre>\s|m)(?<tag>@[^ \]:;.?!,("'\n]+)(?:(?<lparen>\()(?<value>.*?)(?<rparen>\)))?/) do
1070
1082
  m = Regexp.last_match
1071
1083
  last_color = m.pre_match.last_color_code
1072
1084
  [
1073
- m['pre'],
1085
+ m["pre"],
1074
1086
  tag_color,
1075
- m['tag'],
1076
- m['lparen'],
1087
+ m["tag"],
1088
+ m["lparen"],
1077
1089
  value_color,
1078
- m['value'],
1090
+ m["value"],
1079
1091
  tag_color,
1080
- m['rparen'],
1092
+ m["rparen"],
1081
1093
  xc,
1082
- last_color
1094
+ last_color,
1083
1095
  ].join
1084
1096
  end
1085
1097
  end
@@ -1089,14 +1101,14 @@ module Redcarpet
1089
1101
  content = Regexp.last_match(1)
1090
1102
  [
1091
1103
  pre_element,
1092
- color('link brackets'),
1093
- '[[',
1094
- color('link text'),
1104
+ color("link brackets"),
1105
+ "[[",
1106
+ color("link text"),
1095
1107
  content,
1096
- color('link brackets'),
1097
- ']]',
1108
+ color("link brackets"),
1109
+ "]]",
1098
1110
  xc,
1099
- post_element
1111
+ post_element,
1100
1112
  ].join
1101
1113
  end
1102
1114
  end