-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathepub-trans.sh
More file actions
executable file
·179 lines (148 loc) · 6.01 KB
/
epub-trans.sh
File metadata and controls
executable file
·179 lines (148 loc) · 6.01 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/sh
# Preslovljava e-knjigu
# Ulazni parametar 1: Ime EPUB datoteke
# Dodeliti bilo koju drugu vrednost (osim 1)
# ako se ne zeli zamena praznina u HTML datotekama
REPLACE_HTML_SPACES=1
# Podesiti putanje ukoliko se razlikuju na ovom sistemu
# Put do komande "file"
FILE_CMD=/usr/bin/file
# Put do komande "tr"
TR_CMD=/usr/bin/tr
ROMANS=../roman.txt
LATINS=../lat.txt
if [ $# -eq 0 ]; then
echo Morate zadati naziv elektronske knjige.
exit 1
fi
function trans_toc {
# Translate toc.ncx
if [ -f toc.ncx ]; then
echo "Pretvaram toc.ncx (pronadjen u $1) ..."
uniconv.py -i toc.ncx -t xml -b -o toc-roman.ncx
echo " Obrada rimskih brojeva ..."
subroman.py -i toc-roman.ncx -o toc-sr.ncx
if [ "${REPLACE_HTML_SPACES}" -eq 1 ]; then
sed -i 's/%20/-/g' toc-sr.ncx
fi
if [ -f toc-sr.ncx ]; then
mv toc-sr.ncx toc.ncx
rm -f toc-roman.ncx
fi
fi
}
function trans_content {
# Translate content.opf
if [ -f content.opf ]; then
echo "Pretvaram content.opf (pronadjen u $1) ..."
# Use flag -b for handling tags in eBooks
uniconv.py -i content.opf -t xml -b -o content-sr.opf
if [ "${REPLACE_HTML_SPACES}" -eq 1 ]; then
sed -i 's/%20/-/g' content-sr.opf
fi
if [ -f content-sr.opf ]; then
mv content-sr.opf content.opf
fi
fi
}
function remove_spaces_in_file_names {
echo "Uklanjam razmake iz naziva datoteka ..."
for extension in css jpg jpeg png gif
do
find . -type f -name "*.${extension}" -print0 | while read -d '' -r file
do
filenosp=`echo "${file}" | tr [[:blank:]] '-' | tr -s '-'`
if [ "${filenosp}" != "${file}" ]; then
mv "${file}" "${filenosp}"
echo Datoteka "${file}" preimenovana u "${filenosp}"
fi
done
done
}
function trans_xhtml {
find . \( -name "*ml" -o -name "*ML" -o -name "*htm" \) -type f -print0 | while read -d '' -r file
do
# Uzmi prezime datoteke
file_ext_orig=${file##*.}
filext=`echo "$file_ext_orig" | tr [[:upper:]] [[:lower:]]`
if [ "${filext}" == "xml" ]; then
uniconv.py -i "${file}" -t xml -o "${file}".1
else
uniconv.py -i "${file}" -t html -o "${file}".1
fi
# Rimski brojevi
subroman.py -i "${file}".1 -o "${file}".sr
# Ukloni
sed -i 's/ //g' "${file}".sr
# Ukloni prazne linije
sed -i 's///g' "${file}".sr
sed -i '/^$/d' "${file}".sr
# Ukloni praznine - OPREZ!!!
if [ "${REPLACE_HTML_SPACES}" -eq 1 ]; then
sed -i 's/%20/-/g' "${file}".sr
fi
# Zameni odredjene regularne izraze
regexconv.py -i "${file}".sr -o "${file}".regex
filenosp=`echo "${file}" | ${TR_CMD} [[:blank:]] '-' | ${TR_CMD} -s '-'`
[ -f "${file}" ] && mv "${file}".regex "${filenosp}"
rm -f "${file}".sr "${file}".1
if [ "${filenosp}" != "${file}" ]; then
rm -f "${file}"
fi
done
}
EBOOK_NAME=$1
EBOOK_BASENAME=`basename "${EBOOK_NAME}" .epub`
EBOOK_CLEAN_NAME=`echo "$EBOOK_BASENAME" | tr [[:upper:]] [[:lower:]] | tr [[:punct:]] '-' | tr [[:blank:]] '-' | tr -s '-'`-sr
if [ ! -d ${EBOOK_CLEAN_NAME} ]; then
echo Direktorijum ${EBOOK_CLEAN_NAME} ne postoji, izlazim ...
exit 2
fi
OLDPWD=`pwd`
cd ${EBOOK_CLEAN_NAME}
echo "Pretvaram poglavlja ..."
trans_xhtml
trans_toc ${EBOOK_CLEAN_NAME}
trans_content ${EBOOK_CLEAN_NAME}
echo "Strana slova:" > ${LATINS}
echo " " >> ${LATINS}
# Provera da li je nesto preslovljeno, a nije trebalo ...
# Nesrpska :) slova
find . \( -name "*ml" -o -name "*ML" -o -name "*htm" -o -name "*[Tt][Oo][Cc]" -o -name "*[Nn][Cc][Xx]" \) -type f -print -exec egrep -n '[XxYyQqWw]' {} \; >> ${LATINS}
# Nemacka slova
find . \( -name "*ml" -o -name "*ML" -o -name "*htm" -o -name "*[Tt][Oo][Cc]" -o -name "*[Nn][Cc][Xx]" \) -type f -print -exec egrep -n '[äöüßÄÖÜẞ]' {} \; >> ${LATINS}
# Francuska slova, bez nemackih
find . \( -name "*ml" -o -name "*ML" -o -name "*htm" -o -name "*[Tt][Oo][Cc]" -o -name "*[Nn][Cc][Xx]" \) -type f -print -exec egrep -n '[àâôéèëêïîçùûÿæœÀÂÔÉÈËÊÏΟÇÙÛÆŒ]' {} \; >> ${LATINS}
# Poljska slova
find . \( -name "*ml" -o -name "*ML" -o -name "*htm" -o -name "*[Tt][Oo][Cc]" -o -name "*[Nn][Cc][Xx]" \) -type f -print -exec egrep -n '[ąęłńóśźżĄĘŁŃÓŚŹŻ]' {} \; >> ${LATINS}
# Italijanska slova, bez francuskih i nemackih
find . \( -name "*ml" -o -name "*ML" -o -name "*htm" -o -name "*[Tt][Oo][Cc]" -o -name "*[Nn][Cc][Xx]" \) -type f -print -exec egrep -n '[ìíîòúÌÍÎÒÚ]' {} \; >> ${LATINS}
# Spanska slova
find . \( -name "*ml" -o -name "*ML" -o -name "*htm" -o -name "*[Tt][Oo][Cc]" -o -name "*[Nn][Cc][Xx]" \) -type f -print -exec egrep -n '[áñÁÑ]' {} \; >> ${LATINS}
echo " " >> ${LATINS}
echo "Moguce strane reci:" > ${LATINS}
echo " " >> ${LATINS}
find . \( -name "*ml" -o -name "*ML" -o -name "*htm" -o -name "*[Tt][Oo][Cc]" -o -name "*[Nn][Cc][Xx]" \) -type f -print -exec egrep -n 'аа|бб|цц|дд|ее|фф|гг|хх|ии|лл|мм|нн|оо|пп|рр|сс|тт|уу|вв|зз|цх|гх|кх|пх|сх[^в]|оу|Тх|гли' {} \; >> ${LATINS}
sed -i G ${LATINS}
> ${ROMANS}
# Pronaci rimsko 4 ("IV") ili 6 ("VI") koji je mozda deo nekog naslova
echo "Pronalazim rimsko IV ..."
find . \( -name "*ml" -o -name "*ML" -o -name "*htm" -o -name "*[Tt][Oo][Cc]" -o -name "*[Nn][Cc][Xx]" \) -type f -print -exec egrep -n '[АБВГДЂЕЖЗИЈКЛЉМНЉОПРСТЋУФХЦЧЏШ]*IV[АБВГДЂЕЖЗИЈКЛЉМНЉОПРСТЋУФХЦЧЏШ]*' {} \; >> ${ROMANS}
echo "Pronalazim rimsko VI ..."
find . \( -name "*ml" -o -name "*ML" -o -name "*htm" -o -name "*[Tt][Oo][Cc]" -o -name "*[Nn][Cc][Xx]" \) -type f -print -exec egrep -n '[АБВГДЂЕЖЗИЈКЛЉМНЉОПРСТЋУФХЦЧЏШ]*VI[АБВГДЂЕЖЗИЈКЛЉМНЉОПРСТЋУФХЦЧЏШ]*' {} \; >> ${ROMANS}
echo "Pronalazim rimsko I i V ..."
find . \( -name "*ml" -o -name "*ML" -o -name "*htm" -o -name "*[Tt][Oo][Cc]" -o -name "*[Nn][Cc][Xx]" \) -type f -print -exec egrep -n '[^“\?\!\.]\s+[И|В]\s+' {} \; >> ${ROMANS}
sed -i G ${ROMANS}
for dir in OEBPS OPS
do
if [ -d $dir ]; then
cd $dir
trans_toc $dir
trans_content $dir
remove_spaces_in_file_names
fi
done
cd "${OLDPWD}"
echo ${EBOOK_BASENAME} je preslovljena na cirilicu.
echo Proveriti datoteke ${ROMANS} i ${LATINS}.