Module: Rabbit::Parser::Ext::Enscript
Constant Summary collapse
- @@enscript_highlight =
{}
Constants included from GetText
Class Method Summary collapse
- .check_availability(lang, logger) ⇒ Object
- .element_to_rabbit(element, logger) ⇒ Object
- .extract_newline_around_pre(html) ⇒ Object
- .find_element(node, name) ⇒ Object
- .highlight(lang, text, logger) ⇒ Object
- .html_to_rabbit(html, logger) ⇒ Object
- .node_to_rabbit(node, logger) ⇒ Object
Methods included from GetText
Class Method Details
.check_availability(lang, logger) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/rabbit/parser/ext/enscript.rb', line 26 def check_availability(lang, logger) if @@enscript_highlight.has_key?(lang) true else logger.warn(_("enscript: unsupported language: %s") % lang) false end end |
.element_to_rabbit(element, logger) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rabbit/parser/ext/enscript.rb', line 98 def element_to_rabbit(element, logger) case element.name when "pre" PreformattedText.new when "b" Keyword.new when "i" Element::Comment.new when "font" text = TextContainer.new color = element.get_attribute("color").to_s text.add_default_prop("foreground", color) text else format = _("enscript: unsupported element name: %s") logger.warn(format % element.name) nil end end |
.extract_newline_around_pre(html) ⇒ Object
75 76 77 78 |
# File 'lib/rabbit/parser/ext/enscript.rb', line 75 def extract_newline_around_pre(html) html = html.gsub(/<pre[^>]*>\n/i, '<pre\1>') html.gsub(/\n<\/pre>/i, '</pre>') end |
.find_element(node, name) ⇒ Object
80 81 82 |
# File 'lib/rabbit/parser/ext/enscript.rb', line 80 def find_element(node, name) node.css(name)[0] end |
.highlight(lang, text, logger) ⇒ Object
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 |
# File 'lib/rabbit/parser/ext/enscript.rb', line 35 def highlight(lang, text, logger) begin require 'nokogiri' rescue logger.warning("Syntax highlight by enscript requires nokogiri.") return nil end src_file = Tempfile.new("rabbit-enscript") src_file.open src_file.print(text) src_file.close html_file = Tempfile.new("rabbit-enscript-html") args = [ "--quiet", "--color", "--language=html", "--highlight=#{@@enscript_highlight[lang]}", "--output=#{html_file.path}", src_file.path, ] if SystemRunner.run("enscript", *args) html_file.open html_to_rabbit(html_file.read, logger) else nil end end |
.html_to_rabbit(html, logger) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rabbit/parser/ext/enscript.rb', line 62 def html_to_rabbit(html, logger) node = Nokogiri::HTML(extract_newline_around_pre(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 PreformattedBlock.new(element) else nil end end |
.node_to_rabbit(node, logger) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rabbit/parser/ext/enscript.rb', line 84 def node_to_rabbit(node, logger) element = element_to_rabbit(node, logger) return nil if element.nil? node.children.each do |child| if child.text? element << Text.new(Escape.(child.text)) else child_element = node_to_rabbit(child, logger) element << child_element unless child_element.nil? end end element end |