in src/doc_builder/build_doc.py [0:0]
def resolve_autodoc(content, package, return_anchors=False, page_info=None, version_tag_suffix="src/"):
"""
Replaces [[autodoc]] special syntax by the corresponding generated documentation in some content.
Args:
content (`str`): The documentation to treat.
package (`types.ModuleType`): The package where to look for objects to document.
return_anchors (`bool`, *optional*, defaults to `False`):
Whether or not to return the list of anchors generated.
page_info (`Dict[str, str]`, *optional*): Some information about the page.
version_tag_suffix (`str`, *optional*, defaults to `"src/"`):
Suffix to add after the version tag (e.g. 1.3.0 or main) in the documentation links.
For example, the default `"src/"` suffix will result in a base link as `https://github.com/huggingface/{package_name}/blob/{version_tag}/src/`.
For example, `version_tag_suffix=""` will result in a base link as `https://github.com/huggingface/{package_name}/blob/{version_tag}/`.
"""
idx_last_heading = None
is_inside_codeblock = False
lines = content.split("\n")
new_lines = []
source_files = None
if return_anchors:
anchors = []
errors = []
idx = 0
while idx < len(lines):
if _re_autodoc.search(lines[idx]) is not None:
object_name = _re_autodoc.search(lines[idx]).groups()[0]
autodoc_indent = find_indent(lines[idx])
idx += 1
while idx < len(lines) and is_empty_line(lines[idx]):
idx += 1
if (
idx < len(lines)
and find_indent(lines[idx]) > autodoc_indent
and _re_list_item.search(lines[idx]) is not None
):
methods = []
methods_indent = find_indent(lines[idx])
while is_empty_line(lines[idx]) or (
find_indent(lines[idx]) == methods_indent and _re_list_item.search(lines[idx]) is not None
):
if not is_empty_line(lines[idx]):
methods.append(_re_list_item.search(lines[idx]).groups()[0])
idx += 1
if idx >= len(lines):
break
else:
methods = None
doc = autodoc_svelte(
object_name,
package,
methods=methods,
return_anchors=return_anchors,
page_info=page_info,
version_tag_suffix=version_tag_suffix,
)
if return_anchors:
if len(doc[1]) and idx_last_heading is not None:
object_anchor = doc[1][0]
new_lines[idx_last_heading] += f"[[{object_anchor}]]"
idx_last_heading = None
anchors.extend(doc[1])
errors.extend(doc[2])
doc = doc[0]
new_lines.append(doc)
try:
source_files = source_files = get_source_path(object_name, package)
except (AttributeError, OSError, TypeError):
# tokenizers obj do NOT have `__module__` attribute & can NOT be used with inspect.getfile
source_files = None
else:
new_lines.append(lines[idx])
if lines[idx].startswith("```"):
is_inside_codeblock = not is_inside_codeblock
if lines[idx].startswith("#") and not is_inside_codeblock:
idx_last_heading = len(new_lines) - 1
idx += 1
new_content = "\n".join(new_lines)
return (new_content, anchors, source_files, errors) if return_anchors else new_content