|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import pathlib |
| 17 | +import shutil |
| 18 | +import sys |
| 19 | +import yaml |
| 20 | +import pypandoc |
| 21 | + |
| 22 | +def build_docfx(current_dir, repo_root, docs_map): |
| 23 | + current_dir = pathlib.Path(current_dir) |
| 24 | + repo_root = pathlib.Path(repo_root) |
| 25 | + output_dir = current_dir / "docs" / "_build" |
| 26 | + |
| 27 | + if output_dir.exists(): |
| 28 | + shutil.rmtree(output_dir) |
| 29 | + output_dir.mkdir(parents=True) |
| 30 | + |
| 31 | + # Ensure pandoc is available (pypandoc will download it if not found in PATH) |
| 32 | + try: |
| 33 | + pypandoc.get_pandoc_version() |
| 34 | + except OSError: |
| 35 | + print("Pandoc not found. Downloading...") |
| 36 | + pypandoc.download_pandoc() |
| 37 | + |
| 38 | + toc = [] |
| 39 | + |
| 40 | + for title, source in docs_map.items(): |
| 41 | + source_path = pathlib.Path(source) |
| 42 | + if not source_path.is_absolute(): |
| 43 | + source_path = current_dir / source_path |
| 44 | + |
| 45 | + filename = source_path.name |
| 46 | + |
| 47 | + if filename.endswith(".rst"): |
| 48 | + target_filename = filename.replace(".rst", ".md") |
| 49 | + print(f"Converting {filename} -> {target_filename} using pandoc") |
| 50 | + if source_path.exists(): |
| 51 | + # Use pandoc to convert RST to GFM (GitHub Flavored Markdown) |
| 52 | + output = pypandoc.convert_file( |
| 53 | + str(source_path), |
| 54 | + 'gfm', |
| 55 | + format='rst' |
| 56 | + ) |
| 57 | + (output_dir / target_filename).write_text(output) |
| 58 | + else: |
| 59 | + print(f"Warning: Source {source_path} not found.") |
| 60 | + (output_dir / target_filename).write_text(f"# {title}\n\nContent missing.") |
| 61 | + href = target_filename |
| 62 | + else: |
| 63 | + print(f"Copying {filename}") |
| 64 | + if source_path.exists(): |
| 65 | + shutil.copy(source_path, output_dir / filename) |
| 66 | + else: |
| 67 | + print(f"Warning: Source {source_path} not found.") |
| 68 | + (output_dir / filename).write_text(f"# {title}\n\nContent missing.") |
| 69 | + href = filename |
| 70 | + |
| 71 | + toc.append({"name": title, "href": href}) |
| 72 | + |
| 73 | + # Write toc.yaml |
| 74 | + toc_path = output_dir / "toc.yaml" |
| 75 | + with open(toc_path, "w") as f: |
| 76 | + yaml.dump(toc, f, default_flow_style=False) |
| 77 | + |
| 78 | + print(f"DocFX build complete in {output_dir}") |
| 79 | + print(f"Generated TOC: {toc}") |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + # Simple argument parsing: current_dir, repo_root, then pairs of Title,Source |
| 83 | + curr = sys.argv[1] |
| 84 | + root = sys.argv[2] |
| 85 | + d_map = {} |
| 86 | + for i in range(3, len(sys.argv), 2): |
| 87 | + d_map[sys.argv[i]] = sys.argv[i+1] |
| 88 | + |
| 89 | + build_docfx(curr, root, d_map) |
0 commit comments