#!/bin/bash
#Converts tex file to html file that fits my site's format
#USAGE:
#converter.sh -p I.2.7 Ip2p8.tex
#this script is assuming that sed is GNU sed 4.4
#...and probably some other assumptions too
OUTFILE_MOD="_new"
SAMPLE_FILE="sample_hartshorned.html"
sample_file="$SAMPLE_FILE"
TEX_FILES_DIR="texfiles"
function ptodot() {
ok1=$(sed 's/p/\./g' <<< $1)
ok1=$(sed 's/n/\,/g' <<< $ok1)
echo $ok1
}
function dottop() {
ok1=$(sed 's/\./p/g' <<< $1)
ok1=$(sed 's/\,/n/g' <<< $ok1)
echo $ok1
}
#Get last argument
for i in $@; do :; done
#Rest of code actually does the tex -> html conversion
infile="$i"
exid_P=$(cut -d '.' -f 1 <<< "$infile")
ext=$(cut -d '.' -f 2 <<< "$infile")
past_exid=""
date="_DATE_"
#if tex file supplied as argument, run make4ht
if [ ! $ext = "tex" ]
then
echo "Argument must be a tex file. Exiting..." >&2
exit 1
fi
#make4ht --utf8 $infile
make4ht $infile
if [ $? -eq 0 ]
then
ext="html"
infile=${exid_P}.${ext}
else
echo "Something went wrong with make4ht" >&2
exit 1
fi
#make output file
outfile="$exid_P""$OUTFILE_MOD"."$ext"
#-p option takes the past exercise id as argument
#-d option takes the date as argument
past_exid=""
while getopts "d:p:" o; do
case $o in
p)
past_exid="$OPTARG"
;;
d)
date="$OPTARG"
;;
esac
done
if [[ $past_exid = "" ]]
then
echo "Must supply past exercise id with option -p"
exit 1
fi
#used cut at first but sed is quicker lol
#chapter=$(cut -d "p" -f 1 <<< $exid_P)
#section=$(cut -d "p" -f 2 <<< $exid_P)
#exercise=$(cut -d "p" -f 3 <<< $exid_P)
#exid="$chapter"."$section"."$exercise"
exid=$(ptodot $exid_P)
past_exid_P=$(dottop $past_exid)
#Grab body of make4ht output, store it in new file
sed '1,/^<\/head>/d;/^<\/body><\/html>/d' "$infile" > "$outfile"
sed '1d' -i "$outfile" #forgot what the purpose of this was lol
sed '/END_HEADER/,$d' "$sample_file" > header.html
sed '1,/START_FOOTER/d' "$sample_file" > footer.html
sed '/START_HEADER/d' -i header.html
sed '/END_FOOTER/d' -i footer.html
#concat header, body, footer
cat header.html "$outfile" footer.html > tmp.html
mv tmp.html "$outfile"
rm header.html footer.html
#Finding img tags and moving them over to the next line
sed -ni '//; p; n; s/(\/>)/\1<\/span>/; :a; p;' $outfile
#getting rid of something that messes up the color
sed -i 's/id="textcolor."//g' $outfile
#Replace placeholders
sed -i "s/_THIS_EX_/$exid/g" $outfile
sed -i "s/_PTHIS_EX_/$exid_P/g" $outfile
sed -i "s/_PAST_EX_/$past_exid/g" $outfile
sed -i "s/_PPAST_EX_/$past_exid_P/g" $outfile
sed -i 's/STARTDIAGSEC/
/g' $outfile
sed -i 's/ENDDIAGSEC/<\/p><\/div>/g' $outfile
sed -i 's/STARTBIGCURSIVE//g' $outfile
sed -i 's/ENDBIGCURSIVE/<\/span>/g' $outfile
sed -i 's/STARTBIGTEXT//g' $outfile
sed -i 's/ENDBIGTEXT/<\/span>/g' $outfile
sed -i 's/STARTBIGGERTEXT//g' $outfile
sed -i 's/ENDBIGGERTEXT/<\/span>/g' $outfile
sed -i 's/STARTBIGGESTTEXT//g' $outfile
sed -i 's/ENDBIGGESTTEXT/<\/span>/g' $outfile
sed -i 's/STARTCENTER//g' $outfile
sed -i 's/ENDCENTER/<\/span>/g' $outfile
sed -i 's/STARTSOUT/ /g' $outfile
sed -i 's/ENDCAPTION/<\/p><\/div>/g' $outfile
sed -i 's/STARTCMT//g' $outfile
sed -i 's/ENDSOUT/<\/s>/g' $outfile
sed -i 's/STARTGREENTEXT//g' $outfile
sed -i 's/ENDGREENTEXT/<\/span>/g' $outfile
sed -i 's/STARTYELLOWTEXT//g' $outfile
sed -i 's/ENDYELLOWTEXT/<\/span>/g' $outfile
sed -i 's/STARTCAPTION/
)@\1\2\3@g' -i $outfile