mdless 0.0.10 → 0.0.12

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
- SHA1:
3
- metadata.gz: 618302eecf35ed202db845ca4ddd45fcb6f7df8c
4
- data.tar.gz: 9413e7e0106c478d120d475efd4d62bde3075d29
2
+ SHA256:
3
+ metadata.gz: bfcee3f2aa45bb9582ae159a7eb7633a002ab6ebc4ff26785f7ed6fa04e4d2aa
4
+ data.tar.gz: ff9bb80e0c4b3cbfe8951f8bae3f82150b561c77523dda0f93451119a94dd185
5
5
  SHA512:
6
- metadata.gz: dba40280383d15349c27b69c91cb536ec2f63cdb787cba3da251738957a41e4e3b2a7a38bdf25d333d5cb452042e4d9b8516fe8983a8f17a0f31bd9151fa0ece
7
- data.tar.gz: ecdd1dbdf0b44d29d4c34e74435ca38da3861c92df9d634ac2f90144f806c26313f46bf84cfe4fe51a6254479b26f062421c104825c806690a05fe07dbcc3928
6
+ metadata.gz: '08248246829c7fd58677f2456a4d4ded712d2905c249f298e3fc2e98ee6eee17f4cd24e46a3376262dab5a1312a7c2d8d6342c7cea412bef63900d51c45a0068'
7
+ data.tar.gz: 84f9faa5f08157313d9dc0b8d91a661c401b4bc52c0e2f4db68cd9025b11bf9f7d5be5e84423c7bfe52c30f91388ce54db70a6299715c23c30e02a5f0aecb22a
@@ -14,7 +14,7 @@ module CLIMarkdown
14
14
 
15
15
  @options = {}
16
16
  optparse = OptionParser.new do |opts|
17
- opts.banner = "#{version} by Brett Terpstra\n\n> Usage: #{CLIMarkdown::EXECUTABLE_NAME} [options] path\n\n"
17
+ opts.banner = "#{version} by Brett Terpstra\n\n> Usage: #{CLIMarkdown::EXECUTABLE_NAME} [options] [path]\n\n"
18
18
 
19
19
  @options[:section] = nil
20
20
  opts.on( '-s', '--section=TITLE', 'Output only a headline-based section of the input (numeric from -l)' ) do |section|
@@ -119,7 +119,7 @@ module CLIMarkdown
119
119
  end
120
120
  }
121
121
  printout
122
- elsif STDIN.stat.size > 0
122
+ elsif ! STDIN.tty?
123
123
  @file = nil
124
124
  begin
125
125
  input = STDIN.read.force_encoding('utf-8')
@@ -307,6 +307,38 @@ module CLIMarkdown
307
307
  "#{c([:x,:red])}!#{c([:b,:black])}[#{c([:x,:cyan])}#{text}#{c([:b,:black])}](#{c([:u,:yellow])}#{url}#{c([:b,:black])})" + find_color(line)
308
308
  end
309
309
 
310
+ def hiliteCode(language, codeBlock, leader, block)
311
+ if exec_available('pygmentize')
312
+ lexer = language.nil? ? '-g' : "-l #{language}"
313
+ begin
314
+ hilite, s = Open3.capture2(%Q{pygmentize #{lexer} 2> /dev/null}, :stdin_data=>codeBlock)
315
+
316
+ if s.success?
317
+ hilite = hilite.split(/\n/).map{|l| "#{c([:x,:black])}~ #{xc}" + l}.join("\n")
318
+ end
319
+ rescue => e
320
+ @log.error(e)
321
+ hilite = block
322
+ end
323
+ else
324
+ hilite = codeBlock.split(/\n/).map do |line|
325
+ new_code_line = line.gsub(/\t/, ' ')
326
+ orig_length = new_code_line.size + 3
327
+ new_code_line.gsub!(/ /, "#{c(%i[x white on_black])} ")
328
+
329
+ [
330
+ "#{c(%i[x black])}~ #{c(%i[x white on_black])} ",
331
+ new_code_line,
332
+ c(%i[x white on_black]),
333
+ ' ' * [@cols - orig_length, 0].max,
334
+ xc
335
+ ].join
336
+ end.join("\n")
337
+ end
338
+
339
+ "#{c(%i[x magenta])}#{leader}\n#{hilite}#{xc}"
340
+ end
341
+
310
342
  def convert_markdown(input)
311
343
  @headers = get_headers(input)
312
344
  # yaml/MMD headers
@@ -401,34 +433,28 @@ module CLIMarkdown
401
433
  "#" * (match[1].length - h_adjust)
402
434
  end
403
435
 
436
+ # code block parsing
404
437
  input.gsub!(/(?i-m)([`~]{3,})([\s\S]*?)\n([\s\S]*?)\1/ ) do |cb|
405
438
  m = Regexp.last_match
406
- leader = m[2] ? m[2].upcase + ":" : 'CODE:'
407
- leader += xc
408
-
409
- if exec_available('pygmentize')
410
- lexer = m[2].nil? ? '-g' : "-l #{m[2]}"
411
- begin
412
- hilite, s = Open3.capture2(%Q{pygmentize #{lexer} 2> /dev/null}, :stdin_data=>m[3])
413
-
414
- if s.success?
415
- hilite = hilite.split(/\n/).map{|l| "#{c([:x,:black])}~ #{xc}" + l}.join("\n")
416
- end
417
- rescue => e
418
- @log.error(e)
419
- hilite = m[0]
420
- end
421
-
439
+ if m.to_s.include? '#!'
440
+ @log.warn('Code block contains Shebang')
441
+ shebang = m.to_s.match(/(#!(?:\/)?)([a-z]\w*)/)
442
+ language = shebang[2]
443
+ codeBlock = m[3].to_s.gsub(shebang[1]+shebang[2], '').strip
444
+ leader = shebang[2] ? shebang[2].upcase + ':' : 'CODE:'
422
445
  else
423
-
446
+ # Ignore leading spaces, and use only first word
424
447
  hilite = m[3].split(/\n/).map{|l|
425
448
  new_code_line = l.gsub(/\t/,' ')
426
449
  orig_length = new_code_line.size + 3
427
450
  new_code_line.gsub!(/ /,"#{c([:x,:white,:on_black])} ")
428
- "#{c([:x,:black])}~ #{c([:x,:white,:on_black])} " + new_code_line + c([:x,:white,:on_black]) + " "*(@cols - orig_length) + xc
451
+ pad_count = [@cols - orig_length, 0].max
452
+ "#{c([:x,:black])}~ #{c([:x,:white,:on_black])} " + new_code_line + c([:x,:white,:on_black]) + " "*pad_count + xc
429
453
  }.join("\n")
454
+ leader = language ? language.upcase + ":" : 'CODE:'
430
455
  end
431
- "#{c([:x,:magenta])}#{leader}\n#{hilite}#{xc}"
456
+ leader += xc
457
+ hiliteCode(language, codeBlock, leader, m[0])
432
458
  end
433
459
 
434
460
  # remove empty links
@@ -742,7 +768,7 @@ module CLIMarkdown
742
768
 
743
769
  def which_pager
744
770
  pagers = [ENV['GIT_PAGER'], ENV['PAGER'],
745
- `git config --get-all core.pager`.split.first,
771
+ `git config --get-all core.pager || true`.split.first,
746
772
  'less', 'more', 'cat', 'pager']
747
773
  pagers.select! do |f|
748
774
  if f
@@ -1,3 +1,3 @@
1
1
  module CLIMarkdown
2
- VERSION = '0.0.10'
2
+ VERSION = '0.0.12'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mdless
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brett Terpstra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-03 00:00:00.000000000 Z
11
+ date: 2019-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -101,8 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  - !ruby/object:Gem::Version
102
102
  version: '0'
103
103
  requirements: []
104
- rubyforge_project:
105
- rubygems_version: 2.6.13
104
+ rubygems_version: 3.0.2
106
105
  signing_key:
107
106
  specification_version: 4
108
107
  summary: A pager like less, but for Markdown files