|
| 1 | +import os |
| 2 | +import re |
| 3 | + |
| 4 | +BRANCH_NAME = os.getenv("SAGEMAKER_BRANCH", "main") |
| 5 | + |
| 6 | + |
| 7 | +def process_readme_files(): |
| 8 | + print( |
| 9 | + "Processing README.md files generated from the Jupyter Notebooks in docs/sagemaker/notebooks/sagemaker-sdk..." |
| 10 | + ) |
| 11 | + os.makedirs("source/examples", exist_ok=True) |
| 12 | + |
| 13 | + # NOTE: at the moment only `sagemaker-sdk` but left here to easily include new files from other sources |
| 14 | + for dirname in {"sagemaker-sdk"}: |
| 15 | + for root, _, files in os.walk(f"notebooks/{dirname}"): |
| 16 | + for file in files: |
| 17 | + if file == "sagemaker-notebook.md": |
| 18 | + process_file(root, file, dirname) |
| 19 | + |
| 20 | + |
| 21 | +def process_file(root, file, dirname): |
| 22 | + parsed_dirname = ( |
| 23 | + dirname if not dirname.__contains__("/") else dirname.replace("/", "-") |
| 24 | + ) |
| 25 | + |
| 26 | + file_path = os.path.join(root, file) |
| 27 | + subdir = root.replace(f"notebooks/{dirname}/", "") |
| 28 | + base = os.path.basename(subdir) |
| 29 | + |
| 30 | + # NOTE: temporarily disabled |
| 31 | + if file_path == f"examples/{dirname}/README.md": |
| 32 | + target = f"source/examples/{parsed_dirname}-index.mdx" |
| 33 | + else: |
| 34 | + target = f"source/examples/{parsed_dirname}-{base}.mdx" |
| 35 | + |
| 36 | + print(f"Processing {file_path} to {target}") |
| 37 | + with open(file_path, "r") as f: |
| 38 | + content = f.read() |
| 39 | + |
| 40 | + # For Juypter Notebooks, remove the comment i.e. `<!--` and the `--!>` but keep the metadata |
| 41 | + content = re.sub(r"<!-- (.*?) -->", r"\1", content, flags=re.DOTALL) |
| 42 | + |
| 43 | + # Replace image and link paths |
| 44 | + content = re.sub( |
| 45 | + r"\(\./(imgs|assets)/([^)]*\.png)\)", |
| 46 | + rf"(https://raw.githubusercontent.com/huggingface/hub-docs/refs/heads/{BRANCH_NAME}/docs/sagemaker/" |
| 47 | + + root |
| 48 | + + r"/\1/\2)", |
| 49 | + content, |
| 50 | + ) |
| 51 | + content = re.sub( |
| 52 | + r"\(\.\./([^)]+)\)", |
| 53 | + rf"(https://github.com/huggingface/hub-docs/tree/{BRANCH_NAME}/docs/sagemaker/notebooks/" |
| 54 | + + dirname |
| 55 | + + r"/\1)", |
| 56 | + content, |
| 57 | + ) |
| 58 | + content = re.sub( |
| 59 | + r"\(\.\/([^)]+)\)", |
| 60 | + rf"(https://github.com/huggingface/hub-docs/tree/{BRANCH_NAME}/docs/sagemaker/" |
| 61 | + + root |
| 62 | + + r"/\1)", |
| 63 | + content, |
| 64 | + ) |
| 65 | + |
| 66 | + def replacement(match): |
| 67 | + block_type = match.group(1) |
| 68 | + content = match.group(2) |
| 69 | + |
| 70 | + # Remove '> ' from the beginning of each line |
| 71 | + lines = [line[2:] for line in content.split("\n") if line.strip()] |
| 72 | + |
| 73 | + # Determine the Tip type |
| 74 | + tip_type = " warning" if block_type == "WARNING" else "" |
| 75 | + |
| 76 | + # Construct the new block |
| 77 | + new_block = f"<Tip{tip_type}>\n\n" |
| 78 | + new_block += "\n".join(lines) |
| 79 | + new_block += "\n\n</Tip>\n" |
| 80 | + |
| 81 | + return new_block |
| 82 | + |
| 83 | + # Regular expression to match the specified blocks |
| 84 | + pattern = r"> \[!(NOTE|WARNING)\]\n((?:>.*(?:\n|$))+)" |
| 85 | + |
| 86 | + # Perform the transformation |
| 87 | + content = re.sub(pattern, replacement, content, flags=re.MULTILINE) |
| 88 | + |
| 89 | + # Remove any remaining '>' or '> ' at the beginning of lines |
| 90 | + content = re.sub(r"^>[ ]?", "", content, flags=re.MULTILINE) |
| 91 | + |
| 92 | + # Check for remaining relative paths |
| 93 | + if re.search(r"\(\.\./|\(\./", content): |
| 94 | + print("WARNING: Relative paths still exist in the processed file.") |
| 95 | + print( |
| 96 | + "The following lines contain relative paths, consider replacing those with GitHub URLs instead:" |
| 97 | + ) |
| 98 | + for i, line in enumerate(content.split("\n"), 1): |
| 99 | + if re.search(r"\(\.\./|\(\./", line): |
| 100 | + print(f"{i}: {line}") |
| 101 | + else: |
| 102 | + print("No relative paths found in the processed file.") |
| 103 | + |
| 104 | + # Calculate the example URL |
| 105 | + example_url = f"https://github.com/huggingface/hub-docs/tree/{BRANCH_NAME}/{root}" |
| 106 | + if file.__contains__("sagemaker-notebook"): |
| 107 | + example_url += "/sagemaker-notebook.ipynb" |
| 108 | + |
| 109 | + # Add the final note |
| 110 | + content += f"\n\n---\n<Tip>\n\n📍 Find the complete example on GitHub [here]({example_url})!\n\n</Tip>" |
| 111 | + |
| 112 | + with open(target, "w") as f: |
| 113 | + f.write(content) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + process_readme_files() |
0 commit comments