Created
April 29, 2025 08:31
-
-
Save SonOfLilit/08bf57449a214ccd2d3d85f0348e4e4b to your computer and use it in GitHub Desktop.
Fix epub Remarkable black bar issue
This file contains hidden or 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 | |
set -e # Exit immediately if a command exits with a non-zero status. | |
# --- Configuration --- | |
# CSS rule to comment out (adjust if needed, mindful of regex special chars) | |
TARGET_RULE_TEXT="background-color: inherit;" | |
# Regex pattern for sed (basic version: handles spaces around colon) | |
# Using \| as delimiter to avoid escaping slashes in rule | |
SED_PATTERN='background-color[[:space:]]*:[[:space:]]*inherit;' | |
# Replacement pattern for sed (comments out the matched pattern) | |
SED_REPLACEMENT='/* & */' # & substitutes the matched pattern | |
# --- Input Validation --- | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <input_epub_file>" | |
exit 1 | |
fi | |
input_epub="$1" | |
if [ ! -f "$input_epub" ]; then | |
echo "Error: Input file not found: '$input_epub'" | |
exit 1 | |
fi | |
# --- Prepare Filenames and Directories --- | |
# Attempt to get absolute path for robustness | |
# Fix: Correctly combine directory and basename in fallback using echo | |
input_epub_abs=$(readlink -f "$input_epub" 2>/dev/null || echo "$(cd "$(dirname "$input_epub")" && pwd)/$(basename "$input_epub")") | |
# Derive output filename relative to the original file's directory | |
base_name=$(basename "$input_epub" .epub) | |
dir_name=$(dirname "$input_epub_abs") | |
output_epub="$dir_name/${base_name}-FIXED.epub" | |
# Create temporary directory safely | |
tmp_dir=$(mktemp -d -t epub_fix_XXXXXX) | |
echo "Using temporary directory: $tmp_dir" | |
# --- Cleanup Function --- | |
# Ensures temporary directory is removed on exit, error, or interrupt | |
cleanup() { | |
echo "Cleaning up temporary directory: $tmp_dir" | |
rm -rf "$tmp_dir" | |
} | |
trap cleanup EXIT HUP INT QUIT TERM | |
# --- Unzip EPUB --- | |
echo "Unzipping '$input_epub_abs' to '$tmp_dir'..." | |
# Use -o to overwrite files without prompting if dir wasn't properly cleaned before | |
unzip -o -q "$input_epub_abs" -d "$tmp_dir" | |
if [ $? -ne 0 ]; then | |
echo "Error: Failed to unzip '$input_epub_abs'" | |
exit 1 # Exit is handled by set -e, but explicit exit is clearer | |
fi | |
echo "Unzip complete." | |
# --- Modify CSS Files --- | |
echo "Searching for CSS/HTML/XHTML files and commenting out '$TARGET_RULE_TEXT'..." | |
# Use find with -exec to directly call sed on found files. | |
# Using {} + executes sed on multiple files at once for efficiency. | |
# -i.bak creates backups, ensuring portability (GNU/BSD sed). | |
# Using | as sed delimiter avoids issues with slashes in patterns. | |
find "$tmp_dir" -type f \( -name "*.css" -o -name "*.xhtml" -o -name "*.html" \) \ | |
-exec sed -i.bak "s|$SED_PATTERN|$SED_REPLACEMENT|g" {} + | |
# Check the exit status of find (optional, set -e should handle it) | |
if [ $? -ne 0 ]; then | |
echo "Warning: find or sed command might have failed. Check output and .bak files in $tmp_dir if necessary." | |
# Decide if script should exit here or continue. Continuing allows zipping partially modified files. | |
fi | |
# Clean up backup files created by sed -i.bak | |
echo "Cleaning up sed backup files..." | |
find "$tmp_dir" -type f -name "*.bak" -delete | |
echo "CSS processing complete." | |
# --- Re-zip EPUB --- | |
echo "Re-zipping contents into '$output_epub'..." | |
mimetype_path="$tmp_dir/mimetype" | |
if [ ! -f "$mimetype_path" ]; then | |
echo "Error: 'mimetype' file not found in '$tmp_dir'. Cannot create valid EPUB." | |
exit 1 | |
fi | |
# Navigate to temp directory for correct relative paths in zip | |
original_pwd=$(pwd) | |
cd "$tmp_dir" | |
# Create the new EPUB, ensuring mimetype is first and uncompressed (-X0) | |
# Using a subshell to avoid permission issues with output file if created in restricted dir | |
(zip -X0 "$output_epub" mimetype) | |
if [ $? -ne 0 ]; then | |
echo "Error: Failed to add 'mimetype' to '$output_epub'" | |
cd "$original_pwd" # Go back before exiting | |
exit 1 | |
fi | |
# Add the rest of the files, recursively (-r), grow existing archive (-g), exclude mimetype | |
# -D prevents adding directory entries, often needed for EPUB validation | |
(zip -rgD "$output_epub" . -x mimetype) | |
if [ $? -ne 0 ]; then | |
echo "Error: Failed to add contents to '$output_epub'" | |
cd "$original_pwd" # Go back before exiting | |
exit 1 | |
fi | |
# Return to original directory | |
cd "$original_pwd" | |
echo "Successfully created fixed EPUB: '$output_epub'" | |
# Cleanup is handled by the trap EXIT | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment