Skip to content

Commit 7ab4cba

Browse files
authored
fix: ignore cross reference conversion in examples (#332)
* fix: ignore cross reference conversion in examples * test: update unit test
1 parent 9a2e2e7 commit 7ab4cba

2 files changed

Lines changed: 50 additions & 5 deletions

File tree

packages/gcp-sphinx-docfx-yaml/docfx_yaml/extension.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,14 @@ def find_uid_to_convert(
13951395
return None
13961396

13971397

1398-
def convert_cross_references(content: str, current_object_name: str, known_uids: List[str]) -> str:
1398+
# TODO(https://github.com/googleapis/sphinx-docfx-yaml/issues/331): Improve
1399+
# converting cross references for code content.
1400+
def convert_cross_references(
1401+
content: str,
1402+
current_object_name: str,
1403+
known_uids: List[str],
1404+
ignore_examples: Optional[bool] = False,
1405+
) -> str:
13991406
"""Finds and replaces references that should be a cross reference in given content.
14001407
14011408
This should not convert any references that contain `current_object_name`,
@@ -1407,10 +1414,13 @@ def convert_cross_references(content: str, current_object_name: str, known_uids:
14071414
content: body of content to parse and look for references in
14081415
current_object_name: the name of the current Python object being processed
14091416
known_uids: list of uid references to look for
1417+
ignore_examples: Don't convert references in example content
1418+
if set to True. False by default.
14101419
14111420
Returns:
14121421
content that has been modified with proper cross references if found.
14131422
"""
1423+
example_text = "Examples:"
14141424
words = content.split(" ")
14151425

14161426
# Contains a list of words that is not a valid reference or converted
@@ -1428,19 +1438,26 @@ def convert_cross_references(content: str, current_object_name: str, known_uids:
14281438
}
14291439
known_uids.extend(hard_coded_references.keys())
14301440

1441+
# Used to keep track of current position to avoid converting if needed.
1442+
example_index = len(content)
14311443
for index, word in enumerate(words):
1444+
if ignore_examples and example_text in word:
1445+
example_index = index
14321446
uid = find_uid_to_convert(
14331447
word, words, index, known_uids, current_object_name, processed_words, hard_coded_references
14341448
)
14351449

1436-
if uid:
1450+
# If the reference is found after example section, ignore it.
1451+
if uid and (
1452+
not ignore_examples or
1453+
(ignore_examples and index < example_index)
1454+
):
14371455
cross_reference = f"<a href=\"{hard_coded_references[uid]}\">{uid}</a>" \
14381456
if uid in hard_coded_references else \
14391457
f"<xref uid=\"{uid}\">{uid}</xref>"
14401458

14411459
processed_words.append(word.replace(uid, cross_reference))
14421460
print(f"Converted {uid} into cross reference in: \n{content}")
1443-
14441461
else:
14451462
# If cross reference has not been found, add current unchanged content.
14461463
processed_words.append(word)
@@ -1452,7 +1469,12 @@ def convert_cross_references(content: str, current_object_name: str, known_uids:
14521469
# For now, we inspect summary, syntax and attributes.
14531470
def search_cross_references(obj, current_object_name: str, known_uids: List[str]):
14541471
if obj.get("summary"):
1455-
obj["summary"] = convert_cross_references(obj["summary"], current_object_name, known_uids)
1472+
obj["summary"] = convert_cross_references(
1473+
obj["summary"],
1474+
current_object_name,
1475+
known_uids,
1476+
ignore_examples=True,
1477+
)
14561478

14571479
if obj.get("syntax"):
14581480
if obj["syntax"].get("parameters"):

packages/gcp-sphinx-docfx-yaml/tests/test_helpers.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def test_extract_keyword(self):
119119
[
120120
"google.iam.v1.iam_policy_pb2.GetIamPolicyRequest",
121121
"<a href=\"http://github.com/googleapis/python-grpc-google-iam-v1/blob/8e73b45993f030f521c0169b380d0fbafe66630b/google/iam/v1/iam_policy_pb2_grpc.py#L111-L118\">google.iam.v1.iam_policy_pb2.GetIamPolicyRequest</a>"
122-
]
122+
],
123123
]
124124
@parameterized.expand(cross_references_testdata)
125125
def test_convert_cross_references(self, content, content_want):
@@ -133,6 +133,29 @@ def test_convert_cross_references(self, content, content_want):
133133
content_got = extension.convert_cross_references(content, current_object_name, keyword_map)
134134
self.assertEqual(content_got, content_want)
135135

136+
cross_references_test_data = [
137+
[
138+
"""
139+
Examples:
140+
google.cloud.bigquery_storage_v1.types.SplitReadStreamResponse: test content.
141+
""",
142+
"""
143+
Examples:
144+
google.cloud.bigquery_storage_v1.types.SplitReadStreamResponse: test content.
145+
""",
146+
],
147+
]
148+
@parameterized.expand(cross_references_testdata)
149+
def test_does_not_convert_for_examples(self, content, content_want):
150+
# Check that entries correctly turns into cross references.
151+
keyword_map = [
152+
"google.cloud.bigquery_storage_v1.types.SplitReadStreamResponse",
153+
"google.cloud.bigquery_storage_v1.types.SplitResponse"
154+
]
155+
current_object_name = "google.cloud.bigquery_storage_v1.types.SplitResponse"
156+
157+
content_got = extension.convert_cross_references(content, current_object_name, keyword_map, ignore_examples=True)
158+
self.assertEqual(content_got, content_want)
136159

137160
# Test data used to test for processing already-processed cross references.
138161
cross_references_short_testdata = [

0 commit comments

Comments
 (0)