Skip to content

Instantly share code, notes, and snippets.

@sjx95
Created January 30, 2020 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sjx95/d25726209cf6cd2b00c7e55633cea4ca to your computer and use it in GitHub Desktop.
Save sjx95/d25726209cf6cd2b00c7e55633cea4ca to your computer and use it in GitHub Desktop.
A simple converter for css in EPUB which covert 100vw/100vh to 100%
#!/bin/bash
# For batch convertion:
find *.epub | xargs -I FILE ./convert.sh FILE
#!/bin/bash
usage() {
echo "Usage: ./convert.sh FILE.epub"
echo "Will save new EPUB under output/FILE.epub"
exit 1
}
# Params check
if [ $# != 1 ]; then
usage
fi
FILE=$1
if [ ! -f "$FILE" ]; then
usage
fi
# Clear ./tmp/
if [ -d tmp ]; then
rm -r tmp
fi
mkdir tmp
cd tmp
# Unzip EPUB
unzip -q "../$FILE"
if [ -d OEBPS/Styles ]; then
cd OEBPS/Styles
else
exit 1
fi
# For check
echo "========== Match: 100vw =========="
grep "100vw" *.css
echo "========== Match: 100vh =========="
grep "100vh" *.css
echo "=================================="
# Change 100vw/100vh -> 100%
sed "s/100vw/100\%/g" -i *.css
sed "s/100vh/100\%/g" -i *.css
cd ../..
# Pack new EPUB files
if [ ! -d "../output" ]; then
mkdir ../output
fi
zip -qr "../output/$FILE" *
# Clean tmp/
cd ..
rm -r tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment