#!/bin/bash

# 输出的Markdown文件名
output_file="python_files_output.md"

# 清空或创建输出文件
> "$output_file"

# 递归函数来处理目录
process_directory() {
    local dir="$1"
    local depth="$2"

    # 添加目录作为标题
    echo "$(printf '%0.s#' $(seq 1 $depth)) ${dir##*/}" >> "$output_file"
    echo "" >> "$output_file"

    # 遍历当前目录中的所有.py文件
    for file in "$dir"/*.py; do
        # 检查文件是否存在（以防止没有.py文件的情况）
        if [ -f "$file" ]; then
            # 将文件名作为子标题写入md文件
            echo "$(printf '%0.s#' $(seq 1 $((depth + 1)))) ${file##*/}" >> "$output_file"
            echo "" >> "$output_file"
            
            # 添加代码块开始标记
            echo '```python' >> "$output_file"
            
            # 将Python文件内容添加到md文件
            cat "$file" >> "$output_file"
            
            # 添加代码块结束标记
            echo '```' >> "$output_file"
            echo "" >> "$output_file"
        fi
    done

    # 递归处理子目录
    for subdir in "$dir"/*/; do
        if [ -d "$subdir" ]; then
            process_directory "$subdir" $((depth + 1))
        fi
    done
}

# 从当前目录开始处理
process_directory "." 1

echo "Markdown文件已生成: $output_file"
