-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimplode-assembly.sh
More file actions
executable file
·166 lines (141 loc) · 5.53 KB
/
implode-assembly.sh
File metadata and controls
executable file
·166 lines (141 loc) · 5.53 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
#!/bin/bash
output_root="$HOME/imploded_assemblies"
mkdir -p "$output_root"
suppress_output=false
declare -a args=()
for arg in "$@"; do
case "$arg" in
-q|--quiet)
suppress_output=true
;;
*)
args+=("$arg")
;;
esac
done
ai_comment_block='// This file contains a fully expanded version of an OpenShift documentation *assembly* written in AsciiDoc.
// It is not valid AsciiDoc for publishing, because it includes both the original `include::` statements
// and the full contents of the referenced *module* or *snippet* files placed immediately underneath each include.
// Includes that reference `_attributes/` files remain untouched, as attribute files are not inlined.
// The purpose of this imploded version is to expose the raw AsciiDoc used throughout the document
// so that AI tools can analyze document structure, content organization, and AsciiDoc conventions
// without needing to resolve external files.'
inline_file_recursively() {
local file_path="$1"
local file_dir="$(dirname "$file_path")"
while IFS= read -r line || [[ -n "$line" ]]; do
if echo "$line" | grep -q 'include::.*\.adoc'; then
include_target="$(echo "$line" | sed -nE 's/.*include::([^\[]+\.adoc).*/\1/p')"
resolved_path="$file_dir/$include_target"
if [[ "$include_target" == */_attributes/* ]]; then
echo "$line"
elif [[ -f "$resolved_path" ]]; then
echo "// BEGIN inlined: $include_target"
inline_file_recursively "$resolved_path"
echo "// END inlined: $include_target"
else
echo "// WARNING: could not resolve $include_target"
fi
else
echo "$line"
fi
done < "$file_path"
}
implode_file() {
local input_file="$1"
local label="$2"
local -a included_modules=()
local -a included_snippets=()
local temp_content temp_final
temp_content=$(mktemp)
temp_final=$(mktemp)
local timestamp
timestamp="$(date '+%Y-%m-%d %H:%M:%S')"
local input_dir="$(dirname "$input_file")"
local git_branch
git_branch="$(git -C "$input_dir" rev-parse --abbrev-ref HEAD 2>/dev/null)"
git_branch="${git_branch:-not-in-git}"
local base_name="$(basename "$input_file" .adoc)"
local input_base="$(basename "$label")"
local rel_path="${label%/*}"
local output_subdir="$output_root/$rel_path"
mkdir -p "$output_subdir"
local output_file=""
local counter=1
while true; do
output_file="$output_subdir/${base_name}_${git_branch}_v${counter}.txt"
[[ ! -e "$output_file" ]] && break
((counter++))
done
echo "// Imploded on: $timestamp" >> "$temp_content"
echo "// Git branch: $git_branch" >> "$temp_content"
echo "" >> "$temp_content"
while IFS= read -r line || [[ -n "$line" ]]; do
echo "$line" >> "$temp_content"
if echo "$line" | grep -q 'include::modules/.*\.adoc'; then
module_path="$(echo "$line" | sed -nE 's/.*include::(modules\/[^\[]+\.adoc).*/\1/p')"
[[ -n "$module_path" ]] && included_modules+=("$module_path")
resolved_path="$input_dir/$module_path"
if [[ -f "$resolved_path" ]]; then
echo "// BEGIN inlined: $module_path" >> "$temp_content"
inline_file_recursively "$resolved_path" >> "$temp_content"
echo "// END inlined: $module_path" >> "$temp_content"
else
echo "// WARNING: missing module $module_path" >> "$temp_content"
fi
elif echo "$line" | grep -q 'include::snippets/.*\.adoc'; then
snippet_path="$(echo "$line" | sed -nE 's/.*include::(snippets\/[^\[]+\.adoc).*/\1/p')"
[[ -n "$snippet_path" ]] && included_snippets+=("$snippet_path")
resolved_path="$input_dir/$snippet_path"
if [[ -f "$resolved_path" ]]; then
echo "// BEGIN inlined: $snippet_path" >> "$temp_content"
inline_file_recursively "$resolved_path" >> "$temp_content"
echo "// END inlined: $snippet_path" >> "$temp_content"
else
echo "// WARNING: missing snippet $snippet_path" >> "$temp_content"
fi
fi
done < "$input_file"
echo "$ai_comment_block" > "$temp_final"
echo "" >> "$temp_final"
cat "$temp_content" >> "$temp_final"
mv "$temp_final" "$output_file"
rm "$temp_content"
if [[ "$suppress_output" == false ]]; then
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Output file: $output_file"
echo " Source: $label"
echo " Timestamp: $timestamp"
echo " Git branch: $git_branch"
echo " Modules: ${#included_modules[@]}"
for m in "${included_modules[@]}"; do echo " • $m"; done
echo " Snippets: ${#included_snippets[@]}"
for s in "${included_snippets[@]}"; do echo " • $s"; done
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
else
if [[ "$quiet_header_shown" != true ]]; then
echo "Generated files:"
quiet_header_shown=true
fi
echo "$output_file"
fi
}
# MAIN
if [[ "${#args[@]}" -eq 0 ]]; then
echo "Usage: $0 [--quiet|-q] <assembly.adoc> [more_files_or_dirs...]"
exit 1
fi
for arg in "${args[@]}"; do
if [[ -f "$arg" && "$arg" == *.adoc ]]; then
abs_path="$(cd "$(dirname "$arg")" && pwd)/$(basename "$arg")"
implode_file "$abs_path" "$arg"
elif [[ -d "$arg" ]]; then
find "$arg" -type f -name '*.adoc' | while IFS= read -r file; do
abs_path="$(cd "$(dirname "$file")" && pwd)/$(basename "$file")"
implode_file "$abs_path" "$file"
done
else
echo "Skipping unrecognized argument: $arg"
fi
done