-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch-convert-callouts.sh
More file actions
executable file
·165 lines (137 loc) · 3.84 KB
/
batch-convert-callouts.sh
File metadata and controls
executable file
·165 lines (137 loc) · 3.84 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
#!/bin/bash
# Batch convert callouts to definition lists in multiple AsciiDoc files
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONVERTER="$SCRIPT_DIR/convert-callouts-to-deflist.py"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
usage() {
cat <<EOF
Usage: $(basename "$0") [OPTIONS] <directory|file...>
Batch convert AsciiDoc callouts to definition list format.
OPTIONS:
-h, --help Show this help message
-n, --dry-run Preview changes without modifying files
-v, --verbose Enable verbose output
-r, --recursive Process directories recursively
EXAMPLES:
# Convert all .adoc files in current directory
$(basename "$0") .
# Dry run on specific files
$(basename "$0") --dry-run file1.adoc file2.adoc
# Recursively process a directory tree
$(basename "$0") --recursive docs/
# Verbose mode for debugging
$(basename "$0") --verbose --dry-run modules/
EOF
exit 0
}
# Parse arguments
DRY_RUN=""
VERBOSE=""
RECURSIVE=""
FILES=()
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
;;
-n|--dry-run)
DRY_RUN="--dry-run"
shift
;;
-v|--verbose)
VERBOSE="--verbose"
shift
;;
-r|--recursive)
RECURSIVE="yes"
shift
;;
*)
FILES+=("$1")
shift
;;
esac
done
if [ ${#FILES[@]} -eq 0 ]; then
echo -e "${RED}Error: No files or directories specified${NC}"
usage
fi
if [ ! -f "$CONVERTER" ]; then
echo -e "${RED}Error: Converter script not found at $CONVERTER${NC}"
exit 1
fi
# Function to process a single file
process_file() {
local file="$1"
if [ ! -f "$file" ]; then
echo -e "${YELLOW}Skipping: $file (not a file)${NC}"
return
fi
if [[ ! "$file" =~ \.adoc$ ]]; then
[ -n "$VERBOSE" ] && echo -e "${YELLOW}Skipping: $file (not .adoc)${NC}"
return
fi
echo "Processing: $file"
if python3 "$CONVERTER" $DRY_RUN $VERBOSE "$file"; then
echo -e "${GREEN}✓ Success: $file${NC}"
return 0
else
echo -e "${RED}✗ Failed: $file${NC}"
return 1
fi
}
# Function to process directory
process_directory() {
local dir="$1"
if [ ! -d "$dir" ]; then
echo -e "${YELLOW}Warning: $dir is not a directory${NC}"
return
fi
local find_args="-maxdepth 1"
[ -n "$RECURSIVE" ] && find_args=""
while IFS= read -r -d '' file; do
process_file "$file"
done < <(find "$dir" $find_args -name "*.adoc" -type f -print0 | sort -z)
}
# Main processing
TOTAL=0
SUCCESS=0
FAILED=0
for target in "${FILES[@]}"; do
if [ -f "$target" ]; then
((TOTAL++))
if process_file "$target"; then
((SUCCESS++))
else
((FAILED++))
fi
elif [ -d "$target" ]; then
echo -e "\n${GREEN}Processing directory: $target${NC}"
while IFS= read -r -d '' file; do
((TOTAL++))
if process_file "$file"; then
((SUCCESS++))
else
((FAILED++))
fi
done < <(find "$target" $([ -z "$RECURSIVE" ] && echo "-maxdepth 1") -name "*.adoc" -type f -print0 | sort -z)
else
echo -e "${RED}Error: $target does not exist${NC}"
((FAILED++))
fi
done
# Summary
echo ""
echo "=========================================="
echo "Summary:"
echo " Total files: $TOTAL"
echo -e " ${GREEN}Successful: $SUCCESS${NC}"
[ $FAILED -gt 0 ] && echo -e " ${RED}Failed: $FAILED${NC}" || echo " Failed: $FAILED"
echo "=========================================="
[ -n "$DRY_RUN" ] && echo -e "\n${YELLOW}DRY RUN - No files were modified${NC}"
exit $([ $FAILED -eq 0 ] && echo 0 || echo 1)