#!/bin/rc
# epub2html - convert epub to html
# usage: epub2html file.epub
# bugs: only one epub at a time
# set some defaults
rfork e
cwd=`{pwd}
fn usage{
echo Usage: epub2html file.epub >[1=2]
exit usage
}
if(! ~ $#* 1) usage
file=$1
if(! ~ $file /*) file=`{cleanname $cwd/$1}
if(! test -f $file && ! ~ $1 *.[Ee][Pp][Uu][Bb]) usage
name=`{basename $1 | sed 's/\.[Ee][Pp][Uu][Bb]//'}
dir=$name^_files
# determine directory name of toc file
fn ops{
ops=`{ls -p $1 | grep -i '^o.*ps'}
if(~ $#ops 0) echo $1
if not{
toc=`{ls -p $1/$ops | grep -i 'toc.ncx'}
if(~ $#toc 0) echo $1
if not echo $1/$ops
}
}
# extract epub and chapter information
mkdir -p $dir && cd $dir
unzip -af $file >/dev/null >[2=1]
ops=`{ops $cwd/$dir} && cd $ops
cat [Tt][Oo][Cc].[Nn][Cc][Xx] | sed -n '/(.*)<\/text>.*/\1/p' > chaps
cat [Tt][Oo][Cc].[Nn][Cc][Xx] | sed -n '/ links
# generate html index
cat < $cwd/$name.html
Contents
Contents:
eof
for(i in `{seq `{cat links | wc -l}}){
link=`{sed -n $i^p links}
chap=`{sed -n $i^p chaps}
echo ' '$"chap'
' \
>> $cwd/$name.html
}
cat <> $cwd/$name.html
eof