2025-03-20 14:54:46 +02:00

63 lines
2.2 KiB
Bash

#!/bin/bash
#set -x
INPUT="$*"
OUTDIR=$(mktemp -d)
IMGDIR="$GITEA_WORK_DIR/custom/public/assets/img"
ln -s "$INPUT" "$OUTDIR"/schematic.kicad_sch
if [[ -d "$IMGDIR" && -w "$IMGDIR" ]]; then
# We can use caching
HASH=$(sha256sum "$INPUT" | cut -c1-64)
shopt -s nullglob
IMAGES=("${IMGDIR}"/${HASH}.*.svg)
shopt -u nullglob
if [ ${#IMAGES[@]} -le 0 ]; then
mapfile -t FILES \
< <(kicad-cli sch export svg --exclude-drawing-sheet --no-background-color --output "$OUTDIR" "$OUTDIR/schematic.kicad_sch" | grep -E 'Plotted to' | sed -r "s/^[^']+'|'[^']+$//g" )
file_num=1
for FILE in "${FILES[@]}"; do
FN="${IMGDIR}/${HASH}.${file_num}.svg"
inkscape --actions="page-fit-to-selection" -o "$FN" "$FILE"
file_num=$((file_num + 1))
break # TODO: find some way to handle related (hierarchical) schematic files, as currently only the active file is made available
done
shopt -s nullglob
IMAGES=("${IMGDIR}"/${HASH}.*.svg)
shopt -u nullglob
fi
for FILE in "${IMAGES[@]}"; do
echo '<div class="file"><div class="page">'
# echo "bla"
echo -n '<img src="/assets/img/'
echo -n "$(basename "${FILE}")"
echo -n '"'
echo -n ' class="full_width kicad_sch pan_zoom"'
echo '/>'
echo '</div></div>'
done
else
# We'll be generating the images on the fly, including them as base64
mapfile -t FILES \
< <(kicad-cli sch export svg --exclude-drawing-sheet --no-background-color --output "$OUTDIR" "$OUTDIR/schematic.kicad_sch" | grep -E 'Plotted to' | sed -r "s/^[^']+'|'[^']+$//g" )
for FILE in "${FILES[@]}"; do
echo -n '<div class="file"><div class="page">'
echo -n '<img src="data:image/svg+xml;base64,'
inkscape --actions="page-fit-to-selection" -o - "$FILE" | base64 --wrap=0
echo -n '"'
echo -n ' class="full_width kicad_sch pan_zoom"'
echo -n '/>'
echo '</div></div>'
break # TODO: find some way to handle related (hierarchical) schematic files, as currently only the active file is made available
done
fi
rm -R "$OUTDIR"