2
2
import warnings
3
3
4
4
5
- def _remove_comments (string ) :
5
+ def _remove_comments (string : str ) -> str :
6
6
"""Remove comments unless the comment character is the last non-whitespace character
7
7
in a line. (This is often used in macros etc.)
8
8
"""
@@ -23,21 +23,21 @@ def _remove_comments(string):
23
23
return string
24
24
25
25
26
- def _remove_trailing_whitespace (string ) :
26
+ def _remove_trailing_whitespace (string : str ) -> str :
27
27
return "\n " .join ([line .rstrip () for line in string .split ("\n " )])
28
28
29
29
30
- def _remove_multiple_spaces (string ) :
30
+ def _remove_multiple_spaces (string : str ) -> str :
31
31
"""Replaces multiple spaces by one, except after a newline."""
32
32
return re .sub ("([^\n ]) +" , r"\1 " , string )
33
33
34
34
35
- def _remove_multiple_newlines (string ) :
35
+ def _remove_multiple_newlines (string : str ) -> str :
36
36
string = re .sub ("\n \n \n \n +" , "\n \n \n " , string )
37
37
return string
38
38
39
39
40
- def _remove_whitespace_around_brackets (string ) :
40
+ def _remove_whitespace_around_brackets (string : str ) -> str :
41
41
string = re .sub ("{[ \t ]+" , "{" , string )
42
42
string = re .sub ("[ \t ]+}" , "}" , string )
43
43
string = re .sub ("\\ ([ \t ]+" , "(" , string )
@@ -46,7 +46,7 @@ def _remove_whitespace_around_brackets(string):
46
46
return string
47
47
48
48
49
- def _replace_dollar_dollar (string ) :
49
+ def _replace_dollar_dollar (string : str ) -> str :
50
50
"""Replace $$...$$ by \\ [...\\ ]."""
51
51
p = re .compile (r"\$\$" )
52
52
locations = [m .start () for m in p .finditer (string )]
@@ -63,7 +63,7 @@ def _replace_dollar_dollar(string):
63
63
return _substitute_string_ranges (string , ranges , replacements )
64
64
65
65
66
- def _replace_dollar (string ) :
66
+ def _replace_dollar (string : str ) -> str :
67
67
"""Replace $...$ by \\ (...\\ ). See <https://tex.stackexchange.com/q/510/13262>."""
68
68
# (?<!\\\\) checks there is no backslash before (negative lookbehind)
69
69
# (?:\\\\{2})* matches all even numbers of backslashes
@@ -82,7 +82,7 @@ def _replace_dollar(string):
82
82
return _substitute_string_ranges (string , ranges , replacements )
83
83
84
84
85
- def _replace_obsolete_text_mods (string ) :
85
+ def _replace_obsolete_text_mods (string : str ) -> str :
86
86
string = string .replace ("{\\ bf " , "\\ textbf{" )
87
87
string = string .replace ("{\\ it " , "\\ textit{" )
88
88
string = string .replace ("{\\ rm " , "\\ textrm{" )
@@ -97,27 +97,22 @@ def _replace_obsolete_text_mods(string):
97
97
return string
98
98
99
99
100
- def _add_space_after_single_subsuperscript (string ) :
100
+ def _add_space_after_single_subsuperscript (string : str ) -> str :
101
101
string = re .sub (r"([\^])([^{\\])([^_\^\s\$})])" , r"\1\2 \3" , string )
102
102
return string
103
103
104
104
105
- def _replace_dots (string ) :
105
+ def _replace_dots (string : str ) -> str :
106
106
string = re .sub (r"\.\.\." , r"\\dots" , string )
107
107
string = re .sub (r",\\cdots," , r",\\dots," , string )
108
108
return string
109
109
110
110
111
- def _replace_punctuation_outside_math (string ):
112
- string = re .sub (r"\.\$" , "$." , string )
113
- string = re .sub (r",\$" , "$," , string )
114
- string = re .sub (r";\$" , "$;" , string )
115
- string = re .sub (r"!\$" , "$!" , string )
116
- string = re .sub (r"\?\$" , "$?" , string )
117
- return string
111
+ def _replace_punctuation_at_math_end (string : str ) -> str :
112
+ return re .sub (r"([\.,;!\?])\\\)" , r"\)\1" , string )
118
113
119
114
120
- def _remove_whitespace_before_punctuation (string ) :
115
+ def _remove_whitespace_before_punctuation (string : str ) -> str :
121
116
string = re .sub (r"\s+\." , "." , string )
122
117
string = re .sub (r"\s+," , "," , string )
123
118
string = re .sub (r"\s+;" , ";" , string )
@@ -126,25 +121,24 @@ def _remove_whitespace_before_punctuation(string):
126
121
return string
127
122
128
123
129
- def _add_nbsp_before_reference (string ) :
124
+ def _add_nbsp_before_reference (string : str ) -> str :
130
125
string = re .sub (r"\s+\\ref{" , r"~\\ref{" , string )
131
126
string = re .sub (r"\s+\\eqref{" , r"~\\eqref{" , string )
132
127
string = re .sub (r"\s+\\cite" , r"~\\cite" , string )
133
128
return string
134
129
135
130
136
- def _replace_double_nbsp (string ):
137
- string = re .sub ("~~" , r"\\quad " , string )
138
- return string
131
+ def _replace_double_nbsp (string : str ) -> str :
132
+ return re .sub ("~~" , r"\\quad " , string )
139
133
140
134
141
- def _replace_nbsp_space (string ) :
135
+ def _replace_nbsp_space (string : str ) -> str :
142
136
string = re .sub ("~ " , " " , string )
143
137
string = re .sub (" ~" , " " , string )
144
138
return string
145
139
146
140
147
- def _substitute_string_ranges (string , ranges , replacements ):
141
+ def _substitute_string_ranges (string : str , ranges , replacements ) -> str :
148
142
if ranges :
149
143
lst = [string [: ranges [0 ][0 ]]]
150
144
for k , replacement in enumerate (replacements [:- 1 ]):
@@ -154,7 +148,7 @@ def _substitute_string_ranges(string, ranges, replacements):
154
148
return string
155
149
156
150
157
- def _replace_over (string ) :
151
+ def _replace_over (string : str ) -> str :
158
152
p = re .compile (r"\\over[^a-z]" )
159
153
locations = [m .start () for m in p .finditer (string )]
160
154
@@ -210,11 +204,11 @@ def _replace_over(string):
210
204
return _substitute_string_ranges (string , ranges , fracs )
211
205
212
206
213
- def _add_linebreak_after_double_backslash (string ) :
207
+ def _add_linebreak_after_double_backslash (string : str ) -> str :
214
208
return re .sub (r"\\\\([^\n])" , r"\\\\\n\1" , string )
215
209
216
210
217
- def _add_backslash_for_keywords (string ) :
211
+ def _add_backslash_for_keywords (string : str ) -> str :
218
212
insert = []
219
213
for keyword in ["max" , "min" , "log" , "sin" , "cos" , "exp" ]:
220
214
p = re .compile (fr"[^A-Za-z]{ keyword } [^A-Za-z]" )
@@ -228,7 +222,7 @@ def _add_backslash_for_keywords(string):
228
222
)
229
223
230
224
231
- def _add_curly_brackets_around_round_brackets_with_exponent (string ) :
225
+ def _add_curly_brackets_around_round_brackets_with_exponent (string : str ) -> str :
232
226
p = re .compile (r"\)\^" )
233
227
locations = [m .start () for m in p .finditer (string )]
234
228
@@ -258,7 +252,7 @@ def _add_curly_brackets_around_round_brackets_with_exponent(string):
258
252
return _substitute_string_ranges (string , [(i , i ) for i in insert ], replacements )
259
253
260
254
261
- def _replace_def_by_newcommand (string ) :
255
+ def _replace_def_by_newcommand (string : str ) -> str :
262
256
p = re .compile (r"\\def\\[A-Za-z]+" )
263
257
264
258
ranges = []
@@ -270,7 +264,7 @@ def _replace_def_by_newcommand(string):
270
264
return _substitute_string_ranges (string , ranges , replacements )
271
265
272
266
273
- def _add_linebreak_around_begin_end (string ) :
267
+ def _add_linebreak_around_begin_end (string : str ) -> str :
274
268
string = re .sub (r"([^\n ]) *(\\begin{.*?})" , r"\1\n\2" , string )
275
269
string = re .sub (r"(\\begin{.*?}) *([^\n ])" , r"\1\n\2" , string )
276
270
@@ -285,38 +279,38 @@ def _add_linebreak_around_begin_end(string):
285
279
return string
286
280
287
281
288
- def _replace_centerline (string ) :
282
+ def _replace_centerline (string : str ) -> str :
289
283
return re .sub (r"\\centerline{" , r"{\\centering " , string )
290
284
291
285
292
- def _replace_eqnarray (string ) :
286
+ def _replace_eqnarray (string : str ) -> str :
293
287
return re .sub ("eqnarray" , "align" , string )
294
288
295
289
296
- def _put_spec_on_same_line_as_environment (string ) :
290
+ def _put_spec_on_same_line_as_environment (string : str ) -> str :
297
291
string = re .sub (r"(\\begin{.*?})\s*(\[.*?\])\n" , r"\1\2" , string )
298
292
string = re .sub (r"(\\begin{.*?})\s*(\[.*?\])([^\n])" , r"\1\2\n\3" , string )
299
293
return string
300
294
301
295
302
- def _put_label_on_same_line_as_environment (string ) :
296
+ def _put_label_on_same_line_as_environment (string : str ) -> str :
303
297
out = re .sub (r"(\\begin{.*?})(\[.*?])?\s+(\\label{.*?})(\n)?" , r"\1\2\3\4" , string )
304
298
out = re .sub (r"(\\section{.*?})\s+(\\label{.*?})(\n)?" , r"\1\2\3" , out )
305
299
out = re .sub (r"(\\subsection{.*?})\s+(\\label{.*?})(\n)?" , r"\1\2\3" , out )
306
300
return out
307
301
308
302
309
- def _replace_colon_equal_by_coloneqq (string ) :
303
+ def _replace_colon_equal_by_coloneqq (string : str ) -> str :
310
304
out = re .sub (r":\s*=" , r"\\coloneqq " , string )
311
305
out = re .sub (r"=\s*:" , r"\\eqqcolon " , out )
312
306
return out
313
307
314
308
315
- def _remove_space_before_tabular_column_specification (string ) :
309
+ def _remove_space_before_tabular_column_specification (string : str ) -> str :
316
310
return re .sub (r"(\\begin{tabular})\s*({.*?})" , r"\1\2" , string )
317
311
318
312
319
- def _add_spaces_around_equality_sign (string ) :
313
+ def _add_spaces_around_equality_sign (string : str ) -> str :
320
314
string = re .sub (r"([^\s&])=" , r"\1 =" , string )
321
315
string = re .sub (r"([^\s])&=" , r"\1 &=" , string )
322
316
@@ -325,21 +319,21 @@ def _add_spaces_around_equality_sign(string):
325
319
return string
326
320
327
321
328
- def _si_percentage (string ) :
322
+ def _si_percentage (string : str ) -> str :
329
323
# match float like https://stackoverflow.com/a/12643073/353337
330
324
string = re .sub (r"([+-]?([0-9]*[.])?[0-9]+)[ \t]*\\%" , r"\\SI{\1}{\%}" , string )
331
325
return string
332
326
333
327
334
- def clean (string , keep_comments = False , keep_dollar = False ):
328
+ def clean (string : str , keep_comments : bool = False , keep_dollar : bool = False ) -> str :
335
329
out = string
336
330
out = _remove_trailing_whitespace (out )
337
331
if not keep_comments :
338
332
out = _remove_comments (out )
339
- out = _replace_punctuation_outside_math (out )
340
333
out = _replace_dollar_dollar (out )
341
334
if not keep_dollar :
342
335
out = _replace_dollar (out )
336
+ out = _replace_punctuation_at_math_end (out )
343
337
out = _replace_obsolete_text_mods (out )
344
338
out = _remove_whitespace_around_brackets (out )
345
339
out = _add_space_after_single_subsuperscript (out )
0 commit comments