Skip to content

Commit 97a90bd

Browse files
authored
Merge pull request #44 from nschloe/math-punctuation
Math punctuation
2 parents 74eb11a + 6bb1219 commit 97a90bd

File tree

7 files changed

+46
-47
lines changed

7 files changed

+46
-47
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
strategy:
2626
matrix:
2727
os: [ubuntu-latest]
28-
python-version: [3.6, 3.7, 3.8, 3.9]
28+
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]
2929
include:
3030
- python-version: 3.8
3131
os: windows-latest

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ build/
99
.cache/
1010
*.egg-info/
1111
.pytest_cache/
12+
.tox/

.pre-commit-config.yaml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
repos:
22
- repo: https://github.com/PyCQA/isort
3-
rev: 5.9.1
3+
rev: 5.9.3
44
hooks:
55
- id: isort
66

7-
- repo: https://github.com/python/black
8-
rev: 21.6b0
7+
- repo: https://github.com/psf/black
8+
rev: 21.9b0
99
hooks:
1010
- id: black
1111
language_version: python3
1212

13-
- repo: https://gitlab.com/pycqa/flake8
14-
rev: 3.9.2
13+
- repo: https://github.com/PyCQA/flake8
14+
rev: 4.0.1
1515
hooks:
1616
- id: flake8

setup.cfg

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = blacktex
3-
version = 0.5.1
3+
version = 0.5.2
44
author = Nico Schlömer
55
author_email = [email protected]
66
description = Cleans up your LaTeX files
@@ -22,6 +22,7 @@ classifiers =
2222
Programming Language :: Python :: 3.7
2323
Programming Language :: Python :: 3.8
2424
Programming Language :: Python :: 3.9
25+
Programming Language :: Python :: 3.10
2526
Topic :: Text Editors :: Text Processing
2627
Topic :: Text Processing
2728
Topic :: Text Processing :: Markup

src/blacktex/main.py

+34-40
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import warnings
33

44

5-
def _remove_comments(string):
5+
def _remove_comments(string: str) -> str:
66
"""Remove comments unless the comment character is the last non-whitespace character
77
in a line. (This is often used in macros etc.)
88
"""
@@ -23,21 +23,21 @@ def _remove_comments(string):
2323
return string
2424

2525

26-
def _remove_trailing_whitespace(string):
26+
def _remove_trailing_whitespace(string: str) -> str:
2727
return "\n".join([line.rstrip() for line in string.split("\n")])
2828

2929

30-
def _remove_multiple_spaces(string):
30+
def _remove_multiple_spaces(string: str) -> str:
3131
"""Replaces multiple spaces by one, except after a newline."""
3232
return re.sub("([^\n ]) +", r"\1 ", string)
3333

3434

35-
def _remove_multiple_newlines(string):
35+
def _remove_multiple_newlines(string: str) -> str:
3636
string = re.sub("\n\n\n\n+", "\n\n\n", string)
3737
return string
3838

3939

40-
def _remove_whitespace_around_brackets(string):
40+
def _remove_whitespace_around_brackets(string: str) -> str:
4141
string = re.sub("{[ \t]+", "{", string)
4242
string = re.sub("[ \t]+}", "}", string)
4343
string = re.sub("\\([ \t]+", "(", string)
@@ -46,7 +46,7 @@ def _remove_whitespace_around_brackets(string):
4646
return string
4747

4848

49-
def _replace_dollar_dollar(string):
49+
def _replace_dollar_dollar(string: str) -> str:
5050
"""Replace $$...$$ by \\[...\\]."""
5151
p = re.compile(r"\$\$")
5252
locations = [m.start() for m in p.finditer(string)]
@@ -63,7 +63,7 @@ def _replace_dollar_dollar(string):
6363
return _substitute_string_ranges(string, ranges, replacements)
6464

6565

66-
def _replace_dollar(string):
66+
def _replace_dollar(string: str) -> str:
6767
"""Replace $...$ by \\(...\\). See <https://tex.stackexchange.com/q/510/13262>."""
6868
# (?<!\\\\) checks there is no backslash before (negative lookbehind)
6969
# (?:\\\\{2})* matches all even numbers of backslashes
@@ -82,7 +82,7 @@ def _replace_dollar(string):
8282
return _substitute_string_ranges(string, ranges, replacements)
8383

8484

85-
def _replace_obsolete_text_mods(string):
85+
def _replace_obsolete_text_mods(string: str) -> str:
8686
string = string.replace("{\\bf ", "\\textbf{")
8787
string = string.replace("{\\it ", "\\textit{")
8888
string = string.replace("{\\rm ", "\\textrm{")
@@ -97,27 +97,22 @@ def _replace_obsolete_text_mods(string):
9797
return string
9898

9999

100-
def _add_space_after_single_subsuperscript(string):
100+
def _add_space_after_single_subsuperscript(string: str) -> str:
101101
string = re.sub(r"([\^])([^{\\])([^_\^\s\$})])", r"\1\2 \3", string)
102102
return string
103103

104104

105-
def _replace_dots(string):
105+
def _replace_dots(string: str) -> str:
106106
string = re.sub(r"\.\.\.", r"\\dots", string)
107107
string = re.sub(r",\\cdots,", r",\\dots,", string)
108108
return string
109109

110110

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)
118113

119114

120-
def _remove_whitespace_before_punctuation(string):
115+
def _remove_whitespace_before_punctuation(string: str) -> str:
121116
string = re.sub(r"\s+\.", ".", string)
122117
string = re.sub(r"\s+,", ",", string)
123118
string = re.sub(r"\s+;", ";", string)
@@ -126,25 +121,24 @@ def _remove_whitespace_before_punctuation(string):
126121
return string
127122

128123

129-
def _add_nbsp_before_reference(string):
124+
def _add_nbsp_before_reference(string: str) -> str:
130125
string = re.sub(r"\s+\\ref{", r"~\\ref{", string)
131126
string = re.sub(r"\s+\\eqref{", r"~\\eqref{", string)
132127
string = re.sub(r"\s+\\cite", r"~\\cite", string)
133128
return string
134129

135130

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)
139133

140134

141-
def _replace_nbsp_space(string):
135+
def _replace_nbsp_space(string: str) -> str:
142136
string = re.sub("~ ", " ", string)
143137
string = re.sub(" ~", " ", string)
144138
return string
145139

146140

147-
def _substitute_string_ranges(string, ranges, replacements):
141+
def _substitute_string_ranges(string: str, ranges, replacements) -> str:
148142
if ranges:
149143
lst = [string[: ranges[0][0]]]
150144
for k, replacement in enumerate(replacements[:-1]):
@@ -154,7 +148,7 @@ def _substitute_string_ranges(string, ranges, replacements):
154148
return string
155149

156150

157-
def _replace_over(string):
151+
def _replace_over(string: str) -> str:
158152
p = re.compile(r"\\over[^a-z]")
159153
locations = [m.start() for m in p.finditer(string)]
160154

@@ -210,11 +204,11 @@ def _replace_over(string):
210204
return _substitute_string_ranges(string, ranges, fracs)
211205

212206

213-
def _add_linebreak_after_double_backslash(string):
207+
def _add_linebreak_after_double_backslash(string: str) -> str:
214208
return re.sub(r"\\\\([^\n])", r"\\\\\n\1", string)
215209

216210

217-
def _add_backslash_for_keywords(string):
211+
def _add_backslash_for_keywords(string: str) -> str:
218212
insert = []
219213
for keyword in ["max", "min", "log", "sin", "cos", "exp"]:
220214
p = re.compile(fr"[^A-Za-z]{keyword}[^A-Za-z]")
@@ -228,7 +222,7 @@ def _add_backslash_for_keywords(string):
228222
)
229223

230224

231-
def _add_curly_brackets_around_round_brackets_with_exponent(string):
225+
def _add_curly_brackets_around_round_brackets_with_exponent(string: str) -> str:
232226
p = re.compile(r"\)\^")
233227
locations = [m.start() for m in p.finditer(string)]
234228

@@ -258,7 +252,7 @@ def _add_curly_brackets_around_round_brackets_with_exponent(string):
258252
return _substitute_string_ranges(string, [(i, i) for i in insert], replacements)
259253

260254

261-
def _replace_def_by_newcommand(string):
255+
def _replace_def_by_newcommand(string: str) -> str:
262256
p = re.compile(r"\\def\\[A-Za-z]+")
263257

264258
ranges = []
@@ -270,7 +264,7 @@ def _replace_def_by_newcommand(string):
270264
return _substitute_string_ranges(string, ranges, replacements)
271265

272266

273-
def _add_linebreak_around_begin_end(string):
267+
def _add_linebreak_around_begin_end(string: str) -> str:
274268
string = re.sub(r"([^\n ]) *(\\begin{.*?})", r"\1\n\2", string)
275269
string = re.sub(r"(\\begin{.*?}) *([^\n ])", r"\1\n\2", string)
276270

@@ -285,38 +279,38 @@ def _add_linebreak_around_begin_end(string):
285279
return string
286280

287281

288-
def _replace_centerline(string):
282+
def _replace_centerline(string: str) -> str:
289283
return re.sub(r"\\centerline{", r"{\\centering ", string)
290284

291285

292-
def _replace_eqnarray(string):
286+
def _replace_eqnarray(string: str) -> str:
293287
return re.sub("eqnarray", "align", string)
294288

295289

296-
def _put_spec_on_same_line_as_environment(string):
290+
def _put_spec_on_same_line_as_environment(string: str) -> str:
297291
string = re.sub(r"(\\begin{.*?})\s*(\[.*?\])\n", r"\1\2", string)
298292
string = re.sub(r"(\\begin{.*?})\s*(\[.*?\])([^\n])", r"\1\2\n\3", string)
299293
return string
300294

301295

302-
def _put_label_on_same_line_as_environment(string):
296+
def _put_label_on_same_line_as_environment(string: str) -> str:
303297
out = re.sub(r"(\\begin{.*?})(\[.*?])?\s+(\\label{.*?})(\n)?", r"\1\2\3\4", string)
304298
out = re.sub(r"(\\section{.*?})\s+(\\label{.*?})(\n)?", r"\1\2\3", out)
305299
out = re.sub(r"(\\subsection{.*?})\s+(\\label{.*?})(\n)?", r"\1\2\3", out)
306300
return out
307301

308302

309-
def _replace_colon_equal_by_coloneqq(string):
303+
def _replace_colon_equal_by_coloneqq(string: str) -> str:
310304
out = re.sub(r":\s*=", r"\\coloneqq ", string)
311305
out = re.sub(r"=\s*:", r"\\eqqcolon ", out)
312306
return out
313307

314308

315-
def _remove_space_before_tabular_column_specification(string):
309+
def _remove_space_before_tabular_column_specification(string: str) -> str:
316310
return re.sub(r"(\\begin{tabular})\s*({.*?})", r"\1\2", string)
317311

318312

319-
def _add_spaces_around_equality_sign(string):
313+
def _add_spaces_around_equality_sign(string: str) -> str:
320314
string = re.sub(r"([^\s&])=", r"\1 =", string)
321315
string = re.sub(r"([^\s])&=", r"\1 &=", string)
322316

@@ -325,21 +319,21 @@ def _add_spaces_around_equality_sign(string):
325319
return string
326320

327321

328-
def _si_percentage(string):
322+
def _si_percentage(string: str) -> str:
329323
# match float like https://stackoverflow.com/a/12643073/353337
330324
string = re.sub(r"([+-]?([0-9]*[.])?[0-9]+)[ \t]*\\%", r"\\SI{\1}{\%}", string)
331325
return string
332326

333327

334-
def clean(string, keep_comments=False, keep_dollar=False):
328+
def clean(string: str, keep_comments: bool = False, keep_dollar: bool = False) -> str:
335329
out = string
336330
out = _remove_trailing_whitespace(out)
337331
if not keep_comments:
338332
out = _remove_comments(out)
339-
out = _replace_punctuation_outside_math(out)
340333
out = _replace_dollar_dollar(out)
341334
if not keep_dollar:
342335
out = _replace_dollar(out)
336+
out = _replace_punctuation_at_math_end(out)
343337
out = _replace_obsolete_text_mods(out)
344338
out = _remove_whitespace_around_brackets(out)
345339
out = _add_space_after_single_subsuperscript(out)

test/test_blacktex.py tests/test_blacktex.py

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ def test_readme():
7171
("a,\\cdots,b", "a,\\dots,b"),
7272
# punctuation outside math:
7373
("$a+b.$", "\\(a+b\\)."),
74+
(".$a+b$", ".\\(a+b\\)"),
75+
# <https://github.com/nschloe/blacktex/issues/43>
76+
(r"$a$\,$b$", r"\(a\)\,\(b\)"),
7477
# whitespace before punctuation:
7578
("Some text .", "Some text."),
7679
# nbsp before ref:

test/test_cli.py tests/test_cli.py

File renamed without changes.

0 commit comments

Comments
 (0)