docs: fix api ref link autogeneration (#20766)

This commit is contained in:
Bagatur 2024-04-22 17:36:41 -07:00 committed by GitHub
parent c807f0a6dd
commit be51cd3bc9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -13,10 +13,10 @@ logger = logging.getLogger(__name__)
_BASE_URL = "https://api.python.langchain.com/en/latest/" _BASE_URL = "https://api.python.langchain.com/en/latest/"
# Regular expression to match Python code blocks # Regular expression to match Python code blocks
code_block_re = re.compile(r"^(```python\n)(.*?)(```\n)", re.DOTALL | re.MULTILINE) code_block_re = re.compile(r"^(```\s?python\n)(.*?)(```)", re.DOTALL | re.MULTILINE)
# Regular expression to match langchain import lines # Regular expression to match langchain import lines
_IMPORT_RE = re.compile( _IMPORT_RE = re.compile(
r"from\s+(langchain(?:_\w+)?\.\w+(?:\.\w+)*?)\s+import\s+" r"from\s+(langchain(?:_\w+)?(?:\.\w+)*?)\s+import\s+"
r"((?:\w+(?:,\s*)?)*" # Match zero or more words separated by a comma+optional ws r"((?:\w+(?:,\s*)?)*" # Match zero or more words separated by a comma+optional ws
r"(?:\s*\(.*?\))?)", # Match optional parentheses block r"(?:\s*\(.*?\))?)", # Match optional parentheses block
re.DOTALL, # Match newlines as well re.DOTALL, # Match newlines as well
@ -64,7 +64,6 @@ def main():
global_imports = {} global_imports = {}
for file in find_files(args.docs_dir): for file in find_files(args.docs_dir):
print(f"Adding links for imports in {file}") # noqa: T201
file_imports = replace_imports(file) file_imports = replace_imports(file)
if file_imports: if file_imports:
@ -179,6 +178,8 @@ def replace_imports(file):
# Use re.sub to replace each Python code block # Use re.sub to replace each Python code block
data = code_block_re.sub(replacer, data) data = code_block_re.sub(replacer, data)
if all_imports:
print(f"Adding {len(all_imports)} links for imports in {file}") # noqa: T201
with open(file, "w") as f: with open(file, "w") as f:
f.write(data) f.write(data)
return all_imports return all_imports