Skip to content

Instantly share code, notes, and snippets.

@AaronDMarasco-VSI
Created January 25, 2019 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AaronDMarasco-VSI/c6079be0affb2652002cdce4879b2b18 to your computer and use it in GitHub Desktop.
Save AaronDMarasco-VSI/c6079be0affb2652002cdce4879b2b18 to your computer and use it in GitHub Desktop.
Clean up LibreOffice FODP files
#!/bin/bash
# There are many times OpenOffice / LibreOffice will put in useless spans.
# What it does:
# Finds a span with a style that has the format <capital letter><1 or more digits>
# Finds the close tag and then looks to see if the NEXT span has the same style defined
# If they match, the first end and the second begin are stripped
# (This is why it has to loop repeatedly, a third won't be seen unless a fourth - they
# would match as well)
[ "$1" == '' ] && echo "No parameters given: try '$0 *' or '$0 *fodp'"
for f in "$@"; do
[ -f $f ] || { echo "Skipping non-file '$f'"; continue; }
[ ${f##*.} == "fodp" ] || { echo "Skipping non-fodp '$f'"; continue; }
NEW=1
FIRST=$(stat -c %s $f)
echo -n "Processing $f: "
while [ $((ORIG-NEW)) -ne 0 ]; do
ORIG=$(stat -c %s $f)
perl -i -pe 's|<text:span text:style-name="([A-Z]\d+)">([^<]*?)</text:span><text:span text:style-name="\1">([^<]*?)</text:span>|<text:span text:style-name="\1">\2\3</text:span>|g' $f
NEW=$(stat -c %s $f)
done
DELTA=$((FIRST-NEW))
if [ $DELTA -ne 0 ]; then
echo "stripped $((FIRST-NEW)) bytes"
# git diff --color=always --stat=,1 $f | cut -f2- -d'|' | head -1
else
echo "(no change)"
fi
done
@AaronDMarasco-VSI
Copy link
Author

AaronDMarasco-VSI commented Jan 25, 2019

Seriously... FODP files change so much when you just hit "save."

-      <text:p text:style-name="P21"><text:span text:style-name="T11">Devic</text:span><text:span text:style-name="T11">e </text:span><text:span text:style-name="T11">Suppo</text:span><text:span text:style-name="T11">rt </text:span><text:span text:style-name="T11">Stack</text:span></text:p>
+      <text:p text:style-name="P21"><text:span text:style-name="T11">Device Support Stack</text:span></text:p>

Processing Extra_Device_Support_Development.fodp: stripped 34264 bytes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment