Class: Mathemagical::LaTeX::Parser

Inherits:
Object
  • Object
show all
Includes:
Mathemagical::LaTeX, BuiltinCommands, BuiltinEnvironments, BuiltinGroups
Defined in:
lib/mathemagical/latex.rb

Defined Under Namespace

Classes: CircularReferenceCommand

Constant Summary collapse

BUILTIN_MACRO =
<<'EOS'
\newenvironment{smallmatrix}{\begin{matrix}}{\end{matrix}}
\newenvironment{pmatrix}{\left(\begin{matrix}}{\end{matrix}\right)}
\newenvironment{bmatrix}{\left[\begin{matrix}}{\end{matrix}\right]}
\newenvironment{Bmatrix}{\left\{\begin{matrix}}{\end{matrix}\right\}}
\newenvironment{vmatrix}{\left|\begin{matrix}}{\end{matrix}\right|}
\newenvironment{Vmatrix}{\left\|\begin{matrix}}{\end{matrix}\right\|}
EOS

Constants included from BuiltinCommands

BuiltinCommands::OVERS, BuiltinCommands::UNDERS

Constants included from Mathemagical::LaTeX

MBEC

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BuiltinCommands

#cmd_backslash, #cmd_entity, #cmd_frac, #cmd_hat_etc, #cmd_it_etc, #cmd_mathit_etc, #cmd_mbox, #cmd_quad_etc, #cmd_sqrt, #cmd_stackrel, #cmd_underbrace_etc

Methods included from BuiltinGroups

#add_environment, #grp_begin, #grp_left_etc

Methods included from BuiltinEnvironments

#env_array, #env_array_row, #env_equation, #env_matrix, #env_matrix_row

Constructor Details

#initialize(opt = {}) ⇒ Parser

Returns a new instance of Parser.



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/mathemagical/latex.rb', line 365

def initialize(opt={})
	@unsecure_entity = false
	@entities = Hash.new
	@commands = Hash.new
	@symbols = Hash.new
	@delimiters = Array.new
	@group_begins = Hash.new
	@group_ends = Hash.new
	@macro = Macro.new
	@macro.parse(BUILTIN_MACRO)
	@expanded_command = Array.new
	@expanded_environment = Array.new
	@symbol_table = opt[:symbol] || Mathemagical::Symbol::Default
	@symbol_table = Mathemagical::Symbol::MAP[@symbol_table] if @symbol_table.is_a?(::Symbol)

	super()
end

Instance Attribute Details

#macroObject (readonly)

Returns the value of attribute macro.



362
363
364
# File 'lib/mathemagical/latex.rb', line 362

def macro
  @macro
end

#symbol_tableObject (readonly)

Returns the value of attribute symbol_table.



363
364
365
# File 'lib/mathemagical/latex.rb', line 363

def symbol_table
  @symbol_table
end

#unsecure_entityObject

Returns the value of attribute unsecure_entity.



361
362
363
# File 'lib/mathemagical/latex.rb', line 361

def unsecure_entity
  @unsecure_entity
end

Instance Method Details

#add_commands(*a) ⇒ Object



415
416
417
418
419
420
421
# File 'lib/mathemagical/latex.rb', line 415

def add_commands(*a)
	if a.size==1 && Hash===a[0]
		@commands.merge!(a[0])
	else
		a.each{|i| @commands[i] = false}
	end
end

#add_delimiter(list) ⇒ Object



431
432
433
# File 'lib/mathemagical/latex.rb', line 431

def add_delimiter(list)
	@delimiters.concat(list)
end

#add_entity(list) ⇒ Object



383
384
385
386
387
# File 'lib/mathemagical/latex.rb', line 383

def add_entity(list)
	list.each do |i|
		@entities[i] = true
	end
end

#add_group(begin_name, end_name, method = nil) ⇒ Object



435
436
437
438
# File 'lib/mathemagical/latex.rb', line 435

def add_group(begin_name, end_name, method=nil)
	@group_begins[begin_name] = method
	@group_ends[end_name] = begin_name
end

#add_multi_command(m, *a) ⇒ Object



423
424
425
# File 'lib/mathemagical/latex.rb', line 423

def add_multi_command(m, *a)
	a.each{|i| @commands[i] = m}
end

#add_plugin(plugin) ⇒ Object



411
412
413
# File 'lib/mathemagical/latex.rb', line 411

def add_plugin(plugin)
	self.extend(plugin)
end

#add_sym_cmd(hash) ⇒ Object



427
428
429
# File 'lib/mathemagical/latex.rb', line 427

def add_sym_cmd(hash)
	@symbols.merge!(hash)
end

#parse(src, displaystyle = false) ⇒ Object



389
390
391
392
393
394
395
396
397
398
# File 'lib/mathemagical/latex.rb', line 389

def parse(src, displaystyle=false)
	@ds = displaystyle
	@math = Math.new(@ds)
	begin
		parse_into(src, @math, Font::NORMAL)
	rescue ParseError => e
		e.done = src[0...(src.size - e.rest.size)]
		raise
	end
end

#push_container(container, scanner = @scanner, font = @font) ⇒ Object



400
401
402
403
404
405
406
407
408
409
# File 'lib/mathemagical/latex.rb', line 400

def push_container(container, scanner=@scanner, font=@font)
	data = [@container, @scanner, @font]
	@container, @scanner, @font = [container, scanner, font]
	begin
		yield container
		container
	ensure
		@container, @scanner, @font = data
	end
end