class Qwik::TextParser

Public Class Methods

inline(str) click to toggle source
# File vendor/qwik/lib/qwik/parser.rb, line 121
def self.inline(str)
  return str if str.is_a? Array

  tokens = InlineTokenizer.tokenize(str)
  tree = InlineParser.make_tree(tokens)
  return tree
end
make_tree(tokens) click to toggle source

Make a tree from tokens.

# File vendor/qwik/lib/qwik/parser.rb, line 16
def self.make_tree(tokens)
  ar = []

  tokens.each {|token|
    tag = token.first
    case tag

    when :h2, :h3, :h4, :h5, :h6
      ar << [tag, *inline(token[1])]

    when :ul, :ol
      lu = ar
      token[1].times {
        if lu.last && lu.last.is_a?(Array) && lu.last.first == tag
          lu = lu.last
        else
          a = [tag]
          lu << a
          lu = a
        end
      }
      lu << [:li, *inline(token[2])]

    when :pre, :blockquote
      if ar.last && ar.last.first == tag
        ar.last[1] << "\n"+token[1]
      else
        ar << [tag, token[1]]
      end

    when :dl
      a = []
      a << [:dt, *inline(token[1])] if token[1]
      a << [:dd, *inline(token[2])] if token[2]
      if ar.last && ar.last.first == tag
        ar.last << a[0]
        ar.last << a[1] if a[1]
      else
        ar << [tag, *a]
      end

    when :table
      token.shift
      table_ar = token.map {|td|
        inline_ar = inline(td)
        inline_ar << '' if inline_ar.empty?
        [:td] + inline_ar
      }
      tr = [:tr] + table_ar
      if ar.last && ar.last.first == tag
        ar.last << tr
      else
        ar << [tag, tr]
      end

    when :text
      line_ar = inline(token[1])
      if ar.last && ar.last.is_a?(Array) && ar.last.first == :p
        ar[-1] << "\n" if ar.last.last != [:br]
        ar[-1] += line_ar
      else
        ar << [:p, *line_ar]
      end

    when :empty
      ar << ["\n"]

    when :plugin
      ar << parse_plugin(token)

    when :html
      ar << parse_html(token)

    else
      ar << token
    end
  }

  nar = []
  ar.each {|block|
    tag = block.first
    case tag
    when :blockquote
      str = block[1]

      tokens = TextTokenizer.tokenize(str)
      tree = make_tree(tokens)             # Recursive.
      nar << [tag, *tree]

    when :p
      while block[1] == [:br]
        block.delete_at(1)
      end
      while block.last == [:br]
        block.pop
      end
      nar << block
    else
      nar << block
    end
  }

  return nar
end
parse_html(token) click to toggle source
# File vendor/qwik/lib/qwik/parser.rb, line 137
def self.parse_html(token)
  str = token[1]
  wabisabi = HtmlToWabisabi.parse(str)

  v = WabisabiValidator.valid?(wabisabi)
  if v == true
    return [:html, *wabisabi]
  else
    return [:div, {:class=>'error'}, "can not use [#{v}]"]
  end
end
parse_plugin(token) click to toggle source
# File vendor/qwik/lib/qwik/parser.rb, line 129
def self.parse_plugin(token)
  method = token[1].to_s
  param = token[2].to_s
  plugin = [:plugin, {:method=>method, :param=>param}]
  plugin << token[3].to_s if token[3]
  return plugin
end