Module: Snibbets::Highlight

Defined in:
lib/snibbets/highlight.rb

Class Method Summary collapse

Class Method Details

.highlight(code, filename, syntax, theme = nil) ⇒ Object



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
# File 'lib/snibbets/highlight.rb', line 56

def highlight(code, filename, syntax, theme = nil)
  unless $stdout.isatty
    return Snibbets.options[:all_notes] && code.replace_blocks[0].notes? ? code : code.clean_code
  end

  return highlight_fences(code, filename, syntax) if code.fenced?

  theme ||= Snibbets.options[:highlight_theme] || 'monokai'
  syntax ||= Lexers.syntax_from_extension(filename)
  syntax = Lexers.normalize_lexer(syntax)

  return code if ['text'].include?(syntax)

  skylight = TTY::Which.which('skylighting')
  pygments = TTY::Which.which('pygmentize')

  if Snibbets.options[:highlighter] =~ /^s/ && !skylight.nil?
    if !Lexers.skylight_lexer?(syntax) && !pygments.nil?
      return highlight_pygments(pygments, code, syntax, 'monokai')
    else
      return highlight_skylight(skylight, code, syntax, theme)
    end
  elsif Snibbets.options[:highlighter] =~ /^p/ && !pygments.nil?
    return highlight_pygments(pygments, code, syntax, theme)
  elsif !skylight.nil?
    return highlight_skylight(skylight, code, syntax, theme)
  elsif !pygments.nil?
    return highlight_pygments(pygments, code, syntax, theme)
  end

  code
end

.highlight_fences(code, filename, syntax) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/snibbets/highlight.rb', line 43

def highlight_fences(code, filename, syntax)
  content = code.dup

  content.fences.each do |f|
    rx = Regexp.new(Regexp.escape(f[:code]))
    syn = Lexers.normalize_lexer(f[:lang] || syntax)
    highlighted = highlight(f[:code].gsub(/\\k</, '\k\<'), filename, syn).strip
    code.sub!(/#{rx}/, highlighted)
  end

  Snibbets.options[:all_notes] ? code.gsub(/k\\</, 'k<') : code.gsub(/k\\</, 'k<').clean_code
end

.highlight_pygments(executable, code, syntax, theme) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/snibbets/highlight.rb', line 15

def highlight_pygments(executable, code, syntax, theme)
  syntax = syntax.nil? || syntax.empty? ? '-g' : "-l #{syntax}"
  theme = theme.nil? || theme.empty? ? '' : ",style=#{theme}"
  command = "#{executable} -O full#{theme} #{syntax}"
  fallback = "#{executable} -O full#{theme} -g"
  run_command_with_input(command, input: code, fallback: fallback)
end

.highlight_skylight(executable, code, syntax, theme) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/snibbets/highlight.rb', line 23

def highlight_skylight(executable, code, syntax, theme)
  theme ||= 'nord'
  theme_file = if theme =~ %r{^[/~].*?(\.theme)?$}
                 theme = theme.sub(/(\.theme)?$/, '.theme')
                 File.expand_path(theme)
               else
                 theme = theme.sub(/\.theme$/, '')
                 File.join(__dir__, '..', 'themes', "#{theme}.theme")
               end

  theme = if File.exist?(theme_file)
            "-t #{theme_file} "
          else
            ''
          end
  return code if syntax.nil? || syntax.empty? || !Lexers.skylight_lexer?(syntax)

  run_command_with_input("#{executable} #{theme}--syntax #{syntax}", input: code)
end

.run_command_with_input(*cmd, input: nil, fallback: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/snibbets/highlight.rb', line 4

def run_command_with_input(*cmd, input: nil, fallback: nil)
  stdout, _stderr, status = Open3.capture3(*cmd, stdin_data: input)
  if status.success?
    stdout
  elsif fallback.nil?
    input
  else
    run_command_with_input(fallback, input: input, fallback: nil)
  end
end