mdless 2.0.12 → 2.0.14

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: c79d8ac3d3e9579dd89e46ba18e9e0e91eba3c4c47d3af390c897ebe1067ac6d
4
- data.tar.gz: 27c2b032b65a05d9c8109e41b4a50e8906335d008335752d69c395eb79832bb1
3
+ metadata.gz: cdb39184ea897e4e43081add5c4c70791f4c04af12b4d54f8cb67e1ba262a551
4
+ data.tar.gz: 0160b5cda00979788b772a7db721abbe46ec3e6c39d2c1a4459b8fc8f04141b1
5
5
  SHA512:
6
- metadata.gz: a3289ed0b1bf504f5bb3c044e123184ba0f96fdd14498d1f94d3a1b51dd13dd20a0a97dee523ed63fa0abe3c34cb7aeebc50b0fbddc57d69bdbba01c57bc73f6
7
- data.tar.gz: b11921b4b2bc1af7ecf58c075f6af118bd2378ad7d7a7385923b2ace54077d1224ecf64cc99cdbba2b1017c1e54d67eb33a006bf3068a9dca155c5efd14e947c
6
+ metadata.gz: 7f1d7fdd5b1b26ca1a1baf8bf9492c5d1ff51ba5b07c16aaeb1dfe43a91ff0e2025196a08e71687da15b380dedcd5649d06e3107e1e449d3f064e5b178b7f11d
7
+ data.tar.gz: ca412964bf3a3af4dd6d63453ed818d9715615682866e70602f116fe050174e14f21d60c13f11b1b2d236f14637eeeb5711d7c7eb591538cd45808906331600c
data/README.md CHANGED
@@ -58,15 +58,16 @@ The pager used is determined by system configuration in this order of preference
58
58
  -c, --[no-]color Colorize output (default on)
59
59
  -d, --debug LEVEL Level of debug messages to output (1-4, 4 to see all messages)
60
60
  -h, --help Display this screen
61
- -i, --images=TYPE Include [local|remote (both)] images in output (requires chafa or imgcat, default NONE).
61
+ -i, --images=TYPE Include [local|remote (both)|none] images in output (requires chafa or imgcat, default none).
62
62
  -I, --all-images Include local and remote images in output (requires imgcat or chafa)
63
63
  --syntax Syntax highlight code blocks
64
- --links=FORMAT Link style ([inline, reference], default inline) [NOT CURRENTLY IMPLEMENTED]
64
+ --links=FORMAT Link style ([inline, reference, paragraph], default inline, "paragraph" will position reference links after each paragraph)
65
65
  -l, --list List headers in document and exit
66
66
  -p, --[no-]pager Formatted output to pager (default on)
67
67
  -P Disable pager (same as --no-pager)
68
68
  -s, --section=NUMBER[,NUMBER] Output only a headline-based section of the input (numeric from --list)
69
69
  -t, --theme=THEME_NAME Specify an alternate color theme to load
70
+ -@, --at_tags Highlight @tags and values in the document
70
71
  -v, --version Display version number
71
72
  -w, --width=COLUMNS Column width to format for (default: terminal width)
72
73
  --[no-]inline_footnotes Display footnotes immediately after the paragraph that references them
data/lib/mdless/colors.rb CHANGED
@@ -114,62 +114,6 @@ module CLIMarkdown
114
114
  escape + "\e[#{[fg, bg].delete_if(&:nil?).join(';')}m"
115
115
  end
116
116
 
117
- # Get the calculated ANSI color at the end of the
118
- # string
119
- #
120
- # @return ANSI escape sequence to match color
121
- #
122
- def last_color_code
123
- m = scan(ESCAPE_REGEX)
124
-
125
- em = ['0']
126
- fg = nil
127
- bg = nil
128
- rgbf = nil
129
- rgbb = nil
130
-
131
- m.each do |c|
132
- case c
133
- when '0'
134
- em = ['0']
135
- fg, bg, rgbf, rgbb = nil
136
- when /^[34]8/
137
- case c
138
- when /^3/
139
- fg = nil
140
- rgbf = c
141
- when /^4/
142
- bg = nil
143
- rgbb = c
144
- end
145
- else
146
- c.split(/;/).each do |i|
147
- x = i.to_i
148
- if x <= 9
149
- em << x
150
- elsif x >= 30 && x <= 39
151
- rgbf = nil
152
- fg = x
153
- elsif x >= 40 && x <= 49
154
- rgbb = nil
155
- bg = x
156
- elsif x >= 90 && x <= 97
157
- rgbf = nil
158
- fg = x
159
- elsif x >= 100 && x <= 107
160
- rgbb = nil
161
- bg = x
162
- end
163
- end
164
- end
165
- end
166
-
167
- escape = "\e[#{em.join(';')}m"
168
- escape += "\e[#{rgbb}m" if rgbb
169
- escape += "\e[#{rgbf}m" if rgbf
170
- escape + "\e[#{[fg, bg].delete_if(&:nil?).join(';')}m"
171
- end
172
-
173
117
  def blackout(bgcolor)
174
118
  key = bgcolor.to_sym
175
119
  bg = COLORS.key?(key) ? COLORS[key] : 40
@@ -3,6 +3,7 @@ module Redcarpet
3
3
  class Console < Base
4
4
  include CLIMarkdown::Colors
5
5
  include CLIMarkdown::Theme
6
+
6
7
  attr_writer :theme, :cols, :log, :options, :file
7
8
 
8
9
  @@listitemid = 0
@@ -550,8 +551,8 @@ module Redcarpet
550
551
  @@headers
551
552
  end
552
553
 
553
- def preprocess(input)
554
- in_yaml = false
554
+ def color_meta(text)
555
+ input = text.dup
555
556
  first_line = input.split("\n").first
556
557
  if first_line =~ /(?i-m)^---[ \t]*?$/
557
558
  @log.info('Found YAML')
@@ -592,12 +593,19 @@ module Redcarpet
592
593
  line + xc
593
594
  end.join("\n") + "#{"\u00A0" * longest}#{xc}\n"
594
595
  end
595
-
596
596
  end
597
597
 
598
+ input
599
+ end
600
+
601
+ def preprocess(input)
602
+ in_yaml = false
603
+
604
+ input = color_meta(input)
605
+
598
606
  ## Replace setex headers with ATX
599
- input.gsub!(/^([^\n]+)\n={3,}\s*$/m, "# \\1\n")
600
- input.gsub!(/^([^\n]+?)\n-{3,}\s*$/m, "## \\1\n")
607
+ input.gsub!(/^([^\n]+)\n={2,}\s*$/m, "# \\1\n")
608
+ input.gsub!(/^([^\n]+?)\n-{2,}\s*$/m, "## \\1\n")
601
609
 
602
610
  @@headers = get_headers(input)
603
611
 
@@ -639,6 +647,20 @@ module Redcarpet
639
647
  "#{color('dd term')}#{m['term']}#{xc}#{color('dd color')}#{color_dd_def(m['def'])}"
640
648
  end
641
649
 
650
+ if @options[:taskpaper] == :auto
651
+ @options[:taskpaper] = if @file =~ /\.taskpaper/
652
+ @log.info('TaskPaper extension detected')
653
+ true
654
+ elsif CLIMarkdown::TaskPaper.is_taskpaper?(input)
655
+ @log.info('TaskPaper document detected')
656
+ true
657
+ else
658
+ false
659
+ end
660
+ end
661
+
662
+ input = CLIMarkdown::TaskPaper.highlight(input, @theme) if @options[:taskpaper]
663
+
642
664
  input
643
665
  end
644
666
 
@@ -808,6 +830,27 @@ module Redcarpet
808
830
  end
809
831
  end
810
832
 
833
+ def highlight_tags(input)
834
+ tag_color = color('at_tags tag')
835
+ value_color = color('at_tags value')
836
+ input.gsub(/(?<pre>\s|m)(?<tag>@[^ ("']+)(?:(?<lparen>\()(?<value>.*?)(?<rparen>\)))?/) do
837
+ m = Regexp.last_match
838
+ last_color = m.pre_match.last_color_code
839
+ [
840
+ m['pre'],
841
+ tag_color,
842
+ m['tag'],
843
+ m['lparen'],
844
+ value_color,
845
+ m['value'],
846
+ tag_color,
847
+ m['rparen'],
848
+ xc,
849
+ last_color
850
+ ].join
851
+ end
852
+ end
853
+
811
854
  def postprocess(input)
812
855
  input.scrub!
813
856
 
@@ -832,6 +875,7 @@ module Redcarpet
832
875
  # lists
833
876
  input = fix_lists(input, 0)
834
877
  input = render_images(input) if @options[:local_images]
878
+ input = highlight_tags(input) if @options[:at_tags] || @options[:taskpaper]
835
879
  fix_colors(input)
836
880
  end
837
881
  end
@@ -45,7 +45,7 @@ module CLIMarkdown
45
45
  @options[:local_images] ||= false
46
46
  @options[:remote_images] ||= false
47
47
  opts.on('-i', '--images=TYPE',
48
- 'Include [local|remote (both)] images in output (requires chafa or imgcat, default NONE).') do |type|
48
+ 'Include [local|remote (both)|none] images in output (requires chafa or imgcat, default none).') do |type|
49
49
  if exec_available('imgcat') || exec_available('chafa')
50
50
  case type
51
51
  when /^(r|b|a)/i
@@ -75,6 +75,23 @@ module CLIMarkdown
75
75
  @options[:syntax_higlight] = p
76
76
  end
77
77
 
78
+ @options[:update_config] ||= false
79
+ opts.on('--update_config', 'Update the configuration file with new keys and current command line options') do
80
+ @options[:update_config] = true
81
+ end
82
+
83
+ @options[:taskpaper] ||= false
84
+ opts.on('--taskpaper=OPTION', 'Highlight TaskPaper format (true|false|auto)') do |tp|
85
+ @options[:taskpaper] = case tp
86
+ when /^[ty1]/
87
+ true
88
+ when /^a/
89
+ :auto
90
+ else
91
+ false
92
+ end
93
+ end
94
+
78
95
  @options[:links] ||= :inline
79
96
  opts.on('--links=FORMAT',
80
97
  'Link style ([inline, reference, paragraph], default inline, "paragraph" will position reference links after each paragraph)') do |fmt|
@@ -88,6 +105,16 @@ module CLIMarkdown
88
105
  end
89
106
  end
90
107
 
108
+ @options[:lax_spacing] ||= true
109
+ opts.on('--[no-]lax-spacing', 'Allow lax spacing') do |opt|
110
+ @options[:lax_spacing] = opt
111
+ end
112
+
113
+ @options[:intra_emphasis] ||= true
114
+ opts.on('--[no-]intra-emphasis', 'Parse emphasis inside of words (e.g. Mark_down_)') do |opt|
115
+ @options[:intra_emphasis] = opt
116
+ end
117
+
91
118
  @options[:list] ||= false
92
119
  opts.on('-l', '--list', 'List headers in document and exit') do
93
120
  @options[:list] = true
@@ -98,6 +125,7 @@ module CLIMarkdown
98
125
  @options[:pager] = p
99
126
  end
100
127
 
128
+ @options[:pager] ||= true
101
129
  opts.on('-P', 'Disable pager (same as --no-pager)') do
102
130
  @options[:pager] = false
103
131
  end
@@ -113,6 +141,11 @@ module CLIMarkdown
113
141
  @options[:theme] = theme
114
142
  end
115
143
 
144
+ @options[:at_tags] ||= false
145
+ opts.on('-@', '--at_tags', 'Highlight @tags and values in the document') do
146
+ @options[:at_tags] = true
147
+ end
148
+
116
149
  opts.on('-v', '--version', 'Display version number') do
117
150
  puts version
118
151
  exit
@@ -137,12 +170,13 @@ module CLIMarkdown
137
170
  exit 1
138
171
  end
139
172
 
140
- unless File.exist?(config)
173
+ if !File.exist?(config) || @options[:update_config]
141
174
  FileUtils.mkdir_p(File.dirname(config))
142
175
  File.open(config, 'w') do |f|
143
176
  opts = @options.dup
144
177
  opts.delete(:list)
145
178
  opts.delete(:section)
179
+ opts.delete(:update_config)
146
180
  f.puts YAML.dump(opts)
147
181
  warn "Config file saved to #{config}"
148
182
  end
@@ -165,12 +199,13 @@ module CLIMarkdown
165
199
  renderer.options = @options
166
200
 
167
201
  markdown = Redcarpet::Markdown.new(renderer,
202
+ no_intra_emphasis: !@options[:intra_emphasis],
168
203
  autolink: true,
169
204
  fenced_code_blocks: true,
170
205
  footnotes: true,
171
206
  hard_wrap: false,
172
207
  highlight: true,
173
- lax_spacing: true,
208
+ lax_spacing: @options[:lax_spacing],
174
209
  quote: false,
175
210
  space_after_headers: false,
176
211
  strikethrough: true,
@@ -189,7 +224,7 @@ module CLIMarkdown
189
224
  rescue StandardError
190
225
  input = IO.read(file)
191
226
  end
192
- input.gsub!(/\r?\n/, "\n")
227
+ input.gsub!(/\r?\n/, "\n").scrub!
193
228
  if @options[:list]
194
229
  puts list_headers(input)
195
230
  Process.exit 0
@@ -200,11 +235,7 @@ module CLIMarkdown
200
235
  printout
201
236
  elsif !$stdin.isatty
202
237
  @file = nil
203
- begin
204
- input = $stdin.read.force_encoding('utf-8')
205
- rescue StandardError
206
- input = $stdin.read
207
- end
238
+ input = $stdin.read.scrub
208
239
  input.gsub!(/\r?\n/, "\n")
209
240
  if @options[:list]
210
241
  puts list_headers(input)
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CLIMarkdown
4
+ module TaskPaper
5
+ TASK_RX = /^(?<indent>(?: |\t)+)(?<marker>-)(?<task>\s+\S.*?)$/
6
+ PROJECT_RX = /^(?<indent>(?: |\t)*)(?<project>\S.*?:)(?<tags> @\S+)*$/
7
+ NOTE_RX = /^(?<indent>(?: |\t)+)(?<note>(?<!- ).*?(?!:))$/
8
+
9
+ class << self
10
+ include CLIMarkdown::Colors
11
+ attr_writer :theme
12
+
13
+ def color(key)
14
+ val = nil
15
+ keys = key.split(/[ ,>]/)
16
+ if @theme.key?(keys[0])
17
+ val = @theme[keys.shift]
18
+ else
19
+ @log.error("Invalid theme key: #{key}") unless keys[0] =~ /^text/
20
+ return c([:reset])
21
+ end
22
+ keys.each do |k|
23
+ if val.key?(k)
24
+ val = val[k]
25
+ else
26
+ @log.error("Invalid theme key: #{k}")
27
+ return c([:reset])
28
+ end
29
+ end
30
+ if val.is_a? String
31
+ val = "x #{val}"
32
+ res = val.split(/ /).map(&:to_sym)
33
+ c(res)
34
+ else
35
+ c([:reset])
36
+ end
37
+ end
38
+
39
+ def is_taskpaper?(input)
40
+ projects = input.split(PROJECT_RX)
41
+ tasks = 0
42
+ projects.each do |proj|
43
+ tasks += proj.scan(TASK_RX).count
44
+ end
45
+
46
+ tasks >= 6
47
+ end
48
+
49
+ def highlight(input, theme)
50
+ @theme = theme
51
+ mc = color('taskpaper marker')
52
+ tc = color('taskpaper task')
53
+ pc = color('taskpaper project')
54
+ nc = color('taskpaper note')
55
+
56
+ input.gsub!(PROJECT_RX) do
57
+ m = Regexp.last_match
58
+ "#{m['indent']}#{pc}#{m['project']}#{m['tags']}"
59
+ end
60
+
61
+ input.gsub!(TASK_RX) do
62
+ m = Regexp.last_match
63
+ "#{m['indent']}#{mc}- #{tc}#{m['task']}"
64
+ end
65
+
66
+ input.gsub!(NOTE_RX) do
67
+ m = Regexp.last_match
68
+ "#{m['indent']}#{nc}#{m['note']}"
69
+ end
70
+
71
+ input
72
+ end
73
+ end
74
+ end
75
+ end
data/lib/mdless/theme.rb CHANGED
@@ -103,7 +103,17 @@ module CLIMarkdown
103
103
  'equation' => 'b blue'
104
104
  },
105
105
  'super' => 'b green',
106
- 'text' => 'white'
106
+ 'text' => 'white',
107
+ 'at_tags' => {
108
+ 'tag' => 'magenta',
109
+ 'value' => 'b white'
110
+ },
111
+ 'taskpaper' => {
112
+ 'marker' => 'b white',
113
+ 'project' => 'b green',
114
+ 'task' => 'white',
115
+ 'note' => 'd white'
116
+ }
107
117
  }
108
118
 
109
119
  def load_theme_file(theme_file)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CLIMarkdown
4
- VERSION = '2.0.12'
4
+ VERSION = '2.0.14'
5
5
  end
data/lib/mdless.rb CHANGED
@@ -9,6 +9,7 @@ require 'mdless/colors'
9
9
  require 'mdless/tables'
10
10
  require 'mdless/hash'
11
11
  require 'mdless/string'
12
+ require 'mdless/taskpaper'
12
13
  require 'mdless/theme'
13
14
  require 'redcarpet'
14
15
  require 'mdless/console'
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.12
4
+ version: 2.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brett Terpstra
@@ -84,6 +84,7 @@ files:
84
84
  - lib/mdless/hash.rb
85
85
  - lib/mdless/string.rb
86
86
  - lib/mdless/tables.rb
87
+ - lib/mdless/taskpaper.rb
87
88
  - lib/mdless/theme.rb
88
89
  - lib/mdless/version.rb
89
90
  homepage: http://brettterpstra.com/project/mdless/