class MathML::Util::SimpleLaTeX

Constants

DEFAULT

Public Class Methods

decode(src, data) click to toggle source
    # File lib/math_ml/util.rb
323 def self.decode(src, data)
324         @@default_latex.decode(src, data)
325 end
encode(src) click to toggle source
    # File lib/math_ml/util.rb
318 def self.encode(src)
319         @@default_latex = self.new unless @@default_latex
320         @@default_latex.encode(src)
321 end
new(options = {}) click to toggle source
    # File lib/math_ml/util.rb
 91 def initialize(options = {})
 92         @params = DEFAULT.merge(options)
 93         @params[:parser] = MathML::LaTeX::Parser.new unless @params[:parser] || @params[:without_parse]
 94 
 95         @params[:math_envs] = collect_regexp(@params[:math_env_list])
 96         @params[:dmath_envs] = collect_regexp(@params[:dmath_env_list])
 97         @params[:escapes] = collect_regexp(@params[:escape_list])
 98         @params[:throughs] = collect_regexp(@params[:through_list])
 99         reset_encode_proc
100         reset_rescue_proc
101         reset_decode_proc
102         reset_unencode_proc
103 end

Public Instance Methods

decode(encoded, data, without_parsed = false, &proc) click to toggle source
    # File lib/math_ml/util.rb
233 def decode(encoded, data, without_parsed = false, &proc)
234         return nil if encoded==nil
235         proc = @decode_proc unless proc
236         encoded.gsub(/#{Regexp.escape(@params[:delimiter])}([demu])(\d+)#{Regexp.escape(@params[:delimiter])}/) do
237                 i = $2.to_i
238                 t, d, s =
239                         case $1
240                         when "d"
241                                 [:dmath, without_parsed ? escapeXML(data.dsrc_list[i], true) : data.dmath_list[i], data.dsrc_list[i]]
242                         when "e"
243                                 [:escape, data.escape_list[i], data.esrc_list[i]]
244                         when "m"
245                                 [:math, without_parsed ? escapeXML(data.msrc_list[i], true) : data.math_list[i], data.msrc_list[i]]
246                         when "u"
247                                 [:user, data.user_list[i], data.usrc_list[i]]
248                         end
249                 if proc
250                         proc.call(d, :type=>t, :index=>i, :src=>s) || d
251                 else
252                         d
253                 end
254         end
255 end
decode_partial(type, encoded, data, &proc) click to toggle source
    # File lib/math_ml/util.rb
257 def decode_partial(type, encoded, data, &proc)
258         return nil if encoded==nil
259         head =
260                 case type
261                 when :math
262                         "m"
263                 when :dmath
264                         "d"
265                 when :escape
266                         "e"
267                 when :user
268                         "u"
269                 else
270                         return
271                 end
272         encoded.gsub(/#{Regexp.escape(@params[:delimiter])}#{head}(\d+)#{Regexp.escape(@params[:delimiter])}/) do
273                 i = $1.to_i
274                 t, d, s =
275                         case head
276                         when "d"
277                                 [:dmath, data.dmath_list[i], data.dsrc_list[i]]
278                         when "e"
279                                 [:escape, data.escape_list[i], data.esrc_list[i]]
280                         when "m"
281                                 [:math, data.math_list[i], data.msrc_list[i]]
282                         when "u"
283                                 [:user, data.user_list[i], data.usrc_list[i]]
284                         end
285                 if proc
286                         proc.call(d, :type=>t, :index=>i, :src=>s) || "#{@params[:delimiter]}#{head}#{i}#{@params[:delimiter]}"
287                 else
288                         d
289                 end
290         end
291 end
encode(src, *proc_re, &proc) click to toggle source
    # File lib/math_ml/util.rb
139 def encode(src, *proc_re, &proc)
140         if proc_re.size>0 && proc_re[0].is_a?(MathData)
141                 data = proc_re.shift
142         else
143                 data = MathData.new
144         end
145 
146         proc_re = proc_re.size==0 ? @encode_proc_re : collect_regexp(proc_re)
147         proc = @encode_proc unless proc
148 
149         s = StringScanner.new(src)
150         encoded = ""
151 
152         until s.eos?
153                 if s.scan(/(.*?)(((((#{@params[:throughs]})|#{@params[:dmath_envs]})|#{@params[:math_envs]})|#{proc_re})|#{@params[:escapes]})/m)
154                         encoded << s[1]
155                         case
156                         when s[6]
157                                 encoded << s[6]
158                         when s[5], s[4]
159                                 env_src = s[5] || s[4]
160                                 if @params[:dmath_envs]=~env_src
161                                         encoded << "#{@params[:delimiter]}d#{data.dsrc_list.size}#{@params[:delimiter]}"
162                                         data.dsrc_list << env_src
163                                 else
164                                         encoded << "#{@params[:delimiter]}m#{data.msrc_list.size}#{@params[:delimiter]}"
165                                         data.msrc_list << env_src
166                                 end
167                         when s[3]
168                                 size = s[3].size
169                                 s.pos = left = s.pos-size
170                                 if r=proc.call(s)
171                                         right = s.pos
172                                         encoded << "#{@params[:delimiter]}u#{data.user_list.size}#{@params[:delimiter]}"
173                                         data.user_list << r
174                                         data.usrc_list << s.string[left...right]
175                                 else
176                                         encoded << s.peek(size)
177                                         s.pos = s.pos+size
178                                 end
179                         when s[2]
180                                 encoded << "#{@params[:delimiter]}e#{data.escape_list.size}#{@params[:delimiter]}"
181                                 @params[:escapes]=~s[2]
182                                 data.esrc_list << s[2]
183                                 data.escape_list << escapeXML($+, true)
184                         end
185                 else
186                         encoded << s.rest
187                         s.terminate
188                 end
189         end
190 
191         parse(data, @params[:parser]) unless @params[:without_parse]
192 
193         return encoded, data
194 end
error_to_html(e) click to toggle source
    # File lib/math_ml/util.rb
196 def error_to_html(e)
197         "<br />\n#{escapeXML(e.message)}<br />\n<code>#{escapeXML(e.done).gsub(/\n/, "<br />\n")}<strong>#{escapeXML(e.rest).gsub(/\n/, "<br />\n")}</strong></code><br />"
198 end
latex_parser() click to toggle source
    # File lib/math_ml/util.rb
200 def latex_parser
201         @params[:parser] = MathML::LaTeX::Parser.new unless @params[:parser]
202         @params[:parser]
203 end
parse(data, parser=nil) click to toggle source
    # File lib/math_ml/util.rb
205 def parse(data, parser=nil)
206         parser = latex_parser unless parser
207         (data.math_list.size...data.msrc_list.size).each do |i|
208                 begin
209                         @params[:math_envs]=~data.msrc_list[i]
210                         data.math_list[i] = parser.parse($+)
211                 rescue MathML::LaTeX::ParseError => e
212                         if @rescue_proc
213                                 data.math_list[i] = @rescue_proc.call(e)
214                         else
215                                 data.math_list[i] = error_to_html(e)
216                         end
217                 end
218         end
219         (data.dmath_list.size...data.dsrc_list.size).each do |i|
220                 begin
221                         @params[:dmath_envs]=~data.dsrc_list[i]
222                         data.dmath_list[i] = parser.parse($+, true)
223                 rescue MathML::LaTeX::ParseError => e
224                         if @rescue_proc
225                                 data.dmath_list[i] = @rescue_proc.call(e)
226                         else
227                                 data.dmath_list[i] = error_to_html(e)
228                         end
229                 end
230         end
231 end
parse_eqnarray(src, parser=nil) click to toggle source
    # File lib/math_ml/util.rb
327 def parse_eqnarray(src, parser=nil)
328         src = "\\begin{array}{ccc}#{src}\\end{array}"
329         parser = latex_parser unless parser
330         begin
331                 parser.parse(src, true)
332         rescue MathML::LaTeX::ParseError => e
333                 e = MathML::LaTeX::ParseError.new(e.message,
334                         e.rest.sub(/\\end\{array\}\z/, '\end{eqnarray}'),
335                         e.done.sub(/\A\\begin\{array\}\{ccc\}/, '\begin{eqnarray}'))
336                 @rescue_proc ? @rescue_proc.call(e) : error_to_html(e)
337         end
338 end
parse_single_command(src, parser=nil) click to toggle source
    # File lib/math_ml/util.rb
340 def parse_single_command(src, parser=nil)
341         s = src[SINGLE_COMMAND_RE, 1]
342         parser = latex_parser unless parser
343         begin
344                 parser.parse(s)
345         rescue MathML::LaTeX::ParseError => e
346                 src[SINGLE_COMMAND_RE, 2]
347         end
348 end
reset_decode_proc() click to toggle source
    # File lib/math_ml/util.rb
123 def reset_decode_proc
124         @decode_proc = nil
125 end
reset_encode_proc() click to toggle source
    # File lib/math_ml/util.rb
105 def reset_encode_proc
106         @encode_proc_re = INVALID_RE
107         @encode_proc = nil
108 end
reset_rescue_proc() click to toggle source
    # File lib/math_ml/util.rb
115 def reset_rescue_proc
116         @rescue_proc = nil
117 end
reset_unencode_proc() click to toggle source
    # File lib/math_ml/util.rb
135 def reset_unencode_proc
136         @unencode_proc = nil
137 end
set_decode_proc(&proc) click to toggle source
    # File lib/math_ml/util.rb
127 def set_decode_proc(&proc)
128         @decode_proc = proc
129 end
set_encode_proc(*re, &proc) click to toggle source
    # File lib/math_ml/util.rb
110 def set_encode_proc(*re, &proc)
111         @encode_proc_re = collect_regexp(re)
112         @encode_proc = proc
113 end
set_rescue_proc(&proc) click to toggle source
    # File lib/math_ml/util.rb
119 def set_rescue_proc(&proc)
120         @rescue_proc = proc
121 end
set_unencode_proc(&proc) click to toggle source
    # File lib/math_ml/util.rb
131 def set_unencode_proc(&proc)
132         @unencode_proc = proc
133 end
unencode(encoded, data, without_escape=false, &proc) click to toggle source
    # File lib/math_ml/util.rb
293 def unencode(encoded, data, without_escape=false, &proc)
294         return nil if encoded==nil
295         proc = @unencode_proc unless proc
296         encoded.gsub(/#{Regexp.escape(@params[:delimiter])}([demu])(\d+)#{Regexp.escape(@params[:delimiter])}/) do
297                 i = $2.to_i
298                 t, s =
299                         case $1
300                         when "d"
301                                 [:dmath, data.dsrc_list[i]]
302                         when "e"
303                                 [:escape, data.esrc_list[i]]
304                         when "m"
305                                 [:math, data.msrc_list[i]]
306                         when "u"
307                                 [:user, data.usrc_list[i]]
308                         end
309                 s = escapeXML(s, true) unless without_escape
310                 if proc
311                         proc.call(s, :type=>t, :index=>i) || s
312                 else
313                         s
314                 end
315         end
316 end