Class: Mathemagical::Util::SimpleLaTeX

Inherits:
Object
  • Object
show all
Includes:
Mathemagical::Util
Defined in:
lib/mathemagical/util.rb

Constant Summary collapse

DEFAULT =
{
	:delimiter=>"\001",
	:math_env_list=>[
		/\$((?:\\.|[^\\\$])#{Mathemagical::LaTeX::MBEC}*?)\$/m,
		/\\\((#{Mathemagical::LaTeX::MBEC}*?)\\\)/m
	],
	:dmath_env_list=>[
		/\$\$(#{Mathemagical::LaTeX::MBEC}*?)\$\$/m,
		/\\\[(#{Mathemagical::LaTeX::MBEC}*?)\\\]/m
	],
	:escape_list=>[
		/\\(.)/m
	],
	:through_list=>[
	],
	:escape_any=> false,
	:without_parse=>false
}
@@default_latex =
nil

Constants included from Mathemagical::Util

EQNARRAY_RE, ESCAPES, INVALID_RE, SINGLE_COMMAND_RE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mathemagical::Util

collect_regexp, #collect_regexp, #escapeXML, escapeXML

Constructor Details

#initialize(options = {}) ⇒ SimpleLaTeX

Returns a new instance of SimpleLaTeX.



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mathemagical/util.rb', line 92

def initialize(options = {})
	@params = DEFAULT.merge(options)
	@params[:parser] = Mathemagical::LaTeX::Parser.new unless @params[:parser] || @params[:without_parse]

	@params[:math_envs] = collect_regexp(@params[:math_env_list])
	@params[:dmath_envs] = collect_regexp(@params[:dmath_env_list])
	@params[:escapes] = collect_regexp(@params[:escape_list])
	@params[:throughs] = collect_regexp(@params[:through_list])
	reset_encode_proc
	reset_rescue_proc
	reset_decode_proc
	reset_unencode_proc
end

Class Method Details

.decode(src, data) ⇒ Object



324
325
326
# File 'lib/mathemagical/util.rb', line 324

def self.decode(src, data)
	@@default_latex.decode(src, data)
end

.encode(src) ⇒ Object



319
320
321
322
# File 'lib/mathemagical/util.rb', line 319

def self.encode(src)
	@@default_latex = self.new unless @@default_latex
	@@default_latex.encode(src)
end

Instance Method Details

#decode(encoded, data, without_parsed = false, &proc) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/mathemagical/util.rb', line 234

def decode(encoded, data, without_parsed = false, &proc)
	return nil if encoded==nil
	proc = @decode_proc unless proc
	encoded.gsub(/#{Regexp.escape(@params[:delimiter])}([demu])(\d+)#{Regexp.escape(@params[:delimiter])}/) do
		i = $2.to_i
		t, d, s =
			case $1
			when "d"
				[:dmath, without_parsed ? escapeXML(data.dsrc_list[i], true) : data.dmath_list[i], data.dsrc_list[i]]
			when "e"
				[:escape, data.escape_list[i], data.esrc_list[i]]
			when "m"
				[:math, without_parsed ? escapeXML(data.msrc_list[i], true) : data.math_list[i], data.msrc_list[i]]
			when "u"
				[:user, data.user_list[i], data.usrc_list[i]]
			end
		if proc
			proc.call(d, :type=>t, :index=>i, :src=>s) || d
		else
			d
		end
	end
end

#decode_partial(type, encoded, data, &proc) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/mathemagical/util.rb', line 258

def decode_partial(type, encoded, data, &proc)
	return nil if encoded==nil
	head =
		case type
		when :math
			"m"
		when :dmath
			"d"
		when :escape
			"e"
		when :user
			"u"
		else
			return
		end
	encoded.gsub(/#{Regexp.escape(@params[:delimiter])}#{head}(\d+)#{Regexp.escape(@params[:delimiter])}/) do
		i = $1.to_i
		t, d, s =
			case head
			when "d"
				[:dmath, data.dmath_list[i], data.dsrc_list[i]]
			when "e"
				[:escape, data.escape_list[i], data.esrc_list[i]]
			when "m"
				[:math, data.math_list[i], data.msrc_list[i]]
			when "u"
				[:user, data.user_list[i], data.usrc_list[i]]
			end
		if proc
			proc.call(d, :type=>t, :index=>i, :src=>s) || "#{@params[:delimiter]}#{head}#{i}#{@params[:delimiter]}"
		else
			d
		end
	end
end

#encode(src, *proc_re, &proc) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/mathemagical/util.rb', line 140

def encode(src, *proc_re, &proc)
	if proc_re.size>0 && proc_re[0].is_a?(MathData)
		data = proc_re.shift
	else
		data = MathData.new
	end

	proc_re = proc_re.size==0 ? @encode_proc_re : collect_regexp(proc_re)
	proc = @encode_proc unless proc

	s = StringScanner.new(src)
	encoded = ""

	until s.eos?
		if s.scan(/(.*?)(((((#{@params[:throughs]})|#{@params[:dmath_envs]})|#{@params[:math_envs]})|#{proc_re})|#{@params[:escapes]})/m)
			encoded << s[1]
			case
			when s[6]
				encoded << s[6]
			when s[5], s[4]
				env_src = s[5] || s[4]
				if @params[:dmath_envs]=~env_src
					encoded << "#{@params[:delimiter]}d#{data.dsrc_list.size}#{@params[:delimiter]}"
					data.dsrc_list << env_src
				else
					encoded << "#{@params[:delimiter]}m#{data.msrc_list.size}#{@params[:delimiter]}"
					data.msrc_list << env_src
				end
			when s[3]
				size = s[3].size
				s.pos = left = s.pos-size
				if r=proc.call(s)
					right = s.pos
					encoded << "#{@params[:delimiter]}u#{data.user_list.size}#{@params[:delimiter]}"
					data.user_list << r
					data.usrc_list << s.string[left...right]
				else
					encoded << s.peek(size)
					s.pos = s.pos+size
				end
			when s[2]
				encoded << "#{@params[:delimiter]}e#{data.escape_list.size}#{@params[:delimiter]}"
				@params[:escapes]=~s[2]
				data.esrc_list << s[2]
				data.escape_list << escapeXML($+, true)
			end
		else
			encoded << s.rest
			s.terminate
		end
	end

	parse(data, @params[:parser]) unless @params[:without_parse]

	return encoded, data
end

#error_to_html(e) ⇒ Object



197
198
199
# File 'lib/mathemagical/util.rb', line 197

def error_to_html(e)
	"<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 />"
end

#latex_parserObject



201
202
203
204
# File 'lib/mathemagical/util.rb', line 201

def latex_parser
	@params[:parser] = Mathemagical::LaTeX::Parser.new unless @params[:parser]
	@params[:parser]
end

#parse(data, parser = nil) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/mathemagical/util.rb', line 206

def parse(data, parser=nil)
	parser = latex_parser unless parser
	(data.math_list.size...data.msrc_list.size).each do |i|
		begin
			@params[:math_envs]=~data.msrc_list[i]
			data.math_list[i] = parser.parse($+)
		rescue Mathemagical::LaTeX::ParseError => e
			if @rescue_proc
				data.math_list[i] = @rescue_proc.call(e)
			else
				data.math_list[i] = error_to_html(e)
			end
		end
	end
	(data.dmath_list.size...data.dsrc_list.size).each do |i|
		begin
			@params[:dmath_envs]=~data.dsrc_list[i]
			data.dmath_list[i] = parser.parse($+, true)
		rescue Mathemagical::LaTeX::ParseError => e
			if @rescue_proc
				data.dmath_list[i] = @rescue_proc.call(e)
			else
				data.dmath_list[i] = error_to_html(e)
			end
		end
	end
end

#parse_eqnarray(src, parser = nil) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/mathemagical/util.rb', line 328

def parse_eqnarray(src, parser=nil)
	src = "\\begin{array}{ccc}#{src}\\end{array}"
	parser = latex_parser unless parser
	begin
		parser.parse(src, true)
	rescue Mathemagical::LaTeX::ParseError => e
		e = Mathemagical::LaTeX::ParseError.new(e.message,
			e.rest.sub(/\\end\{array\}\z/, '\end{eqnarray}'),
			e.done.sub(/\A\\begin\{array\}\{ccc\}/, '\begin{eqnarray}'))
		@rescue_proc ? @rescue_proc.call(e) : error_to_html(e)
	end
end

#parse_single_command(src, parser = nil) ⇒ Object



341
342
343
344
345
346
347
348
349
# File 'lib/mathemagical/util.rb', line 341

def parse_single_command(src, parser=nil)
	s = src[SINGLE_COMMAND_RE, 1]
	parser = latex_parser unless parser
	begin
		parser.parse(s)
	rescue Mathemagical::LaTeX::ParseError => e
		src[SINGLE_COMMAND_RE, 2]
	end
end

#reset_decode_procObject



124
125
126
# File 'lib/mathemagical/util.rb', line 124

def reset_decode_proc
	@decode_proc = nil
end

#reset_encode_procObject



106
107
108
109
# File 'lib/mathemagical/util.rb', line 106

def reset_encode_proc
	@encode_proc_re = INVALID_RE
	@encode_proc = nil
end

#reset_rescue_procObject



116
117
118
# File 'lib/mathemagical/util.rb', line 116

def reset_rescue_proc
	@rescue_proc = nil
end

#reset_unencode_procObject



136
137
138
# File 'lib/mathemagical/util.rb', line 136

def reset_unencode_proc
	@unencode_proc = nil
end

#set_decode_proc(&proc) ⇒ Object



128
129
130
# File 'lib/mathemagical/util.rb', line 128

def set_decode_proc(&proc)
	@decode_proc = proc
end

#set_encode_proc(*re, &proc) ⇒ Object



111
112
113
114
# File 'lib/mathemagical/util.rb', line 111

def set_encode_proc(*re, &proc)
	@encode_proc_re = collect_regexp(re)
	@encode_proc = proc
end

#set_rescue_proc(&proc) ⇒ Object



120
121
122
# File 'lib/mathemagical/util.rb', line 120

def set_rescue_proc(&proc)
	@rescue_proc = proc
end

#set_unencode_proc(&proc) ⇒ Object



132
133
134
# File 'lib/mathemagical/util.rb', line 132

def set_unencode_proc(&proc)
	@unencode_proc = proc
end

#unencode(encoded, data, without_escape = false, &proc) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/mathemagical/util.rb', line 294

def unencode(encoded, data, without_escape=false, &proc)
	return nil if encoded==nil
	proc = @unencode_proc unless proc
	encoded.gsub(/#{Regexp.escape(@params[:delimiter])}([demu])(\d+)#{Regexp.escape(@params[:delimiter])}/) do
		i = $2.to_i
		t, s =
			case $1
			when "d"
				[:dmath, data.dsrc_list[i]]
			when "e"
				[:escape, data.esrc_list[i]]
			when "m"
				[:math, data.msrc_list[i]]
			when "u"
				[:user, data.usrc_list[i]]
			end
		s = escapeXML(s, true) unless without_escape
		if proc
			proc.call(s, :type=>t, :index=>i) || s
		else
			s
		end
	end
end