Class: MifTableParser

Inherits:
Object
  • Object
show all
Includes:
MifParserUtils
Defined in:
app/models/mif_table_parser.rb

Constant Summary

Constants included from MifParserUtils

MifParserUtils::AMEND_REF, MifParserUtils::COMPRESS_WHITESPACE, MifParserUtils::COMPRESS_WHITESPACE_2, MifParserUtils::COMPRESS_WHITESPACE_3, MifParserUtils::COMPRESS_WHITESPACE_4, MifParserUtils::COMPRESS_WHITESPACE_5, MifParserUtils::LINK_REGEX, MifParserUtils::NEED_SPACE_BETWEEN_LABEL_AND_NUMBER_REGEX, MifParserUtils::NEED_SPACE_BETWEEN_LABEL_AND_NUMBER_REGEX_2, MifParserUtils::NEED_SPACE_BETWEEN_LABEL_AND_XREF_REGEX, MifParserUtils::NEED_SPACE_BETWEEN_LABEL_AND_XREF_REGEX_2, MifParserUtils::SPAN_REGEX, MifParserUtils::TOGGLE_SHOW_REGEXP, MifParserUtils::TOGGLE_SHOW_REGEXP_2

Instance Method Summary collapse

Methods included from MifParserUtils

#clean, #for_each_match, #format_haml, #get_attributes, #get_char, #get_uid, #make_attr, #postprocess, #preprocess, #start_tag

Instance Method Details

#add_text(text, tables) ⇒ Object


226
227
228
# File 'app/models/mif_table_parser.rb', line 226

def add_text text, tables
  tables[@current_table_id] << text
end

#get_table_css_class(node) ⇒ Object


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'app/models/mif_table_parser.rb', line 259

def get_table_css_class node
  css_class = ""
  if !@format_info[@table_tag]["border_top"].empty? && !@format_info[@table_tag]["border_right"].empty? && !@format_info[@table_tag]["border_bottom"].empty? && !@format_info[@table_tag]["border_left"].empty?
    css_class = "allborders"
  else
    unless @format_info[@table_tag]["border_top"].empty?
      css_class += " topborder "
    end
    unless @format_info[@table_tag]["border_right"].empty?
      css_class += " rightborder"
    end
    unless @format_info[@table_tag]["border_bottom"].empty?
      css_class += " bottomborder"
    end
    unless @format_info[@table_tag]["border_left"].empty?
      css_class += " leftborder"
    end
    
    if node.parent.at('TblFormat/TblTRuling/text()') && node.parent.at('TblFormat/TblLRuling/text()') && node.parent.at('TblFormat/TblBRuling/text()') && node.parent.at('TblFormat/TblRRuling/text()')
      css_class = "allborders"
    else
      if node.parent.at('TblFormat/TblTRuling/text()')
        css_class += " topborder" unless css_class.include?("topborder")
      end
      if node.parent.at('TblFormat/TblRRuling/text()')
        css_class += " rightborder" unless css_class.include?("rightborder")
      end
      if node.parent.at('TblFormat/TblBRuling/text()')
        css_class += " bottomborder" unless css_class.include?("bottomborder")
      end
      if node.parent.at('TblFormat/TblLRuling/text()')
        css_class += " leftborder" unless css_class.include?("leftborder")
      end
    end
  end
  
  css_class.strip
end

#get_tables(doc) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/models/mif_table_parser.rb', line 7

def get_tables doc
  @format_info = {}
  table_formats = (doc/'TblCatalog/TblFormat')
  table_formats.each do |format|
    handle_format format
  end
  
  tables = (doc/'Tbls/Tbl')
  tables.inject({}) do |hash, table|
    handle_table(table, hash)
  end
end

#handle_attribute(node, tables) ⇒ Object


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/models/mif_table_parser.rb', line 199

def handle_attribute node, tables
  if clean(node.at('AttrName')) == 'Align'
    attr_value = clean(node.at('AttrValue'))
    alignment = ''
    case attr_value
      when 'Center'
        alignment = 'centered'
      when 'Right'
        alignment = 'right'
    end
    cell_start = tables[@current_table_id].pop
    if cell_start.include?('class=')
      cell_start.gsub!('">', %Q| #{alignment}">|)
    else
      cell_start.gsub!('>', %Q| class="#{alignment}">|)
    end
    tables[@current_table_id] << cell_start
  end
end

#handle_body(tables) ⇒ Object


219
220
221
222
223
224
# File 'app/models/mif_table_parser.rb', line 219

def handle_body tables
  tables[@current_table_id] << '</CellH></Row>' if @in_heading
  @in_row = false
  @in_cell = false
  @in_heading = false
end

#handle_cell(node, tables) ⇒ Object


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
180
181
182
183
184
185
186
187
188
# File 'app/models/mif_table_parser.rb', line 101

def handle_cell node, tables
  if @colspan_target > 0
    if @colspan_count < @colspan_target
      @colspan_count += 1
      @cell_count += 1
      return
    else
      @colspan_target = 0
    end
  end
  
  colspan = 1
  if node.at('CellColumns/text()')
    colspan = node.at('CellColumns/text()').to_s
    colspan = colspan.to_i
  end
  
  first = ' class="first"'
  if @in_cell
    first = ""
    if @in_heading
      tables[@current_table_id] << "</CellH>"
    else
      tables[@current_table_id] << "</Cell>"
    end
  end
  cell_id = node.at('Element/Unique/text()').to_s
  @in_cell = true
  
  css_class = ""
  unless @format_info[@table_tag]["col_x_border"].empty?
    if (@format_info[@table_tag]["col_x"] == @cell_count.to_s && !@in_heading) ||
      (@format_info[@table_tag]["col_x"].to_i == @cell_count.to_i + (colspan-1) && !@in_heading)
      if first == ""
        css_class = %Q| class="leftborder"|
        if @no_of_cols.to_i+-1 != @cell_count.to_i+(colspan-1)
          css_class = %Q| class="leftborder rightborder"|
        end
      else
        if @no_of_cols.to_i-1 != @cell_count+(colspan-1)
          css_class = %Q| class="rightborder"|
        end
      end
    end
  end
  
  unless @format_info[@table_tag]["col_border"].empty?
    if first == ""
      css_class = %Q| class="leftborder"|
      if @no_of_cols.to_i+-1 != @cell_count.to_i+(colspan-1)
        css_class = %Q| class="leftborder rightborder"|
      end
    else
      if @no_of_cols.to_i-1 != @cell_count+(colspan-1)
        css_class = %Q| class="rightborder"|
      end
    end
  end
  
  #assumes that if a Ruling node is used in this position then it is set to a valid value
  if node.at('CellTRuling/text()') && node.at('CellRRuling/text()') && node.at('CellBRuling/text()') && node.at('CellLRuling/text()')
    css_class = %Q| class="allborders"|
  else
    if node.at('CellTRuling/text()')
      css_class.gsub!('class="', 'class="topborder ') unless css_class.include?('topborder')
    end
    if node.at('CellRRuling/text()')
      css_class.gsub!('class="', 'class="rightborder ') unless css_class.include?('rightborder')
    end
    if node.at('CellBRuling/text()')
      css_class.gsub!('class="', 'class="bottomborder ') unless css_class.include?('bottomborder')
    end
    if node.at('CellLRuling/text()')
      css_class.gsub!('class="', 'class="leftborder ') unless css_class.include?('leftborder')
    end
  end      
  
  if first != "" && css_class != ""
    first = ""
    css_class.gsub!('class="', 'class="first ')
  end
  if @in_heading
    tables[@current_table_id] << %Q|<CellH id="#{cell_id}"#{first}#{css_class}>|
  else
    tables[@current_table_id] << %Q|<Cell id="#{cell_id}"#{first}#{css_class}>|
  end
  @cell_count += 1
end

#handle_cell_columns(node, tables) ⇒ Object


190
191
192
193
194
195
196
197
# File 'app/models/mif_table_parser.rb', line 190

def handle_cell_columns node, tables
  colspan = node.at('text()').to_s
  cell = tables[@current_table_id].pop
  cell = cell.gsub(">", %Q| colspan="#{colspan}">|)
  tables[@current_table_id] << cell
  @colspan_target = colspan.to_i
  @colspan_count = 1
end

#handle_format(node) ⇒ Object


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

def handle_format node    
  current_tag = clean(node.at('TblTag/text()'))
  
  border_top    = clean(node.at('TblTRuling/text()'))
  border_left   = clean(node.at('TblLRuling/text()'))
  border_bottom = clean(node.at('TblBRuling/text()'))
  border_right  = clean(node.at('TblRRuling/text()'))
  
  hf_separator  = clean(node.at('TblHFRowRuling/text()'))
  col_border    = clean(node.at('TblColumnRuling/text()'))
  row_border    = clean(node.at('TblBodyRowRuling/text()'))
  
  col_x = node.at('TblXColumnNum/text()')
  if node.at('TblXColumnRuling/text()')
    col_x_border = clean(node.at('TblXColumnRuling/text()'))
  end
  
  @format_info.merge!({
    "#{current_tag}" => {
      "border_top" => "#{border_top}", 
      "border_left" => "#{border_left}",
      "border_bottom" => "#{border_bottom}",
      "border_right" => "#{border_right}",
      "hf_separator" => "#{hf_separator}",
      "col_border" => "#{col_border}",
      "row_border" => "#{row_border}",
      "col_x" => "#{col_x}",
      "col_x_border" => "#{col_x_border}"
    }})
end

#handle_id(node) ⇒ Object


51
52
53
# File 'app/models/mif_table_parser.rb', line 51

def handle_id node
  @current_table_id = node.at('text()').to_s
end

#handle_node(node, tables) ⇒ Object


230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'app/models/mif_table_parser.rb', line 230

def handle_node node, tables
  do_break = false

  case node.name
    when 'TblID'
      handle_id node
    when 'TblTag'
      do_break = handle_tag(node, tables)
    when 'Row'
      handle_row node, tables
    when 'Cell'
      handle_cell node, tables
    when 'Attribute'
      handle_attribute node, tables
    when 'CellColumns'
      handle_cell_columns node, tables
    when 'TblH'
      @in_heading = true
    when 'TblBody'
      handle_body tables
    when 'String'
      add_text clean(node), tables
    when 'Char'
      add_text get_char(node), tables
  end

  do_break
end

#handle_row(node, tables) ⇒ Object


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/mif_table_parser.rb', line 80

def handle_row node, tables
  @cell_count = 0
  if @in_row
    if @in_heading
      tables[@current_table_id] << "</CellH></Row>"
    else
      tables[@current_table_id] << "</Cell></Row>"
    end
    @in_row = false
    @in_cell = false
  end
  row_id = node.at('Element/Unique/text()').to_s
  @in_row = true
  
  css_class = ""
  if !@format_info[@table_tag]["hf_separator"].empty? && @in_heading
    css_class = %Q| class="bottomborder"|
  end
  tables[@current_table_id] << %Q|<Row id="#{row_id}"#{css_class}>|
end

#handle_table(table_xml, tables) ⇒ Object


298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'app/models/mif_table_parser.rb', line 298

def handle_table table_xml, tables
  @current_table_id = nil
  @in_heading = false
  @in_row = false
  @in_cell= false
  @colspan_count = 0
  @colspan_target = 0
  @table_tag = ""
  table_count = tables.size
  @no_of_cols = table_xml.at('TblNumColumns/text()').to_s

  table_xml.traverse_element do |node|
    do_break = handle_node node, tables
    break if do_break
  end

  if tables.size > table_count
    if @in_heading
      add_text "</CellH></Row></TableData>", tables
    else
      add_text "</Cell></Row></TableData>", tables
    end
  end

  tables
end

#handle_tag(node, tables) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/mif_table_parser.rb', line 55

def handle_tag node, tables
  tag = clean(node)
  @table_tag = tag
  if tag != 'Table' && tag != 'RepealContinue' && tag != 'RepealsSchedules'
    do_break = true
  else
    css_class = get_table_css_class(node)
    
    xml_tag = start_tag('TableData', node)
          
    unless css_class.empty?
      if xml_tag.include?('class=')
        xml_tag.gsub!('">', %Q| #{css_class}">|)
      else
        xml_tag.gsub!('>', %Q| class="#{css_class}">|)
      end
    end
    
    tables[@current_table_id] = [xml_tag]
    
    do_break = false
  end
  do_break
end