mirror of
https://github.com/dolanmiu/docx.git
synced 2026-12-01 16:55:59 +01:00
* chore: organise the demos into a folder for each topic The demos were one flat list of 124 numbered files, which made it hard to find the one you wanted. Each is now in a folder for its topic, such as demo/tables/basic-table.ts, with a name that says what it shows instead of a number. npm run demo lists every demo and narrows the list as you type. It also takes a demo's path or name, a topic, or search words. The demos' images moved to demo/assets/images, as images is now a topic. The docs' links, the Demos workflow, the shape render script and its reference images use the new paths and names. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ * fix: list only a topic's own demos, and drop stale output file names npm run demo text also offered the text-boxes demos and others with "text" in their name, so a topic now searches for "text/". Three demos logged their old numbered file name after writing My Document.docx. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
83 lines
4.2 KiB
Bash
Executable File
83 lines
4.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Runs the shape and chart demos, checks where their drawings' Word 2010 extensions are and that their drawing ids are
|
|
# unique, and draws each page as a PNG with LibreOffice. The Demos workflow checks them with the Open XML SDK validator.
|
|
#
|
|
# Usage: scripts/render-shape-demos.sh [output directory] [demo ...]
|
|
#
|
|
# Each demo is its topic and name, such as shapes/shape-groups. The output directory (default build/shape-demos) gets
|
|
# each demo's .docx, .pdf and one PNG per page, named after the demo. Without demos, all the shape and chart demos are
|
|
# run. Needs the package built (npm run build) and perl.
|
|
#
|
|
# The pages are drawn with LibreOffice (set SOFFICE if soffice isn't on the PATH) and pdftoppm from Poppler.
|
|
# When SHAPE_RENDER_IMAGE names a Docker image built from scripts/shape-demos/Dockerfile, they are drawn in
|
|
# that image instead, with the pinned LibreOffice and fonts the visual regression tests compare against:
|
|
#
|
|
# docker build --platform linux/amd64 -t docx-shape-renderer scripts/shape-demos
|
|
# SHAPE_RENDER_IMAGE=docx-shape-renderer scripts/render-shape-demos.sh
|
|
# npm run run-ts -- scripts/compare-shape-renders.ts
|
|
|
|
set -euo pipefail
|
|
|
|
OUT="${1:-build/shape-demos}"
|
|
shift || true
|
|
DEMOS=("$@")
|
|
if [ ${#DEMOS[@]} -eq 0 ]; then
|
|
DEMOS=(shapes/inline-shapes shapes/shapes shapes/shape-groups shapes/shape-connectors shapes/shape-styles shapes/shape-diagrams shapes/shape-layout shapes/shape-document-styles shapes/shape-swimlanes shapes/shape-page-layout styles/theme shapes/custom-shapes charts/charts charts/chart-options charts/charts-in-templates charts/chart-data-in-templates charts/more-charts)
|
|
fi
|
|
SOFFICE="${SOFFICE:-soffice}"
|
|
|
|
mkdir -p "$OUT"
|
|
# Pages left over from an earlier run would be compared as if they were new
|
|
rm -f "$OUT"/*.png "$OUT"/*.pdf
|
|
failed=0
|
|
|
|
for demo in "${DEMOS[@]}"; do
|
|
echo "::group::$demo"
|
|
npm run --silent run-ts -- "./demo/$demo.ts"
|
|
docx="$OUT/$(basename "$demo").docx"
|
|
cp "My Document.docx" "$docx"
|
|
|
|
# Where the Word 2010 drawing extensions (wp14) are: a floating drawing's relative sizes come after its graphic, and
|
|
# a percentage offset is the choice for a position, with its offset in EMUs as the fallback
|
|
extracted="$(mktemp -d)"
|
|
unzip -q -o "$docx" -d "$extracted"
|
|
misplaced="$(perl -0ne '$all = () = /<wp14:(?:sizeRel[HV]|pctPos[HV]Offset)\b/g;
|
|
$placed = () = /<wp:position([HV]) relativeFrom="\w+"><mc:AlternateContent><mc:Choice Requires="wp14"><wp14:pctPos\1Offset>-?\d+<\/wp14:pctPos\1Offset><\/mc:Choice><mc:Fallback><wp:posOffset>-?\d+<\/wp:posOffset><\/mc:Fallback><\/mc:AlternateContent><\/wp:position\1>/g;
|
|
while (/<\/a:graphic>((?:<wp14:sizeRel[HV]\b.*?<\/wp14:sizeRel[HV]>)+)<\/wp:anchor>/gs) { my $sizes = $1; $placed += () = $sizes =~ /<wp14:sizeRel[HV]\b/g; }
|
|
print $all - $placed' "$extracted/word/document.xml")"
|
|
if [ "$misplaced" != 0 ]; then
|
|
echo "::error::$demo has $misplaced Word 2010 drawing extensions where they don't go"
|
|
failed=1
|
|
fi
|
|
|
|
# Drawing ids must be unique across the document
|
|
repeated="$(grep -ho '\(wp:docPr\|wps:cNvPr\|pic:cNvPr\|wpg:cNvPr\) id="[0-9]*"' "$extracted"/word/*.xml | sed 's/.*id=//' | sort | uniq -d)"
|
|
if [ -n "$repeated" ]; then
|
|
echo "::error::$demo repeats drawing ids: $repeated"
|
|
failed=1
|
|
fi
|
|
rm -rf "$extracted"
|
|
echo "::endgroup::"
|
|
done
|
|
|
|
# Draw the pages
|
|
echo "::group::Drawing the pages"
|
|
DRAW='for docx in "$@"; do
|
|
soffice ${PROFILE:+"-env:UserInstallation=file://$PROFILE"} --headless --convert-to pdf --outdir "$(dirname "$docx")" "$docx" > /dev/null 2>&1
|
|
pdftoppm -png -r 96 "${docx%.docx}.pdf" "${docx%.docx}"
|
|
done'
|
|
FILES=()
|
|
for demo in "${DEMOS[@]}"; do
|
|
FILES+=("$(basename "$demo").docx")
|
|
done
|
|
if [ -n "${SHAPE_RENDER_IMAGE:-}" ]; then
|
|
# LibreOffice keeps its settings in a profile, which has to be somewhere the user running it can write
|
|
docker run --rm --platform linux/amd64 --user "$(id -u):$(id -g)" -e PROFILE=/tmp/profile -v "$(cd "$OUT" && pwd):/work" -w /work \
|
|
"$SHAPE_RENDER_IMAGE" sh -c "$DRAW" sh "${FILES[@]}"
|
|
else
|
|
(cd "$OUT" && PATH="$(dirname "$(command -v "$SOFFICE")"):$PATH" sh -c "$DRAW" sh "${FILES[@]}")
|
|
fi
|
|
echo "::endgroup::"
|
|
|
|
exit $failed
|