Skip to content

Commit

Permalink
Merge branch 'release/2.1.11'
Browse files Browse the repository at this point in the history
* release/2.1.11:
  ### 2.1.11
  better regex for tag highlighting
  fix highlighting html tags breaking pre/post conversion
  fix for indentation in highlighted code blocks
  • Loading branch information
ttscoff committed Dec 3, 2023
2 parents 73d01ab + 66ac250 commit 663f937
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 35 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2.1.11
: Better regex for highlighting raw HTML
: Indentation in highlighted code blocks
: HTML Tag highlighting breaking pre/post color conversion

2.1.10
: Spinner while processing to indicate working
: Image links can now be converted to reference format, with correct coloring
Expand Down
1 change: 1 addition & 0 deletions lib/mdless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'tty-which'
require 'tty-screen'
require 'tty-spinner'
require 'rouge'
require 'mdless/version.rb'
require 'mdless/colors'
require 'mdless/tables'
Expand Down
6 changes: 4 additions & 2 deletions lib/mdless/colors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ def wrap(width=78, foreground=:x)
# input.gsub!(/\[.*?\]\(.*?\)/) do |link|
# link.gsub(/ /, "\u00A0")
# end
input.split(/\s+/).each do |word|
input.split(/\s/).each do |word|
last_ansi = line.last_color_code
if visible_width + word.size_clean >= width
if word =~ /[\s\t]/
line << word
elsif visible_width + word.size_clean >= width
lines << line + xc
visible_width = word.size_clean
line = last_ansi + word
Expand Down
97 changes: 67 additions & 30 deletions lib/mdless/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,41 @@ def code_bg(input, width)
end

def hilite_code(code_block, language)
longest_line = code_block.uncolor.split(/\n/).longest_element.length + 4
longest_line = longest_line > MDLess.cols ? MDLess.cols : longest_line

# if MDLess.options[:syntax_higlight]
# formatter = Rouge::Formatters::Terminal256
# lexer = if language
# Object.const_get("Rouge::Lexers::#{language.capitalize}") rescue Rouge::Lexer.guess(source: code_block)
# else
# Rouge::Lexer.guess(source: code_block)
# end
# hilite = formatter.format(lexer.lex(code_block))
# hilite = xc + hilite.split(/\n/).map do |l|
# [
# color('code_block marker'),
# MDLess.theme['code_block']['character'],
# "#{color('code_block bg')}#{l.rstrip}#{xc}"
# ].join
# end.join("\n").blackout(MDLess.theme['code_block']['bg']) + "#{xc}\n"
# else
# hilite = code_block.split(/\n/).map do |line|
# [
# color('code_block marker'),
# MDLess.theme['code_block']['character'],
# color('code_block color'),
# line,
# xc
# ].join
# end.join("\n").blackout(MDLess.theme['code_block']['bg']) + "#{xc}\n"
# end

if MDLess.options[:syntax_higlight] && !exec_available('pygmentize')
MDLess.log.error('Syntax highlighting requested by pygmentize is not available')
MDLess.options[:syntax_higlight] = false
end

longest_line = code_block.uncolor.split(/\n/).longest_element.length + 4
longest_line = longest_line > MDLess.cols ? MDLess.cols : longest_line

if MDLess.options[:syntax_higlight]
pyg = TTY::Which.which('pygmentize')
lexer = language&.valid_lexer? ? "-l #{language}" : '-g'
Expand All @@ -93,15 +120,17 @@ def hilite_code(code_block, language)
].join(' ')
hilite, s = Open3.capture2(cmd,
stdin_data: code_block)

if s.success?
hilite = xc + hilite.split(/\n/).map do |l|
[
color('code_block marker'),
MDLess.theme['code_block']['character'],
"#{color('code_block bg')}#{l.rstrip}#{xc}"
"#{color('code_block bg')}#{l}#{xc}"
].join
end.join("\n").blackout(MDLess.theme['code_block']['bg']) + "#{xc}\n"
end

rescue StandardError => e
MDLess.log.error(e)
hilite = code_block
Expand All @@ -123,7 +152,6 @@ def hilite_code(code_block, language)
else
"--[ #{language} ]#{'-' * (longest_line - 6 - language.length)}"
end

[
xc,
color('code_block border'),
Expand Down Expand Up @@ -234,10 +262,15 @@ def hrule()
end

def paragraph(text)
if MDLess.options[:preserve_linebreaks]
"#{xc}#{text.gsub(/ +/, ' ').strip}#{xc}#{x}\n\n"
out = if MDLess.options[:preserve_linebreaks]
"#{xc}#{text.gsub(/ +/, ' ').strip}#{xc}#{x}\n\n"
else
"#{xc}#{text.gsub(/ +/, ' ').gsub(/\n+(?![:-])/, ' ').strip}#{xc}#{x}\n\n"
end
if MDLess.options[:at_tags] || MDLess.options[:taskpaper]
highlight_tags(out)
else
"#{xc}#{text.gsub(/ +/, ' ').gsub(/\n+(?![:-])/, ' ').strip}#{xc}#{x}\n\n"
out
end
end

Expand Down Expand Up @@ -481,7 +514,7 @@ def link(link, title, content)
end

def color_tags(html)
html.gsub(%r{(<\S+( [^>]+)?>)}, "#{color('html brackets')}\\1#{xc}")
html.gsub(%r{((?!<)</?\w+( [^>]+)?>)}, "#{color('html brackets')}\\1#{xc}")
end

def raw_html(raw_html)
Expand Down Expand Up @@ -580,26 +613,31 @@ def indent_lines(input, spaces)
end

def color_list_item(indent, content, type, counter)
case type
when :unordered
[
indent,
color('list bullet'),
MDLess.theme['list']['ul_char'].strip,
' ',
color('list color'),
indent_lines(content, indent).strip,
xc
].join
when :ordered
[
indent,
color('list number'),
"#{counter}. ",
color('list color'),
indent_lines(content, indent).strip,
xc
].join
out = case type
when :unordered
[
indent,
color('list bullet'),
MDLess.theme['list']['ul_char'].strip,
' ',
color('list color'),
indent_lines(content, indent).strip,
xc
].join
when :ordered
[
indent,
color('list number'),
"#{counter}. ",
color('list color'),
indent_lines(content, indent).strip,
xc
].join
end
if MDLess.options[:at_tags] || MDLess.options[:taskpaper]
color_tags(out)
else
out
end
end

Expand Down Expand Up @@ -1035,7 +1073,6 @@ def postprocess(input)
input = reference_links(input) if MDLess.options[:links] == :reference || MDLess.options[:links] == :paragraph
# lists
input = fix_lists(input)
input = highlight_tags(input) if MDLess.options[:at_tags] || MDLess.options[:taskpaper]
fix_colors(input)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mdless/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def initialize(args)
end

default(:at_tags, false)
opts.on('-@', '--at_tags', 'Highlight @tags and values in the document') do
MDLess.options[:at_tags] = true
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
Expand Down
2 changes: 1 addition & 1 deletion lib/mdless/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module CLIMarkdown
VERSION = '2.1.10'
VERSION = '2.1.11'
end
1 change: 1 addition & 0 deletions mdless.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ spec = Gem::Specification.new do |s|
s.add_dependency 'tty-which', '~> 0.5'
s.add_dependency 'tty-screen', '~> 0.8'
s.add_dependency 'tty-spinner', '~> 0.8'
s.add_dependency 'rouge', '~> 4.2'
s.add_development_dependency 'rake', '~> 13'
s.add_development_dependency 'rdoc', '>= 6.3.1'
end

0 comments on commit 663f937

Please sign in to comment.