Class: Infoboxer::Tree::Table

Inherits:
Compound show all
Defined in:
lib/infoboxer/tree/table.rb

Overview

Represents table. Tables are complicated!

Instance Attribute Summary

Attributes inherited from Compound

#children

Attributes inherited from Node

#params, #parent

Instance Method Summary collapse

Methods inherited from Compound

#index_of, #initialize, #to_tree

Methods inherited from Node

#==, #children, coder, def_readers, #index, #initialize, #inspect, #next_siblings, #prev_siblings, #siblings, #text_, #to_s, #to_tree

Methods included from Navigation::Sections::Node

#in_sections

Methods included from Navigation::Shortcuts::Node

#bold?, #categories, #external_links, #heading?, #headings, #images, #infobox, #infoboxes, #italic?, #lists, #paragraphs, #tables, #templates, #wikilinks

Methods included from Navigation::Lookup::Node

#_lookup, #_lookup_children, #_lookup_next_siblings, #_lookup_parents, #_lookup_prev_siblings, #_lookup_siblings, #_matches?, #has_parent?, #lookup, #lookup_children, #lookup_next_siblings, #lookup_parents, #lookup_prev_siblings, #lookup_siblings, #matches?

Constructor Details

This class inherits a constructor from Infoboxer::Tree::Compound

Instance Method Details

#body_rowsObject

For now, returns all table rows except #heading_row



33
34
35
36
37
# File 'lib/infoboxer/tree/table.rb', line 33

def body_rows
  rows.first && rows.first.children.all?(&call(matches?: TableHeading)) ?
    rows[1..-1] :
    rows
end

#captionObject

Table caption, if exists.



19
20
21
# File 'lib/infoboxer/tree/table.rb', line 19

def caption
  children.detect(&fltr(itself: TableCaption))
end

#empty?Boolean

Internal, used by Parser

Returns:

  • (Boolean)


9
10
11
# File 'lib/infoboxer/tree/table.rb', line 9

def empty?
  false
end

#heading_rowObject

For now, returns first table row, if it consists only of Infoboxer::Tree::TableHeadings.

FIXME: it can easily be several table heading rows



27
28
29
30
# File 'lib/infoboxer/tree/table.rb', line 27

def heading_row
  rows.first && rows.first.children.all?(&call(matches?: TableHeading)) ?
    rows.first : nil
end

#rowsObject

All table rows.



14
15
16
# File 'lib/infoboxer/tree/table.rb', line 14

def rows
  children.select(&fltr(itself: TableRow))
end

#textObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/infoboxer/tree/table.rb', line 39

def text
  table = Terminal::Table.new
  if caption
    table.title = caption.text.sub(/\n+\Z/, '')
  end
  
  if heading_row
    table.headings = heading_row.children.map(&:text).
      map(&call(sub: [/\n+\Z/, '']))
  end

  table.rows = body_rows.map{|r|
    r.children.map(&:text).
      map(&call(sub: [/\n+\Z/, '']))
  }
  table.to_s + "\n\n"
end