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:



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
1021
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
# File 'lib/math_ml/latex.rb', line 973

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 = []
  vlines = []
  vlined = l.check(/\|/)
  columned = false
  until l.eos?
    c = l.scan_any
    unless c =~ /[clr|@]/
      raise ParseError.new(
        'Syntax error.',
        layout[/\A.*(#{Regexp.escape(c + l.rest)}.*\z)/m, 1]
      )
    end

    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, 'Need parameter here.' if layout == ''

  hlines = []
  row_parsed = false
  hlined = false
  until @scanner.peek_command == 'end'
    raise ParseError, '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, 'Environment mismatched.' unless @scanner.check_block && @scanner[1] == 'array'

  @scanner.scan_block
  t
end

#env_array_row(layout) ⇒ Object

Raises:



1053
1054
1055
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
# File 'lib/math_ml/latex.rb', line 1053

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, 'Too many column.' if @scanner.check(/&/)

  r
end

#env_matrixObject

Raises:



1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
# File 'lib/math_ml/latex.rb', line 1089

def env_matrix
  t = Table.new
  hlines = []
  hlined = false
  row_parsed = false
  until @scanner.peek_command == 'end'
    raise ParseError, '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, 'Need \\end{array}.' unless @scanner.peek_command == 'end'

  @scanner.scan_command
  raise ParseError, 'Environment mismatched.' unless @scanner.check_block && @scanner[1] == 'matrix'

  @scanner.scan_block
  t
end

#env_matrix_rowObject



1132
1133
1134
1135
1136
1137
1138
1139
1140
# File 'lib/math_ml/latex.rb', line 1132

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



967
968
969
970
971
# File 'lib/math_ml/latex.rb', line 967

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

  super
end