module WikiThat::List

Lexer module for handling List elements @author Bryan T. Meyers

Lexer module for handling List elements @author Bryan T. Meyers

Public Instance Methods

check_item(prefix, depth, value) click to toggle source

Check if this item belongs to the current list @param [String] prefix the prefix of the current list @param [Integer] depth the depth of the current list @param [String] value the value of the current list item @return [Boolean] True if belongs to list

# File lib/wiki-that/parser/elements/list.rb, line 29
def check_item(prefix, depth, value)
  (0..depth).each do |i|
    case value[i]
      when prefix[i]
        # good
      when ':'
        unless prefix[i] == ';'
          return false
        end
      when ';'
        unless prefix[i] == ':'
          return false
        end
      else
        return false
    end
  end
  true
end
lex_list() click to toggle source

Lex the current text as a list

# File lib/wiki-that/lexer/tokens/list.rb, line 30
def lex_list
  buff = ''
  while match? LIST_SPECIAL
    buff += current
    advance
  end
  append Token.new(:list_item, buff)
end
parse_items(curr, depth) click to toggle source

Parse all the items at the current depth @param [String] curr the list item string @param [Integer] depth the current nesting depth

# File lib/wiki-that/parser/elements/list.rb, line 54
def parse_items(curr, depth)
  items = []
  while match? [:list_item] and check_item(curr, depth, current.value)
    case current.value[depth]
      when ';'
        item = Element.new(:dt)
      when ':'
        item = Element.new(:dd)
      else
        item = Element.new(:li)
    end
    if depth < (current.value.length - 1)
      if items.length == 0
        item.add_child(Element.new(:br))
        item.add_child(parse_list2(current.value, depth+1))
        items.push(item)
      else
        items.last.add_child(parse_list2(current.value, depth+1))
      end
    else
      advance
      item.add_children(*parse_inline)
      items.push(item)
    end

    if not end? and current.type == :break
      @line += current.value
      advance
    end
  end
  items
end
parse_list() click to toggle source

Parse the current text as a list

# File lib/wiki-that/parser/elements/list.rb, line 109
def parse_list
  parse_list2(current.value, 0)
end
parse_list2(curr, depth) click to toggle source

Parse a level of a list @param [String] curr the list item string @param [Integer] depth the current nesting depth @return [String] the parsed list

# File lib/wiki-that/parser/elements/list.rb, line 93
def parse_list2(curr, depth)
  case curr[depth]
    when ';', ':'
      list = Element.new(:dl)
    when '#'
      list = Element.new(:ol)
    else
      list = Element.new(:ul)
  end
  list.add_children(*parse_items(curr, depth))
  list
end