Generic script
#!/bin/bash
# Input SVG file
input_svg="$1"
if [ ! -f "$input_svg" ]; then
echo "Error: Input file '$input_svg' not found." >&2
exit 1
fi
# Get total number of symbols
num_symbols=$(xmlstarlet sel -t -m "//symbol" -v "count()" "$input_svg")
if [ "$num_symbols" -eq 0 ]; then
echo "No symbols found in '$input_svg'." >&2
exit 0
fi
# Process each symbol
for i in $(seq 1 "$num_symbols"); do
# Extract symbol attributes and content
symbol_id=$(xmlstarlet sel -t -m "//symbol[$i]" -v "@id" "$input_svg")
viewBox=$(xmlstarlet sel -t -m "//symbol[$i]" -v "@viewBox" "$input_svg")
width_attr=$(xmlstarlet sel -t -m "//symbol[$i]" -v "@width" "$input_svg")
height_attr=$(xmlstarlet sel -t -m "//symbol[$i]" -v "@height" "$input_svg")
preserveAspectRatio=$(xmlstarlet sel -t -m "//symbol[$i]" -v "@preserveAspectRatio" "$input_svg")
symbol_element=$(xmlstarlet sel -t -m "//symbol[$i]" -c . -n "$input_svg")
# Determine root SVG attributes
svg_viewBox="$viewBox"
svg_width="$width_attr"
svg_height="$height_attr"
# Handle missing viewBox
if [ -z "$svg_viewBox" ]; then
if [ -n "$svg_width" ] && [ -n "$svg_height" ]; then
svg_viewBox="0 0 $svg_width $svg_height"
else
svg_viewBox="0 0 100 100"
[ -z "$svg_width" ] && svg_width="100"
[ -z "$svg_height" ] && svg_height="100"
fi
elif [ -z "$svg_width" ] || [ -z "$svg_height" ]; then
# Extract viewBox dimensions if missing width/height
viewBox_width=$(echo "$svg_viewBox" | awk '{print $3}')
viewBox_height=$(echo "$svg_viewBox" | awk '{print $4}')
[ -z "$svg_width" ] && svg_width="$viewBox_width"
[ -z "$svg_height" ] && svg_height="$viewBox_height"
fi
# Create output file
output_file="${symbol_id}.svg"
cat > "$output_file" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
EOF
# Add SVG attributes if present
[ -n "$svg_viewBox" ] && echo " viewBox=\"$svg_viewBox\"" >> "$output_file"
[ -n "$svg_width" ] && echo " width=\"$svg_width\"" >> "$output_file"
[ -n "$svg_height" ] && echo " height=\"$svg_height\"" >> "$output_file"
[ -n "$preserveAspectRatio" ] && echo " preserveAspectRatio=\"$preserveAspectRatio\"" >> "$output_file"
# Complete SVG structure
cat >> "$output_file" << EOF
>
$symbol_element
<use xlink:href="#${symbol_id}" x="0" y="0" width="100%" height="100%" />
</svg>
EOF
echo "Created '$output_file'"
done
echo "Processed $num_symbols symbols."#!/bin/bash
# Check if input file is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <svg_file>"
exit 1
fi
input_file="$1"
i=0
while true; do
# Get symbol XML for current index
symbol_xml=$(dasel select -r -f xml "svg.symbol[$i]" "$input_file")
# Break if no more symbols
if [ -z "$symbol_xml" ]; then
break
fi
# Extract symbol ID
symbol_id=$(echo "$symbol_xml" | dasel select -r -f yaml 'id')
# Skip if no ID found
if [ -z "$symbol_id" ]; then
echo "Warning: Symbol at index $i has no ID, skipping"
i=$((i + 1))
continue
fi
# Extract viewBox (with default fallback)
symbol_viewBox=$(echo "$symbol_xml" | dasel select -r -f yaml 'viewBox')
: "${symbol_viewBox:='0 0 100 100'}"
# Create new SVG content
new_svg="<svg xmlns='http://www.w3.org/2000/svg' viewBox='$symbol_viewBox'>
$(echo "$symbol_xml" | sed 's/^/ /')
<use href='#$symbol_id' />
</svg>"
# Write to output file
echo "$new_svg" > "${symbol_id}.svg"
echo "Created ${symbol_id}.svg"
i=$((i + 1))
done
echo "Extraction complete"