forked from xianyu110/awesome-openclaw-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-code-language.sh
More file actions
206 lines (179 loc) · 5.72 KB
/
Copy pathadd-code-language.sh
File metadata and controls
206 lines (179 loc) · 5.72 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
200
201
202
203
204
205
206
#!/bin/bash
# 为代码块自动添加语言标注
# 根据代码块内容智能判断语言类型
set -e
DOCS_DIR="docs"
BACKUP_DIR=".backup-$(date +%Y%m%d-%H%M%S)"
echo "🔍 开始为代码块添加语言标注..."
echo ""
# 创建备份
echo "📦 创建备份到 $BACKUP_DIR..."
mkdir -p "$BACKUP_DIR"
cp -r "$DOCS_DIR" "$BACKUP_DIR/"
echo "✅ 备份完成"
echo ""
# 统计信息
total_files=0
total_blocks=0
bash_blocks=0
json_blocks=0
text_blocks=0
yaml_blocks=0
python_blocks=0
javascript_blocks=0
typescript_blocks=0
# 处理单个文件
process_file() {
local file="$1"
local temp_file="${file}.tmp"
local in_code_block=0
local code_content=""
local modified=0
while IFS= read -r line || [ -n "$line" ]; do
# 检测代码块开始
if [[ "$line" =~ ^\`\`\`[[:space:]]*$ ]]; then
if [ $in_code_block -eq 0 ]; then
# 开始新的代码块
in_code_block=1
code_content=""
echo "$line" >> "$temp_file"
else
# 代码块结束,判断语言类型
in_code_block=0
local lang=$(detect_language "$code_content")
if [ -n "$lang" ]; then
# 回到文件开头,替换最后一个 ```
local last_line=$(tail -1 "$temp_file")
if [[ "$last_line" =~ ^\`\`\`[[:space:]]*$ ]]; then
# 删除最后一行
sed -i '' -e '$ d' "$temp_file"
# 添加带语言标注的代码块开始
echo "\`\`\`$lang" >> "$temp_file"
# 添加代码内容(已经在之前添加过了)
modified=1
case "$lang" in
bash) ((bash_blocks++)) ;;
json) ((json_blocks++)) ;;
text) ((text_blocks++)) ;;
yaml) ((yaml_blocks++)) ;;
python) ((python_blocks++)) ;;
javascript) ((javascript_blocks++)) ;;
typescript) ((typescript_blocks++)) ;;
esac
fi
fi
echo "$line" >> "$temp_file"
code_content=""
fi
elif [ $in_code_block -eq 1 ]; then
# 收集代码块内容
code_content+="$line"$'\n'
echo "$line" >> "$temp_file"
else
# 普通行
echo "$line" >> "$temp_file"
fi
done < "$file"
if [ $modified -eq 1 ]; then
mv "$temp_file" "$file"
return 0
else
rm -f "$temp_file"
return 1
fi
}
# 智能检测代码块语言
detect_language() {
local content="$1"
# 移除空白行
content=$(echo "$content" | sed '/^[[:space:]]*$/d')
# 如果内容为空,返回 text
if [ -z "$content" ]; then
echo "text"
return
fi
# 检测 Bash/Shell 命令
if echo "$content" | grep -qE '^(openclaw|clawhub|npm|pnpm|yarn|git|docker|curl|wget|ssh|cd|ls|mkdir|rm|cp|mv|cat|echo|export|source|\$)'; then
echo "bash"
return
fi
# 检测 JSON
if echo "$content" | grep -qE '^\s*\{' && echo "$content" | grep -qE '\}\s*$'; then
echo "json"
return
fi
if echo "$content" | grep -qE '^\s*\[' && echo "$content" | grep -qE '\]\s*$'; then
echo "json"
return
fi
if echo "$content" | grep -qE '"[^"]+"\s*:\s*'; then
echo "json"
return
fi
# 检测 YAML
if echo "$content" | grep -qE '^[a-zA-Z_][a-zA-Z0-9_]*:\s*'; then
if ! echo "$content" | grep -qE '\{|\}'; then
echo "yaml"
return
fi
fi
# 检测 Python
if echo "$content" | grep -qE '^(def |class |import |from |if __name__|print\(|return )'; then
echo "python"
return
fi
# 检测 JavaScript
if echo "$content" | grep -qE '^(const |let |var |function |class |import |export |require\(|module\.exports)'; then
echo "javascript"
return
fi
# 检测 TypeScript
if echo "$content" | grep -qE '^(interface |type |enum |namespace |declare )'; then
echo "typescript"
return
fi
if echo "$content" | grep -qE ':\s*(string|number|boolean|any|void|never)'; then
echo "typescript"
return
fi
# 检测输出内容(包含特定关键词)
if echo "$content" | grep -qE '(Output:|Error:|Success:|Warning:|INFO|DEBUG|✅|❌|⚠️)'; then
echo "text"
return
fi
# 检测配置文件路径
if echo "$content" | grep -qE '^(~\/|\/|\.\/|[A-Z]:\\)'; then
echo "text"
return
fi
# 默认返回 text
echo "text"
}
# 导出函数供子进程使用
export -f detect_language
# 遍历所有 Markdown 文件
echo "📝 处理 Markdown 文件..."
while IFS= read -r -d '' file; do
echo "处理: $file"
if process_file "$file"; then
((total_files++))
fi
done < <(find "$DOCS_DIR" -name "*.md" -type f -print0)
echo ""
echo "✅ 处理完成!"
echo ""
echo "📊 统计信息:"
echo " - 修改文件数: $total_files"
echo " - 添加语言标注:"
echo " - bash: $bash_blocks"
echo " - json: $json_blocks"
echo " - text: $text_blocks"
echo " - yaml: $yaml_blocks"
echo " - python: $python_blocks"
echo " - javascript: $javascript_blocks"
echo " - typescript: $typescript_blocks"
echo ""
echo "💡 提示:"
echo " - 备份位置: $BACKUP_DIR"
echo " - 如需恢复: cp -r $BACKUP_DIR/docs/* docs/"
echo ""