|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Read configuration from readmetreerc.yml |
| 4 | +if [ -f readmetreerc.yml ]; then |
| 5 | + file_names=$(grep -A5 "fileNames:" readmetreerc.yml | grep -v "fileNames:" | sed 's/^[ \t]*-[ \t]*//' | tr -d ' ') |
| 6 | + chapter=$(grep "chapter:" readmetreerc.yml | cut -d':' -f2 | tr -d ' ') |
| 7 | +else |
| 8 | + file_names="README.md" |
| 9 | + chapter="Tree" |
| 10 | +fi |
| 11 | + |
| 12 | +# Generate tree and replace backticks with regular pipes |
| 13 | +tree_output=$(tree -I ".git|node_modules|.github" --noreport --charset ascii . | sed "s/\`/|/g") |
| 14 | + |
| 15 | +# Format the file_names variable |
| 16 | +file_names=$(echo "$file_names" | tr ',' ' ') |
| 17 | + |
| 18 | +# Update each file specified in the config |
| 19 | +for file_name in $file_names; do |
| 20 | + if [ ! -f "$file_name" ]; then |
| 21 | + echo "File $file_name not found, skipping." |
| 22 | + continue |
| 23 | + fi |
| 24 | + |
| 25 | + # Create the final markdown tree block |
| 26 | + final_tree="## ${chapter}\n\`\`\`\n${tree_output}\n\`\`\`" |
| 27 | + |
| 28 | + # Update the file with the new tree structure |
| 29 | + if grep -q "^## ${chapter}" "$file_name"; then |
| 30 | + # Find the section and replace it |
| 31 | + awk -v chapter="$chapter" -v tree="$final_tree" ' |
| 32 | + BEGIN {p=1; found=0} |
| 33 | + $0 ~ "^## "chapter"$" {print tree; p=0; found=1; next} |
| 34 | + $0 ~ /^## / && p==0 {p=1} |
| 35 | + p==1 {print} |
| 36 | + END {if (found==0) print "\n" tree} |
| 37 | + ' "$file_name" > "$file_name.new" |
| 38 | + mv "$file_name.new" "$file_name" |
| 39 | + else |
| 40 | + # If the section doesn't exist, add it at the end |
| 41 | + echo -e "\n${final_tree}" >> "$file_name" |
| 42 | + fi |
| 43 | +done |
0 commit comments