-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathepub-fixup
executable file
·139 lines (116 loc) · 3.66 KB
/
epub-fixup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
# SPDX-FileCopyrightText: 2021 - 2024 sudorook <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -euo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command find sed zip unzip && exit 3
#
# Functions
#
# Add read and write permissions to CSS files
function set_rw {
find . -type d -exec chmod u+rwx,+rx {} \;
find . -type f -iname "*.css" -exec chmod u+w,+r {} \;
}
# Remove justified text
function unjustify {
find . -type f -iname "*.css" -exec sed -i \
-e "s,text-align\s*:\s*justify;,/*text-align: justify;*/,g" \
-e "s,text-align\s*:\s*justify !important;,/*text-align: justify !important;*/,g" \
-e "s,text-align\s*:\s*justify[\n\r]*$,/*text-align: justify*/,g" \
-e "s,text-align\s*:\s*justify},/*text-align: justify*/},g" {} \;
}
# Remove hard-coded colors
function unset_colors {
find . -type f -iname "*.css" -exec sed -i \
-e "s,background-color\ *:\ *#\([[:xdigit:]]\+\);,/*background-color: \1;*/,g" \
-e "s,background-color\ *:\ *#\([[:xdigit:]]\+\)$,/*background-color: \1*/,g" \
-e "s,background-color\ *:\ *#\([[:xdigit:]]\+\)},/*background-color: \1*/},g" \
-e "s,background-color\ *:\ *\(rgba([\d[[:digit:]]\,\ ]\+)\);,/*background-color: \1;*/,g" \
-e "s,background-color\ *:\ *\(rgba([\d[[:digit:]]\,\ ]\+)\)$,/*background-color: \1*/,g" \
-e "s,background-color\ *:\ *\(rgba([\d[[:digit:]]\,\ ]\+)\)},/*background-color: \1*/},g" \
-e "s,\(;*\ *\)color\ *:\ *#\([[:xdigit:]]\+\);,\1/*color: \2;*/,g" \
-e "s,\(;*\ *\)color\ *:\ *#\([[:xdigit:]]\+\)$,\1/*color: \2*/,g" \
-e "s,\(;*\ *\)color\ *:\ *#\([[:xdigit:]]\+\)},\1/*color: \2*/},g" {} \;
}
# Check if file is ePub
function is_epub {
local in="${1}"
local ext="${1##*.}"
local mime
mime="$(file -b --mime-type "${in}")"
if [[ "${mime}" =~ epub ]] || [[ "${ext,,}" = epub ]]; then
return 0
else
return 1
fi
}
function print_usage {
show_header "Usage: epub-fixup -c|--unset_color <input>"
show_listitem " -c (optional) comment out hard-coded colors"
show_listitem " -h print (this) help message"
}
#
# Main
#
UNSETCOLOR=false
OPTIONS=ch
LONGOPTIONS=unset_color,help
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ "${#}" -ge 1 ]; do
case "${1}" in
-c | --unset_color)
UNSETCOLOR=true
shift 1
;;
-h | --help)
print_usage
exit
;;
--)
shift
break
;;
*)
show_error "ERROR: invalid flag."
print_usage
exit 3
;;
esac
done
if ! is_epub "${1}"; then
show_error "ERROR: Input not ePub format."
exit 3
fi
FILE="$(basename "${1}")"
NAME="${1%.*}"
EXTENSION="${1##*.}"
BUILDDIR="$(mktemp -d)"
trap 'rm -rf "${BUILDDIR}"; exit' INT TERM ERR EXIT
cp "${1}" "${BUILDDIR}"
pushd "${BUILDDIR}" > /dev/null
unzip "${FILE}"
set_rw
unjustify
if ${UNSETCOLOR}; then
unset_colors
fi
zip -u "${FILE}" || true # override return false if no update performed
popd > /dev/null
mv "${BUILDDIR}/${FILE}" "${NAME} new.${EXTENSION}"