class Ritex::Parser

The parser for itex and the main entry point for Ritex. This class is partially defined here and partially generated by Racc from lib/parser.y.

Create the parser with new. Parse strings with parse. That’s all there is to it.

Constants

FORMATS
Racc_arg
Racc_debug_parser
Racc_token_to_s_table

Attributes

format[R]
merror[RW]

If true, Ritex will output a <merror>…</merror> message in the MathML if an unknown entity is encountered. If false (the default), Ritex will throw a Ritex::Error.

Public Class Methods

new(format = :mathml) click to toggle source

format is the desired output format and must be in the FORMATS list. Right now that's just :mathml.

# File lib/ritex.rb, line 39
def initialize format = :mathml
  self.format = format
  @macros = {}
  @merror = false
end

Public Instance Methods

_reduce_none(val, _values, result) click to toggle source
# File lib/ritex/parser.rb, line 1181
def _reduce_none(val, _values, result)
  val[0]
end
flush_macros() click to toggle source

Delete all macros

# File lib/ritex.rb, line 74
def flush_macros; @macros = {}; end
format=(format) click to toggle source
# File lib/ritex.rb, line 68
def format= format
  raise ArgumentError, "format must be one of #{FORMATS * ', '}" unless FORMATS.include? format
  @format = format
end
handle_mathml_markup(what, tag, opts) click to toggle source
# File lib/ritex.rb, line 83
def handle_mathml_markup what, tag, opts
  tag, opts = case tag
    when String
      [tag, opts]
    when Symbol
      a, b = MathML::MARKUP[tag]
      [a, [b, opts].flatten.compact.join(" ")]
    end
  unless opts.empty?
    "<#{tag} #{opts}>#{what}</#{tag}>"
  else
    "<#{tag}>#{what}</#{tag}>"
  end
end
op(o, opts=[]) click to toggle source
# File lib/ritex.rb, line 128
def op o, opts=[]
  case @format
  when :mathml; markup(token(o), "mo", opts)
  when :raw; o
  end
end
parse(s, opts={}) click to toggle source

Parse a string. Returns the MathML output in string form. Note that macro definitions are cumulative and persistent across calls to parse. If you don’t want this behavior, you must explicitly call flush_macros after every parse call.

opts is a hash of options:

nowrap, if true, will omit wrapping the output in a top-level XML math tag. Only useful if you're generating these tags yourself.

display, if true, emits display markup, as opposed to inline markup. For mathml output this only has an effect if nowrap is true.

# File lib/ritex.rb, line 57
def parse s, opts={}
  nowrap = opts[:nowrap]
  display = opts[:display]
  @lex = Lexer.new self, s
  r = yyparse @lex, :lex
  r = markup r, (display ? :displaymath : :math) unless nowrap
  r = raw_blob_to_string(r) if @format == :raw
  r
end