# class MathML::Util::SimpleLaTeX

### Constants

DEFAULT

### Public Class Methods

decode(src, data) click to toggle source

        # File lib/math_ml/util.rb
    332 def self.decode(src, data)
    333   @@default_latex.decode(src, data)
    334 end

encode(src) click to toggle source

        # File lib/math_ml/util.rb
    327 def self.encode(src)
    328   @@default_latex ||= new
    329   @@default_latex.encode(src)
    330 end

new(options = {}) click to toggle source

        # File lib/math_ml/util.rb
     94 def initialize(options = {})
     95   @params = DEFAULT.merge(options)
     96   @params[:parser] = MathML::LaTeX::Parser.new unless @params[:parser] || @params[:without_parse]
     97
     98   @params[:math_envs] = collect_regexp(@params[:math_env_list])
     99   @params[:dmath_envs] = collect_regexp(@params[:dmath_env_list])
    100   @params[:escapes] = collect_regexp(@params[:escape_list])
    101   @params[:throughs] = collect_regexp(@params[:through_list])
    102   reset_encode_proc
    103   reset_rescue_proc
    104   reset_decode_proc
    105   reset_unencode_proc
    106 end

### Public Instance Methods

decode(encoded, data, without_parsed = false, &proc) click to toggle source

        # File lib/math_ml/util.rb
    239 def decode(encoded, data, without_parsed = false, &proc)
    240   return nil if encoded.nil?
    241
    242   proc ||= @decode_proc
    243   encoded.gsub(/#{Regexp.escape(@params[:delimiter])}([demu])(\d+)#{Regexp.escape(@params[:delimiter])}/) do
    244     i = $2.to_i
    245     t, d, s =
    246       case $1
    247       when 'd'
    248         [:dmath, without_parsed ? escapeXML(data.dsrc_list[i], true) : data.dmath_list[i], data.dsrc_list[i]]
    249       when 'e'
    250         [:escape, data.escape_list[i], data.esrc_list[i]]
    251       when 'm'
    252         [:math, without_parsed ? escapeXML(data.msrc_list[i], true) : data.math_list[i], data.msrc_list[i]]
    253       when 'u'
    254         [:user, data.user_list[i], data.usrc_list[i]]
    255       end
    256     if proc
    257       proc.call(d, type: t, index: i, src: s) || d
    258     else
    259       d
    260     end
    261   end
    262 end

decode_partial(type, encoded, data, &proc) click to toggle source

        # File lib/math_ml/util.rb
    264 def decode_partial(type, encoded, data, &proc)
    265   return nil if encoded.nil?
    266
    267   head =
    268     case type
    269     when :math
    270       'm'
    271     when :dmath
    272       'd'
    273     when :escape
    274       'e'
    275     when :user
    276       'u'
    277     else
    278       return
    279     end
    280   encoded.gsub(/#{Regexp.escape(@params[:delimiter])}#{head}(\d+)#{Regexp.escape(@params[:delimiter])}/) do
    281     i = $1.to_i
    282     t, d, s =
    283       case head
    284       when 'd'
    285         [:dmath, data.dmath_list[i], data.dsrc_list[i]]
    286       when 'e'
    287         [:escape, data.escape_list[i], data.esrc_list[i]]
    288       when 'm'
    289         [:math, data.math_list[i], data.msrc_list[i]]
    290       when 'u'
    291         [:user, data.user_list[i], data.usrc_list[i]]
    292       end
    293     if proc
    294       proc.call(d, type: t, index: i, src: s) || "#{@params[:delimiter]}#{head}#{i}#{@params[:delimiter]}"
    295     else
    296       d
    297     end
    298   end
    299 end

encode(src, *proc_re, &proc) click to toggle source

        # File lib/math_ml/util.rb
    142 def encode(src, *proc_re, &proc)
    143   data = if proc_re.size > 0 && proc_re[0].is_a?(MathData)
    144            proc_re.shift
    145          else
    146            MathData.new
    147          end
    148
    149   proc_re = proc_re.size == 0 ? @encode_proc_re : collect_regexp(proc_re)
    150   proc ||= @encode_proc
    151
    152   s = StringScanner.new(src)
    153   encoded = ''
    154
    155   until s.eos?
    156     if s.scan(/
    157       (.*?)
    158       (((((#{@params[:throughs]})|
    159       #{@params[:dmath_envs]})|
    160       #{@params[:math_envs]})|
    161       #{proc_re})|
    162       #{@params[:escapes]})
    163     /mx)
    164       encoded << s[1]
    165       if s[6]
    166         encoded << s[6]
    167       elsif s[5] || s[4]
    168         env_src = s[5] || s[4]
    169         if @params[:dmath_envs] =~ env_src
    170           encoded << "#{@params[:delimiter]}d#{data.dsrc_list.size}#{@params[:delimiter]}"
    171           data.dsrc_list << env_src
    172         else
    173           encoded << "#{@params[:delimiter]}m#{data.msrc_list.size}#{@params[:delimiter]}"
    174           data.msrc_list << env_src
    175         end
    176       elsif s[3]
    177         size = s[3].size
    178         s.pos = left = s.pos - size
    179         if r = proc.call(s)
    180           right = s.pos
    181           encoded << "#{@params[:delimiter]}u#{data.user_list.size}#{@params[:delimiter]}"
    182           data.user_list << r
    183           data.usrc_list << s.string[left...right]
    184         else
    185           encoded << s.peek(size)
    186           s.pos = s.pos + size
    187         end
    188       elsif s[2]
    189         encoded << "#{@params[:delimiter]}e#{data.escape_list.size}#{@params[:delimiter]}"
    190         @params[:escapes] =~ s[2]
    191         data.esrc_list << s[2]
    192         data.escape_list << escapeXML($+, true)
    193       end
    194     else
    195       encoded << s.rest
    196       s.terminate
    197     end
    198   end
    199
    200   parse(data, @params[:parser]) unless @params[:without_parse]
    201
    202   [encoded, data]
    203 end

error_to_html(e) click to toggle source

        # File lib/math_ml/util.rb
    205 def error_to_html(e)
    206   "<br />\n#{escapeXML(e.message)}<br />\n<code>#{escapeXML(e.done).gsub(/\n/, "<br />\n")}" \
    207     "<strong>#{escapeXML(e.rest).gsub(/\n/, "<br />\n")}</strong></code><br />"
    208 end

latex_parser() click to toggle source

        # File lib/math_ml/util.rb
    210 def latex_parser
    211   @params[:parser] = MathML::LaTeX::Parser.new unless @params[:parser]
    212   @params[:parser]
    213 end

parse(data, parser = nil) click to toggle source

        # File lib/math_ml/util.rb
    215 def parse(data, parser = nil)
    216   parser ||= latex_parser
    217   (data.math_list.size...data.msrc_list.size).each do |i|
    218     @params[:math_envs] =~ data.msrc_list[i]
    219     data.math_list[i] = parser.parse($+)
    220   rescue MathML::LaTeX::ParseError => e
    221     data.math_list[i] = if @rescue_proc
    222                           @rescue_proc.call(e)
    223                         else
    224                           error_to_html(e)
    225                         end
    226   end
    227   (data.dmath_list.size...data.dsrc_list.size).each do |i|
    228     @params[:dmath_envs] =~ data.dsrc_list[i]
    229     data.dmath_list[i] = parser.parse($+, true)
    230   rescue MathML::LaTeX::ParseError => e
    231     data.dmath_list[i] = if @rescue_proc
    232                            @rescue_proc.call(e)
    233                          else
    234                            error_to_html(e)
    235                          end
    236   end
    237 end

parse_eqnarray(src, parser = nil) click to toggle source

        # File lib/math_ml/util.rb
    336 def parse_eqnarray(src, parser = nil)
    337   src = "\\begin{array}{ccc}#{src}\\end{array}"
    338   parser ||= latex_parser
    339   begin
    340     parser.parse(src, true)
    341   rescue MathML::LaTeX::ParseError => e
    342     e = MathML::LaTeX::ParseError.new(
    343       e.message,
    344       e.rest.sub(/\\end\{array\}\z/, '\end{eqnarray}'),
    345       e.done.sub(/\A\\begin\{array\}\{ccc\}/, '\begin{eqnarray}')
    346     )
    347     @rescue_proc ? @rescue_proc.call(e) : error_to_html(e)
    348   end
    349 end

parse_single_command(src, parser = nil) click to toggle source

        # File lib/math_ml/util.rb
    351 def parse_single_command(src, parser = nil)
    352   s = src[SINGLE_COMMAND_RE, 1]
    353   parser ||= latex_parser
    354   begin
    355     parser.parse(s)
    356   rescue MathML::LaTeX::ParseError => e
    357     src[SINGLE_COMMAND_RE, 2]
    358   end
    359 end

reset_decode_proc() click to toggle source

        # File lib/math_ml/util.rb
    126 def reset_decode_proc
    127   @decode_proc = nil
    128 end

reset_encode_proc() click to toggle source

        # File lib/math_ml/util.rb
    108 def reset_encode_proc
    109   @encode_proc_re = INVALID_RE
    110   @encode_proc = nil
    111 end

reset_rescue_proc() click to toggle source

        # File lib/math_ml/util.rb
    118 def reset_rescue_proc
    119   @rescue_proc = nil
    120 end

reset_unencode_proc() click to toggle source

        # File lib/math_ml/util.rb
    138 def reset_unencode_proc
    139   @unencode_proc = nil
    140 end

set_decode_proc(&proc) click to toggle source

        # File lib/math_ml/util.rb
    130 def set_decode_proc(&proc)
    131   @decode_proc = proc
    132 end

set_encode_proc(*re, &proc) click to toggle source

        # File lib/math_ml/util.rb
    113 def set_encode_proc(*re, &proc)
    114   @encode_proc_re = collect_regexp(re)
    115   @encode_proc = proc
    116 end

set_rescue_proc(&proc) click to toggle source

        # File lib/math_ml/util.rb
    122 def set_rescue_proc(&proc)
    123   @rescue_proc = proc
    124 end

set_unencode_proc(&proc) click to toggle source

        # File lib/math_ml/util.rb
    134 def set_unencode_proc(&proc)
    135   @unencode_proc = proc
    136 end

unencode(encoded, data, without_escape = false, &proc) click to toggle source

        # File lib/math_ml/util.rb
    301 def unencode(encoded, data, without_escape = false, &proc)
    302   return nil if encoded.nil?
    303
    304   proc ||= @unencode_proc
    305   encoded.gsub(/#{Regexp.escape(@params[:delimiter])}([demu])(\d+)#{Regexp.escape(@params[:delimiter])}/) do
    306     i = $2.to_i
    307     t, s =
    308       case $1
    309       when 'd'
    310         [:dmath, data.dsrc_list[i]]
    311       when 'e'
    312         [:escape, data.esrc_list[i]]
    313       when 'm'
    314         [:math, data.msrc_list[i]]
    315       when 'u'
    316         [:user, data.usrc_list[i]]
    317       end
    318     s = escapeXML(s, true) unless without_escape
    319     if proc
    320       proc.call(s, type: t, index: i) || s
    321     else
    322       s
    323     end
    324   end
    325 end