mdless 2.1.23 → 2.1.24

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: 5706f3396d726dcdd4d8c0e7a6e93f60f43ed35d8ab56e55cdb76f812230123c
4
- data.tar.gz: 6be63ef01249321bcb16aedfbbb9cb1320c99f0dd74f2ab39071fe30f9328ef3
3
+ metadata.gz: daa606283636cef493f39150cf92cba89cb92774e1a1d864f08b8e3f30c1d956
4
+ data.tar.gz: 22aa99bdb5ecf9eb1e29a79595048098101ccb12b676481cf219bf00fff56ff2
5
5
  SHA512:
6
- metadata.gz: e333519a46675abb2a8aaf9092c9ea1274faf67d156fc6754fda18d86b53fa92efe78a75ccbdd493c822a124afcd1a855fcfe286dbb7cc156d5380d72de770e9
7
- data.tar.gz: 691565d52e2f9fe3e652ad7c02f435957df92ad72cc98ddb1133262bace8d3a167e97c9ad3e740aa153700c90f8403f014e76cf2cba6877739aef3434096a7b0
6
+ metadata.gz: 351d4923b3d652d4ca6900ad691ceea22f33be02127c2ed5c654a4a48ef2b3feb6daa1f086cb98c398bc4f2ed40b7e797d6946d2358c0fb3c721ee5068437176
7
+ data.tar.gz: 992a8936d9426acbd56f146606d0b3f42c49900a9e60a70e943a17eef17b450fbe40ee106150135cc755c61407ee05aa2ba2337f400ad38528e0c8a0d0949f03
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ 2.1.24
2
+ : Save MultiMarkdown metadata into a hash
3
+ : Allow [%metakey] replacements
4
+ : Allow {{filename}} transcusions (MultiMarkdown), respects "transclude base:" metadata
5
+ : Transclude documents with `{{filename}}`, nesting allowed, "transclude base:" metadata respected (even in YAML)
6
+ : Metadata can be used in `[%key]` format to have it replaced in the output based on metadata values
7
+
1
8
  2.1.23
2
9
  : Fix release pipeline to get version number correct in git release
3
10
  : Changelog mismatch
@@ -743,15 +743,16 @@ module Redcarpet
743
743
  def color_meta(text)
744
744
  input = text.dup
745
745
  input.clean_empty_lines!
746
-
746
+ MDLess.meta = {}
747
747
  first_line = input.split("\n").first
748
748
  if first_line =~ /(?i-m)^---[ \t]*?$/
749
749
  MDLess.log.info('Found YAML')
750
750
  # YAML
751
751
  in_yaml = true
752
- input.sub!(/(?i-m)^---[ \t]*\n([\s\S]*?)\n[-.]{3}[ \t]*\n/m) do
752
+ input.sub!(/(?i-m)^---[ \t]*\n(?<content>(?:[\s\S]*?))\n[-.]{3}[ \t]*\n/m) do
753
753
  m = Regexp.last_match
754
754
  MDLess.log.info('Processing YAML Header')
755
+ MDLess.meta = YAML.safe_load(m['content']).map { |k, v| "#{k.downcase}" => v }
755
756
  lines = m[0].split(/\n/)
756
757
  longest = lines.longest_element.length
757
758
  longest = longest < MDLess.cols ? longest + 1 : MDLess.cols
@@ -779,18 +780,47 @@ module Redcarpet
779
780
 
780
781
  lines.map do |line|
781
782
  line.sub!(/^(.*?:)[ \t]+(\S)/, '\1 \2')
783
+ parts = line.match(/[ \t]*(.*?): +(.*?)$/)
784
+ key = parts[1].gsub(/[^a-z0-9\-_]/i, '')
785
+ value = parts[2].strip
786
+ MDLess.meta[key] = value
782
787
  line = "#{color('metadata marker')}%#{color('metadata color')}#{line}"
783
788
  line += "\u00A0" * (longest - line.uncolor.strip.length) if (longest - line.uncolor.strip.length).positive?
784
789
  line + xc
785
- end.join("\n") + "#{"\u00A0" * longest}#{xc}\n"
790
+ end.join("\n") + "#{xc}\n"
786
791
  end
787
792
  end
788
793
 
789
794
  input
790
795
  end
791
796
 
797
+ def mmd_transclude(input)
798
+ input.gsub(/^{{(.*?)}}/) do |m|
799
+ filename = Regexp.last_match(1).strip
800
+ file = if MDLess.meta.key?('transcludebase')
801
+ File.join(File.expand_path(MDLess.meta['transcludebase']), filename)
802
+ else
803
+ File.join(File.dirname(MDLess.file), filename)
804
+ end
805
+ File.exist?(file) ? "\n\n#{mmd_transclude(IO.read(file).remove_meta)}\n\n" : m
806
+ end
807
+ end
808
+
809
+ def mmd_metadata_replace(input)
810
+ input.gsub(/\[%(.*?)\]/) do |m|
811
+ key = Regexp.last_match(1)
812
+ if MDLess.meta.key?(key)
813
+ MDLess.meta[key]
814
+ else
815
+ m
816
+ end
817
+ end
818
+ end
819
+
792
820
  def preprocess(input)
793
821
  input = color_meta(input)
822
+ input = mmd_transclude(input) if MDLess.options[:transclude]
823
+ input = mmd_metadata_replace(input) if MDLess.options[:mmd_metadata]
794
824
 
795
825
  replaced_input = input.clone
796
826
  ## Replace setex headers with ATX
@@ -184,6 +184,11 @@ module CLIMarkdown
184
184
  MDLess.options[:preserve_linebreaks] = opt
185
185
  end
186
186
 
187
+ default(:mmd_metadata, true)
188
+ opts.on('--[no-]metadata', 'Replace [%key] with values from metadata') do |opt|
189
+ MDLess.options[:mmd_metadata] = opt
190
+ end
191
+
187
192
  default(:syntax_higlight, false)
188
193
  opts.on('--[no-]syntax', 'Syntax highlight code blocks') do |opt|
189
194
  MDLess.options[:syntax_higlight] = opt
@@ -212,6 +217,11 @@ module CLIMarkdown
212
217
  end
213
218
  end
214
219
 
220
+ default(:transclude, true)
221
+ opts.on('--[no-]transclude', 'Transclude documents with {{filename}} syntax') do |opt|
222
+ MDLess.options[:transclude] = opt
223
+ end
224
+
215
225
  default(:update_config, false)
216
226
  opts.on('--update-config', '--update_config', 'Update the configuration file with new keys and current command line options') do
217
227
  MDLess.options[:update_config] = true
data/lib/mdless/string.rb CHANGED
@@ -56,6 +56,7 @@ class ::String
56
56
  @cols = cols
57
57
  input = dup
58
58
  input.clean_empty_lines!
59
+ MDLess.meta = {}
59
60
 
60
61
  in_yaml = false
61
62
  first_line = input.split("\n").first
@@ -63,10 +64,11 @@ class ::String
63
64
  MDLess.log.info('Found YAML')
64
65
  # YAML
65
66
  in_yaml = true
66
- input.sub!(/(?i-m)^---[ \t]*\n([\s\S]*?)\n[-.]{3}[ \t]*\n/m) do
67
+ input.sub!(/(?i-m)^---[ \t]*\n(?<content>[\s\S]*?)\n[-.]{3}[ \t]*\n/m) do
67
68
  m = Regexp.last_match
68
69
  MDLess.log.info('Processing YAML Header')
69
- lines = m[0].split(/\n/)
70
+ MDLess.meta = YAML.safe_load(m['content']).map { |k, v| "#{k.downcase}" => v }
71
+ lines = m['content'].split(/\n/)
70
72
  longest = lines.inject { |memo, word| memo.length > word.length ? memo : word }.length
71
73
  longest = longest < @cols ? longest + 1 : @cols
72
74
  lines.map do |line|
@@ -93,7 +95,11 @@ class ::String
93
95
  longest = longest < @cols ? longest + 1 : @cols
94
96
  lines.map do |line|
95
97
  line.sub!(/^(.*?:)[ \t]+(\S)/, '\1 \2')
96
- line = "#{color('metadata color')}#{line}"
98
+ parts = line.match(/[ \t]*(.*?): +(.*?)$/)
99
+ key = parts[1].gsub(/[^a-z0-9\-_]/i, '')
100
+ value = parts[2].strip
101
+ MDLess.meta[key] = value
102
+ line = "#{color('metadata color')}#{line}#{xc}"
97
103
  line += "\u00A0" * (longest - line.uncolor.strip.length) if (longest - line.uncolor.strip.length).positive?
98
104
  line + xc
99
105
  end.join("\n") + "#{"\u00A0" * longest}#{xc}\n"
@@ -139,6 +145,17 @@ class ::String
139
145
  MDLess.pygments_styles.include?(self)
140
146
  end
141
147
 
148
+ def remove_meta
149
+ first_line = split("\n").first
150
+ if first_line =~ /(?i-m)^---[ \t]*?$/
151
+ sub(/(?im)^---[ \t]*\n([\s\S\n]*?)\n[-.]{3}[ \t]*\n/, '')
152
+ elsif first_line =~ /(?i-m)^[\w ]+:\s+\S+/
153
+ sub(/(?im)^([\S ]+:[\s\S]*?)+(?=\n *\n)/, '')
154
+ else
155
+ self
156
+ end
157
+ end
158
+
142
159
  def valid_lexer?
143
160
  return false unless TTY::Which.exist?('pygmentize')
144
161
 
@@ -36,16 +36,6 @@ module CLIMarkdown
36
36
  end
37
37
  end
38
38
 
39
- def remove_meta(input)
40
- first_line = input.split("\n").first
41
- if first_line =~ /(?i-m)^---[ \t]*?$/
42
- input.sub!(/(?im)^---[ \t]*\n([\s\S\n]*?)\n[-.]{3}[ \t]*\n/, '')
43
- elsif first_line =~ /(?i-m)^[\w ]+:\s+\S+/
44
- input.sub!(/(?im)^([\S ]+:[\s\S]*?)+(?=\n\n)/, '')
45
- end
46
- input
47
- end
48
-
49
39
  def is_taskpaper?(input)
50
40
  return true if MDLess.file =~ /\.taskpaper$/
51
41
 
@@ -61,7 +51,7 @@ module CLIMarkdown
61
51
  if tasks >= 6
62
52
  return true
63
53
  else
64
- tst = remove_meta(input.dup)
54
+ tst = input.dup.remove_meta
65
55
  tst = tst.gsub(PROJECT_RX, '')
66
56
  tst = tst.gsub(TASK_RX, '')
67
57
  tst = tst.gsub(NOTE_RX, '')
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CLIMarkdown
4
- VERSION = '2.1.23'
4
+ VERSION = '2.1.24'
5
5
  end
data/lib/mdless.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'optparse'
2
4
  require 'shellwords'
3
5
  require 'open3'
@@ -7,7 +9,7 @@ require 'tty-which'
7
9
  require 'tty-screen'
8
10
  require 'tty-spinner'
9
11
  require 'rouge'
10
- require 'mdless/version.rb'
12
+ require 'mdless/version'
11
13
  require 'mdless/colors'
12
14
  require 'mdless/tables'
13
15
  require 'mdless/hash'
@@ -26,7 +28,7 @@ end
26
28
  module MDLess
27
29
  class << self
28
30
  include CLIMarkdown::Theme
29
- attr_accessor :options, :cols, :file
31
+ attr_accessor :options, :cols, :file, :meta
30
32
 
31
33
  def log
32
34
  @log ||= Logger.new($stderr)
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.1.23
4
+ version: 2.1.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brett Terpstra