Module: Rabbit::Parser::Ext::Emacs

Includes:
Element, GetText
Defined in:
lib/rabbit/parser/ext/emacs.rb

Constant Summary

Constants included from GetText

GetText::DOMAIN

Class Method Summary collapse

Methods included from GetText

included

Class Method Details

.find_element(node, name) ⇒ Object



73
74
75
# File 'lib/rabbit/parser/ext/emacs.rb', line 73

def find_element(node, name)
  node.css(name)[0]
end

.highlight(text, logger, mode_line = nil) ⇒ Object



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
# File 'lib/rabbit/parser/ext/emacs.rb', line 12

def highlight(text, logger, mode_line=nil)
  begin
    require 'nokogiri'
  rescue
    logger.warning("Syntax highlight by Emacs requires nokogiri.")
    return nil
  end

  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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rabbit/parser/ext/emacs.rb', line 39

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rabbit/parser/ext/emacs.rb', line 77

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



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rabbit/parser/ext/emacs.rb', line 100

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



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rabbit/parser/ext/emacs.rb', line 59

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



54
55
56
57
# File 'lib/rabbit/parser/ext/emacs.rb', line 54

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



113
114
115
116
117
# File 'lib/rabbit/parser/ext/emacs.rb', line 113

def text_to_rabbit(text, type)
  escaped_text = Escape.escape_meta_character(text)
  text_element = SyntaxHighlightingText.new(Text.new(escaped_text))
  CustomTag.new("syntax-#{type}", text_element)
end