Class: ODDB::HtmlTableHandler

Inherits:
Object
  • Object
show all
Defined in:
src/util/html_parser.rb

Defined Under Namespace

Classes: Cell, Row

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (HtmlTableHandler) initialize(attributes)

A new instance of HtmlTableHandler



283
284
285
286
287
# File 'src/util/html_parser.rb', line 283

def initialize(attributes)
  @attributes = attributes
  @rows = []
  @rowspans = []
end

Instance Attribute Details

- (Object) attributes (readonly)

Returns the value of attribute attributes



282
283
284
# File 'src/util/html_parser.rb', line 282

def attributes
  @attributes
end

Instance Method Details

- (Object) add_child(child)



288
289
290
# File 'src/util/html_parser.rb', line 288

def add_child(child)
  @current_row.add_child(child)
end

- (Object) cdata(x, y)



291
292
293
294
295
# File 'src/util/html_parser.rb', line 291

def cdata(x, y)
  if(@rows.size > y)
    @rows.at(y).cdata(x)
  end
end

- (Object) children(x, y)



296
297
298
299
300
# File 'src/util/html_parser.rb', line 296

def children(x, y)
  if(@rows.size > y)
    @rows.at(y).children(x)
  end
end

- (Object) current_colspan



301
302
303
# File 'src/util/html_parser.rb', line 301

def current_colspan
  @current_row.current_colspan
end

- (Object) each_row(&block)



304
305
306
# File 'src/util/html_parser.rb', line 304

def each_row(&block)
  @rows.each(&block)
end

- (Object) extract_cdata(template)



307
308
309
310
311
312
313
# File 'src/util/html_parser.rb', line 307

def extract_cdata(template)
  template.inject({}) { |inj, pair|
    key, pos = pair
    inj.store(key, cdata(*pos))
    inj
  }
end

- (Object) next_cell(attr, keep = false)



317
318
319
320
321
322
323
324
325
326
# File 'src/util/html_parser.rb', line 317

def next_cell(attr, keep=false)
  row = @current_row || next_row({})
  @rows.each_with_index { |rw, idy|
    test = @rows.size - idy
    while((cl = rw.cells[row.cells.size]) && (cl.rowspan >= test))
      row.next_cell({}, keep)
    end
  }
  row.next_cell(attr, keep)
end

- (Object) next_line



314
315
316
# File 'src/util/html_parser.rb', line 314

def next_line
  @current_row.next_line
end

- (Object) next_row(attr = {})



327
328
329
330
331
# File 'src/util/html_parser.rb', line 327

def next_row(attr={})
  @current_row = Row.new(attr)
  @rows.push(@current_row)
  @current_row
end

- (Object) send_cdata(data) Also known as: <<



332
333
334
# File 'src/util/html_parser.rb', line 332

def send_cdata(data)
  (@current_row || next_row).send_cdata(data.gsub(/\s/u, ' '))
end

- (Object) to_s



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'src/util/html_parser.rb', line 336

def to_s
  if(@rows.empty?)
    return ''
  end
  hline = "-" * width
  lines = [ hline ]
  rowspans = []
  @rows.each_with_index { |row, rdx|
    hline = "-" * width
    row.height.times { |idy|
      cells = []
      colspan = 1
      #row_w = 1
      @column_widths.each_with_index { |pad, idx|
        if(colspan > 1)
          colspan -= 1
          next
        end
        cdata = ''
        #formatted_cdata = ''
        if(cell = row.cells[idx])
          rowspan = cell.rowspan
          #str = " " * pad
          if(idy == 0)
            if(rowspan > 1)
              rowspans[idx] = rowspan
            end
            if(rowspans[idx].to_i > 1)
              rowspans[idx] -= 1
              #hline[row_w - 1, str.length+4] = '  ' << str << '  '
            end
          end
          colspan = cell.colspan
          cdata = cell._cdata(false)[idy].to_s.strip
          if(colspan > 1)
            total_w = 0
            colspan.times { |offset|
              total_w += @column_widths[offset + idx]
            }
            #total_w += (colspan - 1) * 3
            total_w += (colspan - 1) * 2
            pad = total_w - cdata.size 
          else
            pad -= cdata.size
          end
        end

        padded = cdata << (" " * pad)
        #row_w += padded.length + 3
        cells.push(padded)
      }
      #lines.push("| " << cells.join(' | ') << " |")
      lines.push(cells.join('  '))
    }
    #lines.push(hline)
  }
  lines.push(hline)
  lines.join("\n") << "\n"
end

- (Object) width



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'src/util/html_parser.rb', line 395

def width
  @column_widths = []
  @rows.each { |row|
    row.each_cell_with_index { |cell, idx|
      oldval = @column_widths[idx].to_i
      newval = cell.width 
      @column_widths[idx] = [oldval, newval].max
    }
  }
  width = @column_widths.inject { |wdt, inj|
    wdt + inj 
  }
  #width + (@column_widths.size * 3) + 1
  width + ((@column_widths.size - 1) * 2)
end