mdless 2.0.5 → 2.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ccc91a0517db5cc507c8b0ff8c25eb6973374d8754c1b9e0eaa5c0b87c756c2b
4
- data.tar.gz: 7fcb9c187a276e2b599c1446df99592b506f82fe808e896c2d12611619d1c666
3
+ metadata.gz: 2153423a3c896ca8522fd330478ce3125fd68a2bcc6a8a4c52b0efea31033484
4
+ data.tar.gz: 4b039579d2315650038408b954d00689929530c322441aacca52c13e77f4065f
5
5
  SHA512:
6
- metadata.gz: e0875e8f64bbd79ca3d24a5510ea4a1a7034a2799a468b9d21a67dec592e56a766e4d622ea8c944928ef24e2ad8cf444922a1db2d99522b6662208980e9c8206
7
- data.tar.gz: 5e8c1982080a3e273e1c0f9be224d9b9a5f8e4e059ae74f621d114e3a12371937beb4f141090103a4d25dc925d134c4a72abcf8b5b211376bc96ca0aedec7e34
6
+ metadata.gz: 40fbf0a45ede5519964948b6fc83b1f2f7dd6beea06f2faec2be9b8606d1bfe8d1f176597e49a318a205cae66b1ad855afc0951ec29f0e39f6f662d34535c816
7
+ data.tar.gz: c2adc031a649dfbc3cfd4254df7acfb990f2b86a31bea7f4d58405a7c2853122431b5057dfe5cd225b6510e206a45b2b999f10a372f00e6128b74cce66cf6978
@@ -9,6 +9,8 @@ module Redcarpet
9
9
  @@listid = 0
10
10
  @@footnotes = []
11
11
  @@headers = []
12
+ @@links = []
13
+ @@footer_links = []
12
14
 
13
15
  def xc
14
16
  x + color('text')
@@ -393,11 +395,11 @@ module Redcarpet
393
395
  end
394
396
  end
395
397
 
396
- def linebreak()
397
- "\n"
398
+ def linebreak
399
+ " \n"
398
400
  end
399
401
 
400
- def link(link, title, content)
402
+ def color_link(link, title, content)
401
403
  [
402
404
  color('link brackets'),
403
405
  '[',
@@ -414,6 +416,50 @@ module Redcarpet
414
416
  ].join
415
417
  end
416
418
 
419
+ def color_link_reference(link, idx, content)
420
+ [
421
+ color('link brackets'),
422
+ '[',
423
+ color('link text'),
424
+ content,
425
+ color('link brackets'),
426
+ '][',
427
+ color('link url'),
428
+ idx,
429
+ color('link brackets'),
430
+ ']',
431
+ xc
432
+ ].join
433
+ end
434
+
435
+ def color_reference_link(link, title, content)
436
+ [
437
+ color('link brackets'),
438
+ '[',
439
+ color('link text'),
440
+ content,
441
+ color('link brackets'),
442
+ ']:',
443
+ color('text'),
444
+ ' ',
445
+ color('link url'),
446
+ link,
447
+ title.nil? ? '' : %( "#{title}"),
448
+ xc
449
+ ].join
450
+ end
451
+
452
+ def link(link, title, content)
453
+ res = color_link(link, title, content)
454
+ @@links << {
455
+ link: res,
456
+ url: link,
457
+ title: title,
458
+ content: content
459
+ }
460
+ res
461
+ end
462
+
417
463
  def color_tags(html)
418
464
  html.gsub(%r{(<\S+( .*?)?/?>)}, "#{color('html brackets')}\\1#{xc}")
419
465
  end
@@ -538,13 +584,13 @@ module Redcarpet
538
584
  longest = longest < @cols ? longest + 1 : @cols
539
585
  lines.map do |line|
540
586
  if line =~ /^[-.]{3}\s*$/
541
- line = "#{color('metadata marker')}#{'%' * longest }"
587
+ line = "#{color('metadata marker')}#{'%' * longest}"
542
588
  else
543
589
  line.sub!(/^(.*?:)[ \t]+(\S)/, '\1 \2')
544
590
  line = "#{color('metadata color')}#{line}"
545
591
  end
546
592
 
547
- line += ("\u00A0" * (longest - line.uncolor.strip.length) ) + xc
593
+ line += "\u00A0" * (longest - line.uncolor.strip.length) + xc
548
594
  line
549
595
  end.join("\n") + "#{xc}\n"
550
596
  end
@@ -559,7 +605,7 @@ module Redcarpet
559
605
  lines.map do |line|
560
606
  line.sub!(/^(.*?:)[ \t]+(\S)/, '\1 \2')
561
607
  line = "#{color('metadata color')}#{line}"
562
- line += "\u00A0" * (longest - line.uncolor.strip.length )
608
+ line += "\u00A0" * (longest - line.uncolor.strip.length)
563
609
  line + xc
564
610
  end.join("\n") + "#{"\u00A0" * longest}#{xc}\n"
565
611
  end
@@ -626,6 +672,51 @@ module Redcarpet
626
672
  end
627
673
  end
628
674
 
675
+ def color_links(input)
676
+ input.gsub(/(?mi)(?<!\\e)\[(?<text>[^\[]+)\]\((?<url>\S+)(?: +"(?<title>.*?)")?\)/) do
677
+ m = Regexp.last_match
678
+ color_link(m['url'].uncolor, m['title']&.uncolor, m['text'].uncolor)
679
+ end
680
+ end
681
+
682
+ def reference_links(input)
683
+ grafs = input.split(/\n{2,}/)
684
+ counter = 1
685
+
686
+ grafs.map! do |graf|
687
+ next if graf =~ /^$/
688
+
689
+ links_added = false
690
+
691
+ @@links.each do |link|
692
+ if graf =~ /#{Regexp.escape(link[:link])}/
693
+ url = link[:url].uncolor
694
+ content = link[:content]
695
+ title = link[:title]&.uncolor
696
+ graf.gsub!(/#{Regexp.escape(link[:link])}/, color_link_reference(url, counter, content))
697
+ if @options[:links] == :paragraph
698
+ if links_added
699
+ graf += "\n#{color_reference_link(url, title, counter)}"
700
+ else
701
+ graf = "#{graf}\n\n#{color_reference_link(url, title, counter)}"
702
+ links_added = true
703
+ end
704
+ else
705
+ @@footer_links << color_reference_link(url, title, counter)
706
+ end
707
+ counter += 1
708
+ end
709
+ end
710
+ "\n#{graf}\n"
711
+ end
712
+
713
+ if @options[:links] == :paragraph
714
+ grafs.join("\n")
715
+ else
716
+ grafs.join("\n") + "\n#{@@footer_links.join("\n")}\n"
717
+ end
718
+ end
719
+
629
720
  def postprocess(input)
630
721
  if @options[:inline_footnotes]
631
722
  input = insert_footnotes(input)
@@ -653,6 +744,10 @@ module Redcarpet
653
744
  end
654
745
  # misc html
655
746
  input.gsub!(%r{<br */?>}, "\n")
747
+ # format links
748
+ if @options[:links] == :reference || @options[:links] == :paragraph
749
+ input = reference_links(input)
750
+ end
656
751
  # lists
657
752
  fix_lists(input, 0)
658
753
  end
@@ -73,8 +73,15 @@ module CLIMarkdown
73
73
 
74
74
  @options[:links] ||= :inline
75
75
  opts.on('--links=FORMAT',
76
- 'Link style ([inline, reference], default inline) [NOT CURRENTLY IMPLEMENTED]') do |format|
77
- @options[:links] = :reference if format =~ /^r/i
76
+ 'Link style ([inline, reference, paragraph], default inline, "paragraph" will position reference links after each paragraph)') do |fmt|
77
+ @options[:links] = case fmt
78
+ when /^:?r/i
79
+ :reference
80
+ when /^:?p/i
81
+ :paragraph
82
+ else
83
+ :inline
84
+ end
78
85
  end
79
86
 
80
87
  @options[:list] ||= false
@@ -275,6 +282,7 @@ module CLIMarkdown
275
282
  @headers = get_headers(input)
276
283
  last_level = 0
277
284
  headers_out = []
285
+ len = (@headers.count + 1).to_s.length
278
286
  @headers.each_with_index do |h, idx|
279
287
  level = h[0].length - 1
280
288
  title = h[1]
@@ -295,7 +303,7 @@ module CLIMarkdown
295
303
  else
296
304
  ' '
297
305
  end
298
- headers_out.push format('%<d>d: %<s>s',
306
+ headers_out.push format("%<d>#{len}d: %<s>s",
299
307
  d: idx + 1,
300
308
  s: "#{c(%i[x black])}#{'.' * level}#{c(%i[x yellow])}#{subdoc}#{title.strip}#{xc}")
301
309
  end
@@ -1,3 +1,3 @@
1
1
  module CLIMarkdown
2
- VERSION = '2.0.5'
2
+ VERSION = '2.0.6'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mdless
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.5
4
+ version: 2.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brett Terpstra