From 5c9a10a895f9b9b8a6d120f6bfd7932af4ad77d2 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Wed, 28 Jan 2026 22:21:13 +0000 Subject: [PATCH 1/4] chore: cache pandoc rst conversion --- gapic/utils/rst.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/gapic/utils/rst.py b/gapic/utils/rst.py index dd1d65d75d..9956b6d9b0 100644 --- a/gapic/utils/rst.py +++ b/gapic/utils/rst.py @@ -17,8 +17,23 @@ import pypandoc # type: ignore +import functools + from gapic.utils.lines import wrap +@functools.lru_cache(maxsize=2048) +def _convert_pandoc(text: str, columns: int, source_format: str) -> str: + """Cached helper to run pypandoc with specific column width. + + Args: + columns: Must be positive. Enforced by caller. + """ + return pypandoc.convert_text(text, 'rst', + format=source_format, + extra_args=['--columns=%d' % columns], + ).strip() + + def rst( text: str, @@ -56,17 +71,11 @@ def rst( ) else: # Convert from CommonMark to ReStructured Text. - answer = ( - pypandoc.convert_text( - text, - "rst", - format=source_format, - verify_format=False, - extra_args=["--columns=%d" % (width - indent)], - ) - .strip() - .replace("\n", f"\n{' ' * indent}") - ) + answer = _convert_pandoc( + str(text), + width - indent, + str(source_format) + ).replace('\n', f"\n{' ' * indent}") # Add a newline to the end of the document if any line breaks are # already present. From 6af16c1529e4a78a5475c848d591f974edd49cc2 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Thu, 29 Jan 2026 01:03:54 +0000 Subject: [PATCH 2/4] wip --- gapic/utils/rst.py | 5 +++-- tests/unit/utils/test_rst.py | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/gapic/utils/rst.py b/gapic/utils/rst.py index 9956b6d9b0..828902098b 100644 --- a/gapic/utils/rst.py +++ b/gapic/utils/rst.py @@ -21,7 +21,7 @@ from gapic.utils.lines import wrap -@functools.lru_cache(maxsize=2048) +@functools.lru_cache(maxsize=None) def _convert_pandoc(text: str, columns: int, source_format: str) -> str: """Cached helper to run pypandoc with specific column width. @@ -31,6 +31,7 @@ def _convert_pandoc(text: str, columns: int, source_format: str) -> str: return pypandoc.convert_text(text, 'rst', format=source_format, extra_args=['--columns=%d' % columns], + verify_format=False, ).strip() @@ -62,7 +63,7 @@ def rst( # do not convert it. # (This makes code generation significantly faster; calling out to pandoc # is by far the most expensive thing we do.) - if not re.search(r"[|*`_[\]]", text): + if not re.search(r"(? Date: Thu, 29 Jan 2026 21:57:55 +0000 Subject: [PATCH 3/4] wip --- gapic/utils/rst.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/gapic/utils/rst.py b/gapic/utils/rst.py index 828902098b..d1eec2e87a 100644 --- a/gapic/utils/rst.py +++ b/gapic/utils/rst.py @@ -63,7 +63,7 @@ def rst( # do not convert it. # (This makes code generation significantly faster; calling out to pandoc # is by far the most expensive thing we do.) - if not re.search(r"(? Date: Thu, 29 Jan 2026 23:30:02 +0000 Subject: [PATCH 4/4] wip --- gapic/utils/rst.py | 46 ++++++++++------------------------------------ 1 file changed, 10 insertions(+), 36 deletions(-) diff --git a/gapic/utils/rst.py b/gapic/utils/rst.py index d1eec2e87a..0cac3e09f2 100644 --- a/gapic/utils/rst.py +++ b/gapic/utils/rst.py @@ -17,23 +17,14 @@ import pypandoc # type: ignore -import functools - from gapic.utils.lines import wrap -@functools.lru_cache(maxsize=None) -def _convert_pandoc(text: str, columns: int, source_format: str) -> str: - """Cached helper to run pypandoc with specific column width. - - Args: - columns: Must be positive. Enforced by caller. - """ - return pypandoc.convert_text(text, 'rst', - format=source_format, - extra_args=['--columns=%d' % columns], - verify_format=False, - ).strip() +import functools + +@functools.lru_cache(maxsize=None) +def _p(t, c, f): + return pypandoc.convert_text(t, "rst", format=f, verify_format=False, extra_args=["--columns=%d" % c]).strip() def rst( @@ -71,29 +62,12 @@ def rst( width=width - indent, ) else: - # Check if snake_case-only AND NOT a list. - # This regex excludes underscores surrounded by word characters, meaning it finds "real" formatting. - has_real_formatting = re.search(r"(?