Skip to content

Instantly share code, notes, and snippets.

@Wneh
Last active December 15, 2015 06:29
Show Gist options
  • Save Wneh/5216890 to your computer and use it in GitHub Desktop.
Save Wneh/5216890 to your computer and use it in GitHub Desktop.
Script that autogenerate a pdf using latex with all the source code content in a folder Start it with the first parameter as the path to the folder that contains the source code files
#!/bin/sh
#Copyright (C) <2013> <Carl Eriksson>
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the
#"Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish,
#distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to
#the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
#CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
#SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
PDFCOVER="labbcover13.pdf"
PDFCOVERURL="http://www.csc.kth.se/utbildning/kth/kurser/DD1339/inda12/labbcover13.pdf"
LATEXFILEPATH="./$1.tex"
echo ""
if [ -f $PDFCOVER ];
then
echo "Cover page is already downloaded"
else
echo "No cover page found it directory..."
echo "...Starting to download it!"
wget $PDFCOVERURL
fi
#Remove if there is any old chunk left
rm $LATEXFILEPATH
#Add some pre latex stuff such
echo "\documentclass[a4paper,12pt]{article}">>$LATEXFILEPATH
echo "\usepackage[utf8]{inputenc}">>$LATEXFILEPATH
echo "\usepackage{pdfpages}">>$LATEXFILEPATH
echo "\usepackage{listings}">>$LATEXFILEPATH
echo "\\\begin{document}">>$LATEXFILEPATH
echo "\includepdf{$PDFCOVER}">>$LATEXFILEPATH
echo "\lstset{basicstyle=\\\ttfamily\\\footnotesize,tabsize=2,breaklines=true,frame=single,numbers=left}">>$LATEXFILEPATH
#Get each path to the files from the folder path given in the first argument
for entry in "$1"/*
do
echo "Adding: $entry"
echo "\\\newpage" >> $LATEXFILEPATH
echo "\section{$(basename $entry)}">>$LATEXFILEPATH
echo "\lstinputlisting{$entry}" >> $LATEXFILEPATH
done
#Finish the latex stuff
echo "\end{document}">>$LATEXFILEPATH
echo ""
#Now create a pdf from the latex file
pdflatex $LATEXFILEPATH
#Cleanup
rm $LATEXFILEPATH
rm "./$1.aux"
rm "./$1.log"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment