Module: Rabbit::Parser::Ext::Emacs
Constant Summary
Constants included
from GetText
GetText::DOMAIN
Class Method Summary
collapse
Methods included from GetText
N_, _, bindtextdomain, locale=
Class Method Details
.find_element(node, name) ⇒ Object
68
69
70
|
# File 'lib/rabbit/parser/ext/emacs.rb', line 68
def find_element(node, name)
node.css(name)[0]
end
|
.highlight(text, logger, mode_line = nil) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/rabbit/parser/ext/emacs.rb', line 14
def highlight(text, logger, mode_line=nil)
src_file = Tempfile.new("rabbit-emacs")
src_file.open
src_file.print("#{mode_line}\n") if mode_line
src_file.print(text)
src_file.close
html_file = Tempfile.new("rabbit-emacs-html")
args = [
"--batch",
"--eval",
"(htmlize-file \"#{src_file.path}\" \"#{html_file.path}\")",
]
if SystemRunner.run("emacs", *args)
html_file.open
html_to_rabbit(html_file.read, mode_line, logger)
else
nil
end
end
|
.html_to_rabbit(html, mode_line, logger) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/rabbit/parser/ext/emacs.rb', line 34
def html_to_rabbit(html, mode_line, logger)
html = remove_newline_around_pre(html)
html = remove_mode_line(html, mode_line)
node = Nokogiri::HTML(html)
pre = find_element(node, "pre")
address = find_element(node, "address")
element = node_to_rabbit(pre, logger)
if element
logger.info(address.text) if address
element
else
nil
end
end
|
.node_to_rabbit(node, logger) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/rabbit/parser/ext/emacs.rb', line 72
def node_to_rabbit(node, logger)
case node.name
when "pre"
element = SyntaxHighlightingBlock.new
node.children.each do |child|
if child.text?
element << text_to_rabbit(child.text, "plain")
else
child_element = node_to_rabbit(child, logger)
element << child_element unless child_element.nil?
end
end
element
when "span"
p [node.text, node["class"]] if Utils.syntax_highlighting_debug?
text_to_rabbit(node.text, normalize_class_name(node["class"]))
else
format = _("emacs: unsupported element name: %s")
logger.warn(format % element.name)
nil
end
end
|
.normalize_class_name(name) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/rabbit/parser/ext/emacs.rb', line 95
def normalize_class_name(name)
case name
when "comment-delimiter"
"comment"
when "keyword"
"reserved"
when "variable-name"
"variable"
else
name
end
end
|
.remove_mode_line(html, mode_line) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/rabbit/parser/ext/emacs.rb', line 54
def remove_mode_line(html, mode_line)
return html if mode_line.nil?
html.gsub(/<pre([^>]*)>([^\n]+\n)/i) do
attributes = $1
mode_line_html = $2
mode_line_text = mode_line_html.gsub(/<.+?>/, '')
if mode_line_text.strip == mode_line.strip
"<pre#{attributes}>"
else
"<pre#{attributes}>#{mode_line_html}"
end
end
end
|
.remove_newline_around_pre(html) ⇒ Object
49
50
51
52
|
# File 'lib/rabbit/parser/ext/emacs.rb', line 49
def remove_newline_around_pre(html)
html = html.gsub(/<pre([^>]*)>\n/i, '<pre\1>')
html.gsub(/\n<\/pre>/i, '</pre>')
end
|
.text_to_rabbit(text, type) ⇒ Object