forked from Simpleyyt/ai-manus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_doc.sh
More file actions
executable file
·199 lines (174 loc) · 5.66 KB
/
Copy pathupdate_doc.sh
File metadata and controls
executable file
·199 lines (174 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash
# Script Description: Replace content surrounded by specific comments in all md files with corresponding file content
# Universal format: <!-- filename --> content <!-- /filename -->
# The script will automatically detect all comment tags in this format and replace them with the corresponding file content
# ===============================
# File Sync Configuration Area - Add files to sync here
# ===============================
# Format: filename:code_type (separated by colon)
# If code_type is not specified, the script will automatically infer it based on file extension
# Files to sync (format: "filename:code_type")
FILES_TO_SYNC=(
"docker-compose-example.yml:yaml"
".env.example:env"
# Add more files, uncomment and modify the following examples:
# "package.json:json"
# "requirements.txt:text"
# "Dockerfile:dockerfile"
# "nginx.conf:nginx"
# "config.yaml:yaml"
# "startup.sh:bash"
)
# ===============================
# Script Function Area
# ===============================
# Get filename from config entry
get_filename() {
echo "$1" | cut -d: -f1
}
# Get code type from config entry
get_configured_code_type() {
local entry="$1"
if [[ "$entry" == *":"* ]]; then
echo "$entry" | cut -d: -f2
else
echo ""
fi
}
# Infer code type based on file extension
get_code_type() {
local filename="$1"
local extension="${filename##*.}"
case "$extension" in
yml|yaml) echo "yaml" ;;
json) echo "json" ;;
js|mjs) echo "javascript" ;;
ts) echo "typescript" ;;
py) echo "python" ;;
sh|bash) echo "bash" ;;
css) echo "css" ;;
html|htm) echo "html" ;;
xml) echo "xml" ;;
sql) echo "sql" ;;
md) echo "markdown" ;;
txt|log|conf|config) echo "text" ;;
env|example) echo "env" ;;
dockerfile) echo "dockerfile" ;;
nginx) echo "nginx" ;;
*) echo "text" ;;
esac
}
# Function to check if file exists
check_file_exists() {
local file_path="$1"
if [ ! -f "$file_path" ]; then
echo "Warning: $file_path does not exist, skipping"
return 1
fi
return 0
}
# Function to process file replacement
process_file() {
local md_file="$1"
local source_file="$2"
local start_tag="$3"
local end_tag="$4"
local code_type="$5"
echo "Processing file: $md_file (replacing with $source_file content)"
# Temporary file
temp_file=$(mktemp)
# Use awk to replace content
awk -v source_file="$source_file" -v start_tag="$start_tag" -v end_tag="$end_tag" -v code_type="$code_type" '
BEGIN {
in_block = 0
# Read source file content
while ((getline line < source_file) > 0) {
source_content = source_content line "\n"
}
close(source_file)
}
$0 ~ start_tag {
print $0
print "```" code_type
printf "%s", source_content
print "```"
in_block = 1
next
}
$0 ~ end_tag {
in_block = 0
print $0
next
}
!in_block {
print $0
}
' "$md_file" > "$temp_file"
# Replace original file
mv "$temp_file" "$md_file"
echo "Updated: $md_file"
}
# ===============================
# Main Program
# ===============================
echo "Starting document update process..."
echo ""
# Display configured file list
echo "Configured file list:"
for entry in "${FILES_TO_SYNC[@]}"; do
filename=$(get_filename "$entry")
code_type=$(get_configured_code_type "$entry")
if [ -f "$filename" ]; then
echo " ✓ $filename ($code_type)"
else
echo " ✗ $filename ($code_type) - file not found"
fi
done
echo ""
echo "Starting file replacement process..."
# Process each md file (exclude .venv, .git, node_modules directories)
find . -name "*.md" -type f \
-not -path "./.venv/*" \
-not -path "./.git/*" \
-not -path "./node_modules/*" \
-not -path "./*/.venv/*" \
-not -path "./*/.git/*" \
-not -path "./*/node_modules/*" \
| while read -r md_file; do
echo "Checking file: $md_file"
file_updated=false
# Check configured file list
for entry in "${FILES_TO_SYNC[@]}"; do
source_file=$(get_filename "$entry")
# Check if start and end tags exist
if grep -q "<!-- $source_file -->" "$md_file" && grep -q "<!-- /$source_file -->" "$md_file"; then
# Check if source file exists
if check_file_exists "$source_file"; then
# Get code type (prioritize configured type, otherwise auto-infer)
code_type=$(get_configured_code_type "$entry")
if [ -z "$code_type" ]; then
code_type=$(get_code_type "$source_file")
fi
# Process file
process_file "$md_file" "$source_file" "<!-- $source_file -->" "<!-- /$source_file -->" "$code_type"
file_updated=true
fi
fi
done
if [ "$file_updated" = false ]; then
echo "Skipping file: $md_file (no configured file reference tags found)"
fi
done
echo ""
echo "Script execution completed!"
echo ""
echo "Usage Instructions:"
echo "1. Add files to sync in the FILES_TO_SYNC array at the top of the script"
echo "2. Use format in markdown files: <!-- filename --> ... <!-- /filename -->"
echo "3. Run the script to automatically sync content"
echo ""
echo "Supported code types:"
echo " yaml, json, javascript, typescript, python, bash, css, html, xml, sql, markdown, env, dockerfile, nginx, text"
echo ""
echo "Ignored directories:"
echo " .venv, .git, node_modules (and their subdirectories)"