Skip to content

Commit b914789

Browse files
author
Ricardo Arredondo
committed
Add highlighting through rouge.
1 parent 7a28c2e commit b914789

2 files changed

Lines changed: 70 additions & 39 deletions

File tree

lib/org-ruby/highlighter.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module Orgmode
2+
# Highlight code
3+
module Highlighter
4+
def self.highlight(code, lang)
5+
highlighter = guess_highlighter
6+
highlighter.highlight(code, lang)
7+
end
8+
9+
def self.guess_highlighter
10+
return RougeHighliter if gem_present?('rouge')
11+
return PygmentsHighliter if gem_present?('pygments.rb')
12+
return CodeRayHighliter if gem_present?('coderay')
13+
14+
DefaultHighliter
15+
end
16+
17+
def self.gem_present?(gem)
18+
Gem::Specification.find_all_by_name(gem).any?
19+
end
20+
21+
# Default highliter does nothing to code
22+
class DefaultHighliter
23+
def self.highlight(buffer, _lang)
24+
buffer
25+
end
26+
end
27+
28+
# Pygments wrapper
29+
class PygmentsHighliter
30+
def self.highlight(buffer, lang)
31+
require 'pygments'
32+
if Pygments::Lexer.find_by_alias(lang)
33+
Pygments.highlight(buffer, lexer: lang)
34+
else
35+
Pygments.highlight(buffer, lexer: 'text')
36+
end
37+
end
38+
end
39+
40+
# CodeRay wrapper
41+
class CodeRayHighliter
42+
def self.highlight(buffer, lang)
43+
require 'coderay'
44+
CodeRay.scan(buffer, lang).html(wrap: nil, css: :style)
45+
rescue ArgumentError => _e
46+
CodeRay.scan(buffer, 'text').html(wrap: nil, css: :style)
47+
end
48+
end
49+
50+
# Rouge wrapper
51+
class RougeHighliter
52+
def self.highlight(buffer, lang)
53+
require 'rouge'
54+
formatter = Rouge::Formatters::HTMLLegacy.new
55+
lexer = Rouge::Lexer.find_fancy(lang, buffer) ||
56+
Rouge::Lexers::PlainText
57+
formatter.format(lexer.lex(buffer.strip))
58+
end
59+
end
60+
end
61+
end

lib/org-ruby/html_output_buffer.rb

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Orgmode
22

33
class HtmlOutputBuffer < OutputBuffer
4-
4+
require_relative './highlighter.rb'
55
HtmlBlockTag = {
66
:paragraph => "p",
77
:ordered_list => "ol",
@@ -37,20 +37,6 @@ def initialize(output, opts = {})
3737
@unclosed_tags = []
3838
@logger.debug "HTML export options: #{@options.inspect}"
3939
@custom_blocktags = {} if @options[:markup_file]
40-
41-
unless @options[:skip_syntax_highlight]
42-
begin
43-
require 'pygments'
44-
rescue LoadError
45-
# Pygments is not supported so we try instead with CodeRay
46-
begin
47-
require 'coderay'
48-
rescue LoadError
49-
# No code syntax highlighting
50-
end
51-
end
52-
end
53-
5440
if @options[:markup_file]
5541
do_custom_markup
5642
end
@@ -64,7 +50,7 @@ def push_mode(mode, indent, properties={})
6450
super(mode, indent, properties)
6551
if HtmlBlockTag[mode]
6652
unless ((mode_is_table?(mode) and skip_tables?) or
67-
(mode == :src and !@options[:skip_syntax_highlight] and defined? Pygments))
53+
(mode == :src and !@options[:skip_syntax_highlight]))
6854
css_class = case
6955
when (mode == :src and @block_lang.empty?)
7056
" class=\"src\""
@@ -100,7 +86,7 @@ def pop_mode(mode = nil)
10086
m = super(mode)
10187
if HtmlBlockTag[m]
10288
unless ((mode_is_table?(m) and skip_tables?) or
103-
(m == :src and !@options[:skip_syntax_highlight] and defined? Pygments))
89+
(m == :src and !@options[:skip_syntax_highlight]))
10490
add_paragraph if @new_paragraph
10591
@new_paragraph = true
10692
@logger.debug "</#{HtmlBlockTag[m]}>"
@@ -110,6 +96,10 @@ def pop_mode(mode = nil)
11096
@list_indent_stack.pop
11197
end
11298

99+
def highlight(code, lang)
100+
Highlighter.highlight code, lang
101+
end
102+
113103
def flush!
114104
return false if @buffer.empty?
115105
case
@@ -121,29 +111,9 @@ def flush!
121111
case
122112
when (current_mode == :src and @options[:skip_syntax_highlight])
123113
@buffer = escapeHTML @buffer
124-
when (current_mode == :src and defined? Pygments)
114+
when (current_mode == :src)
125115
lang = normalize_lang @block_lang
126-
@output << "\n" unless @new_paragraph == :start
127-
@new_paragraph = true
128-
129-
begin
130-
@buffer = Pygments.highlight(@buffer, :lexer => lang)
131-
rescue
132-
# Not supported lexer from Pygments, we fallback on using the text lexer
133-
@buffer = Pygments.highlight(@buffer, :lexer => 'text')
134-
end
135-
when (current_mode == :src and defined? CodeRay)
136-
lang = normalize_lang @block_lang
137-
138-
# CodeRay might throw a warning when unsupported lang is set,
139-
# then fallback to using the text lexer
140-
silence_warnings do
141-
begin
142-
@buffer = CodeRay.scan(@buffer, lang).html(:wrap => nil, :css => :style)
143-
rescue ArgumentError
144-
@buffer = CodeRay.scan(@buffer, 'text').html(:wrap => nil, :css => :style)
145-
end
146-
end
116+
@buffer = highlight @buffer, lang
147117
when (current_mode == :html or current_mode == :raw_text)
148118
@buffer.gsub!(/\A\n/, "") if @new_paragraph == :start
149119
@new_paragraph = true

0 commit comments

Comments
 (0)