Class: Latexmath::Aggregator

Inherits:
Object
  • Object
show all
Defined in:
lib/latexmath/aggregator.rb

Constant Summary collapse

OPERATORS =
'+-*/=[]_^{}()'.freeze
OPENING_BRACES =
'{'.freeze
CLOSING_BRACES =
'}'.freeze
OPENING_BRACKET =
'['.freeze
CLOSING_BRACKET =
']'.freeze
OPENING_PARENTHESIS =
'('.freeze
CLOSING_PARENTHESIS =
')'.freeze
BACKSLASH =
'\\\\'.freeze
AMPERSAND =
'&'.freeze
DASH =
'-'.freeze
SUB_SUP =
'_^'.freeze
SUBSCRIPT =
'_'.freeze
SUPERSCRIPT =
'^'.freeze
LATEX_LEFT =

Added prefix LATEX_ to avoid ruby reserved words

'\\left'.freeze
LATEX_RIGHT =
'\\right'.freeze
LATEX_OVER =
'\\over'.freeze
LATEX_HLINE =
'\\hline'.freeze
LATEX_BEGIN =
'\\begin'.freeze
LATEX_FRAC =
'\\frac'.freeze
LATEX_ROOT =
'\\root'.freeze
LATEX_SQRT =
'\\sqrt'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ Aggregator

Returns a new instance of Aggregator.



30
31
32
# File 'lib/latexmath/aggregator.rb', line 30

def initialize(tokens)
  @tokens = tokens
end

Instance Method Details

#aggregate(tokens = @tokens) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/latexmath/aggregator.rb', line 34

def aggregate(tokens = @tokens)
  aggregated = []

  loop do
    begin
      token = next_item_or_group(tokens)
      raise StopIteration if token.nil?

      if token.is_a?(Array)
        aggregated << token
      elsif token == OPENING_BRACKET
        previous = nil
        previous = aggregated[-1] if aggregated.any?
        begin
          g = group(tokens, opening: OPENING_BRACKET, closing: CLOSING_BRACKET)
          if previous == LATEX_SQRT
            root = tokens.shift
            raise StopIteration if root.nil?

            if root == OPENING_BRACES
              begin
                root = group(tokens)
              rescue EmptyGroupError
                root = ''
              end
            end
            aggregated[-1] = LATEX_ROOT
            aggregated << root
          end
          aggregated << g
        rescue EmptyGroupError
          next if previous == LATEX_SQRT

          aggregated += [OPENING_BRACKET, CLOSING_BRACKET]
        end
      elsif LIMITS.include?(token)
        raise StopIteration if tokens.shift.nil?

        a = next_item_or_group(tokens)
        aggregated += [token, a]
      elsif token == '\\limits'
        previous = aggregated.pop
        raise StopIteration if tokens.shift.nil?

        a = next_item_or_group(tokens)
        raise StopIteration if tokens.shift.nil?

        b = next_item_or_group(tokens)
        aggregated += [token, previous, a, b]
      elsif token && SUB_SUP.include?(token)
        aggregated = process_sub_sup(aggregated, token, tokens)
      elsif token.start_with?(LATEX_BEGIN) || MATRICES.include?(token)
        aggregated += environment(token, tokens)
      elsif token == LATEX_OVER
        numerator = aggregated
        aggregated = []
        aggregated << LATEX_FRAC
        aggregated << numerator
        aggregated << aggregate(tokens)
      else
        aggregated << token
      end
    rescue EmptyGroupError
      aggregated += [OPENING_BRACES, CLOSING_BRACES]
      next
    rescue StopIteration
      aggregated << token unless token.nil?
      break
    end
  end

  aggregated
end

#environment(token, tokens) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
# File 'lib/latexmath/aggregator.rb', line 108

def environment(token, tokens)
  env = if token.start_with?(LATEX_BEGIN)
          token[7..-2]
        else
          token[1..token.size]
  end

  alignment = nil
  content = []
  row = []
  has_rowline = false

  loop do
    begin
      token = next_item_or_group(tokens)
      raise StopIteration if token.nil?

      if token.is_a? Array
        begin
          if env == 'array' && token.all? { |x| 'lcr|'.include?(x) }
            alignment = token
          else
            row << process_row(token)
          end
        rescue TypeError
          row << token
        end
      elsif token == "\\end{#{env}}"
        break
      elsif token == AMPERSAND
        row << token
      elsif token == BACKSLASH
        row = group_columns(row) if row.include?(AMPERSAND)
        row.insert(0, LATEX_HLINE) if has_rowline
        content << row
        row = []
        has_rowline = false
      elsif token == LATEX_HLINE
        has_rowline = true
      elsif token == OPENING_BRACKET && content.empty?
        begin
          alignment = group(tokens, opening: OPENING_BRACKET, closing: CLOSING_BRACKET)
        rescue EmptyGroupError
          next
        end
      elsif token == DASH
        next_token = tokens.shift
        raise StopIteration if next_token.nil?

        row << if next_token == "\\end{#{env}}"
                token
              else
                [token, next_token]
              end
      elsif SUB_SUP.include?(token)
        row = process_sub_sup(row, token, tokens)
      elsif token.start_with?(LATEX_BEGIN)
        row += environment(token, tokens)
      else
        row << token
      end
    rescue EmptyGroupError
      row << []
      next
    rescue StopIteration
      break
    end
  end

  if row.any?
    row = group_columns(row) if row.include?(AMPERSAND)
    row.insert(0, LATEX_HLINE) if has_rowline
    content << row
  end

  content = content.pop while content.size == 1 && content.first.is_a?(Array)

  return ["\\#{env}", alignment.join, content] if alignment

  ["\\#{env}", content]
end

#find_opening_parenthesis(tokens) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/latexmath/aggregator.rb', line 268

def find_opening_parenthesis(tokens)
  closing = 0

  tokens.map.with_index { |x, i| [i, x] }.reverse.each do |index, token|
    if token == CLOSING_PARENTHESIS
      closing += 1
    elsif token == OPENING_PARENTHESIS
      return index if closing == 0

      closing -= 1
    end
  end
  raise ExtraLeftOrMissingRight
end

#group(tokens, opening: OPENING_BRACES, closing: CLOSING_BRACES, delimiter: nil) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
233
234
235
236
237
238
239
240
241
242
# File 'lib/latexmath/aggregator.rb', line 190

def group(tokens, opening: OPENING_BRACES, closing: CLOSING_BRACES, delimiter: nil)
  g = []

  if delimiter
    g << delimiter
    g << tokens.shift
  end

  loop do
    begin
      token = tokens.shift
      raise StopIteration if token.nil?

      if token == closing && delimiter.nil?
        break if g.any?

        raise EmptyGroupError
      elsif token == opening
        begin
          g << group(tokens)
        rescue EmptyGroupError
          g += [[]]
        end
      elsif token == LATEX_LEFT
        g << group(tokens, delimiter: token)
      elsif token == LATEX_RIGHT
        g << token
        _token = tokens.shift
        raise StopIteration if _token.nil?

        g << _token
        break
      else
        g << token
      end
    rescue StopIteration
      break
    end
  end

  if delimiter
    right = g.index(LATEX_RIGHT)
    raise ExtraLeftOrMissingRight if right.nil?

    content = g[2..right - 1]
    g_ = g
    g_ = g[0..1] + [aggregate(content)] + g[right..g.size] if content.any?

    return g_
  end

  aggregate(g)
end

#group_columns(row) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/latexmath/aggregator.rb', line 244

def group_columns(row)
  grouped = [[]]
  row.each do |item|
    if item == AMPERSAND
      grouped << []
    else
      grouped[-1] << item
    end
  end

  grouped.map { |item| item.size > 1 ? item : item.pop }
end

#next_item_or_group(tokens) ⇒ Object

Raises:



257
258
259
260
261
262
263
264
265
266
# File 'lib/latexmath/aggregator.rb', line 257

def next_item_or_group(tokens)
  token = tokens.shift
  raise StopIteration if token.nil?

  return group(tokens) if token == OPENING_BRACES

  return group(tokens, delimiter: token) if token == LATEX_LEFT

  token
end

#process_row(tokens) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/latexmath/aggregator.rb', line 283

def process_row(tokens)
  row = []
  content = []

  tokens.each do |token|
    if token == AMPERSAND
      next
    elsif token == BACKSLASH
      content << row if row.any?
      row = []
    else
      row << token
    end
  end

  content << row if row.any?

  content = content.pop while content.size == 1 && content.first.is_a?(Array)

  content
end

#process_sub_sup(aggregated, token, tokens) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/latexmath/aggregator.rb', line 305

def process_sub_sup(aggregated, token, tokens)
  begin
    previous = aggregated.pop
    raise IndexError if previous.nil?

    if previous.is_a?(String) && OPERATORS.include?(previous)
      if (previous == CLOSING_PARENTHESIS) && aggregated.include?(OPENING_PARENTHESIS)
        index = find_opening_parenthesis(aggregated)
        aggregated = aggregated[0, index] + [token] + [aggregated[index..aggregated.size] + [previous]]
      else
        aggregated += [previous, token]
      end
      return aggregated
    end

    begin
      next_token = next_item_or_group(tokens)
      if aggregated.size >= 2
        if aggregated[-2] == SUBSCRIPT && token == SUPERSCRIPT
          aggregated[-2] = SUB_SUP
          aggregated += [previous, next_token]
        elsif (aggregated[-2] == SUPERSCRIPT) && (token == SUBSCRIPT)
          aggregated[-2] = SUB_SUP
          aggregated += [next_token, previous]
        else
          aggregated += [token, previous, next_token]
        end
      else
        aggregated += [token, previous, next_token]
      end
    rescue EmptyGroupError
      aggregated += [token, previous, []]
    end
  rescue IndexError
    next_token = next_item_or_group(tokens)
    aggregated += [token, '', next_token]
  end

  aggregated
end