@@ -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.
14531470def 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" ):
0 commit comments