latex-decode 0.4.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +40 -7
- data/lib/latex/decode/accents.rb +4 -14
- data/lib/latex/decode/base.rb +8 -11
- data/lib/latex/decode/compatibility.rb +4 -82
- data/lib/latex/decode/diacritics.rb +3 -8
- data/lib/latex/decode/greek.rb +3 -1
- data/lib/latex/decode/maths.rb +469 -9
- data/lib/latex/decode/punctuation.rb +9 -2
- data/lib/latex/decode/symbols.rb +2 -4
- data/lib/latex/decode/version.rb +3 -1
- data/lib/latex/decode.rb +32 -23
- metadata +13 -40
- data/.github/workflows/ci.yml +0 -34
- data/.gitignore +0 -7
- data/.simplecov +0 -18
- data/Gemfile +0 -16
- data/Rakefile +0 -38
- data/cucumber.yml +0 -1
- data/features/brackets.feature +0 -11
- data/features/diacritics.feature +0 -40
- data/features/greek.feature +0 -13
- data/features/maths.feature +0 -9
- data/features/non-latex.feature +0 -15
- data/features/punctuation.feature +0 -50
- data/features/special_characters.feature +0 -21
- data/features/step_definitions/latex.rb +0 -12
- data/features/support/env.rb +0 -13
- data/features/symbols.feature +0 -14
- data/features/umlauts.feature +0 -11
- data/features/whitespace.feature +0 -28
- data/latex-decode.gemspec +0 -27
data/lib/latex/decode/maths.rb
CHANGED
|
@@ -1,20 +1,480 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'strscan'
|
|
4
|
+
|
|
1
5
|
module LaTeX
|
|
2
6
|
module Decode
|
|
3
7
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
module Maths
|
|
9
|
+
Span = Data.define(:open, :body, :close) do
|
|
10
|
+
def display?
|
|
11
|
+
open == '$$' || open == '\['
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def to_s
|
|
15
|
+
"#{open}#{body}#{close}"
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
OPEN = /\$\$|\$|\\\(|\\\[/
|
|
20
|
+
|
|
21
|
+
CLOSE = {
|
|
22
|
+
'$$' => '$$',
|
|
23
|
+
'$' => '$',
|
|
24
|
+
'\(' => '\)',
|
|
25
|
+
'\[' => '\]'
|
|
26
|
+
}.freeze
|
|
27
|
+
|
|
28
|
+
# Text up to the next math delimiter; escape sequences
|
|
29
|
+
# (e.g., \$) are taken as a whole.
|
|
30
|
+
TEXT = /(?:[^\\$]|\\[^(\[])+/m
|
|
31
|
+
|
|
32
|
+
# Math bodies up to (and including) the closing delimiter.
|
|
33
|
+
BODIES = {
|
|
34
|
+
'$$' => /((?:[^\\$]|\\.|\$(?!\$))*+)\$\$/m,
|
|
35
|
+
'$' => /((?:[^\\$]|\\.)*+)\$/m,
|
|
36
|
+
'\(' => /((?:[^\\]|\\[^)])*+)\\\)/m,
|
|
37
|
+
'\[' => /((?:[^\\]|\\[^\]])*+)\\\]/m
|
|
38
|
+
}.freeze
|
|
39
|
+
|
|
40
|
+
class << self
|
|
41
|
+
# Converts the span to Unicode, or returns the original TeX
|
|
42
|
+
# if any part of it cannot be converted.
|
|
43
|
+
def to_unicode(span)
|
|
44
|
+
Converter.convert(span.body) || span.to_s
|
|
45
|
+
end
|
|
8
46
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
47
|
+
# Splits the string into text (as Strings) and math (as Spans).
|
|
48
|
+
def split(string)
|
|
49
|
+
segments = []
|
|
50
|
+
text = +''
|
|
51
|
+
unclosed = {}
|
|
52
|
+
scanner = StringScanner.new(string)
|
|
53
|
+
|
|
54
|
+
until scanner.eos?
|
|
55
|
+
if (run = scanner.scan(TEXT))
|
|
56
|
+
text << run
|
|
57
|
+
elsif (open = scanner.scan(OPEN))
|
|
58
|
+
if !unclosed[open] && scanner.scan(BODIES[open])
|
|
59
|
+
segments << text unless text.empty?
|
|
60
|
+
segments << Span.new(open, scanner[1], CLOSE[open])
|
|
61
|
+
text = +''
|
|
62
|
+
else
|
|
63
|
+
# If there is no closing delimiter after this one,
|
|
64
|
+
# there is none after any later one either.
|
|
65
|
+
unclosed[open] = true
|
|
66
|
+
text << open
|
|
67
|
+
end
|
|
68
|
+
else
|
|
69
|
+
# A backslash at the very end.
|
|
70
|
+
text << scanner.getch
|
|
71
|
+
end
|
|
13
72
|
end
|
|
73
|
+
|
|
74
|
+
segments << text unless text.empty?
|
|
75
|
+
segments
|
|
14
76
|
end
|
|
15
|
-
string
|
|
16
77
|
end
|
|
17
78
|
|
|
79
|
+
class Converter
|
|
80
|
+
Unsupported = Class.new(StandardError)
|
|
81
|
+
|
|
82
|
+
SYMBOLS = Hash[*%W{
|
|
83
|
+
times ×
|
|
84
|
+
pm ±
|
|
85
|
+
div ÷
|
|
86
|
+
to →
|
|
87
|
+
gets ←
|
|
88
|
+
mapsto ↦
|
|
89
|
+
implies ⟹
|
|
90
|
+
iff ⟺
|
|
91
|
+
le ≤
|
|
92
|
+
ge ≥
|
|
93
|
+
ne ≠
|
|
94
|
+
neg ¬
|
|
95
|
+
lnot ¬
|
|
96
|
+
land ∧
|
|
97
|
+
lor ∨
|
|
98
|
+
perp ⊥
|
|
99
|
+
ell ℓ
|
|
100
|
+
hbar ℏ
|
|
101
|
+
aleph ℵ
|
|
102
|
+
wp ℘
|
|
103
|
+
Re ℜ
|
|
104
|
+
Im ℑ
|
|
105
|
+
emptyset ∅
|
|
106
|
+
varnothing ∅
|
|
107
|
+
prime ′
|
|
108
|
+
dagger †
|
|
109
|
+
ddagger ‡
|
|
110
|
+
ldots …
|
|
111
|
+
dots …
|
|
112
|
+
langle ⟨
|
|
113
|
+
rangle ⟩
|
|
114
|
+
vert |
|
|
115
|
+
Vert ‖
|
|
116
|
+
colon :
|
|
117
|
+
varepsilon ε
|
|
118
|
+
vartheta ϑ
|
|
119
|
+
varphi φ
|
|
120
|
+
varpi ϖ
|
|
121
|
+
varrho ϱ
|
|
122
|
+
varsigma ς
|
|
123
|
+
varGamma Γ
|
|
124
|
+
varDelta Δ
|
|
125
|
+
varTheta Θ
|
|
126
|
+
varLambda Λ
|
|
127
|
+
varXi Ξ
|
|
128
|
+
varPi Π
|
|
129
|
+
varSigma Σ
|
|
130
|
+
varUpsilon Υ
|
|
131
|
+
varPhi Φ
|
|
132
|
+
varPsi Ψ
|
|
133
|
+
varOmega Ω
|
|
134
|
+
circledR ®
|
|
135
|
+
}].merge(Symbols.map, Greek.map, 'backslash' => '\\').freeze
|
|
136
|
+
|
|
137
|
+
FUNCTIONS = %w{
|
|
138
|
+
arccos arcsin arctan arg cos cosh cot coth csc deg det dim exp gcd
|
|
139
|
+
hom inf ker lg lim liminf limsup ln log max min Pr sec sin sinh sup
|
|
140
|
+
tan tanh mod
|
|
141
|
+
}.to_h { |name| [name, name] }.merge('bmod' => 'mod').freeze
|
|
142
|
+
|
|
143
|
+
# Style commands that change nothing in Unicode.
|
|
144
|
+
STYLES = %w{
|
|
145
|
+
displaystyle textstyle scriptstyle scriptscriptstyle limits nolimits
|
|
146
|
+
}.freeze
|
|
147
|
+
|
|
148
|
+
CONTROL_SYMBOLS = Hash[*%W{
|
|
149
|
+
\{ {
|
|
150
|
+
\} }
|
|
151
|
+
$ $
|
|
152
|
+
% %
|
|
153
|
+
& &
|
|
154
|
+
# #
|
|
155
|
+
_ _
|
|
156
|
+
| ‖
|
|
157
|
+
}].merge(
|
|
158
|
+
',' => "\u202F",
|
|
159
|
+
':' => "\u205F",
|
|
160
|
+
'>' => "\u205F",
|
|
161
|
+
';' => "\u2004",
|
|
162
|
+
'!' => '',
|
|
163
|
+
' ' => ' '
|
|
164
|
+
).freeze
|
|
165
|
+
|
|
166
|
+
SPACES = {
|
|
167
|
+
'quad' => "\u2003",
|
|
168
|
+
'qquad' => "\u2003\u2003"
|
|
169
|
+
}.freeze
|
|
170
|
+
|
|
171
|
+
# Spacing commands absorb adjacent ordinary spaces.
|
|
172
|
+
SPACING = / ?([\u202F\u205F\u2004\u2003]+) ?/
|
|
173
|
+
|
|
174
|
+
FRACTIONS = %w{frac dfrac tfrac}.freeze
|
|
175
|
+
|
|
176
|
+
VULGAR_FRACTIONS = Hash[*%w{
|
|
177
|
+
1/2 ½ 1/3 ⅓ 2/3 ⅔ 1/4 ¼ 3/4 ¾ 1/5 ⅕ 2/5 ⅖ 3/5 ⅗ 4/5 ⅘
|
|
178
|
+
1/6 ⅙ 5/6 ⅚ 1/7 ⅐ 1/8 ⅛ 3/8 ⅜ 5/8 ⅝ 7/8 ⅞ 1/9 ⅑ 1/10 ⅒
|
|
179
|
+
}].freeze
|
|
180
|
+
|
|
181
|
+
# Numerators and denominators containing these need parentheses.
|
|
182
|
+
COMPOUND = /[\s+−±∓×÷·\/=<>≤≥,]/
|
|
183
|
+
|
|
184
|
+
ROOTS = { nil => '√', '2' => '√', '3' => '∛', '4' => '∜' }.freeze
|
|
185
|
+
|
|
186
|
+
# Combining marks for accents on a single symbol.
|
|
187
|
+
ACCENTS = {
|
|
188
|
+
'acute' => 0x0301, 'bar' => 0x0304, 'breve' => 0x0306,
|
|
189
|
+
'check' => 0x030C, 'ddot' => 0x0308, 'dot' => 0x0307,
|
|
190
|
+
'grave' => 0x0300, 'hat' => 0x0302, 'mathring' => 0x030A,
|
|
191
|
+
'tilde' => 0x0303, 'vec' => 0x20D7, 'widehat' => 0x0302,
|
|
192
|
+
'widetilde' => 0x0303
|
|
193
|
+
}.transform_values { |code| code.chr(Encoding::UTF_8) }.freeze
|
|
194
|
+
|
|
195
|
+
# Combining marks applied to every symbol.
|
|
196
|
+
LINES = {
|
|
197
|
+
'overline' => 0x0305, 'underline' => 0x0332
|
|
198
|
+
}.transform_values { |code| code.chr(Encoding::UTF_8) }.freeze
|
|
199
|
+
|
|
200
|
+
DELIMITER_SIZES = %w{
|
|
201
|
+
left right middle big Big bigg Bigg bigl Bigl biggl Biggl
|
|
202
|
+
bigr Bigr biggr Biggr bigm Bigm biggm Biggm
|
|
203
|
+
}.freeze
|
|
204
|
+
|
|
205
|
+
PRIMES = { 1 => '′', 2 => '″', 3 => '‴' }.freeze
|
|
206
|
+
|
|
207
|
+
# Characters that convert to themselves.
|
|
208
|
+
ORDINARY = /[a-zA-Z0-9()\[\]=+<>,.\/|!?:;*@"]+/
|
|
209
|
+
|
|
210
|
+
SUPERSCRIPTS = {
|
|
211
|
+
'0123456789+−=()' => '⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾',
|
|
212
|
+
'abcdefghijklmnoprstuvwxyz' => 'ᵃᵇᶜᵈᵉᶠᵍʰⁱʲᵏˡᵐⁿᵒᵖʳˢᵗᵘᵛʷˣʸᶻ',
|
|
213
|
+
'ABDEGHIJKLMNOPRTUVW' => 'ᴬᴮᴰᴱᴳᴴᴵᴶᴷᴸᴹᴺᴼᴾᴿᵀᵁⱽᵂ',
|
|
214
|
+
'αβγδθιφχ' => 'ᵅᵝᵞᵟᶿᶥᵠᵡ',
|
|
215
|
+
'′″‴*∗∘†‡' => '′″‴**°†‡'
|
|
216
|
+
}.flat_map { |from, to| from.chars.zip(to.chars) }.to_h.freeze
|
|
217
|
+
|
|
218
|
+
SUBSCRIPTS = {
|
|
219
|
+
'0123456789+−=()' => '₀₁₂₃₄₅₆₇₈₉₊₋₌₍₎',
|
|
220
|
+
'aehijklmnoprstuvx' => 'ₐₑₕᵢⱼₖₗₘₙₒₚᵣₛₜᵤᵥₓ',
|
|
221
|
+
'βγρφχ' => 'ᵦᵧᵨᵩᵪ'
|
|
222
|
+
}.flat_map { |from, to| from.chars.zip(to.chars) }.to_h.freeze
|
|
223
|
+
|
|
224
|
+
SCRIPTS = (SUPERSCRIPTS.values + SUBSCRIPTS.values).uniq.join.freeze
|
|
225
|
+
|
|
226
|
+
# Builds a Mathematical Alphanumeric Symbols alphabet from the
|
|
227
|
+
# code points of 'A', 'a' and '0'; letters Unicode encodes
|
|
228
|
+
# elsewhere (e.g., ℝ) are given as exceptions.
|
|
229
|
+
def self.alphabet(upper, lower, digits = nil, exceptions = '')
|
|
230
|
+
ranges = { ('A'..'Z') => upper, ('a'..'z') => lower, ('0'..'9') => digits }
|
|
231
|
+
|
|
232
|
+
ranges.each_with_object({}) { |(range, start), letters|
|
|
233
|
+
next if start.nil?
|
|
234
|
+
range.each_with_index { |char, i| letters[char] = (start + i).chr(Encoding::UTF_8) }
|
|
235
|
+
}.merge(Hash[*exceptions.split]).freeze
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# Bold Latin letters and digits, plus bold Greek letters
|
|
239
|
+
# from the code points of 'Α' and 'α'.
|
|
240
|
+
BOLD = alphabet(0x1D400, 0x1D41A, 0x1D7CE).merge(
|
|
241
|
+
{ 0x0391..0x03A9 => 0x1D6A8, 0x03B1..0x03C9 => 0x1D6C2 }.each_with_object({}) { |(range, start), letters|
|
|
242
|
+
range.each { |code| letters[code.chr(Encoding::UTF_8)] = (start + code - range.first).chr(Encoding::UTF_8) }
|
|
243
|
+
}
|
|
244
|
+
).freeze
|
|
245
|
+
|
|
246
|
+
ALPHABETS = {
|
|
247
|
+
'mathbb' => alphabet(0x1D538, 0x1D552, 0x1D7D8, 'C ℂ H ℍ N ℕ P ℙ Q ℚ R ℝ Z ℤ'),
|
|
248
|
+
'mathcal' => alphabet(0x1D49C, 0x1D4B6, nil,
|
|
249
|
+
'B ℬ E ℰ F ℱ H ℋ I ℐ L ℒ M ℳ R ℛ e ℯ g ℊ o ℴ'),
|
|
250
|
+
'mathfrak' => alphabet(0x1D504, 0x1D51E, nil, 'C ℭ H ℌ I ℑ R ℜ Z ℨ'),
|
|
251
|
+
'mathbf' => BOLD,
|
|
252
|
+
'boldsymbol' => BOLD,
|
|
253
|
+
'bm' => BOLD,
|
|
254
|
+
'mathsf' => alphabet(0x1D5A0, 0x1D5BA, 0x1D7E2),
|
|
255
|
+
'mathtt' => alphabet(0x1D670, 0x1D68A, 0x1D7F6)
|
|
256
|
+
}.freeze
|
|
257
|
+
|
|
258
|
+
# Old-style switches apply to the rest of the group;
|
|
259
|
+
# nil means plain letters.
|
|
260
|
+
SWITCHES = {
|
|
261
|
+
'rm' => nil, 'it' => nil, 'mit' => nil,
|
|
262
|
+
'bf' => BOLD,
|
|
263
|
+
'cal' => ALPHABETS['mathcal'],
|
|
264
|
+
'sf' => ALPHABETS['mathsf'],
|
|
265
|
+
'tt' => ALPHABETS['mathtt']
|
|
266
|
+
}.freeze
|
|
267
|
+
|
|
268
|
+
PLAIN = %w{mathrm mathit mathnormal mathup operatorname}.freeze
|
|
269
|
+
|
|
270
|
+
# Commands whose argument is text, decoded as such.
|
|
271
|
+
TEXT = %w{
|
|
272
|
+
text textrm textup textnormal textit textsl emph mbox hbox
|
|
273
|
+
}.freeze
|
|
274
|
+
|
|
275
|
+
def self.convert(body)
|
|
276
|
+
new(body).convert
|
|
277
|
+
rescue Unsupported
|
|
278
|
+
nil
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def initialize(body)
|
|
282
|
+
@scanner = StringScanner.new(body)
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def convert
|
|
286
|
+
result = sequence
|
|
287
|
+
raise Unsupported unless @scanner.eos?
|
|
288
|
+
|
|
289
|
+
result.tr_s!(" \t\r\n\f\v", ' ')
|
|
290
|
+
result.gsub!(SPACING, '\1') unless result.ascii_only?
|
|
291
|
+
result.strip!
|
|
292
|
+
result
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
private
|
|
296
|
+
|
|
297
|
+
# Converts everything up to the end or a closing brace.
|
|
298
|
+
# Runs of ordinary characters are taken as they are; arguments
|
|
299
|
+
# (e.g., of scripts) are single atoms instead.
|
|
300
|
+
def sequence
|
|
301
|
+
result = +''
|
|
302
|
+
result << (@scanner.scan(ORDINARY) || atom) until @scanner.eos? || @scanner.check(/\}/)
|
|
303
|
+
result
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def atom
|
|
307
|
+
if @scanner.scan(/\\([a-zA-Z]+)/)
|
|
308
|
+
command(@scanner[1])
|
|
309
|
+
elsif @scanner.scan(/\\(.)/m)
|
|
310
|
+
CONTROL_SYMBOLS.fetch(@scanner[1]) { raise Unsupported }
|
|
311
|
+
elsif @scanner.scan(/\{/)
|
|
312
|
+
group
|
|
313
|
+
elsif @scanner.scan(/\^/)
|
|
314
|
+
script(SUPERSCRIPTS)
|
|
315
|
+
elsif @scanner.scan(/_/)
|
|
316
|
+
script(SUBSCRIPTS)
|
|
317
|
+
elsif @scanner.scan(/\s+/)
|
|
318
|
+
' '
|
|
319
|
+
elsif @scanner.scan(/'+/)
|
|
320
|
+
PRIMES.fetch(@scanner.matched.length) { raise Unsupported }
|
|
321
|
+
else
|
|
322
|
+
character(@scanner.getch)
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
def group
|
|
327
|
+
result = sequence
|
|
328
|
+
raise Unsupported unless @scanner.scan(/\}/)
|
|
329
|
+
result
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
# Returns the raw contents of a braced group.
|
|
333
|
+
def text_group
|
|
334
|
+
@scanner.skip(/\s+/)
|
|
335
|
+
raise Unsupported unless @scanner.scan(/\{/)
|
|
336
|
+
|
|
337
|
+
depth = 1
|
|
338
|
+
text = +''
|
|
339
|
+
|
|
340
|
+
loop do
|
|
341
|
+
raise Unsupported if @scanner.eos?
|
|
342
|
+
token = @scanner.scan(/\\.|[^\\{}]+/m) || @scanner.getch
|
|
343
|
+
case token
|
|
344
|
+
when '{' then depth += 1
|
|
345
|
+
when '}' then depth -= 1
|
|
346
|
+
end
|
|
347
|
+
break if depth.zero?
|
|
348
|
+
text << token
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
text
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
# Converts a braced group or a single token.
|
|
355
|
+
def argument
|
|
356
|
+
@scanner.skip(/\s+/)
|
|
357
|
+
raise Unsupported if @scanner.eos? || @scanner.check(/\}/)
|
|
358
|
+
@scanner.scan(/\{/) ? group : atom
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
# Every character of the script must have a Unicode equivalent.
|
|
362
|
+
# Spaces are insignificant inside scripts.
|
|
363
|
+
def script(table)
|
|
364
|
+
argument.gsub(/\s+/, '').each_char.map { |char| table.fetch(char) { raise Unsupported } }.join
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
def command(name)
|
|
368
|
+
if (symbol = SYMBOLS[name] || FUNCTIONS[name])
|
|
369
|
+
symbol
|
|
370
|
+
elsif (letters = ALPHABETS[name])
|
|
371
|
+
font(letters, argument)
|
|
372
|
+
elsif SWITCHES.key?(name)
|
|
373
|
+
switch(SWITCHES[name])
|
|
374
|
+
elsif PLAIN.include?(name)
|
|
375
|
+
argument
|
|
376
|
+
elsif TEXT.include?(name)
|
|
377
|
+
LaTeX.decode(text_group, math: false)
|
|
378
|
+
elsif STYLES.include?(name)
|
|
379
|
+
''
|
|
380
|
+
elsif name == 'sp'
|
|
381
|
+
script(SUPERSCRIPTS)
|
|
382
|
+
elsif name == 'sb'
|
|
383
|
+
script(SUBSCRIPTS)
|
|
384
|
+
elsif (space = SPACES[name])
|
|
385
|
+
space
|
|
386
|
+
elsif FRACTIONS.include?(name)
|
|
387
|
+
fraction
|
|
388
|
+
elsif name == 'sqrt'
|
|
389
|
+
root
|
|
390
|
+
elsif DELIMITER_SIZES.include?(name)
|
|
391
|
+
delimiter
|
|
392
|
+
elsif (mark = ACCENTS[name])
|
|
393
|
+
accent(mark)
|
|
394
|
+
elsif (mark = LINES[name])
|
|
395
|
+
line(mark)
|
|
396
|
+
else
|
|
397
|
+
raise Unsupported
|
|
398
|
+
end
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
def fraction
|
|
402
|
+
numerator, denominator = argument, argument
|
|
403
|
+
|
|
404
|
+
VULGAR_FRACTIONS.fetch("#{numerator}/#{denominator}") {
|
|
405
|
+
[numerator, denominator].map { |part|
|
|
406
|
+
part.match?(COMPOUND) ? "(#{part})" : part
|
|
407
|
+
}.join('/')
|
|
408
|
+
}
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
def root
|
|
412
|
+
index = @scanner[1] if @scanner.scan(/\s*\[([^\]]*)\]/)
|
|
413
|
+
sign = ROOTS.fetch(index) { "#{root_index(index)}√" }
|
|
414
|
+
radicand = argument
|
|
415
|
+
|
|
416
|
+
# Sub- and superscripts do not count towards needing parentheses.
|
|
417
|
+
base = radicand.delete(SCRIPTS)
|
|
418
|
+
if base.length > 1 && !base.match?(/\A\d+\z/)
|
|
419
|
+
"#{sign}(#{radicand})"
|
|
420
|
+
else
|
|
421
|
+
"#{sign}#{radicand}"
|
|
422
|
+
end
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
def root_index(index)
|
|
426
|
+
chars = (self.class.convert(index) || raise(Unsupported)).chars
|
|
427
|
+
raise Unsupported if chars.empty?
|
|
428
|
+
chars.map { |char| SUPERSCRIPTS.fetch(char) { raise Unsupported } }.join
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
def accent(mark)
|
|
432
|
+
base = argument
|
|
433
|
+
raise Unsupported unless base.grapheme_clusters.length == 1
|
|
434
|
+
base + mark
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
def line(mark)
|
|
438
|
+
symbols = argument.grapheme_clusters
|
|
439
|
+
raise Unsupported if symbols.empty?
|
|
440
|
+
symbols.map { |symbol| symbol.match?(/\s/) ? symbol : symbol + mark }.join
|
|
441
|
+
end
|
|
442
|
+
|
|
443
|
+
# Sizing commands are dropped; '.' is the empty delimiter.
|
|
444
|
+
def delimiter
|
|
445
|
+
@scanner.skip(/\s+/)
|
|
446
|
+
@scanner.scan(/\./) ? '' : argument
|
|
447
|
+
end
|
|
448
|
+
|
|
449
|
+
# Every character must exist in the font's alphabet.
|
|
450
|
+
# Letters missing from the font are unsupported; everything else
|
|
451
|
+
# (digits, punctuation, sub- and superscripts) passes through.
|
|
452
|
+
def font(letters, string)
|
|
453
|
+
chars = string.chars
|
|
454
|
+
raise Unsupported if chars.empty?
|
|
455
|
+
|
|
456
|
+
chars.map { |char|
|
|
457
|
+
letters.fetch(char) {
|
|
458
|
+
raise Unsupported if char.match?(/[\p{L}&&\P{Lm}]/)
|
|
459
|
+
char
|
|
460
|
+
}
|
|
461
|
+
}.join
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
def switch(letters)
|
|
465
|
+
@scanner.skip(/\s+/)
|
|
466
|
+
rest = sequence
|
|
467
|
+
letters.nil? ? rest : font(letters, rest.strip)
|
|
468
|
+
end
|
|
469
|
+
|
|
470
|
+
def character(char)
|
|
471
|
+
case char
|
|
472
|
+
when '-' then '−'
|
|
473
|
+
when '&', '#', '%' then raise Unsupported
|
|
474
|
+
else char
|
|
475
|
+
end
|
|
476
|
+
end
|
|
477
|
+
end
|
|
18
478
|
end
|
|
19
479
|
|
|
20
480
|
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module LaTeX
|
|
4
4
|
module Decode
|
|
@@ -45,14 +45,21 @@ module LaTeX
|
|
|
45
45
|
|
|
46
46
|
@map = @macros.merge(@symbols).freeze
|
|
47
47
|
|
|
48
|
+
# Dash and quote ligatures, the only punctuation without a backslash.
|
|
49
|
+
LIGATURES = /(-+|`{1,2}|'{1,2})/
|
|
50
|
+
|
|
48
51
|
@patterns = [
|
|
49
52
|
/\\(#{ @macros.keys.map { |k| Regexp.escape(k) }.compact.join('|') })(?:\{\}|\s+|\b|$)/ou,
|
|
50
|
-
|
|
53
|
+
LIGATURES,
|
|
51
54
|
/()\\([$%;#_&])(\{\})?/,
|
|
52
55
|
/()\\(~)\{\}/,
|
|
53
56
|
/()\\( )/
|
|
54
57
|
].freeze
|
|
55
58
|
|
|
59
|
+
def self.decode_ligatures!(string)
|
|
60
|
+
decode_pattern!(string, LIGATURES)
|
|
61
|
+
end
|
|
62
|
+
|
|
56
63
|
end
|
|
57
64
|
|
|
58
65
|
end
|
data/lib/latex/decode/symbols.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module LaTeX
|
|
4
4
|
module Decode
|
|
@@ -30,8 +30,6 @@ module LaTeX
|
|
|
30
30
|
partial ∂
|
|
31
31
|
exists ∃
|
|
32
32
|
nexists ∄
|
|
33
|
-
set ∅
|
|
34
|
-
Delta ∆
|
|
35
33
|
nabla ∇
|
|
36
34
|
in ∈
|
|
37
35
|
notin ∉
|
|
@@ -212,7 +210,7 @@ module LaTeX
|
|
|
212
210
|
tone1 ˩
|
|
213
211
|
ss ß
|
|
214
212
|
, \u202F
|
|
215
|
-
}
|
|
213
|
+
}].freeze
|
|
216
214
|
|
|
217
215
|
|
|
218
216
|
@patterns = [
|
data/lib/latex/decode/version.rb
CHANGED
data/lib/latex/decode.rb
CHANGED
|
@@ -1,21 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
# LaTeX::Decode
|
|
3
|
-
# Copyright (C) 2011-2013 Sylvester Keil <sylvester.keil.or.at>
|
|
4
|
-
# Copyright (C) 2010 François Charette
|
|
5
|
-
#
|
|
6
|
-
# This program is free software: you can redistribute it and/or modify
|
|
7
|
-
# it under the terms of the GNU General Public License as published by
|
|
8
|
-
# the Free Software Foundation, either version 3 of the License, or
|
|
9
|
-
# (at your option) any later version.
|
|
10
|
-
#
|
|
11
|
-
# This program is distributed in the hope that it will be useful,
|
|
12
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
-
# GNU General Public License for more details.
|
|
15
|
-
#
|
|
16
|
-
# You should have received a copy of the GNU General Public License
|
|
17
|
-
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
18
|
-
#++
|
|
1
|
+
# frozen_string_literal: true
|
|
19
2
|
|
|
20
3
|
require 'latex/decode/version'
|
|
21
4
|
require 'latex/decode/compatibility'
|
|
@@ -23,21 +6,22 @@ require 'latex/decode/base'
|
|
|
23
6
|
|
|
24
7
|
require 'latex/decode/accents'
|
|
25
8
|
require 'latex/decode/diacritics'
|
|
26
|
-
require 'latex/decode/maths'
|
|
27
9
|
require 'latex/decode/punctuation'
|
|
28
10
|
require 'latex/decode/symbols'
|
|
29
11
|
require 'latex/decode/greek'
|
|
12
|
+
require 'latex/decode/maths'
|
|
30
13
|
|
|
31
14
|
module LaTeX
|
|
32
15
|
|
|
16
|
+
NBSP = 0xA0.chr(Encoding::UTF_8).freeze
|
|
17
|
+
|
|
33
18
|
@decoders = [
|
|
34
|
-
Decode::Maths,
|
|
35
19
|
Decode::Accents,
|
|
36
20
|
Decode::Diacritics,
|
|
37
21
|
Decode::Punctuation,
|
|
38
22
|
Decode::Symbols,
|
|
39
23
|
Decode::Greek
|
|
40
|
-
]
|
|
24
|
+
].freeze
|
|
41
25
|
|
|
42
26
|
class << self
|
|
43
27
|
attr_reader :decoders
|
|
@@ -45,7 +29,22 @@ module LaTeX
|
|
|
45
29
|
def decode(string, options = {})
|
|
46
30
|
return string unless string.respond_to?(:to_s)
|
|
47
31
|
|
|
48
|
-
|
|
32
|
+
decoded = Decode::Maths.split(string.to_s).map { |segment|
|
|
33
|
+
if segment.is_a?(Decode::Maths::Span)
|
|
34
|
+
options[:math] == false ? segment.to_s : Decode::Maths.to_unicode(segment)
|
|
35
|
+
else
|
|
36
|
+
decode_text(segment, options)
|
|
37
|
+
end
|
|
38
|
+
}.join
|
|
39
|
+
|
|
40
|
+
# ASCII is always in NFC.
|
|
41
|
+
decoded.ascii_only? ? decoded : LaTeX.normalize_C(decoded)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def decode_text(string, options)
|
|
47
|
+
return decode_plain_text(string, options) unless string.include?('\\')
|
|
49
48
|
|
|
50
49
|
Decode::Base.normalize(string)
|
|
51
50
|
|
|
@@ -55,7 +54,17 @@ module LaTeX
|
|
|
55
54
|
|
|
56
55
|
Decode::Base.strip_braces(string) unless options[:strip_braces] == false
|
|
57
56
|
|
|
58
|
-
|
|
57
|
+
string
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Without a backslash, only non-breaking spaces, dash and quote
|
|
61
|
+
# ligatures and braces can be decoded; this is equivalent to,
|
|
62
|
+
# but much faster than, the full decode_text.
|
|
63
|
+
def decode_plain_text(string, options)
|
|
64
|
+
string.tr!('~', NBSP)
|
|
65
|
+
Decode::Punctuation.decode_ligatures!(string) unless options[:punctuation] == false
|
|
66
|
+
string.delete!('{}') unless options[:strip_braces] == false
|
|
67
|
+
string
|
|
59
68
|
end
|
|
60
69
|
end
|
|
61
70
|
end
|