Module: MathML::LaTeX::BuiltinEnvironments

Included in:
Parser
Defined in:
lib/math_ml/latex.rb,
lib/math_ml/latex.rb

Instance Method Summary collapse

Instance Method Details

#env_arrayObject

Raises:



949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
# File 'lib/math_ml/latex.rb', line 949

def env_array
	layout = @scanner.scan_block ? @scanner.matched : @scanner.scan(/./)
	l = Scanner.new(layout=~RE::BLOCK ? layout[RE::BLOCK, 1] : layout)
	t = Table.new
	aligns = Array.new
	vlines = Array.new
	vlined = l.check(/\|/)
	columned = false
	until l.eos?
		c = l.scan_any
		raise ParseError.new("Syntax error.", layout[/\A.*(#{Regexp.escape(c+l.rest)}.*\z)/m, 1]) unless c=~/[clr\|@]/

		if c=='|'
			aligns << Align::CENTER if vlined
			vlines << Line::SOLID
			vlined = true
			columned = false
		else
			vlines << Line::NONE if columned
			vlined = false
			columned = true
			case c
			when 'l'
				aligns << Align::LEFT
			when 'c'
				aligns << Align::CENTER
			when 'r'
				aligns << Align::RIGHT
			when '@'
				aligns << Align::CENTER
				l.scan_any
			end
		end
	end
	t.aligns = aligns
	t.vlines = vlines

	layout = layout[RE::BLOCK, 1] if layout=~RE::BLOCK
	raise ParseError.new('Need parameter here.') if layout==""

	hlines = Array.new
	row_parsed = false
	hlined = false
	until @scanner.peek_command=="end"
		raise ParseError.new('Matching \end not exist.') if @scanner.eos?
		if @scanner.peek_command=="hline"
			@scanner.scan_command
			t << Tr.new unless row_parsed
			hlines << Line::SOLID
			row_parsed = false
			hlined = true
		else
			hlines << Line::NONE if row_parsed
			t << env_array_row(l.string)
			@scanner.scan(RE::WBSLASH)
			row_parsed = true
			hlined = false
		end
	end
	t.hlines = hlines

	if hlined
		tr = Tr.new
		(0..vlines.size).each {|i| tr << Td.new}
		t << tr
	end

	@scanner.scan_command
	raise ParseError.new("Environment mismatched.") unless @scanner.check_block && @scanner[1]=="array"
	@scanner.scan_block
	t
end

#env_array_row(layout) ⇒ Object

Raises:



1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
# File 'lib/math_ml/latex.rb', line 1022

def env_array_row(layout)
	l = Scanner.new(layout)
	r = Tr.new
	first_column = true
	vlined = l.check(/\|/)
	until l.eos?
		c = l.scan(/./)
		if c=='|'
			r << Td.new if vlined
			vlined = true
			next
		else
			vlined = false
			case c
			when 'r', 'l', 'c'
			when '@'
				r << parse_into(l.scan_any, Td.new)
				next
			end
			if first_column
				first_column = false
			else
				raise ParseError.new("Need more column.", @scanner.matched.to_s) unless @scanner.scan(/&/)
			end
			r << push_container(Td.new) do |td|
				td << parse_to_element(true) until @scanner.peek_command=="end" || @scanner.check(/(&|\\\\)/) || @scanner.eos?
			end
		end
	end
	r << Td.new if vlined
	raise ParseError.new("Too many column.") if @scanner.check(/&/)
	r
end

#env_matrixObject

Raises:



1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
# File 'lib/math_ml/latex.rb', line 1056

def env_matrix
	t = Table.new
	hlines = Array.new
	hlined = false
	row_parsed = false
	until @scanner.peek_command=="end"
		raise ParseError.new('Matching \end not exist.') if @scanner.eos?
		if @scanner.peek_command=="hline"
			@scanner.scan_command
			t << Tr.new unless row_parsed
			hlines << Line::SOLID
			row_parsed = false
			hlined = true
		else
			hlines << Line::NONE if row_parsed
			t << (r = Tr.new)
			r << (td=Td.new)
			until @scanner.check(RE::WBSLASH) || @scanner.peek_command=="end" || @scanner.eos?
				push_container(td) do |e|
					e << parse_to_element(true) until @scanner.peek_command=="end" || @scanner.check(/(&|\\\\)/) || @scanner.eos?
				end
				r << (td=Td.new) if @scanner.scan(/&/)
			end
			@scanner.scan(RE::WBSLASH)
			row_parsed = true
			hlined = false
		end
	end
	t.hlines = hlines

	t << Tr.new if hlined

	raise ParseError.new("Need \\end{array}.") unless @scanner.peek_command=="end"
	@scanner.scan_command
	raise ParseError.new("Environment mismatched.") unless @scanner.check_block && @scanner[1]=="matrix"
	@scanner.scan_block
	t
end

#env_matrix_rowObject



1095
1096
1097
1098
1099
1100
1101
1102
# File 'lib/math_ml/latex.rb', line 1095

def env_matrix_row
	r = Tr.new
	until @scanner.check(RE::WBSLASH) || @scanner.peek_command=="end"
		r << push_container(Td.new) do |td|
			td << parse_to_element(true) until @scanner.peek_command=="end" || @scanner.check(/(&|\\\\)/) || @scanner.eos?
		end
	end
end

#initializeObject



943
944
945
946
947
# File 'lib/math_ml/latex.rb', line 943

def initialize
	add_environment("array", "matrix")

	super
end