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.
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.
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
# File lib/ritex/parser.rb, line 1181 def _reduce_none(val, _values, result) val[0] end
Delete all macros
# File lib/ritex.rb, line 74 def flush_macros; @macros = {}; end
# File lib/ritex.rb, line 68 def format= format raise ArgumentError, "format must be one of #{FORMATS * ', '}" unless FORMATS.include? format @format = format end
# 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
# 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 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