Skip to content

Commit 69f0a59

Browse files
committed
Add script to update sdk docs
Signed-off-by: Marc Duiker <[email protected]>
1 parent e6527cf commit 69f0a59

File tree

1 file changed

+309
-0
lines changed

1 file changed

+309
-0
lines changed

scripts/update-sdk-docs.sh

Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
#!/bin/bash
2+
3+
# Script to convert Hugo tabs shortcodes to tabpane format in markdown files
4+
# Usage: ./convert-tabs-to-tabpanes.sh <directory>
5+
6+
# Removed set -e to prevent immediate exit on errors
7+
8+
if [ $# -eq 0 ]; then
9+
echo "Usage: $0 <directory>"
10+
echo "Example: $0 /path/to/markdown/files"
11+
exit 1
12+
fi
13+
14+
DIRECTORY="$1"
15+
16+
if [ ! -d "$DIRECTORY" ]; then
17+
echo "Error: Directory '$DIRECTORY' not found."
18+
exit 1
19+
fi
20+
21+
# Counter for processed files
22+
processed_files=0
23+
files_with_changes=0
24+
25+
# Function to safely get numeric count from grep
26+
safe_count() {
27+
local pattern="$1"
28+
local file="$2"
29+
local result
30+
31+
# Check if pattern contains regex characters that need -E flag
32+
if [[ "$pattern" =~ .*[\.\*\+\?\[\]\{\}\(\)\|].*$ ]]; then
33+
result=$(grep -E -c "$pattern" "$file" 2>/dev/null || echo "0")
34+
else
35+
result=$(grep -c "$pattern" "$file" 2>/dev/null || echo "0")
36+
fi
37+
38+
# Ensure we have a valid number
39+
if [[ "$result" =~ ^[0-9]+$ ]]; then
40+
echo "$result"
41+
else
42+
echo "0"
43+
fi
44+
}
45+
46+
# Function to process a single markdown file
47+
process_file() {
48+
local file="$1"
49+
echo "Processing: $file"
50+
51+
# Create a backup
52+
cp "$file" "${file}.backup"
53+
54+
# Use a temporary file for processing
55+
local temp_file=$(mktemp)
56+
cp "$file" "$temp_file"
57+
58+
# Track if any changes were made
59+
local changes_made=false
60+
61+
# Step 1: Replace [codetabs] with [tabpane]
62+
if grep -q '\[codetabs\]' "$temp_file"; then
63+
sed -i 's/\[codetabs\]/[tabpane]/g' "$temp_file"
64+
changes_made=true
65+
echo " - Replaced [codetabs] with [tabpane]"
66+
fi
67+
68+
# Step 2: Change {{< ref file.md >}} to {{% ref file.md %}}
69+
# Handle both formats: with and without space before >}}
70+
# Use non-greedy matching to handle multiple links on the same line
71+
if grep -q '{{< ref [^}]*\.md[^}]*>}}' "$temp_file"; then
72+
sed -i 's/{{< ref \([^}]*\.md[^}]*\) >}}/{{% ref \1 %}}/g; s/{{< ref \([^}]*\.md[^}]*\)>}}/{{% ref \1 %}}/g' "$temp_file"
73+
changes_made=true
74+
echo " - Updated ref links from {{< >}} to {{% %}}"
75+
fi
76+
77+
# Step 3: Check if file has codetab or tabs elements to determine if further processing is needed
78+
local has_codetabs=$(safe_count '{{% codetab' "$temp_file")
79+
local has_tabs_elements=$(safe_count '{{< tabs.*>}}' "$temp_file")
80+
81+
if [ "$has_codetabs" -eq 0 ] && [ "$has_tabs_elements" -eq 0 ]; then
82+
echo " - No codetab or tabs elements found, skipping tab processing"
83+
else
84+
echo " - Found tab elements, processing..."
85+
local tabs_processed=false
86+
87+
# Step 4: Replace opening {{% codetab %}} with {{% tab %}}
88+
if [ "$has_codetabs" -gt 0 ]; then
89+
if grep -q '{{% codetab' "$temp_file"; then
90+
sed -i 's/{{% codetab/{{% tab/g' "$temp_file"
91+
changes_made=true
92+
tabs_processed=true
93+
echo " - Replaced {{% codetab with {{% tab"
94+
fi
95+
96+
# Step 5: Replace closing {{% /codetab %}} with {{% /tab %}}
97+
if grep -q '{{% /codetab' "$temp_file"; then
98+
sed -i 's/{{% \/codetab/{{% \/tab/g' "$temp_file"
99+
changes_made=true
100+
echo " - Replaced {{% /codetab with {{% /tab"
101+
fi
102+
fi
103+
104+
# Step 6 & 7: Process tabs with languages using AWK
105+
if [ "$has_tabs_elements" -gt 0 ]; then
106+
awk '
107+
BEGIN {
108+
in_tabs = 0
109+
tab_index = 0
110+
languages_count = 0
111+
file_has_changes = 0
112+
}
113+
114+
# Step 6: Find {{< tabs LANGUAGES >}} and extract languages
115+
/^{{< tabs.*>}}/ {
116+
in_tabs = 1
117+
tab_index = 0
118+
119+
# Extract everything after "tabs" and before ">}}"
120+
line = $0
121+
# Remove the opening {{< tabs part (with optional space)
122+
gsub(/^{{< tabs */, "", line)
123+
gsub(/>}}$/, "", line)
124+
gsub(/^ +/, "", line) # Remove leading spaces
125+
gsub(/ +$/, "", line) # Remove trailing spaces
126+
127+
# Parse quoted and unquoted strings
128+
languages_count = 0
129+
i = 1
130+
while (i <= length(line)) {
131+
# Skip whitespace
132+
while (i <= length(line) && substr(line, i, 1) == " ") {
133+
i++
134+
}
135+
136+
if (i > length(line)) break
137+
138+
# Check if this token starts with a quote
139+
if (substr(line, i, 1) == "\"") {
140+
# Find the closing quote
141+
i++ # Skip opening quote
142+
start = i
143+
while (i <= length(line) && substr(line, i, 1) != "\"") {
144+
i++
145+
}
146+
if (i <= length(line)) {
147+
# Found closing quote
148+
token = substr(line, start, i - start)
149+
if (token != "") {
150+
languages[++languages_count] = token
151+
}
152+
i++ # Skip closing quote
153+
}
154+
} else {
155+
# Unquoted token - read until space or end
156+
start = i
157+
while (i <= length(line) && substr(line, i, 1) != " ") {
158+
i++
159+
}
160+
token = substr(line, start, i - start)
161+
if (token != "") {
162+
languages[++languages_count] = token
163+
}
164+
}
165+
}
166+
167+
print "{{< tabpane text=true >}}"
168+
file_has_changes = 1
169+
next
170+
}
171+
172+
# Step 7: Replace {{< /tabs >}} with {{< /tabpane >}}
173+
/^{{< \/tabs >}}$/ {
174+
in_tabs = 0
175+
print "{{< /tabpane >}}"
176+
file_has_changes = 1
177+
next
178+
}
179+
180+
# Update {{% tab %}} within tabs to include header parameter
181+
/^{{% tab %}}$/ && in_tabs {
182+
if (tab_index < languages_count) {
183+
tab_index++
184+
print "{{% tab header=\"" languages[tab_index] "\" %}}"
185+
file_has_changes = 1
186+
} else {
187+
print $0
188+
}
189+
next
190+
}
191+
192+
# Print all other lines as-is
193+
{
194+
print $0
195+
}
196+
197+
END {
198+
# Return 0 if changes were made, 1 if no changes
199+
exit(file_has_changes ? 0 : 1)
200+
}
201+
' "$temp_file" > "${temp_file}.awk"
202+
203+
# Check if AWK made changes and copy result back to temp file
204+
if [ $? -eq 0 ]; then
205+
mv "${temp_file}.awk" "$temp_file"
206+
changes_made=true
207+
tabs_processed=true
208+
echo " - Processed tabs/tabpane conversions"
209+
else
210+
rm -f "${temp_file}.awk"
211+
fi
212+
fi
213+
214+
# Step 8: Final validation check (only if tabs were processed)
215+
if [ "$tabs_processed" = true ]; then
216+
echo " - Running validation checks..."
217+
218+
# Get counts safely
219+
local opening_tabs=$(safe_count '{{% tab' "$file")
220+
local closing_tabs=$(safe_count '{{% /tab' "$file")
221+
local opening_tabpanes=$(safe_count '{{< tabpane' "$file")
222+
local closing_tabpanes=$(safe_count '{{< /tabpane' "$file")
223+
224+
# Check for mismatches only if elements exist
225+
local validation_issues=false
226+
227+
if [ "$opening_tabs" -gt 0 ] || [ "$closing_tabs" -gt 0 ]; then
228+
if [ "$opening_tabs" -ne "$closing_tabs" ]; then
229+
echo " ⚠️ WARNING: Unmatched tab elements - Opening: $opening_tabs, Closing: $closing_tabs"
230+
validation_issues=true
231+
fi
232+
fi
233+
234+
if [ "$opening_tabpanes" -gt 0 ] || [ "$closing_tabpanes" -gt 0 ]; then
235+
if [ "$opening_tabpanes" -ne "$closing_tabpanes" ]; then
236+
echo " ⚠️ WARNING: Unmatched tabpane elements - Opening: $opening_tabpanes, Closing: $closing_tabpanes"
237+
validation_issues=true
238+
fi
239+
fi
240+
241+
if [ "$validation_issues" = false ]; then
242+
echo " ✅ Validation passed: All tab and tabpane elements are properly matched"
243+
fi
244+
fi
245+
fi
246+
247+
# Copy temp file back to original if changes were made
248+
if [ "$changes_made" = true ]; then
249+
cp "$temp_file" "$file"
250+
fi
251+
252+
# Clean up temp file
253+
rm "$temp_file"
254+
255+
if [ "$changes_made" = true ]; then
256+
echo " ✅ File processed with changes"
257+
((files_with_changes++))
258+
else
259+
echo " ℹ️ No changes needed"
260+
# Remove backup if no changes were made
261+
rm "${file}.backup"
262+
fi
263+
264+
((processed_files++))
265+
echo ""
266+
}
267+
268+
echo "Starting conversion of tabs to tabpanes in directory: $DIRECTORY"
269+
echo "============================================================="
270+
echo ""
271+
272+
# Debug: Show what files will be processed
273+
echo "Discovering markdown files..."
274+
file_count=0
275+
while IFS= read -r -d '' file; do
276+
echo "Found: $file"
277+
((file_count++))
278+
done < <(find "$DIRECTORY" -name "*.md" -type f -print0)
279+
echo "Total files found: $file_count"
280+
echo ""
281+
282+
# Find all markdown files recursively and process them
283+
while IFS= read -r -d '' file; do
284+
process_file "$file"
285+
done < <(find "$DIRECTORY" -name "*.md" -type f -print0)
286+
287+
echo "============================================================="
288+
echo "Conversion completed!"
289+
echo "Files processed: $processed_files"
290+
echo "Files with changes: $files_with_changes"
291+
echo ""
292+
293+
if [ $files_with_changes -gt 0 ]; then
294+
echo "Backup files created with .backup extension for files that were modified."
295+
echo "You can remove them with: find $DIRECTORY -name '*.backup' -delete"
296+
fi
297+
298+
echo ""
299+
echo "Summary of transformations applied:"
300+
echo "1. ✅ [codetabs] → [tabpane]"
301+
echo "2. ✅ {{< ref file.md>}} → {{% ref file.md %}}"
302+
echo "3. ✅ Conditional processing based on presence of codetab/tabs elements"
303+
echo "4. ✅ {{% codetab %}} → {{% tab %}} (if codetabs found)"
304+
echo "5. ✅ {{% /codetab %}} → {{% /tab %}} (if codetabs found)"
305+
echo "6. ✅ {{< tabs LANGUAGES >}} → {{< tabpane text=true >}} (if tabs found)"
306+
echo "7. ✅ {{< /tabs >}} → {{< /tabpane >}} (if tabs found)"
307+
echo "8. ✅ Validation check (only for files with processed tabs)"
308+
309+
exit 0

0 commit comments

Comments
 (0)