Class: ChromaSyntaxHighlighter
- Inherits:
-
Asciidoctor::SyntaxHighlighter::Base
- Object
- Asciidoctor::SyntaxHighlighter::Base
- ChromaSyntaxHighlighter
- Defined in:
- lib/asciidoctor-chroma.rb
Instance Method Summary collapse
- #chroma_available? ⇒ Boolean
- #format(node, lang, opts) ⇒ Object
- #highlight(node, source, lang, opts) ⇒ Object
-
#initialize(*args) ⇒ ChromaSyntaxHighlighter
constructor
A new instance of ChromaSyntaxHighlighter.
Constructor Details
#initialize(*args) ⇒ ChromaSyntaxHighlighter
Returns a new instance of ChromaSyntaxHighlighter.
8 9 10 |
# File 'lib/asciidoctor-chroma.rb', line 8 def initialize *args super end |
Instance Method Details
#chroma_available? ⇒ Boolean
23 24 25 26 27 28 29 30 |
# File 'lib/asciidoctor-chroma.rb', line 23 def chroma_available? @chroma_available ||= begin system('chroma', '--version', out: File::NULL, err: File::NULL) true rescue Errno::ENOENT false end end |
#format(node, lang, opts) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/asciidoctor-chroma.rb', line 12 def format node, lang, opts if chroma_available? source = node.content highlighted = highlight(node, source, lang, opts) return "<div class=\"highlight\">#{highlighted}</div>" else raise "ChromaSyntaxHighlighter: 'chroma' command not found." end super end |
#highlight(node, source, lang, opts) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/asciidoctor-chroma.rb', line 32 def highlight node, source, lang, opts Dir.chdir(Dir.mktmpdir) do output = IO.popen(['chroma', '--html', '--html-only', "--lexer=#{lang}"], 'r+') do |io| io.write(source) io.close_write io.read end raise "Chroma failed with status #{$?.exitstatus}" unless $?.success? return output end end |