Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions gapic/utils/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
from gapic.utils.lines import wrap


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(
text: str,
width: int = 72,
Expand Down Expand Up @@ -55,18 +62,12 @@ def rst(
width=width - indent,
)
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}")
)
# Fast path for snake_case and other simple symbols
if not re.search(r"[*`]|(?<!\w)_|_(?!\w)|\[.*\]\s*[(\[]|^\||^\s*([-+*]|\d+\.)\s+", text, re.M):
answer = wrap(re.sub(r"[ \t]+", " ", text).replace("|", "\\|"), indent=indent, width=width - indent)
else:
# Convert from CommonMark to ReStructured Text (cached).
answer = _p(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.
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/utils/test_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def test_rst_formatted():
assert convert_text.call_count == 1
assert convert_text.mock_calls[0][1][1] == "rst"
assert convert_text.mock_calls[0][2]["format"] == "commonmark"
assert convert_text.mock_calls[0][2]["verify_format"] is False


def test_rst_snake_case():
with mock.patch.object(pypandoc, "convert_text") as convert_text:
s = "some_variable_name"
assert utils.rst(s) == s
assert convert_text.call_count == 0


def test_rst_add_newline():
Expand Down
Loading