11module 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