16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
# File 'lib/mdless/converter.rb', line 16
def initialize(args)
MDLess.log.level = Logger::WARN
MDLess.options = {}
config = File.expand_path("~/.config/mdless/config.yml")
MDLess.options = YAML.load(IO.read(config)) if File.exist?(config)
optparse = OptionParser.new do |opts|
opts.banner = "#{version} by Brett Terpstra\n\n> Usage: #{CLIMarkdown::EXECUTABLE_NAME} [options] [path]\n\n"
default(:color, true)
opts.on("-c", "--[no-]color", "Colorize output (default on)") do |c|
MDLess.options[:color] = c
end
opts.on("-d", "--debug LEVEL", "Level of debug messages to output (1-4, 4 to see all messages)") do |level|
if level.to_i.positive? && level.to_i < 5
MDLess.log.level = 5 - level.to_i
else
puts "Error: Debug level out of range (1-4)"
Process.exit 1
end
end
opts.on("-h", "--help", "Display this screen") do
puts opts
exit
end
default(:local_images, false)
default(:remote_images, false)
opts.on("-i", "--images=TYPE",
"Include [local|remote (both)|none] images in output" \
" (requires chafa or imgcat, default none).") do |type|
if exec_available("imgcat") || exec_available("chafa")
case type
when /^(r|b|a)/i
MDLess.options[:local_images] = true
MDLess.options[:remote_images] = true
when /^l/i
MDLess.options[:local_images] = true
when /^n/
MDLess.options[:local_images] = false
MDLess.options[:remote_images] = false
end
else
MDLess.log.warn("images turned on but imgcat/chafa not found")
end
end
opts.on("-I", "--all-images", "Include local and remote images in output" \
" (requires imgcat or chafa)") do
if exec_available("imgcat") || exec_available("chafa") MDLess.options[:local_images] = true
MDLess.options[:remote_images] = true
else
MDLess.log.warn("images turned on but imgcat/chafa not found")
end
end
default(:list, false)
opts.on("-l", "--list", "List headers in document and exit") do
MDLess.options[:list] = true
end
default(:pager, true)
opts.on("-p", "--[no-]pager", "Formatted output to pager (default on)") do |p|
MDLess.options[:pager] = p
end
default(:pager, true)
opts.on("-P", "Disable pager (same as --no-pager)") do
MDLess.options[:pager] = false
end
default(:section, nil)
opts.on("-s", "--section=NUMBER[,NUMBER]",
"Output only a headline-based section of the input (numeric from --list or text match)") do |section|
sections = section.split(/ *, */).map(&:strip)
MDLess.options[:section] = sections.map do |sect|
if sect =~ /^\d+$/
sect.to_i
else
sect
end
end
end
default(:theme, "default")
opts.on("-t", "--theme=THEME_NAME", "Specify an alternate color theme to load") do |theme|
MDLess.options[:theme] = theme
end
default(:at_tags, false)
opts.on("-@", "--[no-]at-tags", "Highlight @tags and values in the document") do |opt|
MDLess.options[:at_tags] = opt
end
opts.on("-v", "--version", "Display version number") do
puts version
exit
end
default(:width, 0)
opts.on("-w", "--width=COLUMNS", "Column width to format for (default: 0 -> terminal width)") do |columns|
columns = columns.to_i
cols = TTY::Screen.cols
MDLess.cols = columns > 2 ? columns - 2 : cols
MDLess.options[:width] = columns > cols ? cols - 2 : columns - 2
end
MDLess.cols = MDLess.options[:width]
MDLess.cols = TTY::Screen.cols - 2 if MDLess.cols.zero?
default(:autolink, true)
opts.on("--[no-]autolink", "Convert bare URLs and emails to <links>") do |p|
MDLess.options[:autolink] = p
end
opts.on("--config", "Open the config file in default editor") do
raise "No $EDITOR defined" unless ENV["EDITOR"]
`#{ENV["EDITOR"]} '#{File.expand_path("~/.config/mdless/config.yml")}'`
end
opts.on("--changes", "Open the changelog to see recent updates") do
changelog = File.join(File.dirname(__FILE__), "..", "..", "CHANGELOG.md")
system "mdless --linebreaks '#{changelog}'"
Process.exit 0
end
opts.on("--edit-theme", "Open the default/specified theme in default editor, " \
"populating a new theme if needed. Use after --theme in the command.") do
raise "No $EDITOR defined" unless ENV["EDITOR"]
theme = MDLess.options[:theme] =~ /default/ ? "mdless" : MDLess.options[:theme]
theme = File.expand_path("~/.config/mdless/#{theme}.theme")
File.open(theme, "w") { |f| f.puts(YAML.dump(MDLess.theme)) } unless File.exist?(theme)
`#{ENV["EDITOR"]} '#{theme}'`
Process.exit 0
end
default(:inline_footnotes, false)
opts.on("--[no-]inline-footnotes",
"Display footnotes immediately after the paragraph that references them") do |p|
MDLess.options[:inline_footnotes] = p
end
default(:intra_emphasis, true)
opts.on("--[no-]intra-emphasis", "Parse emphasis inside of words (e.g. Mark_down_)") do |opt|
MDLess.options[:intra_emphasis] = opt
end
default(:lax_spacing, true)
opts.on("--[no-]lax-spacing", "Allow lax spacing") do |opt|
MDLess.options[:lax_spacing] = opt
end
default(:links, :inline)
opts.on("--links=FORMAT",
"Link style ([*inline, reference, paragraph]," \
' "paragraph" will position reference links after each paragraph)') do |fmt|
MDLess.options[:links] = case fmt
when /^:?r/i
:reference
when /^:?p/i
:paragraph
else
:inline
end
end
default(:preserve_linebreaks, true)
opts.on("--[no-]linebreaks", "Preserve line breaks") do |opt|
MDLess.options[:preserve_linebreaks] = opt
end
default(:mmd_metadata, true)
opts.on("--[no-]metadata", "Replace [%key] with values from metadata") do |opt|
MDLess.options[:mmd_metadata] = opt
end
default(:syntax_higlight, false)
opts.on("--[no-]syntax", "Syntax highlight code blocks") do |opt|
MDLess.options[:syntax_higlight] = opt
end
MDLess.options[:taskpaper] = if MDLess.options[:taskpaper]
case MDLess.options[:taskpaper].to_s
when /^[ty1]/
true
when /^a/
:auto
else
false
end
else
false
end
opts.on("--taskpaper=OPTION", "Highlight TaskPaper format (true|false|auto)") do |tp|
MDLess.options[:taskpaper] = case tp
when /^[ty1]/
true
when /^a/
:auto
else
false
end
end
default(:transclude, true)
opts.on("--[no-]transclude", "Transclude documents with {{filename}} syntax") do |opt|
MDLess.options[:transclude] = opt
end
default(:update_config, false)
opts.on("--update-config", "--update_config",
"Update the configuration file with new keys and current command line options") do
MDLess.options[:update_config] = true
end
default(:update_theme, false)
opts.on("--update-theme", "Update the current theme file with all available keys") do
MDLess.options[:update_theme] = true
end
default(:wiki_links, false)
opts.on("--[no-]wiki-links", "Highlight [[wiki links]]") do |opt|
MDLess.options[:wiki_links] = opt
end
end
begin
optparse.parse!
rescue OptionParser::ParseError => e
warn "error: #{e.message}"
exit 1
end
if MDLess.options[:update_theme]
FileUtils.mkdir_p(File.dirname(config))
theme = MDLess.options[:theme] =~ /default/ ? "mdless" : MDLess.options[:theme]
theme = File.join(File.dirname(config), "#{theme}.theme")
contents = YAML.dump(MDLess.theme)
File.open(theme, "w") { |f| f.puts contents }
Process.exit 0
end
if !File.exist?(config) || MDLess.options[:update_config]
FileUtils.mkdir_p(File.dirname(config))
File.open(config, "w") do |f|
opts = MDLess.options.dup
opts.delete(:list)
opts.delete(:section)
opts.delete(:update_config)
opts.delete(:update_theme)
opts[:width] = 0
opts = opts.keys.map(&:to_s).sort.map { |k| [k.to_sym, opts[k.to_sym]] }.to_h
f.puts YAML.dump(opts)
warn "Config file saved to #{config}"
end
end
@output = ""
@setheaders = []
input = ""
@ref_links = {}
@footnotes = {}
renderer = Redcarpet::Render::Console.new
markdown = Redcarpet::Markdown.new(renderer,
no_intra_emphasis: !MDLess.options[:intra_emphasis],
autolink: MDLess.options[:autolink],
fenced_code_blocks: true,
footnotes: true,
hard_wrap: false,
highlight: true,
lax_spacing: MDLess.options[:lax_spacing],
quote: false,
space_after_headers: false,
strikethrough: true,
superscript: true,
tables: true,
underline: false)
if !args.empty?
files = args.delete_if { |f| !File.exist?(f) }
@multifile = files.count > 1
files.each do |file|
spinner = TTY::Spinner.new("[:spinner] Processing #{File.basename(file)}...", format: :dots_3, clear: true)
spinner.run do |spinner|
MDLess.log.info(%(Processing "#{file}"))
@output << "#{c(%i[b green])}[#{c(%i[b white])}#{file}#{c(%i[b green])}]#{xc}\n\n" if @multifile
MDLess.file = file
begin
input = IO.read(file).force_encoding("utf-8")
rescue StandardError
input = IO.read(file)
end
raise "Nil input" if input.nil?
input.scrub!
input.gsub!(/\r?\n/, "\n")
@headers = (input)
if MDLess.options[:taskpaper] == :auto
MDLess.options[:taskpaper] = if CLIMarkdown::TaskPaper.is_taskpaper?(input)
MDLess.log.info("TaskPaper detected")
true
else
false
end
end
if MDLess.options[:list]
@output << if MDLess.options[:taskpaper]
CLIMarkdown::TaskPaper.list_projects(input)
else
(input)
end
elsif MDLess.options[:taskpaper]
input = input.color_meta(MDLess.cols)
input = CLIMarkdown::TaskPaper.highlight(input)
@output << input.highlight_tags
else
@output << markdown.render(input)
end
@output << "\n\n"
end
end
printout
elsif !$stdin.isatty
MDLess.log.info(%(Processing STDIN))
spinner = TTY::Spinner.new("[:spinner] Processing ...", format: :dots_3, clear: true)
spinner.run do |spinner|
MDLess.file = nil
input = $stdin.read.scrub
input.gsub!(/\r?\n/, "\n")
if MDLess.options[:taskpaper] == :auto
MDLess.options[:taskpaper] = if CLIMarkdown::TaskPaper.is_taskpaper?(input)
MDLess.log.info("TaskPaper detected")
true
else
false
end
end
@headers = (input)
if MDLess.options[:list]
if MDLess.options[:taskpaper]
puts CLIMarkdown::TaskPaper.list_projects(input)
else
puts (input)
end
Process.exit 0
else
if MDLess.options[:taskpaper]
input = input.color_meta(MDLess.cols)
input = CLIMarkdown::TaskPaper.highlight(input)
@output = input.highlight_tags
else
@output = markdown.render(input)
end
end
end
printout
else
warn "No input"
Process.exit 1
end
end
|