Skip to content

Commit

Permalink
texcleanup: adding option --re-sub
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus committed Feb 2, 2024
1 parent 518ea85 commit 2add51b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/test_cli_texcleanup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import texplain


def test_regex(tmp_path):
text = r"""
This is some {\it italic text} that needes formatting.
"""

formatted = r"""
This is some \emph{italic text} that needes formatting.
"""

fpath = tmp_path / "test.tex"
fpath.write_text(text.strip())

texplain.texcleanup(["--re-sub", r"{\\it\s+(.*)}", r"\\emph{\1}", str(fpath)])
assert fpath.read_text().strip() == formatted.strip()
12 changes: 12 additions & 0 deletions texplain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2751,6 +2751,15 @@ def _texcleanup_parser():
help="Fix non-LaTeX quotation marks: \"...\" -> ``...''",
)

parser.add_argument(
"--re-sub",
nargs=2,
type=str,
action="append",
metavar=("pattern", "repl"),
help="Apply ``re.sub(pattern, repl, text)``.",
)

parser.add_argument("-v", "--version", action="version", version=version)
parser.add_argument("files", nargs="+", type=str, help="TeX file(s) (changed in-place).")

Expand Down Expand Up @@ -2798,6 +2807,9 @@ def texcleanup(args: list[str]):
if args.fix_quotes:
tex.fix_quotes()

for pattern, repl in args.re_sub:
tex.main = re.sub(pattern, repl, tex.main)

if tex.changed():
with open(file, "w") as file:
file.write(str(tex))
Expand Down

0 comments on commit 2add51b

Please sign in to comment.