-
Notifications
You must be signed in to change notification settings - Fork 93
/
html_output_buffer.rb
119 lines (105 loc) · 3.37 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
require OrgRuby.libpath(*%w[org-ruby output_buffer])
module Orgmode
class HtmlOutputBuffer < OutputBuffer
HtmlBlockTag = {
:paragraph => "p",
:ordered_list => "li",
:unordered_list => "li",
:table_row => "tr"
}
ModeTag = {
:unordered_list => "ul",
:ordered_list => "ol",
:table => "table",
:blockquote => "blockquote",
:code => "pre"
}
def initialize(output, opts = {})
super(output)
if opts[:decorate_title] then
@title_decoration = " class=\"title\""
else
@title_decoration = ""
end
end
def push_mode(mode)
if ModeTag[mode] then
output_indentation
@logger.debug "<#{ModeTag[mode]}>\n"
@output << "<#{ModeTag[mode]}>\n"
# Entering a new mode obliterates the title decoration
@title_decoration = ""
end
super(mode)
end
def pop_mode(mode = nil)
m = super(mode)
if ModeTag[m] then
output_indentation
@logger.debug "</#{ModeTag[m]}>\n"
@output << "</#{ModeTag[m]}>\n"
end
end
def flush!
escape_buffer!
if @buffer_mode == :code then
# Whitespace is significant in :code mode. Always output the buffer
# and do not do any additional translation.
#
# FIXME 2009-12-29 bdewey: It looks like I'll always get an extraneous
# newline at the start of code blocks. Find a way to fix this.
@logger.debug "FLUSH CODE ==========> #{@buffer.inspect}"
@output << @buffer << "\n"
else
if (@buffer.length > 0) then
@logger.debug "FLUSH ==========> #{@output_type}"
output_indentation
@output << "<#{HtmlBlockTag[@output_type]}#{@title_decoration}>" \
<< inline_formatting(@buffer) \
<< "</#{HtmlBlockTag[@output_type]}>\n"
@title_decoration = ""
end
end
@buffer = ""
@buffer_mode = nil
end
######################################################################
private
# Escapes any HTML content in the output accumulation buffer @buffer.
def escape_buffer!
@buffer.gsub!(/&/, "&")
@buffer.gsub!(/</, "<")
@buffer.gsub!(/>/, ">")
end
def output_indentation
indent = " " * (@mode_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.rstrip!
str = @re_help.rewrite_emphasis(str) do |marker, s|
"#{Tags[marker][:open]}#{s}#{Tags[marker][:close]}"
end
str = @re_help.rewrite_links(str) do |link, text|
text ||= link
"<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
str
end
end # class HtmlOutputBuffer
end # module Orgmode