Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 47368b1

Browse files
committed
chore: cache pandoc rst conversion
1 parent 72db9d0 commit 47368b1

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

gapic/utils/rst.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,23 @@
1717

1818
import pypandoc # type: ignore
1919

20+
import functools
21+
2022
from gapic.utils.lines import wrap
2123

24+
@functools.lru_cache(maxsize=2048)
25+
def _convert_pandoc(text: str, columns: int, source_format: str) -> str:
26+
"""Cached helper to run pypandoc with specific column width.
27+
28+
Args:
29+
columns: Must be positive. Enforced by caller.
30+
"""
31+
return pypandoc.convert_text(text, 'rst',
32+
format=source_format,
33+
extra_args=['--columns=%d' % columns],
34+
).strip()
35+
36+
2237

2338
def rst(
2439
text: str,
@@ -56,17 +71,11 @@ def rst(
5671
)
5772
else:
5873
# Convert from CommonMark to ReStructured Text.
59-
answer = (
60-
pypandoc.convert_text(
61-
text,
62-
"rst",
63-
format=source_format,
64-
verify_format=False,
65-
extra_args=["--columns=%d" % (width - indent)],
66-
)
67-
.strip()
68-
.replace("\n", f"\n{' ' * indent}")
69-
)
74+
answer = _convert_pandoc(
75+
str(text),
76+
width - indent,
77+
str(source_format)
78+
).replace('\n', f"\n{' ' * indent}")
7079

7180
# Add a newline to the end of the document if any line breaks are
7281
# already present.

0 commit comments

Comments
 (0)