This repository has been archived by the owner on Nov 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pages2epub.sh
executable file
·130 lines (114 loc) · 3.58 KB
/
pages2epub.sh
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
#! /bin/sh
# -*- mode: shell-script; -*-
VERSION=0.1
eval file=\${$#}
if [ ! -e "$file" ] || [ ! "$1" ] ; then
cat <<EOH
pages2ePub v${VERSION}
Usage: pages2ePub.sh [--debug] [--output=file.epub] [--basename=basename] [--identifier=isbn] [--publisher=publisher] [--title=title] [--author=author] file.pages
EOH
else
while getopts "dobipta:-:" OPT
do
# gestion des options longues avec ou sans argument
[ $OPT = "-" ] && case "${OPTARG%%=*}" in
debug) OPT="d" ; debug="true" ;;
output) OPT="o" ; output="${OPTARG#*=}" ;;
basename) OPT="b" ; basename="${OPTARG#*=}" ;;
identifier) OPT="i" ; identifier="${OPTARG#*=}" ;;
publisher) OPT="p" ; publisher="${OPTARG#*=}" ;;
title) OPT="t" ; title="${OPTARG#*=}" ;;
author) OPT="a" ; author="${OPTARG#*=}" ;;
*) echo "option longue non permise -- ${OPTARG%%=*}" >&2 ; exit 65 ;;
esac
done
if [ ! $debug ] ; then
debugzip="-q"
fi
xsltfile=./pages2ePub.xsl
#TODO accepter un répertoire en argument et
#boucler sur tous les fichiers Pages
#for pagefile in $pagedir do
pagefile=$file
basedir=$(pwd)
if [ ! $basename ] ; then
base=$(basename ${pagefile} .pages)
else
base=$basename
fi
mkdir -p /tmp/epub/${base}
epubdir=$(mktemp -d /tmp/epub/${base}/XXX) || exit 1
extractdir=/tmp/extract/${base}
mkdir -p ${extractdir} || exit 1
# echo "Extract dir : $extractdir"
unzip ${debugzip} -o -d ${extractdir} ${pagefile}
xmlfile=${extractdir}/index.xml
xmllint --format $xmlfile > ${extractdir}/${base}.xml
# Récupération de la couverture
# cp ${extractdir}/QuickLook/Thumbnail.jpg ${epubdir}/cover.jpg
# cp ${extractdir}/*.JPG ${epubdir}/
# cp ${extractdir}/*.jpg ${epubdir}/
# cp *.otf ${epubdir}/
convert ${extractdir}/QuickLook/Thumbnail.jpg ${epubdir}/cover.png
# Création du fichier mimetype
echo "application/epub+zip" > ${epubdir}/mimetype
# Copie du fichier de couv
cp cover.html ${epubdir}/
# Création rep META-INF
mkdir ${epubdir}/META-INF
# On crée tous les fichiers via la transformation XSLT
htmlfile=${base}.html
if [ ! $identifier ] ; then
identifier="http://livre.im/content/${base}.epub"
fi
if [ ! "$publisher" ] ; then
publisher="immatériel.fr"
fi
if [ ! "$title" ] ; then
title="undefined"
fi
if [ ! "$author" ] ; then
author="undefined"
fi
# --stringparam title ${title} \
# echo "${epubdir}"
xsltproc --stringparam maindir ${epubdir} \
--stringparam mainfile ${htmlfile} --stringparam identifier ${identifier} --stringparam publisher "${publisher}" \
--stringparam author "${author}" --stringparam basename ${base} \
--stringparam title "${title}" \
${xsltfile} ${xmlfile} >${epubdir}/$htmlfile \
2>${extractdir}/last_transfo.log
# On crée une archive zip
conv_imgs=$(xsltproc --stringparam maindir ${epubdir} --stringparam extractdir ${extractdir} generate_images.xsl ${epubdir}/${base}.html)
if [ $debug ] ; then
echo $conv_imgs
fi
sh -c "${conv_imgs}"
cd ${epubdir}
cnt=$(ls -1 *.html| wc -l)
#echo $cnt
if [ $cnt -gt 4 ] ; then
if [ ! $debug ] ; then
rm ${base}.html
fi
fi
zip ${debugzip} -0Xr ${epubdir}/${base}.zip mimetype
zip ${debugzip} -X9Dr ${epubdir}/${base}.zip .
cd ${basedir}
if [ ! $output ] ; then
output=${basedir}/${base}.epub
fi
mv ${epubdir}/${base}.zip ${output}
if [ ! $debug ] ; then
# On supprime le rep temp
rm -r ${epubdir}
fi
if [ $debug ] ; then
cat <<EOF
*****
* Les fichiers ePub correspondant à ${pagefile} se trouvent dans ${epubdir}
**
EOF
fi
echo "Création du fichier ${output}"
fi