langchain/docs/scripts/copy_templates.py
Erick Friis cdc8e2d0c2
docs: resolve local links script escape (#21840)
Fixing warnings. Needs to be propagated to 0.1 branch if this works.

![Screenshot 2024-05-17 at 2 34
15 PM](https://github.com/langchain-ai/langchain/assets/9557659/e6ac95a9-5686-4747-9ab8-4cb49942dc8d)
2024-05-17 22:59:27 +00:00

46 lines
1.4 KiB
Python

import glob
import os
import re
import shutil
import sys
from pathlib import Path
if __name__ == "__main__":
intermediate_dir = Path(sys.argv[1])
templates_source_dir = Path(os.path.abspath(__file__)).parents[2] / "templates"
templates_intermediate_dir = intermediate_dir / "templates"
readmes = list(glob.glob(str(templates_source_dir) + "/*/README.md"))
destinations = [
readme[len(str(templates_source_dir)) + 1 : -10] + ".md" for readme in readmes
]
for source, destination in zip(readmes, destinations):
full_destination = templates_intermediate_dir / destination
shutil.copyfile(source, full_destination)
with open(full_destination, "r") as f:
content = f.read()
# remove images
content = re.sub(r"\!\[.*?\]\((.*?)\)", "", content)
with open(full_destination, "w") as f:
f.write(content)
sidebar_hidden = """---
sidebar_class_name: hidden
---
"""
# handle index file
templates_index_source = templates_source_dir / "docs" / "INDEX.md"
templates_index_intermediate = templates_intermediate_dir / "index.md"
with open(templates_index_source, "r") as f:
content = f.read()
# replace relative links
content = re.sub(r"\]\(\.\.\/", "](/docs/templates/", content)
with open(templates_index_intermediate, "w") as f:
f.write(sidebar_hidden + content)