Module: Infoboxer::Parser::Table

Includes:
Tree
Included in:
Paragraphs
Defined in:
lib/infoboxer/parser/table.rb

Overview

Instance Method Summary collapse

Instance Method Details

#tableObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/infoboxer/parser/table.rb', line 8

def table
   @context.current =~ /^\s*{\|/ or
    @context.fail!('Something went wrong: trying to parse not a table')

  prms = table_params
  table = Tree::Table.new(Nodes[], prms)

  @context.next!

  loop do
    table_next_line(table) or break
    @context.next!
  end

  # FIXME: not the most elegant way, huh?
  table.children.reject!{|r| r.children.empty?}

  table
end

#table_caption(table) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/infoboxer/parser/table.rb', line 67

def table_caption(table)
  @context.skip(/^\s*\|\+\s*/)

  children = inline(/^\s*([|!]|{\|)/)
  @context.prev! # compensate next! which will be done in table()
  table.push_children(TableCaption.new(children.strip))
end

#table_cell_cont(table) ⇒ Object

Good news, everyone! Table can be IMPLICITLY closed when it's not "cell" context.

Unless it's empty row, which is just skipped.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/infoboxer/parser/table.rb', line 110

def table_cell_cont(table)
  container = case (last = table.children.last)
              when TableRow
                last.children.last
              when TableCaption
                last
              else
                nil
              end

  if !container
    # return "table not continued" unless row is empty
    if @context.current.empty?
      return true
    else
      @context.prev!
      return false
    end
  end
  
  container.push_children(paragraph(/^\s*([|!]|{\|)/))
  table.push_children(container) unless container.parent
  true
end

#table_cells(table, cell_class = TableCell) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/infoboxer/parser/table.rb', line 75

def table_cells(table, cell_class = TableCell)
  table.push_children(TableRow.new()) unless table.children.last.is_a?(TableRow)
  row = table.children.last

  @context.skip(/\s*[!|]\s*/)
  guarded_loop do
    if @context.check(/[^|{|\[]+\|([^\|]|$)/)
      params = parse_params(@context.scan_until(/\|/))
    else
      params = {}
    end
    content = short_inline(/(\|\||!!)/)
    row.push_children(cell_class.new(content, params))
    break if @context.eol?
  end
end

#table_next_line(table) ⇒ Object



33
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
59
60
61
# File 'lib/infoboxer/parser/table.rb', line 33

def table_next_line(table)
  case @context.current
  when /^\s*\|}(.*)$/                 # table end
    @context.scan(/^\s*\|}/)
    return false # should not continue

  when /^\s*!/                        # heading (th) in a row
    table_cells(table, TableHeading)

  when /^\s*\|\+/                     # caption
    table_caption(table)
    
  when /^\s*\|-(.*)$/                 # row start
    table_row(table, $1)

  when /^\s*\|/                       # cell in row
    table_cells(table)

  when /^\s*{{/                       # template can be at row level
    table_template(table)

  when nil
    return false

  else
    return table_cell_cont(table)
  end
  true # should continue parsing
end

#table_paramsObject



28
29
30
31
# File 'lib/infoboxer/parser/table.rb', line 28

def table_params
  @context.skip(/\s*{\|/)
  parse_params(@context.rest)
end

#table_row(table, param_str) ⇒ Object



63
64
65
# File 'lib/infoboxer/parser/table.rb', line 63

def table_row(table, param_str)
  table.push_children(TableRow.new(Nodes[], parse_params(param_str)))
end

#table_template(table) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/infoboxer/parser/table.rb', line 92

def table_template(table)
  contents = paragraph(/^\s*([|!]|{\|)/).to_templates?
  
  if (row = table.children.last).is_a?(TableRow)
    if (cell = row.children.last).is_a?(BaseCell)
      cell.push_children(*contents)
    else
      row.push_children(*contents)
    end
  else
    table.push_children(*contents)
  end
end