mdless 2.0.4 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e50c4e915aa31ffff9b159763d18828ffb1f639588773b45a3aa7528a897cf3
4
- data.tar.gz: b6de738b4cd1f6fd6aa208d4a6607354faa143a5a7f2943147f2a244ba9204b1
3
+ metadata.gz: 2153423a3c896ca8522fd330478ce3125fd68a2bcc6a8a4c52b0efea31033484
4
+ data.tar.gz: 4b039579d2315650038408b954d00689929530c322441aacca52c13e77f4065f
5
5
  SHA512:
6
- metadata.gz: 7df9141901628641895a5d3ac76d37c5af99c30cc94f8fd7e2a1e506b62e9da48267c8173418de66fe139456b120e3b082437d497eb32609a2e4105b1bfa969e
7
- data.tar.gz: eabe40a67402c2a9db8488f74b33b7d48295190e6788690375f71a121b934a6659b10e3c8d8e05c5ae9b1de0aa791ea5d405cf85340c913d317e2d3f50fb7392
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
@@ -530,17 +576,21 @@ module Redcarpet
530
576
  @log.info('Found YAML')
531
577
  # YAML
532
578
  in_yaml = true
533
- input.sub!(/(?i-m)^---[ \t]*\n([\s\S]*?)\n[-.]{3}[ \t]*\n/) do
579
+ input.sub!(/(?i-m)^---[ \t]*\n([\s\S]*?)\n[-.]{3}[ \t]*\n/m) do
534
580
  m = Regexp.last_match
535
581
  @log.info('Processing YAML Header')
536
- m[0].split(/\n/).map do |line|
582
+ lines = m[0].split(/\n/)
583
+ longest = lines.inject { |memo, word| memo.length > word.length ? memo : word }.length
584
+ longest = longest < @cols ? longest + 1 : @cols
585
+ lines.map do |line|
537
586
  if line =~ /^[-.]{3}\s*$/
538
- line = "#{color('metadata marker')}%% #{color('metadata border')}#{line}"
587
+ line = "#{color('metadata marker')}#{'%' * longest}"
539
588
  else
540
589
  line.sub!(/^(.*?:)[ \t]+(\S)/, '\1 \2')
541
- line = "#{color('metadata marker')}%% #{color('metadata color')}#{line}"
590
+ line = "#{color('metadata color')}#{line}"
542
591
  end
543
- line += ' ' * (@cols - line.uncolor.size) if (@cols - line.uncolor.size).positive?
592
+
593
+ line += "\u00A0" * (longest - line.uncolor.strip.length) + xc
544
594
  line
545
595
  end.join("\n") + "#{xc}\n"
546
596
  end
@@ -549,12 +599,15 @@ module Redcarpet
549
599
  if !in_yaml && input.gsub(/\n/, ' ') =~ /(?i-m)^[\w ]+:\s+\S+/
550
600
  @log.info('Found MMD Headers')
551
601
  input.sub!(/(?i-m)^([\S ]+:[\s\S]*?)+(?=\n\n)/) do |mmd|
552
- mmd.split(/\n/).map do |line|
602
+ lines = mmd.split(/\n/)
603
+ longest = lines.inject { |memo, word| memo.length > word.length ? memo : word }.length
604
+ longest = longest < @cols ? longest + 1 : @cols
605
+ lines.map do |line|
553
606
  line.sub!(/^(.*?:)[ \t]+(\S)/, '\1 \2')
554
- line = "#{color('metadata marker')}%% #{color('metadata color')}#{line}"
555
- line += ' ' * (@cols - line.uncolor.size) if (@cols - line.uncolor.size).positive?
556
- line
557
- end.join("\n") + "#{' ' * @cols}#{xc}\n"
607
+ line = "#{color('metadata color')}#{line}"
608
+ line += "\u00A0" * (longest - line.uncolor.strip.length)
609
+ line + xc
610
+ end.join("\n") + "#{"\u00A0" * longest}#{xc}\n"
558
611
  end
559
612
 
560
613
  end
@@ -619,6 +672,51 @@ module Redcarpet
619
672
  end
620
673
  end
621
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
+
622
720
  def postprocess(input)
623
721
  if @options[:inline_footnotes]
624
722
  input = insert_footnotes(input)
@@ -646,6 +744,10 @@ module Redcarpet
646
744
  end
647
745
  # misc html
648
746
  input.gsub!(%r{<br */?>}, "\n")
747
+ # format links
748
+ if @options[:links] == :reference || @options[:links] == :paragraph
749
+ input = reference_links(input)
750
+ end
649
751
  # lists
650
752
  fix_lists(input, 0)
651
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
@@ -234,9 +241,10 @@ module CLIMarkdown
234
241
  end
235
242
  end
236
243
 
237
- def get_headers(input)
244
+ def get_headers(string)
238
245
  unless @headers && !@headers.empty?
239
246
  @headers = []
247
+ input = string.sub(/(?i-m)^---[ \t]*\n([\s\S]*?)\n[-.]{3}[ \t]*\n/m, '')
240
248
  headers = input.scan(/^((?!#!)(\#{1,6})\s*([^#]+?)(?: #+)?\s*|(\S.+)\n([=-]+))$/i)
241
249
 
242
250
  headers.each do |h|
@@ -274,6 +282,7 @@ module CLIMarkdown
274
282
  @headers = get_headers(input)
275
283
  last_level = 0
276
284
  headers_out = []
285
+ len = (@headers.count + 1).to_s.length
277
286
  @headers.each_with_index do |h, idx|
278
287
  level = h[0].length - 1
279
288
  title = h[1]
@@ -294,7 +303,7 @@ module CLIMarkdown
294
303
  else
295
304
  ' '
296
305
  end
297
- headers_out.push format('%<d>d: %<s>s',
306
+ headers_out.push format("%<d>#{len}d: %<s>s",
298
307
  d: idx + 1,
299
308
  s: "#{c(%i[x black])}#{'.' * level}#{c(%i[x yellow])}#{subdoc}#{title.strip}#{xc}")
300
309
  end
@@ -1,3 +1,3 @@
1
1
  module CLIMarkdown
2
- VERSION = '2.0.4'
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.4
4
+ version: 2.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brett Terpstra