-
Notifications
You must be signed in to change notification settings - Fork 12
/
handlers.py
298 lines (254 loc) · 10.6 KB
/
handlers.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
#
# 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/>.
#
r"""
Collection of macro handlers for various LaTeX commands.
"""
import re
from yalafi import defs
from yalafi import utils
from yalafi import scanner
def g_newcommand(overwrite=True):
r"""
Generator for ``\newcommand``, ``\renewcommand`` and ``\providecommand``.
Args:
overwrite: Boolean deciding, whether the returned handler
overwrites existing commands. Defaults to True.
Returns:
Macro Handler for ``\newcommand``, ``\renewcommand`` and
``\providecommand``. Set ``overwrite=False`` for
``\providecommand``.
"""
# pylint: disable-next=redefined-outer-name
def h_newcommand(parser, buf, mac, args, delim, pos):
r"""
Macro handler for `\newcommand` and `\renewcommand`.
"""
name = parser.get_text_direct(args[1])
if name in parser.parms.newcommand_ignore:
return []
if not overwrite and name in parser.the_macros:
return []
nargs = parser.get_text_expanded(args[2])
nargs = int(nargs) if nargs.isdecimal() else 0
for a in [b for b in args[4] if type(b) is defs.ArgumentToken]:
if a.arg < 1 or a.arg > nargs:
return utils.latex_error(parser, 'illegal argument #' + str(a.arg)
+ ' in definition of macro ' + name, a.pos)
if args[3]:
if nargs < 1:
return utils.latex_error(parser,
'illegal default value in definition of macro ' + name,
args[1][0].pos)
parser.the_macros[name] = defs.Macro(parser.parms,
name, args='O' + 'A' * (nargs - 1),
repl=args[4], defaults=[args[3]], scanned=True)
else:
parser.the_macros[name] = defs.Macro(parser.parms,
name, args='A' * nargs,
repl=args[4], scanned=True)
return []
return h_newcommand
h_newcommand = g_newcommand(overwrite=True)
def h_theorem(name):
r"""
Generator for macro handler for theorem like environments.
The returned handler will output the `name` followed by a period.
For named theorems, the optional argument will be inserted in
parenthesis before the period.
Args:
name: name of the theorem like environment.
Returns:
A macro handler for the theorem like environment `name`.
"""
# TODO: Rename in next major release to g_theorem
# reflecting that it generates a macro handler function `handler`.
def handler(parser, buf, mac, args, delim, pos):
out = [defs.TextToken(pos, name, pos_fix=True)]
if args[0]:
# named theorem
out.append(defs.SpaceToken(pos, ' ', pos_fix=True))
out.append(defs.TextToken(pos, '(', pos_fix=True))
out += args[0]
out.append(defs.TextToken(args[0][-1].pos,
').', pos_fix=True))
out.append(defs.SpaceToken(args[0][-1].pos,
'\n', pos_fix=True))
else:
# unnamed theorem
out.append(defs.TextToken(pos, '.', pos_fix=True))
out.append(defs.SpaceToken(pos, '\n', pos_fix=True))
return out
return handler
def h_newtheorem(parser, buf, mac, args, delim, pos):
r"""
Macro handler for `\newtheorem`.
"""
name = parser.get_text_expanded(args[0])
title = parser.get_text_expanded(args[2])
def f(parser, options, position): # pylint: disable=unused-argument
parms = parser.parms
envs = [defs.Environ(parms, name, args='O', repl=h_theorem(title))]
return defs.InitModule(environments=envs)
parser.modify_parameters('<h_newtheorem>', f, [], pos)
return []
def h_heading(parser, buf, mac, args, delim, pos):
r"""
Macro handler for heading commands like ``\section``. It appends a
period to the heading unless the heading ends with a character in
:attr:`yalafi.parameters.Parameters.heading_punct`.
"""
arg = args[2]
txt = parser.get_text_expanded(arg).strip()
if (txt and parser.parms.heading_punct
and txt[-1] not in parser.parms.heading_punct):
arg.append(defs.TextToken(arg[-1].pos, '.'))
return arg
def h_phantom(parser, buf, mac, args, delim, pos):
r"""
Macro handler for `\phantom` and `\hphantom` replacing them with
small space, if they contain text.
"""
if len(parser.get_text_expanded(args[0])) > 0:
return [defs.SpecialToken(pos, '\\;')]
return []
numbers = re.compile(r'\s*(\d+[.,]?\d*|[.,]\d+)')
r"Regex for matching numbers in `\hspace` arguments."
def h_hspace(parser, buf, mac, args, delim, pos):
r"""
Macro handler for `\hspace` replacing it with space, unless the
argument is (explicitly) zero length.
Additions and subtractions of lengths are currently not supported.
"""
arg = parser.get_text_expanded(args[1])
match = numbers.match(arg)
if match and float(match.group(1).replace(',', '.')) == 0:
return []
return [defs.SpaceToken(pos, ' ')]
def h_linebreak(parser, buf, mac, args, delim, pos):
r"""
Handler for macro ``\linebreak`` taking one optional argument.
Adds no additional space for optional argument '0','1','2','3'.
Adds one SpaceToken for argument '4', or no argument. Adds output
from :func:`utils.latex_error` for other arguments, we allow
additional white space.
"""
if len(args[0]) == 0:
return [defs.SpaceToken(pos, ' ')]
else:
arg = parser.get_text_expanded(args[0]).strip()
if arg == '4':
return [defs.SpaceToken(pos, ' ')]
elif arg in ['0','1','2','3']:
# There is no line break for arguments 0,1,2,3
return []
# For all other arguments, return an error
return utils.latex_error(parser, r'`\linebreak` only takes values 0,1,2,3 or 4', pos)
def h_cite(parser, buf, mac, args, delim, pos):
r"""
Macro handler for the default LaTeX `\cite` command.
See :func:`yalafi.packages.biblatex.h_cite` for the corresponding
handler after loading BibLaTeX.
"""
if args[0]:
out = [defs.TextToken(pos, '[0,', pos_fix=True),
defs.SpaceToken(pos, ' ', pos_fix=True)]
out += args[0]
out += [defs.TextToken(args[0][-1].pos, ']'),
defs.ActionToken(args[0][-1].pos)]
else:
out = [defs.TextToken(pos, '[0]', pos_fix=True),
defs.ActionToken(pos)]
return out
def h_load_defs(parser, buf, mac, args, delim, pos):
r"""
Macro handler for ``\LTinput``.
It updates the parser by reading the provided file. Hence, it
updates macro definitions and activates loaded packages. It only
returns the Language Tokens from the loaded file such that switching
languages also works.
If the file could not be found, a LaTeX error is added to the
output. If the file is included recursively, the error is printed to
stderr and the program stops.
"""
if not parser.read_macros:
return []
file = parser.get_text_expanded(args[0])
ok, latex = parser.read_macros(file)
if not ok:
return utils.latex_error(parser, 'could not read file ' + repr(file),
pos)
try:
toks = parser.parser_work(latex, file)
except RecursionError:
utils.fatal('Problem while executing "' + mac.name + '{' + file
+ '}".\n' + '*** Is the file included recursively?')
return utils.filter_set_toks(toks, pos, defs.LanguageToken)
# read definitions for a LaTeX package
#
def h_load_module(prefix):
r"""
Generator for a macro handler function for `\documentclass` and
`\usepackage`.
The returned handler will try to load the package definition from
the python module `prefix.my_package` when using
`\usepackage{my-package}`.
Args:
prefix: Name of the python (sub)package which contains the
modules corresponding to LaTeX packages.
Returns:
A macro handler function for `\documentclass` and `\usepackage`.
See also `yalafi.utils.get_module_handler`.
"""
# TODO: Rename in next major release to g_load_module
# reflecting that it generates a macro handler function `handler`.
def handler(parser, buf, mac, args, delim, pos):
options = parser.parse_keyvals_list(args[0])
options = parser.expand_keyvals(options)
packs = parser.get_text_expanded(args[1])
out = []
for p in packs.split(','):
p = p.strip()
if p:
f = utils.get_module_handler(p, prefix)
out += parser.init_package(p, f, options, pos)
return utils.filter_set_toks(out, pos, None)
return handler
def h_makeLowercase(parser, buf, mac, args, delim, pos):
r"""
Macro handler for `\MakeLowercase`.
The argument is expanded first, then all text is replaced with a
lower case variant.
"""
toks = parser.expand_sequence(scanner.Buffer(args[0].copy()))
def f(t):
if type(t) is defs.TextToken:
t.txt = t.txt.lower()
return t
return [f(t) for t in toks]
def h_makeUppercase(parser, buf, mac, args, delim, pos):
r"""
Macro handler for `\MakeUppercase`.
The argument is expanded first, then all text is replaced with a
upper case variant.
"""
toks = parser.expand_sequence(scanner.Buffer(args[0].copy()))
def f(t):
if type(t) is defs.TextToken:
t.txt = t.txt.upper()
return t
return [f(t) for t in toks]