class Riseup::Parser

Public Class Methods

new(unformated, spec = @@default_spec) click to toggle source
# File lib/riseup/parser.rb, line 23
def initialize(unformated, spec = @@default_spec)
  unless unformated.is_a?(String)
    raise ArgumentError, 'Unformated text (first argument) must be a String'
  end

  unless spec.is_a?(Spec)
    raise ArgumentError, 'Markup specification (second argument) must be a Riseup::Spec'
  end
  @unformated = unformated
  @spec = spec
  tokens
end

Public Instance Methods

parse() click to toggle source
# File lib/riseup/parser.rb, line 40
def parse
  tokens if @tokens.nil?

  token_toggle = Set.new
  new_html = []
  @tokens.each do |t|
    if @spec.include?(t)
      if token_toggle.include?(t)
        token_toggle.delete(t)
        new_html.append(@spec[t].finish) unless @spec[t].substitute?
      else
        if !@spec[t].substitute?
          token_toggle.add(t)
          new_html.append(@spec[t].start)
        else
          new_html.append(@spec[t].substitute)
        end
      end
    else
      new_html.append(t)
    end
  end

  # Fix unterminated tokens
  token_toggle.reverse_each do |token|
    new_html.append(@spec[token].finish) unless @spec[token].substitute?
  end
  new_html.join
end
to_s() click to toggle source
# File lib/riseup/parser.rb, line 70
def to_s
  @tokens.to_s
end
tokens() click to toggle source
# File lib/riseup/parser.rb, line 36
def tokens
  @tokens = @unformated.split(@spec.regex).reject(&:empty?)
end