-
Notifications
You must be signed in to change notification settings - Fork 12
/
parameters.py
621 lines (539 loc) · 21.1 KB
/
parameters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
107
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
189
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#
# YaLafi: Yet another LaTeX filter
# Copyright (C) 2020 Matthias Baumann
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
"""
Default parameters for scanner and parser.
Includes: standard LaTeX macros and environments, language settings,
math material settings.
"""
from yalafi.defs import Environ, EquEnv, Macro
from yalafi import handlers as hs
from yalafi import scanner
class Parameters:
"""
Default settings for parser.
Attributes:
macro_defs_latex:
macro_defs_python:
environment_defs:
parser_lang_settings: Dictionary with language settings.
The keys are language codes like ``en`` or ``de``. And the
values are :class:`ParserLanguageSetting` objects.
parser_lang_stack:
lang_context:
heading_punct:
item_default_label:
item_punctuation:
mark_latex_error:
mark_latex_error_verbose:
macro_filter_add:
macro_filter_alter:
macro_filter_skip:
macro_load_defs:
newcommand_ignore:
comment_skip_begin:
comment_skip_end:
class_modules:
package_modules:
accent_macros:
special_tokens:
math_displayed_simple:
math_ignore:
math_space:
math_operators:
math_text_macros:
math_default_env:
math_punctuation:
multi_language:
ml_continue_thresh:
scanner:
proof_name:
math_repl_inline:
math_repl_inline_vowel:
math_repl_display:
math_repl_display_vowel:
math_op_text:
lang_change_repl:
lang_change_repl_vowel:
short_macros:
active_chars:
"""
def init_macros(self):
"""Initialize standard, default LaTeX macros."""
# LaTeX macro definition as LaTeX code
self.macro_defs_latex = r"""
\newcommand{\AA}{Å}
\newcommand{\aa}{å}
\newcommand{\AE}{Æ}
\newcommand{\addtocounter}[2]{}
\newcommand{\addtolength}[2]{}
\newcommand{\ae}{æ}
\newcommand{\author}[1]{#1.}
\newcommand{\bibitem}[1]{\item}
\newcommand{\bibliographystyle}[1]{}
\newcommand{\footnotemark}[1][]{}
\newcommand{\hfill}{ }
\newcommand{\include}[1]{}
\newcommand{\includeonly}[1]{}
\newcommand{\index}[1]{}
\newcommand{\input}[1]{}
\newcommand{\L}{Ł}
\newcommand{\l}{ł}
\newcommand{\label}[1]{}
\newcommand{\LaTeX}{LaTeX}
\newcommand{\newcounter}[1]{}
\newcommand{\newlength}[1]{}
\newcommand{\newline}{ }
\newcommand{\nobreakspace}{~}
\newcommand{\O}{Ø}
\newcommand{\o}{ø}
\newcommand{\OE}{Œ}
\newcommand{\oe}{œ}
\newcommand{\pagenumbering}[1]{}
\newcommand{\pageref}[1]{0}
\newcommand{\pagestyle}[1]{}
\newcommand{\par}{
}
\newcommand{\qquad}{\;}
\newcommand{\quad}{\;}
\newcommand{\ref}[1]{0}
\newcommand{\refstepcounter}[1]{}
\newcommand{\S}{§}
\newcommand{\setcounter}[2]{}
\newcommand{\setlength}[2]{}
\newcommand{\settodepth}[2]{}
\newcommand{\settoheight}[2]{}
\newcommand{\settowidth}[2]{}
\newcommand{\ss}{ß}
\newcommand{\stepcounter}[1]{}
\newcommand{\TeX}{TeX}
\newcommand{\textasciicircum}{\verb?^?} % \^ is accent
\newcommand{\textasciitilde}{\verb?~?} % \~ is accent
\newcommand{\textbackslash}{\verb?\?} % \\ is line break
\newcommand{\thispagestyle}[1]{}
\newcommand{\vphantom}[1]{}
"""
# LaTeX macro definition as Python code
#
self.macro_defs_python = [
Macro(self, '\\caption', args='OA', extract='#2'),
Macro(self, '\\chapter', args='*OA', repl=hs.h_heading),
Macro(self, '\\cite', args='OA', repl=hs.h_cite),
Macro(self, '\\documentclass', args='OA',
repl=hs.h_load_module(self.class_modules)),
Macro(self, '\\footnote', args='OA', extract='#2'),
Macro(self, '\\footnotetext', args='OA', extract='#2'),
Macro(self, '\\framebox', args='OOA', repl='#3'),
Macro(self, '\\hphantom', args='A', repl=hs.h_phantom),
Macro(self, '\\linebreak', args='O', repl=hs.h_linebreak),
Macro(self, '\\hspace', args='*A', repl=hs.h_hspace),
Macro(self, '\\MakeLowercase', args='A', repl=hs.h_makeLowercase),
Macro(self, '\\MakeUppercase', args='A', repl=hs.h_makeUppercase),
Macro(self, '\\newcommand', args='*AOOA', repl=hs.h_newcommand),
Macro(self, '\\newtheorem', args='AOAO', repl=hs.h_newtheorem),
Macro(self, '\\paragraph', args='*OA', repl=hs.h_heading),
Macro(self, '\\part', args='*OA', repl=hs.h_heading),
Macro(self, '\\phantom', args='A', repl=hs.h_phantom),
Macro(self, '\\providecommand', args='*AOOA', repl=hs.g_newcommand(overwrite=False)),
Macro(self, '\\renewcommand', args='*AOOA', repl=hs.h_newcommand),
Macro(self, '\\section', args='*OA', repl=hs.h_heading),
Macro(self, '\\subsection', args='*OA', repl=hs.h_heading),
Macro(self, '\\subsubsection', args='*OA', repl=hs.h_heading),
Macro(self, '\\title', args='*OA', repl=hs.h_heading),
Macro(self, '\\usepackage', args='OA',
repl=hs.h_load_module(self.package_modules)),
Macro(self, '\\vspace', args='*A', repl=' '),
# \LTadd etc.
#
Macro(self, self.macro_filter_add, args='A', repl='#1'),
Macro(self, self.macro_filter_alter, args='AA', repl='#2'),
Macro(self, self.macro_filter_skip, args='A', repl=''),
Macro(self, self.macro_load_defs, args='A', repl=hs.h_load_defs),
]
def init_environments(self):
"""Initialize default LaTeX environments."""
self.environment_defs = [
Environ(self, 'figure', args='O', add_pars=False),
Environ(self, 'minipage', args='A'),
Environ(self, 'table', args='O', add_pars=False),
Environ(self, 'tabular', args='A', add_pars=False),
Environ(self, 'thebibliography', args='A', add_pars=True),
Environ(self, 'verbatim', remove=False, add_pars=True),
EquEnv(self, 'displaymath'),
EquEnv(self, 'eqnarray'),
EquEnv(self, 'eqnarray*'),
EquEnv(self, 'equation'),
]
# enumerate: generate 1., 2., 3., ...
#
def labs_enumerate(level):
if level == 0:
n = 1
while True:
yield str(n) + '.'
n += 1
else:
c = 'a'
while True:
yield c + '.'
c = chr(ord(c) + 1) if c != 'z' else 'a'
# itemize: use default labels
#
def labs_itemize(level):
while True:
yield self.item_default_label[
min(level, len(self.item_default_label) - 1)]
self.environment_defs += [
Environ(self, 'enumerate', add_pars=False, items=labs_enumerate),
Environ(self, 'itemize', add_pars=False, items=labs_itemize),
]
def init_parser_languages(self, lang):
"""
Initialize language-dependent parser parameters.
Args:
lang: Key for :attr:`parser_lang_settings` with which the
:attr:`parser_lang_stack` is initialized.
Settings for ``en`` are taken as fall back.
"""
settings = self.parser_lang_settings = {}
settings['en'] = ParserLanguageSettings(
proof_name = 'Proof',
math_repl_inline = ['B-B-B', 'C-C-C', 'D-D-D',
'E-E-E', 'F-F-F', 'G-G-G'],
math_repl_inline_vowel = None,
math_repl_display = ['U-U-U', 'V-V-V', 'W-W-W',
'X-X-X', 'Y-Y-Y', 'Z-Z-Z'],
math_repl_display_vowel = None,
math_op_text = {'+': 'plus', '-': 'minus',
'\\cdot': 'times', '\\times': 'times',
'/': 'over',
None: 'equal'}, # default value
lang_change_repl = ['K-K-K', 'L-L-L', 'M-M-M', 'N-N-N'],
lang_change_repl_vowel = None,
short_macros = {}
)
settings['de'] = ParserLanguageSettings(
proof_name = 'Beweis',
math_repl_inline = ['B-B-B', 'C-C-C', 'D-D-D',
'E-E-E', 'F-F-F', 'G-G-G'],
math_repl_inline_vowel = None,
math_repl_display = ['U-U-U', 'V-V-V', 'W-W-W',
'X-X-X', 'Y-Y-Y', 'Z-Z-Z'],
math_repl_display_vowel = None,
math_op_text = {'+': 'plus', '-': 'minus',
'\\cdot': 'mal', '\\times': 'mal',
'/': 'durch',
None: 'gleich'}, # default value
lang_change_repl = ['K-K-K', 'L-L-L', 'M-M-M', 'N-N-N'],
lang_change_repl_vowel = None,
short_macros = {
'"-': '',
'"=': '-',
'"`': '\N{DOUBLE LOW-9 QUOTATION MARK}', # \glqq
'"\'': '\N{LEFT DOUBLE QUOTATION MARK}', # \grqq
'"A': 'Ä',
'"a': 'ä',
'"O': 'Ö',
'"o': 'ö',
'"U': 'Ü',
'"u': 'ü',
'"s': 'ß',
}
)
settings['ru'] = ParserLanguageSettings(
proof_name = 'Доказательство',
math_repl_inline = ['Б-Б-Б', 'В-В-В', 'Г-Г-Г',
'Д-Д-Д', 'Е-Е-Е', 'Ж-Ж-Ж'],
math_repl_inline_vowel = None,
math_repl_display = ['Ц-Ц-Ц', 'Ч-Ч-Ч', 'Ш-Ш-Ш',
'Ы-Ы-Ы', 'Э-Э-Э', 'Ю-Ю-Ю'],
math_repl_display_vowel = None,
math_op_text = {'+': 'плюс', '-': 'минус',
'\\cdot': 'раз', '\\times': 'раз',
'/': 'на',
None: 'равно'}, # default value
lang_change_repl = ['К-К-К', 'Л-Л-Л', 'М-М-М', 'Н-Н-Н'],
lang_change_repl_vowel = None,
short_macros = {}
)
self.parser_lang_stack = [(settings[self.check_parser_lang(lang)],
lang)]
"""
List of tuples `(settings, lang)` where `settings` are the
`ParserLanguageSettings` for the language `lang`. The last list
item is the current language. The previous items are languages
used before.
"""
self.lang_context = self.parser_lang_stack[-1][0]
def init_collections(self):
"""
Initialize more special settings.
This includes punctuation convention, special YaLafi commands,
accent macros, special tokens.
"""
# add dot to heading unless last heading char in ...
# (turn off: set to [])
#
self.heading_punct = ['!', '?']
# labels for \item without [...] (may depend on nesting level)
#
self.item_default_label = ['']
# \item with [...] label: if text before ends with something
# from here, then append it to label
#
self.item_punctuation = ['.', ':', ',', ';', '!', '?']
# mark for parsing problem, should raise message from proofreader
#
self.mark_latex_error = 'LATEXXXERROR'
# True: include error description
#
self.mark_latex_error_verbose = False
# names of special macros
#
self.macro_filter_add = '\\LTadd'
self.macro_filter_alter = '\\LTalter'
self.macro_filter_skip = '\\LTskip'
self.macro_load_defs = '\\LTinput'
# ignore re-definitions in LaTeX text for these macros:
#
self.newcommand_ignore = [
self.macro_filter_add,
self.macro_filter_alter,
self.macro_filter_skip,
self.macro_load_defs,
]
# special LaTeX comments to skip input text
#
self.comment_skip_begin = '%%% LT-SKIP-BEGIN'
self.comment_skip_end = '%%% LT-SKIP-END'
# module directories
#
self.class_modules = 'yalafi.documentclasses'
self.package_modules = 'yalafi.packages'
# accent macros
#
self.accent_macros = {
"\\'": ['ACUTE', 'ACCENT'],
'\\`': ['GRAVE', 'ACCENT'],
'\\^': ['CIRCUMFLEX', 'ACCENT'],
'\\v': ['CARON'],
'\\~': ['TILDE'],
'\\"': ['DIAERESIS'],
'\\r': ['RING ABOVE'],
'\\=': ['MACRON'],
'\\b': ['LINE BELOW'],
'\\u': ['BREVE'],
'\\H': ['DOUBLE ACUTE', 'ACCENT'],
'\\.': ['DOT ABOVE'],
'\\d': ['DOT BELOW'],
'\\c': ['CEDILLA'],
'\\k': ['OGONEK'],
}
# "special" tokens: expanded to key value
#
self.special_tokens = {
'{': '{',
'}': '}',
'$$': '$$',
'$': '$',
'#': '#',
'&': ' ', # expand to space
'_': '_',
'^': '^',
'\\(': '\\(',
'\\)': '\\)',
'\\[': '\\[',
'\\]': '\\]',
'\\\\': ' ', # expand to space
'~': '\N{NO-BREAK SPACE}',
'``': '\N{LEFT DOUBLE QUOTATION MARK}',
"''": '\N{RIGHT DOUBLE QUOTATION MARK}',
'--': '\N{EN DASH}',
'---': '\N{EM DASH}',
'\\ ': ' ',
'\\\t': ' ',
'\\\n': ' ',
'\\,': '\N{NARROW NO-BREAK SPACE}',
'\\:': ' ',
'\\;': ' ',
'\\!': '',
'\\{': '{',
'\\}': '}',
'\\$': '$',
'\\#': '#',
'\\&': '&',
'\\_': '_',
'\\%': '%',
'\\/': '',
}
def init_math_collections(self):
"""
Initialize settings for parsing math material.
"""
self.math_displayed_simple = False
"""
Boolean indicating whether to use simple replacement for
displayed equations.
"""
self.math_ignore = [
'{',
'}',
'\\!',
'\\label',
'\\mathrlap',
'\\nonumber',
]
"""
List of things to be ignored in math mode.
Some entries are redundant, if macros are known from text mode
and expand to “nothing”.
"""
self.math_space = [
'~',
'\\ ',
'\\\t',
'\\\n',
'\\:',
'\\,',
'\\;',
]
"""List of things that generate space even in math mode."""
self.math_operators = [
'+', '-', '\\cdot', '\\times', '/',
'=', '\\eq', '\\ne', '\\neq',
'<', '\\le', '\\leq', '\\ll',
'>', '\\ge', '\\geq', '\\gg',
':', ':=',
'\\to', '\\mapsto',
'\\cap', '\\cup',
'\\Rightarrow', '\\Leftarrow', '\\Leftrightarrow',
'\\subset', '\\subseteq', '\\supset', '\\supseteq',
'\\in', '\\ni',
'\\stackrel',
]
"""
List of math operators.
If one of these operators appear first in a part (only math
tokens) then one of the replacements in `self.math_op_text` is
inserted.
"""
self.math_text_macros = [
'\\mbox',
]
"""
List of macro names whose argument is treated as text even in
math mode.
"""
self.math_default_env = 'displaymath'
r"""
Name of math environment used for `$$` and `\[`.
"""
self.math_punctuation = ['.', ',', ';', ':']
"""
List of punctuation characters extracted from math parts.
If a math part ends with a character from this list, it is
appended to the replacement from `self_repl_xxx`.
"""
def macro_character(self, c):
"""
Determine whether a character may be part of a LaTeX macro name.
Returns ``True``, if `c` is in ``[a-zA-Z@]``.
"""
return c >= 'a' and c <= 'z' or c >= 'A' and c <= 'Z' or c == '@'
def check_parser_lang(self, lang):
"""
Transform `lang` into valid key for language dictionary
:attr:`parser_lang_settings`.
"""
lang = lang[:2].lower()
return lang if lang in self.parser_lang_settings else 'en'
def change_parser_lang(self, tok):
"""
Switch current parser language settings to new language.
"""
if tok.back:
if len(self.parser_lang_stack) > 1:
self.parser_lang_stack.pop()
else:
if tok.hard:
self.parser_lang_stack[-1] = (
self.parser_lang_settings[self.check_parser_lang(tok.lang)],
tok.lang)
else:
self.parser_lang_stack.append(
(self.parser_lang_settings[self.check_parser_lang(tok.lang)],
tok.lang))
# Already defined in self.init_parser_languages():
# pylint: disable-next=attribute-defined-outside-init
self.lang_context = self.parser_lang_stack[-1][0]
def lang_context_lang(self):
"""
Return the language key (e.g. ``en``) for the current language.
"""
return self.parser_lang_stack[-1][1]
def no_specials(self):
"""
Deactivate special macros and magic comments.
"""
# Already defined in self.init_collections():
# pylint: disable-next=attribute-defined-outside-init
self.comment_skip_begin = 'x'
# pylint: disable-next=attribute-defined-outside-init
self.comment_skip_end = 'x'
self.macro_defs_python += [
Macro(self, self.macro_filter_add, args='A', repl=''),
Macro(self, self.macro_filter_alter, args='AA', repl='#1'),
Macro(self, self.macro_filter_skip, args='A', repl='#1'),
]
def __init__(self, language='en'):
self.init_collections()
self.init_math_collections()
self.multi_language = False
self.ml_continue_thresh = 3
self.init_parser_languages(language)
self.scanner = scanner.Scanner(self)
self.init_macros()
self.init_environments()
class ParserLanguageSettings:
"""
Language settings for parser.
"""
def __init__(self, proof_name,
math_repl_inline, math_repl_inline_vowel,
math_repl_display, math_repl_display_vowel,
math_op_text,
lang_change_repl, lang_change_repl_vowel,
short_macros):
self.proof_name = proof_name
self.math_repl_inline = math_repl_inline
if math_repl_inline_vowel is None:
self.math_repl_inline_vowel = math_repl_inline
else:
self.math_repl_inline_vowel = math_repl_inline_vowel
self.math_repl_display = math_repl_display
if math_repl_display_vowel is None:
self.math_repl_display_vowel = math_repl_display
else:
self.math_repl_display_vowel = math_repl_display_vowel
self.math_op_text = math_op_text
self.lang_change_repl = lang_change_repl
if lang_change_repl_vowel is None:
self.lang_change_repl_vowel = lang_change_repl
else:
self.lang_change_repl_vowel = lang_change_repl_vowel
self.short_macros = short_macros
self.active_chars = set(k[0] for k in self.short_macros.keys())