Add eBPF tutorial templates and update SUMMARY files

- Created SUMMARY.md.template and SUMMARY.zh.md.template for eBPF tutorial.
- Updated generate_toc.py to generate table of contents for English and Chinese versions.
- Added configuration files for new eBPF examples, categorizing them by level and type.
- Updated SUMMARY.md and SUMMARY.zh.md to reflect new lessons and reorganized sections.
- Introduced new features related to GPU and tracing in the tutorial.
This commit is contained in:
yunwei37
2025-10-05 20:56:50 -07:00
parent 29e8113081
commit a2e7b20d63
22 changed files with 602 additions and 208 deletions

View File

@@ -12,14 +12,16 @@ def generate_toc(base_dir, project_root):
subsection_titles = {
"Android": "\n\nAndroid:\n\n",
"Networking": "\n\nNetworking:\n\n",
"tracing": "\n\ntracing:\n\n",
"Security": "\n\nSecurity:\n\n",
"GPU": "\n\nGPU:\n\n",
"Scheduler": "\n\nScheduler:\n\n",
"Other": "\n\nOther:\n\n"
"Networking": "\n\nNetworking:\n\n",
"tracing": "\n\nTracing:\n\n",
"Security": "\n\nSecurity:\n\n",
"Features": "\n\nFeatures:\n\n",
"Other": "\n\nFeatures:\n\n"
}
subsection_order = ['Android', 'Networking', 'tracing', 'Security', 'Scheduler', 'Other']
subsection_order = ['GPU', 'Scheduler', 'Networking', 'tracing', 'Security', 'Features', 'Other', 'Android']
# To ensure numeric sorting of directories
def sort_key(directory_name):
@@ -27,8 +29,23 @@ def generate_toc(base_dir, project_root):
sections = {} # {section_level: {subsection_type: [lessons]}}
# Sort directories properly by numeric order
all_dirs = sorted([d for d in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, d))], key=sort_key)
# Collect all directories including subdirectories
all_dirs = []
for item in os.listdir(base_dir):
item_path = os.path.join(base_dir, item)
if os.path.isdir(item_path):
# Add numbered directories directly
if re.match(r'^\d+', item):
all_dirs.append(item)
# Also scan subdirectories (like features/, xpu/)
else:
for subitem in os.listdir(item_path):
subitem_path = os.path.join(item_path, subitem)
if os.path.isdir(subitem_path):
all_dirs.append(os.path.join(item, subitem))
# Sort directories properly by numeric order (non-numeric dirs go to end)
all_dirs = sorted(all_dirs, key=lambda d: sort_key(d) if re.search(r'\d+', d) else [999999])
# Loop over the sorted directories
for directory in all_dirs:
@@ -68,9 +85,15 @@ def generate_toc(base_dir, project_root):
lesson_rel_path = os.path.relpath(readme_path, project_root)
# Prepare lesson data
lesson_number = directory.split('-')[0]
lesson_name = directory.split('-', 1)[1]
link_text = f"lesson {lesson_number}-{lesson_name}"
# Handle both numbered lessons (e.g., "12-profile") and named lessons (e.g., "features/bpf_arena")
if '-' in os.path.basename(directory):
lesson_number = directory.split('-')[0]
lesson_name = directory.split('-', 1)[1]
link_text = f"lesson {lesson_number}-{lesson_name}"
else:
# For non-numbered directories, use the full path as name
link_text = directory.replace('/', ' ')
link = f"{lesson_rel_path}"
# Use description if available, else use first title
lesson_desc = desc if desc else first_title
@@ -117,14 +140,16 @@ def generate_toc_cn(base_dir, project_root):
subsection_titles = {
"Android": "Android:\n\n",
"Networking": "网络:\n\n",
"tracing": "追踪:\n\n",
"Security": "安全:\n\n",
"GPU": "GPU:\n\n",
"Scheduler": "调度器:\n\n",
"Other": "其他:\n\n"
"Networking": "网络:\n\n",
"tracing": "Tracing:\n\n",
"Security": "安全:\n\n",
"Features": "特性:\n\n",
"Other": "特性:\n\n"
}
subsection_order = ['Android', 'Networking', 'tracing', 'Security', 'Scheduler', 'Other']
subsection_order = ['GPU', 'Scheduler', 'Networking', 'tracing', 'Security', 'Features', 'Other', 'Android']
# To ensure numeric sorting of directories
def sort_key(directory_name):
@@ -132,8 +157,23 @@ def generate_toc_cn(base_dir, project_root):
sections = {} # {section_level: {subsection_type: [lessons]}}
# Sort directories properly by numeric order
all_dirs = sorted([d for d in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, d))], key=sort_key)
# Collect all directories including subdirectories
all_dirs = []
for item in os.listdir(base_dir):
item_path = os.path.join(base_dir, item)
if os.path.isdir(item_path):
# Add numbered directories directly
if re.match(r'^\d+', item):
all_dirs.append(item)
# Also scan subdirectories (like features/, xpu/)
else:
for subitem in os.listdir(item_path):
subitem_path = os.path.join(item_path, subitem)
if os.path.isdir(subitem_path):
all_dirs.append(os.path.join(item, subitem))
# Sort directories properly by numeric order (non-numeric dirs go to end)
all_dirs = sorted(all_dirs, key=lambda d: sort_key(d) if re.search(r'\d+', d) else [999999])
# Loop over the sorted directories
for directory in all_dirs:
@@ -173,9 +213,15 @@ def generate_toc_cn(base_dir, project_root):
lesson_rel_path = os.path.relpath(readme_path, project_root)
# Prepare lesson data
lesson_number = directory.split('-')[0]
lesson_name = directory.split('-', 1)[1]
link_text = f"lesson {lesson_number}-{lesson_name}"
# Handle both numbered lessons (e.g., "12-profile") and named lessons (e.g., "features/bpf_arena")
if '-' in os.path.basename(directory):
lesson_number = directory.split('-')[0]
lesson_name = directory.split('-', 1)[1]
link_text = f"lesson {lesson_number}-{lesson_name}"
else:
# For non-numbered directories, use the full path as name
link_text = directory.replace('/', ' ')
link = f"{lesson_rel_path}"
# Use description if available, else use first title
lesson_desc = desc if desc else first_title
@@ -210,10 +256,62 @@ def generate_toc_cn(base_dir, project_root):
toc += "\n持续更新中..."
return toc
# Example usage
base_directory = "src/" # Replace with the actual base directory
project_root = "./" # The root of the project
toc_output = generate_toc(base_directory, project_root)
# toc_output = generate_toc_cn(base_directory, project_root)
# Output the TOC
print(toc_output)
def load_template(template_path):
"""Load a template file and return its content"""
with open(template_path, 'r', encoding='utf-8') as f:
return f.read()
def generate_file_from_template(template_path, output_path, toc_content):
"""Generate a file from template by replacing {{TOC_CONTENT}} placeholder"""
template = load_template(template_path)
output_content = template.replace('{{TOC_CONTENT}}', toc_content)
with open(output_path, 'w', encoding='utf-8') as f:
f.write(output_content)
print(f"Generated: {output_path}")
# 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
# Generate TOC content for English
toc_en = generate_toc(base_directory, project_root)
# Generate TOC content for Chinese
toc_cn = generate_toc_cn(base_directory, 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
)
# 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
)
# Generate README.md from template
generate_file_from_template(
os.path.join(scripts_dir, 'README.md.template'),
'README.md',
toc_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
)
print("\nAll files generated successfully!")