Skip to content

Instantly share code, notes, and snippets.

@dceresoli
Created April 17, 2020 11:23
Show Gist options
  • Save dceresoli/d88ef241523ef017d804b94f667c54d2 to your computer and use it in GitHub Desktop.
Save dceresoli/d88ef241523ef017d804b94f667c54d2 to your computer and use it in GitHub Desktop.
Create a PDF of a monthly CUJ or DDJ issue from the DDJ developers DVD #6
#!/bin/bash
# Create a PDF of a monthly CUJ or DDJ issue from the DDJ developers DVD #6
# Requires: pandoc, pdflatex, eisvogel.tex template, perl, sed, ghostscript
#
# Davide Ceresoli <dceresoli@gmail.com>
# adjust the path to this file
TEMPLATE=$HOME/CNR-COVID19/PANDOC/eisvogel.tex
journal=CUJ
# convert gif images to png
function convert_images {
for f in $(find -name \*.gif)
do
convert $f ${f%.gif}.png
done
}
# make the table of contents
function make_toc {
toc=$(ls *toc*.htm)
name=$(basename $toc .htm)
perl -pi -e 's/[^[:ascii:]]//g' $toc # remove non-ascii characters
pandoc $toc --to latex --listings --template $TEMPLATE -o $name.tex
sed -i "s/logo.gif/logo.png/" $name.tex
pdflatex -interaction=nonstopmode $name.tex
pdflatex -interaction=nonstopmode $name.tex
}
# convert each article into pdf
function make_article {
rm -f tmp.html
mainhtml=$(ls *[a-z].html)
fightml=$(ls *f[0-9].html)
lishtml=$(ls *l[0-9].html)
name=$(basename $mainhtml .html)
cat $mainhtml $fightml $lishtml >tmp.html
perl -pi -e 's/[^[:ascii:]]//g' tmp.html # remove non-ascii characters
pandoc tmp.html --to latex --listings -V code-block-font-size:\\tiny \
--template $TEMPLATE -o $name.tex
rm -f tmp.html
sed -i "s/\.gif}/\.png}/" $name.tex
sed -i "s/\\includegraphics/\\mbox{}\\\newline \\\includegraphics/" $name.tex
pdflatex -interaction=nonstopmode $name.tex
pdflatex -interaction=nonstopmode $name.tex
}
# merge pdf files
function pdfmerge {
gs -sDEVICE=pdfwrite -sPAPERSIZE=a4 -sOutputFile=pdfmerge.pdf -q -dNOPAUSE -dBATCH $*
}
# first create a pdf of the toc
if test ! \( -f toc???.htm -o -f ????toc.htm \)
then
echo "toc???.htm or ????toc.htm file not found, stopping now"
exit 1
fi
echo "converting figures to png..."
convert_images
echo "compiling toc..."
make_toc >out 2>err
# list of articles
articles=$(grep -i HREF *toc*.htm |cut -d\" -f2 |cut -d/ -f 1)
for article in $articles
do
cd $article
echo "compiling article $article..."
make_article >>out 2>>err
cd ..
done
echo pdf-merging...
pdfmerge.sh toc*.pdf $(for f in $articles; do echo -n "$f/$f.pdf "; done)
issue=${PWD##*/}
mv pdfmerge.pdf ${journal}_${issue}.pdf
#!/bin/bash
# Create a PDF of a monthly CUJ or DDJ issue from the DDJ developers DVD #6
# Requires: pandoc, pdflatex, eisvogel.tex template, perl, sed, ghostscript
#
# Davide Ceresoli <dceresoli@gmail.com>
# adjust the path to this file
TEMPLATE=$HOME/CNR-COVID19/PANDOC/eisvogel.tex
journal=DDJ
# convert gif images to png
function convert_images {
for f in $(find -name \*.gif)
do
convert $f ${f%.gif}.png
done
}
# make the table of contents
function make_toc {
toc=$(ls *toc*.htm)
name=$(basename $toc .htm)
perl -pi -e 's/[^[:ascii:]]//g' $toc # remove non-ascii characters
pandoc $toc --to latex --listings --template $TEMPLATE -o $name.tex
sed -i "s/logo.gif/logo.png/" $name.tex
pdflatex -interaction=nonstopmode $name.tex
pdflatex -interaction=nonstopmode $name.tex
}
# convert each article into pdf
function make_article {
rm -f tmp.html
mainhtml=$(ls -S *.htm | head -1)
fightml=$(ls -S *.htm | awk 'NR>1 {print}')
name=$(basename $mainhtml .htm)
cat $mainhtml $fightml >tmp.html
perl -pi -e 's/[^[:ascii:]]//g' tmp.html # remove non-ascii characters
pandoc tmp.html --to latex --listings -V code-block-font-size:\\tiny \
--template $TEMPLATE -o $name.tex
rm -f tmp.html
sed -i "s/\.gif}/\.png}/" $name.tex
sed -i "s/\\includegraphics/\\mbox{}\\\newline \\\includegraphics/" $name.tex
pdflatex -interaction=nonstopmode $name.tex
pdflatex -interaction=nonstopmode $name.tex
}
# merge pdf files
function pdfmerge {
gs -sDEVICE=pdfwrite -sPAPERSIZE=a4 -sOutputFile=pdfmerge.pdf -q -dNOPAUSE -dBATCH $*
}
# first create a pdf of the toc
if test ! \( -f toc???.htm -o -f ????toc.htm \)
then
echo "toc???.htm or ????toc.htm file not found, stopping now"
exit 1
fi
echo "converting figures to png..."
convert_images
echo "compiling toc..."
make_toc >out 2>err
# list of articles
articles=$(grep -i HREF *toc*.htm |cut -d\" -f2 |cut -d/ -f 1)
for article in $articles
do
cd $article
echo "compiling article $article..."
make_article >>out 2>>err
cd ..
done
echo pdf-merging...
pdfmerge.sh ????toc*.pdf $(for f in $articles; do echo -n "$f/$f.pdf "; done)
issue=${PWD##*/}
mv pdfmerge.pdf ${journal}_${issue}.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment