Created
January 14, 2011 16:16
-
-
Save iloveitaly/779813 to your computer and use it in GitHub Desktop.
Fixes the cover page for ePubs created by indesign
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Author: Michael Bianco <http://mabblog.com/> | |
# Adds css width to the cover page, links the cover image on the cover page to a metadata tag | |
# Properly handles the mimetype on recompression | |
# This allows a ePub generated by InDesign to properly display the cover image on iBooks when the ePub is NOT distributed via the iBooks store | |
# xml compression info from: http://www.mobileread.com/forums/showthread.php?t=55681 | |
WORKING_FOLDER=$HOME/Desktop/epub_work | |
PRODUCTION_FOLDER=$HOME/Desktop/epub_final | |
TEST_FILE=$HOME/Desktop/ePubs/test_file.epub | |
FILE_LIST=($TEST_FILE) | |
# == Comment Out the Below Code to Test on a Single ePub == | |
if [[ -z "$1" ]]; then | |
echo "Must specify file or directory" | |
exit 1 | |
fi | |
if [[ -d "$1" ]]; then | |
FILE_LIST=`find "$1" -name *.epub` | |
else | |
FILE_LIST="$1" | |
fi | |
# =================== | |
rm -R "$PRODUCTION_FOLDER" | |
mkdir "$PRODUCTION_FOLDER" | |
rm -R $WORKING_FOLDER | |
mkdir $WORKING_FOLDER | |
cd $WORKING_FOLDER | |
echo "$FILE_LIST" | while read EPUB_FILE; do | |
cp "$EPUB_FILE" "$WORKING_FOLDER" | |
EPUB_NAME=`basename "$EPUB_FILE"` | |
unzip -q "$WORKING_FOLDER/$EPUB_NAME" | |
rm "$WORKING_FOLDER/$EPUB_NAME" | |
echo "Processing E-Pub File: $EPUB_NAME" | |
echo "----------------------------------" | |
# find cover page, if two are found be a bit more specific | |
# note that this assumes that you're cover page contains to the word 'cover' or more specifically 'frontcover' | |
COVER_FILE_NAME=`find $PWD | ack -i "cover.*\.xhtml$"` | |
FILE_COUNT=`echo "$COVER_FILE_NAME" | wc -l` | |
if [[ ${FILE_COUNT/ /} -gt 1 ]]; then | |
COVER_FILE_NAME=`find $PWD | ack -i "frontcover.*\.xhtml$"` | |
fi | |
CONTENT_FILE=$PWD/OEBPS/content.opf | |
if [[ ${#COVER_FILE_NAME} -eq 0 ]]; then | |
echo "Invalid Cover: $EPUB_NAME" | |
rm -R "$WORKING_FOLDER" | |
mkdir "$WORKING_FOLDER" | |
cd "$WORKING_FOLDER" | |
continue | |
fi | |
# add the max width | |
echo "Found Cover File: $COVER_FILE_NAME" | |
sed -ised_backup "s/<img/<img style=\"max-width:100%\"/g" "$COVER_FILE_NAME" | |
# search for cover image | |
COVER_IMAGE_PATH=`cat "$COVER_FILE_NAME" | ack -o '<img[^>]*src="([^"]*)"' --output='$1'` | |
echo "Cover Image Path: $COVER_IMAGE_PATH" | |
# search for the cover id | |
echo "Found Content File: $CONTENT_FILE" | |
COVER_IMAGE_ID_PATTERN='id="([^\"]*)"[^>]*'$COVER_IMAGE_PATH'[^>]*media-type="image/jpeg"/>' | |
COVER_IMAGE_ID=`ack -o -h $COVER_IMAGE_ID_PATTERN --output='$1' < "$CONTENT_FILE"` | |
echo "Cover Image ID: $COVER_IMAGE_ID" | |
# add cover image | |
echo "Adding Cover image" | |
sed -ised_backup "s/<metadata>/<metadata><meta name=\"cover\" content=\"$COVER_IMAGE_ID\" \/>/g" "$CONTENT_FILE" | |
# add the spine | |
echo "Adding Guide" | |
COVER_FILE_BASE_NAME=`basename "$COVER_FILE_NAME"` | |
sed -ised_backup "s/<\/spine>/<\/spine><guide><reference type=\"cover\" title=\"Cover\" href=\"$COVER_FILE_BASE_NAME\" \/><\/guide>/g" "$CONTENT_FILE" | |
# remove the backup files | |
find "$PWD" -name *sed_backup -exec rm "{}" \; | |
# get a MIME type reference | |
zip -q -X0 $PRODUCTION_FOLDER/$EPUB_NAME mimetype | |
zip -q -rDX9 $PRODUCTION_FOLDER/$EPUB_NAME * -x "*.DS_Store" -x mimetype | |
rm -R "$WORKING_FOLDER" | |
mkdir "$WORKING_FOLDER" | |
cd "$WORKING_FOLDER" | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment