module WikiThat::Formatting

Lexer module for inline formatting @author Bryan T. Meyers

Lexer module for inline formatting @author Bryan T. Meyers

Public Instance Methods

lex_formatting() click to toggle source

Lex the current text as inline formatting

# File lib/wiki-that/lexer/tokens/formatting.rb, line 30
def lex_formatting
  #Read opening marks
  count = 0
  while match? FORMAT_SPECIAL
    count += 1
    advance
  end

  if count > 1
    Token.new(:format, count)
  else
    # Actual format character
    rewind
    nil
  end
end
parse_format() click to toggle source

Parse the current text as inline formatting

# File lib/wiki-that/parser/elements/formatting.rb, line 25
def parse_format
  results = []
  start   = current
  advance
  contents = []
  finish   = nil
  until end?
    case current.type
      when :format
        finish = current
        advance
        break
      when :link_start
        contents.push(parse_link)
      when :text
        contents.push(Element.new(:text, current.value))
        advance
      else
        break
    end
  end
  if finish.nil?
    element = Element.new(:text, '')
    (0...start.value).each do
      element.value += "'"
    end
    results.push element
    results.push(*contents)
    return results
  end
  depth = finish.value
  if start.value != finish.value
    warning "Unbalanced inline formatting"
  end
  if start.value < finish.value
    depth = start.value
  end
  case depth
    when 2
      element = Element.new(:i)
      element.add_children(*contents)
      results.push(element)
    when 3
      element = Element.new(:b)
      element.add_children(*contents)
      results.push(element)
    else
      e1 = Element.new(:b)
      e1.add_children(*contents)
      e2 = Element.new(:i)
      e2.add_child(e1)
      results.push(e2)
  end
  results
end