module WikiThat::Header
Lexer
module for handling headers @author Bryan T. Meyers
Lexer
module for handling headers @author Bryan T. Meyers
Public Instance Methods
lex_header()
click to toggle source
Lex the current line as a header
# File lib/wiki-that/lexer/tokens/header.rb, line 30 def lex_header #Read start sequence count = 0 while match? HEADER_SPECIAL count += 1 advance end if count < 2 # Plain old equals rewind lex_text return else append Token.new(:header_start, count) end #Read inner content lex_text(HEADER_SPECIAL) #closing tag count = 0 while match? HEADER_SPECIAL count += 1 advance end case count when 0 # Didn't find a header close when 1 # Just an Equals rewind else append Token.new(:header_end, count) end #trailing text lex_text end
parse_header()
click to toggle source
Parse the current line as a header
# File lib/wiki-that/parser/elements/header.rb, line 26 def parse_header result = [] start = current advance content = parse_inline(:header_end) if not_match? [:header_end] buff = '' (0...start.value).each do buff += '=' end result.push(Element.new(:text, buff)) content.each do |c| result.push(c) end warning 'Incomplete Header Parsed' return result end finish = current advance post = parse_inline fail = false if post.length == 1 if post[0].type == :text post[0].value.each_char do |c| unless " \t".include? c fail = true break end end else fail = true end elsif post.length > 1 fail = true end if fail buff = '' (0...start.value).each do buff += '=' end result.push(Element.new(:text, buff)) content.each do |c| result.push(c) end buff = '' (0...finish.value).each do buff += '=' end result.push(Element.new(:text, buff)) post.each do |c| result.push(c) end error 'Only trailing whitespace characters are allowed on the same line as a header' return result end depth = finish.value if start.value != finish.value warning 'Unbalanced header tags found' end if finish.value > start.value depth = start.value end header = Element.new("h#{depth}".to_sym) header.set_attribute(:id, content.first.value.strip.gsub(' ','_')) header.add_children(*content) header end