Skip to content

Commit

Permalink
slight improvements to image rendering
Browse files Browse the repository at this point in the history
- IMPROVED: Image rendering with chafa improved, still have to figure out a way to make sure content breaks around the embedded image
  • Loading branch information
ttscoff committed Nov 24, 2023
1 parent 07f3c80 commit 49358f5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
20 changes: 11 additions & 9 deletions lib/mdless/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ def image(link, title, alt_text)
end
elsif exec_available('chafa')
@log.info('Using chafa for image rendering')
term = '-f sixels'
term = ENV['TERMINAL_PROGRAM'] =~ /iterm/i ? '-f iterm' : term
term = ENV['TERMINAL_PROGRAM'] =~ /kitty/i ? '-f kitty' : term
term = ''
term = ENV['TERM_PROGRAM'] =~ /iterm/i ? '-f iterm' : term
term = ENV['TERM_PROGRAM'] =~ /kitty/i ? '-f kitty' : term
FileUtils.rm_r '.mdless_tmp', force: true if File.directory?('.mdless_tmp')
Dir.mkdir('.mdless_tmp')
Dir.chdir('.mdless_tmp')
Expand All @@ -377,11 +377,13 @@ def image(link, title, alt_text)
pre = !alt_text.nil? ? " #{c(%i[d blue])}[#{alt_text.strip}]\n" : ''
post = !title.nil? ? "\n #{c(%i[b blue])}-- #{title} --" : ''
if exec_available('imgcat')
img = `imgcat "#{img_path}"`
img = `imgcat -p "#{img_path}"`
@log.info("Rendering image with `imgcat -p #{img_path}`")
elsif exec_available('chafa')
term = '-f sixels'
term = ENV['TERMINAL_PROGRAM'] =~ /iterm/i ? '-f iterm' : term
term = ENV['TERMINAL_PROGRAM'] =~ /kitty/i ? '-f kitty' : term
term = ''
term = ENV['TERM_PROGRAM'] =~ /iterm/i ? '-f iterm' : term
term = ENV['TERM_PROGRAM'] =~ /kitty/i ? '-f kitty' : term
@log.info("Rendering image with `chafa #{term} #{img_path}`")
img = `chafa #{term} "#{img_path}"`
end
result = pre + img + post
Expand Down Expand Up @@ -572,7 +574,8 @@ def get_headers(input)

def preprocess(input)
in_yaml = false
if input.split("\n")[0] =~ /(?i-m)^---[ \t]*?(\n|$)/
first_line = input.split("\n").first
if first_line =~ /(?i-m)^---[ \t]*?$/
@log.info('Found YAML')
# YAML
in_yaml = true
Expand All @@ -596,7 +599,6 @@ def preprocess(input)
end
end

first_line = input.split("\n").first
if !in_yaml && first_line =~ /(?i-m)^[\w ]+:\s+\S+/
@log.info('Found MMD Headers')
input.sub!(/(?i-m)^([\S ]+:[\s\S]*?)+(?=\n\n)/) do |mmd|
Expand Down
8 changes: 6 additions & 2 deletions lib/mdless/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ def initialize(args)
opts.on('-i', '--images=TYPE',
'Include [local|remote (both)] images in output (requires chafa or imgcat, default NONE).') do |type|
if exec_available('imgcat') || exec_available('chafa')
if type =~ /^(r|b|a)/i
case type
when /^(r|b|a)/i
@options[:local_images] = true
@options[:remote_images] = true
elsif type =~ /^l/i
when /^l/i
@options[:local_images] = true
when /^n/
@options[:local_images] = false
@options[:remote_images] = false
end
else
@log.warn('images turned on but imgcat/chafa not found')
Expand Down

0 comments on commit 49358f5

Please sign in to comment.