Pretty Printing C++ Archives from Emails

I'm just putting this here because I nearly managed to lose it. This is a part of a pretty unvarnished BASH script for a very specific purpose, taking an email file containing a ZIP of submitted C++ code from students. This script produces pretty printed PDFs of the source files named after each author to facilitate marking and annotation. It's not a thing of beauty. I think I'll probably write a new cleaner version in future.

#!/bin/bash
# 
# A script to take C++ files in coursework and produce pretty printed PDF
# listings named with the author information.
#
# It takes a ZIP file of .cpp and .h files and produces a ZIP file of PDFs
#

# Requires
#   enscript
#   ps2pdf
#   munpack

#
# Called for each file to be encoded
#
pretty_print_file()
{
  # Extract the Author JavaDoc information
  author=(cat1 | sed -n -e 's/^.*@[Aa]uthor .*/\1/gp')   # And the local part of the email address   author_snip=(cat 1 | sed -n -e 's/^.*@[Aa]uthor.*<<img src="https://www.piglets.org/blog/wp-content/ql-cache/quicklatex.com-6bbee176f5e99b093ce5754610f65df6_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt=".*" title="Rendered by QuickLaTeX.com" height="9" width="12" style="vertical-align: 0px;"/>@.*/\1/gp')

  # How many lines did we get back?
  lines=(echo "author_snip" | wc -l)

  # If we got no author info
  if [ lines -eq "0" ]     then       author="no-author"       author_snip="no-author"   fi    # If we got no author info                                                                                         if [ -z "author_snip" ]
    then
      author="no-author"
      author_snip="no-author"
  fi

  # if we got too many
  if [ lines -ge "2" ]     then       author="multiple-authors"       author_snip="multiple-authors"   fi    echo "File1, Author author (author_snip)"
  output=author_snip-1
  output+=".pdf"
  echo "Encoding output..."   enscript -q --color=1 -C -r -Ecpp -fCourier8 -o -1 | ps2pdf - parsed-output/output }  # # Usage info # if [ ! -f1 ]
  then
    echo "Usage: unpack_coursework <email_file>"
    exit
fi

# Make a temporary directory and copy the email file into it.
echo "Creating temporary directory..."
dir=`mktemp -d`
echo dir cp1 dir # Move to that directory pushddir

# Unpack the email
echo "Unpacking email..."
munpack 1  # Create a directory into which to drop the pretty printed output mkdir parsed-output  # Not so elegant, but extract any .cpp and .h source from resulting ZIPs echo "Unpacking any zips..." for f in *.zip do   unzip -Cjf *.cpp
  unzip -Cj f *.h done  # Produce an author renamed, pretty printed PDF for each header file shopt -s globstar echo "Parse .h files..." for f in *.h do   pretty_print_filef
done

# And the same for source files
echo "Parse .cpp files..."
for f in *.cpp
do
  pretty_print_file f done  # Pull together all the pretty printed content into a new ZIP echo "Zipping parsed output..." cddir/parsed-output
zip parsed-output *.pdf
cd ..

# Back to the directory we started in.
popd

# Copy the parsed ZIP to the current directory for inspection and marking
cp dir/parsed-output/parsed-output.zip .  # Cleanup echo "Deleting temporary directory..." #echodir
rm -rf $dir

 

 

Follow me!

Leave a Reply

Your email address will not be published. Required fields are marked *