forked from wallyqs/org-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_output_buffer.rb
309 lines (276 loc) · 10.1 KB
/
html_output_buffer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
begin
require 'pygments'
rescue LoadError
# Pygments is not supported so we try instead with CodeRay
begin
require 'coderay'
rescue LoadError
# No code syntax highlighting
end
end
module Orgmode
class HtmlOutputBuffer < OutputBuffer
HtmlBlockTag = {
:paragraph => "p",
:unordered_list => "ul",
:ordered_list => "ol",
:list_item => "li",
:definition_list => "dl",
:definition_term => "dt",
:definition_descr => "dd",
:table => "table",
:table_row => "tr",
:table_header => "tr",
:blockquote => "blockquote",
:example => "pre",
:src => "pre",
:inline_example => "pre",
:center => "div",
:heading1 => "h1",
:heading2 => "h2",
:heading3 => "h3",
:heading4 => "h4",
:heading5 => "h5",
:heading6 => "h6"
}
attr_reader :options
def initialize(output, opts = {})
super(output)
if opts[:decorate_title] then
@title_decoration = " class=\"title\""
else
@title_decoration = ""
end
@options = opts
@footnotes = {}
@unclosed_tags = []
@logger.debug "HTML export options: #{@options.inspect}"
end
# Output buffer is entering a new mode. Use this opportunity to
# write out one of the block tags in the HtmlBlockTag constant to
# put this information in the HTML stream.
def push_mode(mode, line)
@list_indent_stack.push(line.indent)
if HtmlBlockTag[mode] then
output_indentation
css_class = @title_decoration
css_class = " class=\"src\"" if mode == :src and @block_lang.empty?
css_class = " class=\"src src-#{@block_lang}\"" if mode == :src and not @block_lang.empty?
css_class = " class=\"example\"" if (mode == :example || mode == :inline_example)
css_class = " style=\"text-align: center\"" if mode == :center
unless ((mode == :table and skip_tables?) or
(mode == :src and defined? Pygments))
@logger.debug "#{mode}: <#{HtmlBlockTag[mode]}#{css_class}>\n"
@output << "<#{HtmlBlockTag[mode]}#{css_class}>"
end
# Entering a new mode obliterates the title decoration
@title_decoration = ""
end
super(mode)
end
# We are leaving a mode. Close any tags that were opened when
# entering this mode.
def pop_mode(mode = nil)
m = super(mode)
if HtmlBlockTag[m] then
output_indentation
unless ((mode == :table and skip_tables?) or
(mode == :src and defined? Pygments))
@logger.debug "</#{HtmlBlockTag[m]}>\n"
@output << "</#{HtmlBlockTag[m]}>\n"
end
end
@list_indent_stack.pop
end
def flush!
if buffer_mode_is_src_block?
# Only try to colorize #+BEGIN_SRC blocks with a specified language,
# but we still have to catch the cases when a lexer for the language was not available
if defined? Pygments or defined? CodeRay
lang = normalize_lang(@block_lang)
# NOTE: CodeRay and Pygments already escape the html once, so no need to escape_buffer!
if defined? Pygments
begin
@buffer = Pygments.highlight(@buffer, :lexer => lang)
rescue
# Not supported lexer from Pygments, we fallback on using the text lexer
@buffer = Pygments.highlight(@buffer, :lexer => 'text')
end
elsif defined? CodeRay
# CodeRay might throw a warning when unsupported lang is set,
# then fallback to using the text lexer
silence_warnings do
begin
@buffer = CodeRay.scan(@buffer, lang).html(:wrap => nil, :css => :style)
rescue ArgumentError
@buffer = CodeRay.scan(@buffer, 'text').html(:wrap => nil, :css => :style)
end
end
end
else
escape_buffer!
end
@logger.debug "FLUSH SRC CODE ==========> #{@buffer.inspect}"
@output << @buffer
elsif mode_is_code(@buffer_mode) then
escape_buffer!
# Whitespace is significant in :code mode. Always output the buffer
# and do not do any additional translation.
@logger.debug "FLUSH CODE ==========> #{@buffer.inspect}"
@output << @buffer
else
escape_buffer!
if @buffer.length > 0 and @output_type == :horizontal_rule then
@output << "<hr />\n"
elsif @buffer.length > 0 and @output_type == :definition_item then
unless buffer_mode_is_table? and skip_tables?
output_indentation
d = @buffer.split("::", 2)
@output << "<#{HtmlBlockTag[:definition_term]}#{@title_decoration}>" << inline_formatting(d[0].strip) \
<< "</#{HtmlBlockTag[:definition_term]}>"
if d.length > 1 then
@output << "<#{HtmlBlockTag[:definition_descr]}#{@title_decoration}>" << inline_formatting(d[1].strip) \
<< "</#{HtmlBlockTag[:definition_descr]}>\n"
else
@output << "\n"
end
@title_decoration = ""
end
elsif @buffer.length > 0 then
unless buffer_mode_is_table? and skip_tables?
@logger.debug "FLUSH ==========> #{@buffer_mode}"
if (@buffered_lines[0].kind_of?(Headline)) then
headline = @buffered_lines[0]
raise "Cannot be more than one headline!" if @buffered_lines.length > 1
if @options[:export_heading_number] then
level = headline.level
heading_number = get_next_headline_number(level)
output << "<span class=\"heading-number heading-number-#{level}\">#{heading_number} </span>"
end
if @options[:export_todo] and headline.keyword then
keyword = headline.keyword
output << "<span class=\"todo-keyword #{keyword}\">#{keyword} </span>"
end
end
@output << inline_formatting(@buffer)
else
@logger.debug "SKIP ==========> #{@buffer_mode}"
end
end
end
clear_accumulation_buffer!
end
def output_footnotes!
return false unless @options[:export_footnotes] and not @footnotes.empty?
@output << "<div id=\"footnotes\">\n<h2 class=\"footnotes\">Footnotes:\n</h2>\n<div id=\"text-footnotes\">\n"
@footnotes.each do |name, defi|
@output << "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.#{name}\" href=\"#fnr.#{name}\">#{name}</a></sup>" \
<< inline_formatting(defi) \
<< "\n</p>\n"
end
@output << "</div>\n</div>\n"
return true
end
######################################################################
private
def skip_tables?
@options[:skip_tables]
end
def buffer_mode_is_table?
@buffer_mode == :table
end
def buffer_mode_is_src_block?
@buffer_mode == :src
end
# Escapes any HTML content in the output accumulation buffer @buffer.
def escape_buffer!
@buffer.gsub!(/&/, "&")
@buffer.gsub!(/</, "<")
@buffer.gsub!(/>/, ">")
end
def output_indentation
indent = " " * (@list_indent_stack.length - 1)
@output << indent
end
Tags = {
"*" => { :open => "<b>", :close => "</b>" },
"/" => { :open => "<i>", :close => "</i>" },
"_" => { :open => "<span style=\"text-decoration:underline;\">",
:close => "</span>" },
"=" => { :open => "<code>", :close => "</code>" },
"~" => { :open => "<code>", :close => "</code>" },
"+" => { :open => "<del>", :close => "</del>" }
}
# Applies inline formatting rules to a string.
def inline_formatting(str)
str = @re_help.rewrite_emphasis(str) do |marker, s|
"#{Tags[marker][:open]}#{s}#{Tags[marker][:close]}"
end
if @options[:use_sub_superscripts] then
str = @re_help.rewrite_subp(str) do |type, text|
if type == "_" then
"<sub>#{text}</sub>"
elsif type == "^" then
"<sup>#{text}</sup>"
end
end
end
str = @re_help.rewrite_images(str) do |link|
"<a href=\"#{link}\"><img src=\"#{link}\" /></a>"
end
str = @re_help.rewrite_links(str) do |link, text|
text ||= link
link = link.sub(/^file:(.*)::(.*?)$/) do
# We don't support search links right now. Get rid of it.
"file:#{$1}"
end
if link.match(/^file:.*\.org$/)
link = link.sub(/\.org$/i, ".html")
end
link = link.sub(/^file:/i, "") # will default to HTTP
text = text.gsub(/([^\]]*\.(jpg|jpeg|gif|png))/xi) do |img_link|
"<img src=\"#{img_link}\" />"
end
"<a href=\"#{link}\">#{text}</a>"
end
if (@output_type == :table_row) then
str.gsub!(/^\|\s*/, "<td>")
str.gsub!(/\s*\|$/, "</td>")
str.gsub!(/\s*\|\s*/, "</td><td>")
end
if (@output_type == :table_header) then
str.gsub!(/^\|\s*/, "<th>")
str.gsub!(/\s*\|$/, "</th>")
str.gsub!(/\s*\|\s*/, "</th><th>")
end
if @options[:export_footnotes] then
str = @re_help.rewrite_footnote(str) do |name, defi|
# TODO escape name for url?
@footnotes[name] = defi if defi
"<sup><a class=\"footref\" name=\"fnr.#{name}\" href=\"#fn.#{name}\">#{name}</a></sup>"
end
end
Orgmode.special_symbols_to_html(str)
str
end
def normalize_lang(lang)
case lang
when 'emacs-lisp', 'common-lisp', 'lisp'
'scheme'
when ''
'text'
else
lang
end
end
# Helper method taken from Rails
# https://github.com/rails/rails/blob/c2c8ef57d6f00d1c22743dc43746f95704d67a95/activesupport/lib/active_support/core_ext/kernel/reporting.rb#L10
def silence_warnings
warn_level = $VERBOSE
$VERBOSE = nil
yield
ensure
$VERBOSE = warn_level
end
end # class HtmlOutputBuffer
end # module Orgmode