Skip to content

Commit

Permalink
Adding function to fix quotation marks (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus authored Jul 7, 2023
1 parent 35dd340 commit 1becc01
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,34 @@ def test_remove_command_d(self):
tex.replace_command(r"{\TG}[1]", "#1", ignore_commented=True)
self.assertEqual(expect.strip(), str(tex).strip())

def test_fix_quote(self):
test = []
test.append(['This is text "with quotes".', "This is text ``with quotes''."])
test.append(
[
'This is text "with quotes" but not matching".',
'This is text "with quotes" but not matching".',
]
)
test.append(["This is text 'with quotes'.", "This is text `with quotes'."])
test.append(
[
"This is text 'with quotes' but not matching'.",
"This is text 'with quotes' but not matching'.",
]
)
test.append(
[
'A "slipping load" is confusing, we replaced it with "frictional strength".',
"A ``slipping load'' is confusing, we replaced it with ``frictional strength''.",
]
)

for source, expect in test:
tex = texplain.TeX(text=source)
tex.fix_quotes().fix_quotes()
self.assertEqual(expect.strip(), str(tex).strip())


if __name__ == "__main__":
unittest.main()
47 changes: 47 additions & 0 deletions texplain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,43 @@ def use_cleveref(self):
re.IGNORECASE,
)

def fix_quotes(self):
r"""
Replace:
* ``"..."`` by ``\`\`...''``.
* ``'...'`` by ``\`...'``.
"""
lines = self.main.splitlines()

for replace, (opening, closing) in zip(['"', "'"], [("``", "''"), ("`", "'")]):
pattern = rf"(.*)({re.escape(replace)})(.*)({re.escape(replace)})(.*)"
for i in range(len(lines)):
if re.match(pattern, lines[i]):
try:
match = np.sort(
find_matching(lines[i], replace, replace, return_array=True)[:, 0]
)
except IndexError:
continue
if len(match) % 2 != 0:
continue
match = match.reshape(-1, 2)
match = match[np.diff(match, axis=1).ravel() > 1]
if match.size == 0:
continue
match = np.vstack((match, [-1, -1]))
ret = lines[i][: match[0, 0]]
for j in range(match.shape[0] - 1):
a = match[j, 0]
b = match[j, 1]
c = match[j + 1, 0]
ret += opening + lines[i][a + 1 : b] + closing + lines[i][b + 1 : c]
lines[i] = ret + lines[i][-1]

self.main = "\n".join(lines)
return self


def bib_select(text: str, keys: list[str]) -> str:
r"""
Expand Down Expand Up @@ -2632,6 +2669,13 @@ def _texcleanup_parser():
help=r'Change "Fig.~\ref{...}" etc. to "\\cref{...}".',
)

parser.add_argument(
"-a",
"--fix-quotes",
action="store_true",
help="Fix non-LaTeX quotation marks: \"...\" -> ``...''",
)

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 @@ -2676,6 +2720,9 @@ def texcleanup(args: list[str]):
if args.use_cleveref:
tex.use_cleveref()

if args.fix_quotes:
tex.fix_quotes()

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

0 comments on commit 1becc01

Please sign in to comment.