#!/bin/zsh
export syncmd='espeak'
export synargs='--stdin -w /proc/self/fd/1'
export playneeded=1
export playcmd='play'
export playargs='-q -'
export tmpdir=`mktemp -d --suffix ebook-speaker`
function doc2txt
{
export origdir=$PWD
getfile $@
catdoc $rawname
cd $origdir
}
function docx2txt
{
export origdir=$PWD
getfile $@
libreoffice -env:UserInstallation=file://$tmpdir --headless --cat $rawname
cd $origdir
}
function epub2txt
{
export origdir=$PWD
getfile $@
unzip -o $rawname > /dev/null 2>/dev/null
mkdir files
for g in `find .|grep -i html|sed s/\ /\?/g;find .|grep -i xml|sed s/\ /\?/g`;do
export i=`echo $g|sed s/\?/\ /g`
mv $i files
unset i
done
cd files
for h in `ls -A --sort=version|sed s/\ /\?/g`; do
export j=`echo $h|sed s/\?/\ /g`
html2txt $j
unset j
done
cd $origdir
}
function getfile
{
if [ -d $tmpdir ];then
true
else
mkdir -p $tmpdir
fi
export origname=`echo $@|tr / \\\n`
export transname=`echo -n $origname|tr \\\n /`
export rawname=`echo $origname|tail -n 1`
export filetype=`echo $rawname|tr . \\\n|tail -n 1`
if echo $transname|cut -c 1|grep -q /;then
export filename=$transname
else
export filename=$PWD/$transname
fi
cp $filename $tmpdir/
cd $tmpdir
}
function html2txt
{
export origdir=$PWD
getfile $@
lynx -force_html -nomargins -nolist -width=10000 -nonumbers -localhost -restrictions=all --dump $tmpdir/$rawname
cd $origdir
}
function jpeg2txt
{
export origdir=$PWD
getfile $@
tesseract $rawname $rawname 2>/dev/null
cat $rawname.txt
cd $origdir
}
function jpg2txt
{
export origdir=$PWD
getfile $@
tesseract $rawname $rawname 2>/dev/null
cat $rawname.txt
cd $origdir
}
function ocrpdf2txt
{
export origdir=$PWD
getfile $@
pdfimages $rawname img
for f in `ls img*|sed s/\?/\ /g`;do
export imagename=`echo $f|sed s/\?/\ /g`
tesseract $imagename $imagename > /dev/null 2> /dev/null
cat $imagename.txt
done
cd $origdir
}
function pdf2txt
{
export origdir=$PWD
getfile $@
pdftotext $rawname $rawname.txt
if cat $rawname.txt|wc -l|grep -qw 0;then
ocrpdf2txt $@
else
cat $rawname.txt
fi
cd $origdir
}
function png2txt
{
export origdir=$PWD
getfile $@
tesseract $rawname $rawname 2>/dev/null
cat $rawname.txt
cd $origdir
}
function pptx2txt
{
export origdir=$PWD
getfile $@
libreoffice -env:UserInstallation=file://$tmpdir --headless --convert-to pdf $rawname > /dev/null 2>/dev/null
pdf2txt `echo -n $rawname|sed "s|.pptx|.pdf|g"`
rm `echo -n $rawname|sed "s|.pptx|.pdf|g"`
cd $origdir
}
function rtf2txt
{
export origdir=$PWD
getfile $@
catdoc $@
cd $origdir
}
function tiff2txt
{
export origdir=$PWD
getfile $@
tesseract $rawname $rawname 2>/dev/null
cat $rawname.txt
cd $origdir
}
function txt2txt
{
export origdir=$PWD
getfile $@
cat $rawname
cd $origdir
}
#speech and format detection functions, not needed for text extraction
export pathcomp=`mktemp -u --suffix a|tr / "\\n"`
export temppath=`echo $pathcomp|tr / "\\n"|grep -nw tmp.|cut -f 1 -d :`
export tempdir=`echo $pathcomp|head -n $(($temppath-1))|tr "\\n" /`
export ctl=$tempdir"ebook-speaker-ctl"
export sts=$tempdir"ebook-speaker-sts"
function checkfile
{
if echo $@|wc -c|grep -qw 1;then
control
exit 0
fi
getfile $@
if which `echo -n $filetype`2txt > /dev/null;then
export readable=1
else
echo "$filetype files are not supported at this time."
exit 1
fi
}
function txtread
{
if [ $readable = 1 ];then
`echo -n $filetype`2txt $filename > $tmpdir/text.txt
export textlines=`cat $tmpdir/text.txt|wc -l`
export line=1
export paused=0
export searchnum=1
export pathcomp=`mktemp -u --suffix a|tr / "\\n"`
export temppath=`echo $pathcomp|tr / "\\n"|grep -nw tmp.|cut -f 1 -d :`
export tempdir=`echo $pathcomp|head -n $(($temppath-1))|tr "\\n" /`
export ctl=$tempdir"ebook-speaker-ctl"
export sts=$tempdir"ebook-speaker-sts"
while true;do
if [ -f $ctl ];then
export cmd=`cat $ctl|head -n 1`
case "$cmd" in
P)
if echo $paused|grep -qw 0;then
export paused="1"
else
export paused="0"
fi
;;
p)
export arg=`cat $ctl|tail -n 1`
export line=$(($line-$arg))
;;
n)
export arg=`cat $ctl|tail -n 1`
export line=$(($line+$arg))
;;
/*)
export arg=`cat $ctl|tail -n 1`
export grepargs=`cat $ctl|head -n 1|tr / -`
if echo $grepargs|wc -w|grep -qw 0;then
cat $tmpdir/text.txt|grep -n $arg|cut -f 1 -d : > $tmpdir/results
else
cat $tmpdir/text.txt|grep -n $grepargs $arg|cut -f 1 -d : > $tmpdir/results
fi
if cat $tmpdir/results|wc -l|grep -qw 0;then
rm $tmpdir/results
else
export totalresults=`cat $tmpdir/results|wc -l`
export line=`cat $tmpdir/results|head -n $searchnum|tail -n 1`
export searchnum=$(($searchnum+1))
if [ $searchnum -gt $totalresults ];then
export searchnum=1
fi
fi
;;
\=)
export line=`cat $ctl|tail -n 1`
;;
*)
;;
esac
echo > $ctl
fi
if echo $paused|grep -qw 1;then
sleep .01
else
echo -e ln:$line\\ntxt:`cat $tmpdir/text.txt|head -n $line|tail -n 1` > $sts
echo $line > /proc/self/fd/2
cat $sts|tail -n 1|sed "s|txt:||g" > /proc/self/fd/2
if [ $playneeded = 1 ];then
cat $tmpdir/text.txt|head -n $line|tail -n 1|`echo -n $syncmd $synargs`|`echo -n $playcmd $playargs` 2> /dev/null
else
cat $tmpdir/text.txt|head -n $line|tail -n 1|`echo -n $syncmd $synargs` 2> /dev/null
fi
export line=$(($line+1))
if [ $line -gt $textlines ];then
break
else
continue
fi
fi
done
else
sleep .01
fi
cd
}
function createmenu
{
if [ -z $tempdir ];then
export pathcomp=`mktemp -u --suffix a|tr / "\\n"`
export temppath=`echo $pathcomp|tr / "\\n"|grep -nw tmp.|cut -f 1 -d :`
export tempdir=`echo $pathcomp|head -n $(($temppath-1))|tr "\\n" /`
fi
export menudesc=`stty -a|grep rows|cut -f 2-6 -d \;|cut -f 3,5,8 -d \ |tr \; \ |sed s/"  "/" "/|sed s/"  "/" "/`
cat $1|tail -n 1 >$tempdir/itemlist
export numitems=`cat $tempdir/itemlist|wc -l`
export linecounter=1
while true;do
cat $tempdir/itemlist|head -n $linecounter|tail -n 1 >>$tempdir/items
export linecounter=$(($linecounter+1))
if [ $linecounter -gt $numitems ];then
break
else
continue
fi
done
echo whiptail --menu `cat $1|head -n 1` `echo -n $menudesc` `cat $1|tail -n 1`>$tempdir/menu
chmod 755 $tempdir/menu
$tempdir/menu 2>$tempdir/choice
export choice=`cat $tempdir/choice|cut -f 2 -d \;`
cat $tempdir/itemlist|sed s/"\"\ /\\n/g"|sed "s/\"//g" > $tempdir/items
export testnum=`cat $tempdir/items|grep -nw $choice|cut -f 1 -d :|head -n 1`
export itemname=`cat $tempdir/items|head -n $(($testnum+1))|tail -n 1`
rm $tempdir/choice $tempdir/menu $tempdir/itemlist $tempdir/items $tempdir/dynmenu
clear
}
function createdynamicmenu
{
if [ -z $tempdir ];then
export pathcomp=`mktemp -u --suffix a|tr / "\\n"`
export temppath=`echo $pathcomp|tr / "\\n"|grep -nw tmp.|cut -f 1 -d :`
export tempdir=`echo $pathcomp|head -n $(($temppath-1))|tr "\\n" /`
fi
$@>$tempdir/itemlist
export numitems=`cat $tempdir/itemlist|wc -l`
export counter=1
if [ -f $tempdir/menutitle ];then
export title=`cat $tempdir/menutitle|head -n 1`
else
export title="$numitems items available"
fi
echo \"$title\" > $tempdir/dynmenu
while true;do
export ltr=`cat $tempdir/itemlist|head -n $counter|tail -n 1|cut -c 1`
export item=`cat $tempdir/itemlist|head -n $counter|tail -n 1`
echo -n \" >> $tempdir/dynmenu
if [ $ltr = \" ];then
export ltr="|"
fi
echo -n $ltr\;$counter >> $tempdir/dynmenu
echo -n \"\  >>$tempdir/dynmenu
echo -n \" >> $tempdir/dynmenu
if echo $item|grep -q \";then
export item=`echo $item|sed "s/\"/\|/g"`
fi
echo -n $item >> $tempdir/dynmenu
echo -n \"\  >>$tempdir/dynmenu
export counter=$(($counter+1))
if [ $counter -gt $numitems ];then
break
else
continue
fi
done
createmenu $tempdir/dynmenu
rm $tempdir/menutitle
}
function control
{
export pathcomp=`mktemp -u --suffix a|tr / "\\n"`
export temppath=`echo $pathcomp|tr / "\\n"|grep -nw tmp.|cut -f 1 -d :`
export tempdir=`echo $pathcomp|head -n $(($temppath-1))|tr "\\n" /`
export ctl=$tempdir"ebook-speaker-ctl"
export sts=$tempdir"ebook-speaker-sts"
echo "\"ebook-speaker control\"" > $tempdir/bcmenu
echo -n "\"P\" \"pause reading\" \"p\" \"previous by user specified lines\" \"n\" \"next by user specified lines\" \"/\" \"search for a text string and start reading from its position\" \"=\" \"Go to a given line number\"" >> $tempdir/bcmenu
createmenu $tempdir/bcmenu
case "$choice" in
P)
echo P > $ctl
;;
p)
clear
echo "enter number of lines to rewind"
read rwlines
if echo $rwlines|wc -c|grep -qw 0;then
export rwlines=1
fi
echo p\\n$rwlines > $ctl
;;
n)
clear
echo "enter number of lines to fast forward"
read fflines
if echo $fflines|wc -c|grep -qw 0;then
export fflines=1
fi
echo p\\n$fflines > $ctl
;;
/)
while true;do
echo enter search options, \? for help
read searchopts
case "$searchopts" in
\?)
grep --help 2>/dev/stdout|less -P "press space for next screen, \? for help on keyboard commands available in pager, or q to quit"
continue
;;
*)
break
;;
esac
done
echo enter search term to start reading from its position, if found
read term
echo /$searchopts\\n$term > $ctl
;;
\=)
echo enter line number
read reqline
echo \=\\n$reqline > $ctl
;;
esac
}
checkfile $@
txtread
rm -rf $tmpdir $ctl $sts
