Class: ActToHtmlParser

Inherits:
Object
  • Object
show all
Defined in:
app/models/act_to_html_parser.rb

Constant Summary collapse

DIV =
%w[LongTitle DateOfEnactment
P1group 
P1para
P2
P2para
P3
P3para
Number
LongTitle
Text].inject({}){|h,v| h[v]=true; h}
DIV_RE =
Regexp.new "(^#{DIV.keys.join("$|")}$)"
SPAN =
%w[DateText Addition
CommentaryRef
Term ].inject({}){|h,v| h[v]=true; h}
SPAN_RE =
Regexp.new "(^#{SPAN.keys.join("$|")}$)"
UL =
%w[Sponsors].inject({}){|h,v| h[v]=true; h}
UL_RE =
Regexp.new "(#{UL.keys.join("|")})"
LI =
%w[Sponsor].inject({}){|h,v| h[v]=true; h}
LI_RE =
Regexp.new "(#{LI.keys.join("|")})"

Instance Method Summary collapse

Instance Method Details

#add(text) ⇒ Object


120
121
122
123
124
125
126
# File 'app/models/act_to_html_parser.rb', line 120

def add text
  if text.nil?
    raise 'text should not be null'
  else
    @html << text
  end
end

#add_html_element(name, node) ⇒ Object


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/act_to_html_parser.rb', line 96

def add_html_element name, node
  start_tag = []
  start_tag << %Q|<#{name} class="#{css_class(node)}"|
  start_tag << %Q| id="#{node['id']}"| if node['id']
  if name == 'hr'
    start_tag << " />"
  else
    start_tag << ">"
  end

  add start_tag.join('')
  
  if name != 'hr'
    node_children_to_html(node)
    add "</#{name}>"
  end
end

#css_class(node) ⇒ Object


114
115
116
117
118
# File 'app/models/act_to_html_parser.rb', line 114

def css_class node
  @last_css_class = node.name.gsub('.','_')
  @last_css_class += " #{node['class']}" unless node['class'].blank?
  @last_css_class
end

#doc_to_html(doc) ⇒ Object


82
83
84
85
86
87
88
# File 'app/models/act_to_html_parser.rb', line 82

def doc_to_html(doc)
  @in_prelims = false
  @in_body= false
  @last_element_P1 = false
  @clause_title = ""
  node_children_to_html(doc.root)
end

#generate_haml(html, options) ⇒ Object


23
24
25
26
27
28
29
30
31
32
# File 'app/models/act_to_html_parser.rb', line 23

def generate_haml html, options
  html_file = Tempfile.new("#{Time.now.to_i.to_s}.html", "#{RAILS_ROOT}/tmp")
  html_file.write html
  html_file.close
  cmd = "html2haml #{html_file.path}"
  haml = `#{cmd}`
  html_file.delete
  
  haml
end

#generate_html(doc, options) ⇒ Object


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
# File 'app/models/act_to_html_parser.rb', line 34

def generate_html doc, options
  if options[:body_only]
    @html = ['<div id="Legislation">']
    doc_to_html(doc)
    add ['</div>']
  else
    @html = ['<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><div id="Legislation">']
    doc_to_html(doc)
    add ['</div></body></html>'] 
  end

  result = @html.join('')
  begin
    doc = REXML::Document.new(result)
  rescue Exception => e
    puts e.to_s
  end
  if options[:indent]
    indented = ''
    doc.write(indented,2)
    indented
  else
    result
  end
end

#node_children_to_html(node) ⇒ Object


90
91
92
93
94
# File 'app/models/act_to_html_parser.rb', line 90

def node_children_to_html(node)
  node.children.each do |child|
    node_to_html(child)
  end if node.children
end

#node_to_html(node) ⇒ Object


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
# File 'app/models/act_to_html_parser.rb', line 128

def node_to_html(node)
  case node.name.gsub('.','_')
    when 'Primary'
      node_children_to_html(node)
    when 'PrimaryPrelims'
      @in_prelims = true
      add_html_element('div', node)
    when 'Body'
      @in_prelims = false
      @in_body = true
      node_children_to_html(node)
    when 'Title'
      if @in_prelims
        add_html_element('h1', node)
      elsif @in_body
        @clause_title = node.children[0].to_s
      end
    when 'P1'
      @last_element_P1 = true
      add_html_element('div', node)
    when 'Pnumber'
      if @last_element_P1
        add %Q|<div class="P1Title">|
        add %Q|<div class="TitleNum">#{node.children[0].to_s}</div>|
        add %Q|<div class="Title">#{@clause_title}</div>|
        add "</div>"
        @last_element_P1 = false
        @clause_title = ""
      else
        add %Q|<div class="Pnumber">(#{node.children[0].to_s})</div>|
      end
    when /^ukm:/
      #ignore metadata for now
    when 'Commentaries'
      #ignore for now
    when DIV_RE
      add_html_element 'div', node
    when SPAN_RE
      add_html_element 'span', node
    else
      raise node.name
      node_children_to_html(node)
  end if node.elem?
  
  if node.text?
    text = node.to_s.gsub("/n", "<br />")
    if @in_prelims
      text.gsub!(" c. ", " Chapter ")
    end
    add text
  end
end

#parse_xml(xml, options = {:format => :html}) ⇒ Object


12
13
14
15
16
17
18
19
20
21
# File 'app/models/act_to_html_parser.rb', line 12

def parse_xml xml, options={:format => :html}
  doc = Hpricot.XML xml
  format = options[:format]
  if format == :html
    generate_html doc, options
  elsif format == :haml
    html = generate_html doc, options
    generate_haml html, options
  end
end

#parse_xml_file(xml_file, options) ⇒ Object

e.g. parser.parse_xml_file(“LawCommissionsAct.xml”)


8
9
10
# File 'app/models/act_to_html_parser.rb', line 8

def parse_xml_file xml_file, options
  parse_xml(IO.read(xml_file), options)
end