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

Commit e5efb61

Browse files
committed
wip
1 parent 9b48d93 commit e5efb61

File tree

1 file changed

+10
-36
lines changed

1 file changed

+10
-36
lines changed

gapic/utils/rst.py

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,14 @@
1717

1818
import pypandoc # type: ignore
1919

20-
import functools
21-
2220
from gapic.utils.lines import wrap
2321

24-
@functools.lru_cache(maxsize=None)
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-
verify_format=False,
35-
).strip()
3622

23+
import functools
24+
25+
@functools.lru_cache(maxsize=None)
26+
def _p(t, c, f):
27+
return pypandoc.convert_text(t, "rst", format=f, verify_format=False, extra_args=["--columns=%d" % c]).strip()
3728

3829

3930
def rst(
@@ -71,29 +62,12 @@ def rst(
7162
width=width - indent,
7263
)
7364
else:
74-
# Check if snake_case-only AND NOT a list.
75-
# This regex excludes underscores surrounded by word characters, meaning it finds "real" formatting.
76-
has_real_formatting = re.search(r"(?<!\w)_|_(?!\w)|[|*`\[\]]", text)
77-
is_list = re.search(r"^\s*([-+*]|\d+\.)\s+", text, re.MULTILINE)
78-
79-
if not has_real_formatting and not is_list:
80-
# Mimic Pandoc behavior for plain text with snake_case:
81-
# 1. Collapse multiple spaces (Markdown behavior)
82-
text_normalized = re.sub(r'[ \t]+', ' ', text)
83-
# 2. Wrap without the specific 'docstring first line' offset
84-
answer = wrap(
85-
text_normalized,
86-
indent=indent,
87-
offset=0,
88-
width=width - indent,
89-
)
65+
# Fast path for snake_case and other simple symbols
66+
if not re.search(r"[*`]|(?<!\w)_|_(?!\w)|\[.*\]\s*[(\[]|^\||^\s*([-+*]|\d+\.)\s+", text, re.M):
67+
answer = wrap(re.sub(r"[ \t]+", " ", text).replace("|", "\\|"), indent=indent, width=width - indent)
9068
else:
91-
# Convert from CommonMark to ReStructured Text.
92-
answer = _convert_pandoc(
93-
str(text),
94-
width - indent,
95-
str(source_format)
96-
).replace('\n', f"\n{' ' * indent}")
69+
# Convert from CommonMark to ReStructured Text (cached).
70+
answer = _p(str(text), width - indent, str(source_format)).replace("\n", f"\n{' ' * indent}")
9771

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

0 commit comments

Comments
 (0)