-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextract-mermaid
More file actions
executable file
Β·456 lines (395 loc) Β· 13.3 KB
/
Copy pathextract-mermaid
File metadata and controls
executable file
Β·456 lines (395 loc) Β· 13.3 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/usr/bin/env bash
# Export ```mermaid blocks from Markdown to SVG or PNG.
# Usage: ./extract-mermaid <file.md> [options]
set -euo pipefail
main() {
if [[ ! -f "$(dirname "$0")/common-functions" ]]; then
echo "Downloading common-functions from GitHub..."
if ! curl -fsSL https://raw.githubusercontent.com/Flower7C3/bash-tools/master/common-functions -o "$(dirname "$0")/common-functions"; then
echo "Failed to download common-functions"
exit 1
fi
fi
# shellcheck source=common-functions
source "$(dirname "$0")/common-functions"
export_mermaid_main "$@"
}
export_mermaid_main() {
local INPUT="" OUT="" FORMAT="svg" WIDTH="" HEIGHT="" SCALE="" PREFIX="" DRY_RUN=0 JOBS=""
local -a _MMDC_CMD=()
local _PUPPETEER_CONFIG=""
show_usage() {
local exit_code="${1:-0}"
log_usage_title '<file.md> [options]'
echo
echo 'Export Mermaid diagrams from Markdown files'
echo
log_header 'Options'
log_usage_options_line '--out;-o <dir>' \
'Output directory (default: <file>-diagrams/ next to source)'
log_usage_options_line '--format;-f <svg|png>' \
'Output format (default: svg)'
log_usage_options_line '--width;-w <px>' \
'Render viewport width (default: 800)'
log_usage_options_line '--height;-H <px>' \
'Render viewport height (default: 600; mermaid fits content)'
log_usage_options_line '--scale;-s <n>' \
'Puppeteer scale for PNG (default: 1; e.g. 2 = retina)'
log_usage_options_line '--jobs;-j <n>' \
'Parallel render jobs (default: half of available CPUs)'
log_usage_options_line '--prefix <text>' \
'Filename prefix'
log_usage_options_line '--dry-run' \
'Show what would be exported without rendering'
log_usage_options_line '-u;--update' \
'Update app'
log_usage_options_line '-h;--help' \
'Show this help message'
echo
echo 'Output filenames come from the nearest heading above each mermaid block'
echo '(e.g. "## 2. Architecture" β architecture.svg). Duplicates get a numeric suffix.'
echo 'Requires: npx @mermaid-js/mermaid-cli (auto-downloaded on first run).'
echo
log_header 'Examples'
log_usage_example_line 'docs/frontend-cache.md'
log_usage_example_line 'docs/frontend-cache.md --format png --width 1600'
log_usage_example_line 'docs/frontend-cache.md --jobs 4 --dry-run'
exit "$exit_code"
}
default_jobs() {
local cpus
cpus=$(getconf _NPROCESSORS_ONLN 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)
echo $((cpus > 1 ? cpus / 2 : 1))
}
parse_positive_int() {
local raw="$1" label="$2"
if [[ ! "$raw" =~ ^[1-9][0-9]*$ ]]; then
die 1 '%s must be a positive integer (px), got: %s' "$label" "$raw"
fi
echo "$raw"
}
parse_positive_number() {
local raw="$1" label="$2"
if ! awk -v v="$raw" 'BEGIN { exit !(v+0 > 0 && v == v+0) }'; then
die 1 '%s must be a positive number, got: %s' "$label" "$raw"
fi
echo "$raw"
}
init_mmdc() {
[[ ${#_MMDC_CMD[@]} -gt 0 ]] && return 0
if command -v mmdc &>/dev/null; then
_MMDC_CMD=(mmdc)
return 0
fi
log_info ---icon 'π¦' 'Preparing Mermaid CLI (first run may download packages)...'
if ! npx -y @mermaid-js/mermaid-cli --version &>/dev/null; then
die 1 'Failed to run @mermaid-js/mermaid-cli'
fi
local npm_cache candidate
npm_cache="${NPM_CONFIG_CACHE:-${npm_config_cache:-$HOME/.npm}}"
while IFS= read -r candidate; do
if [[ -x "$candidate" ]]; then
_MMDC_CMD=("$candidate")
return 0
fi
done < <(find "$npm_cache/_npx" -path '*/node_modules/.bin/mmdc' -type f 2>/dev/null | sort -r)
_MMDC_CMD=(npx -y @mermaid-js/mermaid-cli)
}
write_puppeteer_config() {
local kind="$1" browser="$2"
_PUPPETEER_CONFIG="$_MERMAID_WORK_DIR/puppeteer.json"
python3 - "$kind" "$browser" "$_PUPPETEER_CONFIG" <<'PY'
import json
import sys
kind, browser, dest = sys.argv[1], sys.argv[2], sys.argv[3]
cfg = {"executablePath": browser}
if kind != "shell":
cfg["headless"] = True
with open(dest, "w", encoding="utf-8") as handle:
json.dump(cfg, handle)
PY
}
find_browser() {
local cache="${PUPPETEER_CACHE_DIR:-$HOME/.cache/puppeteer}"
local candidate cmd
while IFS= read -r candidate; do
if [[ -x "$candidate" ]]; then
printf 'shell|%s\n' "$candidate"
return 0
fi
done < <(find "$cache/chrome-headless-shell" -type f -name 'chrome-headless-shell' 2>/dev/null | sort -r)
while IFS= read -r candidate; do
if [[ -x "$candidate" ]]; then
printf 'chrome|%s\n' "$candidate"
return 0
fi
done < <(find "$cache/chrome" \( -name 'Google Chrome for Testing' -o -name 'chrome' \) -type f 2>/dev/null | sort -r)
for candidate in \
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" \
"/Applications/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing" \
"/Applications/Chromium.app/Contents/MacOS/Chromium" \
"/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge" \
"/Applications/Brave Browser.app/Contents/MacOS/Brave Browser"; do
if [[ -x "$candidate" ]]; then
printf 'chrome|%s\n' "$candidate"
return 0
fi
done
for cmd in google-chrome google-chrome-stable chromium chromium-browser microsoft-edge; do
if command_exists "$cmd"; then
printf 'chrome|%s\n' "$(command -v "$cmd")"
return 0
fi
done
return 1
}
ensure_puppeteer_browser() {
local found kind path
if found=$(find_browser); then
kind="${found%%|*}"
path="${found#*|}"
write_puppeteer_config "$kind" "$path"
log_info ---icon 'π' 'Browser: <u>%s</u>' "$path"
return 0
fi
log_info ---icon 'π¦' 'Installing chrome-headless-shell for Puppeteer...'
if ! npx -y @puppeteer/browsers install chrome-headless-shell >/dev/null; then
die 1 'No Chrome/Chromium found and chrome-headless-shell install failed'
fi
if found=$(find_browser); then
kind="${found%%|*}"
path="${found#*|}"
write_puppeteer_config "$kind" "$path"
log_info ---icon 'π' 'Browser: <u>%s</u>' "$path"
return 0
fi
die 1 'Could not find a Chrome/Chromium browser for Mermaid CLI'
}
render_diagram() {
local source_path="$1" output_path="$2"
local -a args=(-i "$source_path" -o "$output_path" -b transparent -q)
[[ -n "$_PUPPETEER_CONFIG" ]] && args+=(-p "$_PUPPETEER_CONFIG")
[[ "$FORMAT" == png ]] && args+=(-e png)
[[ -n "$WIDTH" ]] && args+=(-w "$WIDTH")
[[ -n "$HEIGHT" ]] && args+=(-H "$HEIGHT")
[[ -n "$SCALE" ]] && args+=(-s "$SCALE")
local stderr_file
stderr_file=$(mktemp)
if ! "${_MMDC_CMD[@]}" "${args[@]}" 2>"$stderr_file"; then
local err
err=$(<"$stderr_file")
rm -f "$stderr_file"
printf '%s\n' "$err" >&2
return 1
fi
rm -f "$stderr_file"
}
extract_blocks() {
local file="$1" work_dir="$2"
python3 - "$file" "$work_dir" <<'PY'
import os
import re
import sys
import unicodedata
def slugify(text: str) -> str:
text = re.sub(r"[\U0001F300-\U0001FAFF\U00002600-\U000027BF]", "", text)
text = unicodedata.normalize("NFKD", text)
text = "".join(c for c in text if not unicodedata.combining(c))
text = text.lower()
text = re.sub(r"[^a-z0-9]+", "-", text).strip("-")[:80]
return text
def main() -> None:
md_path, work_dir = sys.argv[1], sys.argv[2]
with open(md_path, encoding="utf-8") as handle:
lines = handle.readlines()
last_heading = ""
in_mermaid = False
current: list[str] = []
start_line = 0
block_index = 0
for line_no, raw in enumerate(lines, 1):
line = raw.rstrip("\n")
heading = re.match(r"^#{1,6}\s+(.+)$", line)
if heading:
last_heading = slugify(heading.group(1))
if re.match(r"^```mermaid\s*$", line):
in_mermaid = True
current = []
start_line = line_no
continue
if in_mermaid and re.match(r"^```\s*$", line):
in_mermaid = False
content = "\n".join(current).strip()
if content:
block_index += 1
path = os.path.join(work_dir, f"block-{block_index}.mmd")
with open(path, "w", encoding="utf-8") as out:
out.write(content + "\n")
print(f"{block_index}|{start_line}|{last_heading}|{path}")
continue
if in_mermaid:
current.append(line)
if __name__ == "__main__":
main()
PY
}
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
show_usage
;;
-u | --update)
update_application
;;
--out | -o)
OUT="$2"
shift 2
;;
--format | -f)
FORMAT="$2"
if [[ "$FORMAT" != svg && "$FORMAT" != png ]]; then
die 1 'Unsupported format: %s. Use svg or png.' "$FORMAT"
fi
shift 2
;;
--width | -w)
WIDTH=$(parse_positive_int "$2" "Width")
shift 2
;;
--height | -H)
HEIGHT=$(parse_positive_int "$2" "Height")
shift 2
;;
--scale | -s)
SCALE=$(parse_positive_number "$2" "Scale")
shift 2
;;
--jobs | -j)
JOBS=$(parse_positive_int "$2" "Jobs")
shift 2
;;
--prefix)
PREFIX="$2"
shift 2
;;
--dry-run)
DRY_RUN=1
shift
;;
-*)
die 1 'Unknown option: %s' "$1"
;;
*)
if [[ -n "$INPUT" ]]; then
die 1 'More than one input file provided: %s' "$1"
fi
INPUT="$1"
shift
;;
esac
done
if [[ -z "$INPUT" ]]; then
show_usage 1
fi
[[ -z "$JOBS" ]] && JOBS=$(default_jobs)
local input_path
input_path=$(cd "$(dirname "$INPUT")" && pwd)/$(basename "$INPUT")
if [[ ! -f "$input_path" ]]; then
die 1 'File not found: %s' "$input_path"
fi
log_title 'Mermaid Diagram Exporter'
_MERMAID_WORK_DIR=$(mktemp -d "${TMPDIR:-/tmp}/mermaid-export-XXXXXX")
trap 'wait 2>/dev/null || true; rm -rf "${_MERMAID_WORK_DIR:-}"' EXIT
local -a manifest=()
while IFS= read -r entry; do
manifest+=("$entry")
done < <(extract_blocks "$input_path" "$_MERMAID_WORK_DIR")
if [[ "${#manifest[@]}" -eq 0 ]]; then
log_warning ---icon 'π' 'No <code>mermaid</code> blocks found in: <u>%s</u>' "$input_path"
return 0
fi
local base_name out_dir
base_name=$(basename "$input_path")
base_name="${base_name%.*}"
if [[ -n "$OUT" ]]; then
out_dir=$(cd "$(dirname "$OUT")" && pwd)/$(basename "$OUT")
else
out_dir="$(dirname "$input_path")/${base_name}-diagrams"
fi
declare -A name_counts=()
local -a names=()
local entry index start_line heading content_file name base count
for entry in "${manifest[@]}"; do
IFS='|' read -r index start_line heading content_file <<<"$entry"
base="$heading"
if [[ -z "$base" ]]; then
base=$(printf 'diagram-%02d' "$index")
fi
count=${name_counts[$base]:-0}
name_counts[$base]=$((count + 1))
if [[ "$count" -eq 0 ]]; then
name="${PREFIX}${base}"
else
name="${PREFIX}${base}-$((count + 1))"
fi
names+=("$name|$start_line|$content_file")
done
log_info ---icon 'π' 'Source: <u>%s</u>' "$input_path"
log_info ---icon 'π' 'Diagrams: <b>%d</b>' "${#manifest[@]}"
log_info ---icon 'π' 'Output directory: <u>%s</u>' "$out_dir"
log_info ---icon 'πΌ' 'Format: <b>%s</b>' "$FORMAT"
[[ -n "$WIDTH" ]] && log_info ---icon 'β' 'Width: <b>%spx</b>' "$WIDTH"
[[ -n "$HEIGHT" ]] && log_info ---icon 'β' 'Height: <b>%spx</b>' "$HEIGHT"
[[ -n "$SCALE" ]] && log_info ---icon 'π' 'Scale: <b>%s</b>' "$SCALE"
[[ "$DRY_RUN" -eq 0 ]] && log_info ---icon 'β' 'Parallel jobs: <b>%d</b>' "$JOBS"
if [[ "$DRY_RUN" -eq 1 ]]; then
local i=0
for entry in "${names[@]}"; do
IFS='|' read -r name start_line _ <<<"$entry"
i=$((i + 1))
log_info ' [%d] line %d β <code>%s.%s</code>' "$i" "$start_line" "$name" "$FORMAT"
done
return 0
fi
init_mmdc
ensure_puppeteer_browser
mkdir -p "$out_dir"
local -a render_queue=()
for entry in "${names[@]}"; do
IFS='|' read -r name start_line content_file <<<"$entry"
local source_path="$_MERMAID_WORK_DIR/${name}.mmd"
cp "$content_file" "$source_path"
render_queue+=("$name|$start_line|$source_path|$out_dir/$name.$FORMAT")
done
local active=0 fail_file had_fail=0
fail_file="$_MERMAID_WORK_DIR/render-failures.log"
: >"$fail_file"
render_worker() {
local name="$1" start_line="$2" source_path="$3" output_path="$4"
if ! render_diagram "$source_path" "$output_path"; then
{
printf '%s\n' "$name"
} >>"$fail_file"
return 1
fi
log_success ---icon 'πΌ' '<code>%s</code> (line %d)' "$(basename "$output_path")" "$start_line"
}
for item in "${render_queue[@]}"; do
IFS='|' read -r name start_line source_path output_path <<<"$item"
while [[ "$active" -ge "$JOBS" ]]; do
wait -n || had_fail=1
active=$((active - 1))
done
render_worker "$name" "$start_line" "$source_path" "$output_path" &
active=$((active + 1))
done
while [[ "$active" -gt 0 ]]; do
wait -n || had_fail=1
active=$((active - 1))
done
if [[ "$had_fail" -ne 0 || -s "$fail_file" ]]; then
die 1 'One or more diagrams failed to render'
fi
log_success ---icon 'β¨' 'Done: <u>%s</u>' "$out_dir"
}
main "$@"