Class: Latexmath::Tokenizer

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

Instance Method Summary collapse

Instance Method Details

#fetch_tokenObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
# File 'lib/latexmath/tokenizer.rb', line 20

def fetch_token
  skip(/\s+/)
  skip(/\$\$/)

  token = if scan(/\\mathbb\{[^}]+}/)
            if Latexmath::Symbol.get(matched)
              "&#x#{Latexmath::Symbol.get(matched)};"
            else
              matched
            end
          elsif scan(/{\\bf [^}]+}/)
            matches = matched.match(/{\\bf ([^}]+)}/)
            symbol = Latexmath::Symbol.get("\\mathbf{#{matches[1]}}")
            "&#x#{symbol};" if symbol
          elsif scan(/\\\\\[\d+mm\]/)
            '\\\\' # matched
          elsif scan(/\\mbox{[^\}]+}/)
            matched
          elsif scan(/\\Delta/)
            matched
          elsif scan(/\\vec{[^\}]+}/)
            matched
          elsif scan(/\\hat{[^\}]+}/)
            matched
          elsif scan(/\\textrm{[^\}]+}/)
            matched
          elsif scan(/{\\rm [^}]+}/)
            matches = matched.match(/{\\rm ([^}]+)}/)
            matches[1]
          elsif scan(/\\[a-z]+\{(bmatrix|Bmatrix|matrix|vmatrix|Vmatrix|array|pmatrix|split)\*?\}/)
            matched
          elsif scan(/\\[a-z]+/)
            matched
          elsif scan(/\\\\/)
            matched
          elsif scan(/\\\[/)
            matched
          elsif scan(/\\\]/)
            matched
          elsif scan(/\\\{/)
            matched
          elsif scan(/\\[:;,]/)
            matched
          elsif scan(/\\/)
            matched
          elsif scan(/[a-z]/)
            matched
          elsif scan(/[0-9]+\.[0-9]+/)
            matched
          elsif scan(/[0-9]+/)
            matched
          elsif scan(/ /)
            matched
          elsif scan(/[\{\}\*]/)
            matched
          elsif scan(/./)
            matched
          end

  token
end

#tokenizeObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/latexmath/tokenizer.rb', line 5

def tokenize
  exp = []

  while token = fetch_token
    exp << token
  end

  if exp.first == '\[' && exp.last == '\]'
    exp.pop
    exp.shift
  end

  exp
end