Skip to content

Instantly share code, notes, and snippets.

@rparree
Last active May 8, 2020 21:11
Show Gist options
  • Select an option

  • Save rparree/6106568 to your computer and use it in GitHub Desktop.

Select an option

Save rparree/6106568 to your computer and use it in GitHub Desktop.
Script i made to clean SVG export from Visio (2007). Testing the cleaned SVGs in Chrome, Firefox and Inkscape.
#! /bin/sh
#set px for font size with metric
perl -pi -e 's/font-size:(\d+);/font-size:$1px;/g' "$1"
#replace em metrics with px (this is a bit brittle, as the ems are relative to the container text)
#First the @dy=".." (multiply by 8)
perl -pi -e 's/dy=\"(\d+(\.\d+)?)em\"/"dy=\"".sprintf("%.1f",$1*8)."px\""/ge' "$1"
#Then all the others (multiply by 12, which is the default font size set bu visio export)
perl -pi -e 's/(\d+(\.\d+)?)em/sprintf("%.1f",$1*12)."px"/ge' "$1"
#Convert in to cm (European ;)
perl -pi -e 's/(\d+(\.\d+)?)in/sprintf("%.1f",$1*2.54)."cm"/eg' "$1"
#Remove comments (just cleanup)
xmlstarlet ed -L -d "//comment()" "$1"
#Remove all nodes from Visio namespace
xmlstarlet ed -L -d "//node()[namespace-uri()='http://schemas.microsoft.com/visio/2003/SVGExtensions/']" "$1"
#Remove all attributes from Visio namespace
xmlstarlet ed -L -d "//attribute::*[namespace-uri()='http://schemas.microsoft.com/visio/2003/SVGExtensions/']" "$1"
#Remove title and desc elements (just cleanup)
xmlstarlet ed -L -d "//node()[local-name()='title']" "$1"
xmlstarlet ed -L -d "//node()[local-name()='desc']" "$1"
#remove visio namespace declaration
perl -pi -e 's/xmlns:v=".*?"//' "$1"
#remove blank lines
sed -i '/^\s*$/d' "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment