feat: Update TOC generation functions to include output file directory parameter and adjust lesson paths in SUMMARY files

This commit is contained in:
Littlefisher
2025-11-12 07:38:53 -08:00
parent 78bad8296c
commit fd03b86248
3 changed files with 123 additions and 117 deletions

View File

@@ -2,7 +2,7 @@ import os
import re
# Define a function to walk through the directory and generate the TOC structure
def generate_toc(base_dir, project_root):
def generate_toc(base_dir, project_root, output_file_dir):
toc = "## Table of Contents\n\n"
section_headers = {
"Basic": "### Getting Started Examples\n\nThis section contains simple eBPF program examples and introductions. It primarily utilizes the `eunomia-bpf` framework to simplify development and introduces the basic usage and development process of eBPF.\n\n",
@@ -81,8 +81,8 @@ def generate_toc(base_dir, project_root):
if ":" in first_title:
first_title = first_title.split(":", 1)[1].strip()
# Get the relative path for the lesson
lesson_rel_path = os.path.relpath(readme_path, project_root)
# Get the relative path for the lesson (relative to the output file's directory)
lesson_rel_path = os.path.relpath(readme_path, output_file_dir)
# Prepare lesson data
# Handle both numbered lessons (e.g., "12-profile") and named lessons (e.g., "features/bpf_arena")
@@ -130,7 +130,7 @@ def generate_toc(base_dir, project_root):
# Define a function to walk through the directory and generate the TOC structure in Chinese
def generate_toc_cn(base_dir, project_root):
def generate_toc_cn(base_dir, project_root, output_file_dir):
toc = "## 目录\n\n"
section_headers = {
"Basic": "### 入门示例\n\n这一部分包含简单的 eBPF 程序示例和介绍。主要利用 `eunomia-bpf` 框架简化开发,介绍 eBPF 的基本用法和开发流程。\n\n",
@@ -209,8 +209,8 @@ def generate_toc_cn(base_dir, project_root):
if ":" in first_title:
first_title = first_title.split(":", 1)[1].strip()
# Get the relative path for the lesson
lesson_rel_path = os.path.relpath(readme_path, project_root)
# Get the relative path for the lesson (relative to the output file's directory)
lesson_rel_path = os.path.relpath(readme_path, output_file_dir)
# Prepare lesson data
# Handle both numbered lessons (e.g., "12-profile") and named lessons (e.g., "features/bpf_arena")
@@ -276,42 +276,48 @@ def generate_file_from_template(template_path, output_path, toc_content):
# Main execution
if __name__ == "__main__":
base_directory = "src/" # Base directory for lessons
project_root = "./" # The root of the project
scripts_dir = "scripts/" # Directory containing templates
# Get the absolute path to the script's directory
script_dir = os.path.dirname(os.path.abspath(__file__))
# Get the project root (parent of scripts directory)
project_root = os.path.dirname(script_dir)
# Generate TOC content for English
toc_en = generate_toc(base_directory, project_root)
base_directory = os.path.join(project_root, "src")
scripts_dir = os.path.join(project_root, "scripts")
# Generate TOC content for Chinese
toc_cn = generate_toc_cn(base_directory, project_root)
# Generate TOC content for SUMMARY.md files (output in src/ directory)
toc_summary_en = generate_toc(base_directory, project_root, os.path.join(project_root, 'src'))
toc_summary_cn = generate_toc_cn(base_directory, project_root, os.path.join(project_root, 'src'))
# Generate TOC content for README.md files (output in project root)
toc_readme_en = generate_toc(base_directory, project_root, project_root)
toc_readme_cn = generate_toc_cn(base_directory, project_root, project_root)
# Generate SUMMARY.md from template
generate_file_from_template(
os.path.join(scripts_dir, 'SUMMARY.md.template'),
os.path.join('src', 'SUMMARY.md'),
toc_en
os.path.join(project_root, 'src', 'SUMMARY.md'),
toc_summary_en
)
# Generate SUMMARY.zh.md from template
generate_file_from_template(
os.path.join(scripts_dir, 'SUMMARY.zh.md.template'),
os.path.join('src', 'SUMMARY.zh.md'),
toc_cn
os.path.join(project_root, 'src', 'SUMMARY.zh.md'),
toc_summary_cn
)
# Generate README.md from template
generate_file_from_template(
os.path.join(scripts_dir, 'README.md.template'),
'README.md',
toc_en
os.path.join(project_root, 'README.md'),
toc_readme_en
)
# Generate README.zh.md from template
generate_file_from_template(
os.path.join(scripts_dir, 'README.zh.md.template'),
'README.zh.md',
toc_cn
os.path.join(project_root, 'README.zh.md'),
toc_readme_cn
)
print("\nAll files generated successfully!")