module WikiThat::Table
Lexer
module for MediaWiki Tables @author Bryan T. Meyers
Parser
module for MediaWiki Tables @author Bryan T. Meyers
Public Instance Methods
lex_table()
click to toggle source
Lex any of the possible table tokens
# File lib/wiki-that/lexer/tokens/table.rb, line 30 def lex_table case current when '{' advance unless current == '|' rewind lex_text return end advance append Token.new(:table_start) lex_text when '|' advance case current when '}' advance append Token.new(:table_end) lex_text when '+' advance append Token.new(:table_caption) lex_text when '-' advance append Token.new(:table_row) lex_text when '|' advance append Token.new(:table_data, 2) lex_text(%w(| !)) else append Token.new(:table_data, 1) lex_text(%w(| !)) end when '!' advance if current == '!' advance append Token.new(:table_header, 2) else append Token.new(:table_header, 1) end lex_text(%w(| !)) end end
parse_attributes(elem)
click to toggle source
Parse all K-V pairs on this line @param [Element] elem the element to set the attributes on
# File lib/wiki-that/parser/elements/table.rb, line 27 def parse_attributes(elem) if match? [:text] found = false current.value.scan(/(\w+)="([^"]*)"/) do |m| elem.set_attribute(m[0], m[1]) found = true end if found advance end end if match? [:break] @line += current.value advance end elem end
parse_caption(elem)
click to toggle source
Parse a caption if it exists on this line @param [Element] elem the table to add the caption to
# File lib/wiki-that/parser/elements/table.rb, line 49 def parse_caption(elem) if match? [:table_caption] advance if not_match? [:break] p = parse_inline if p.length > 0 caption = Element.new(:caption) caption.add_children(*p) elem.add_child(caption) end end end if match? [:break] @line += current.value advance end elem end
parse_cells(row)
click to toggle source
Parse all the table cells on this row @param [Element] row the row to add cells to
# File lib/wiki-that/parser/elements/table.rb, line 110 def parse_cells(row) first = true while match? [:table_header, :table_data] or (first and not end?) if match? [:table_header] cell = Element.new(:th) else cell = Element.new(:td) end if first if current.value == 2 warning 'First cell on a new line should be "|" or "!" ' end first = false elsif current.value != 2 warning 'Inline cells should be "||" or "!!"' end if match? [:table_header, :table_data] advance end cell = parse_attributes(cell) ## skip next tag since attributes were read if cell.attributes.length > 0 and match? [:table_data, :table_header] advance end contents = parse2(true) if contents.is_a? Array if contents.length > 0 cell.add_children(*contents) else break end else cell.add_child(contents) end advance(-1) if match? [:break] first = true end advance ## Parse multi-line cell until end? or match? [:table_header, :table_data, :table_row, :table_end] if match? [:break] @line += current.value advance first = true redo end curr = @index p = parse2(true) if p.nil? and curr == @index error 'Unable to continue parsing table. Is this actually MediaWiki?' end if p.is_a? Array and p.length == 0 break end cell.add_child(p) end row.add_child(cell) end row end
parse_row(elem)
click to toggle source
Parse a table row @param [Element] elem the table to add the row to
# File lib/wiki-that/parser/elements/table.rb, line 72 def parse_row(elem) case current.type when :table_row advance row = Element.new(:tr) row = parse_attributes(row) if match? [:text] whitespace = true current.value.each_char do |c| unless "\t ".include? c whitespace = false break end end if whitespace advance end end if match? [:break] @line += current.value advance end row = parse_cells(row) elem.add_child(row) when :table_header, :table_data row = Element.new(:tr) row = parse_cells(row) elem.add_child(row) else elem end elem end
parse_table()
click to toggle source
Parse an entire table @return [Element] the parsed tabel
# File lib/wiki-that/parser/elements/table.rb, line 176 def parse_table advance table = Element.new(:table) table = parse_attributes(table) table = parse_caption(table) while match? [:table_row, :table_header, :table_data] table = parse_row(table) end if match? [:table_end] advance else warning 'Could not find end of table, missing "|}"' end table end