A short script for creating fancy .pdf versions of NDPR reviews

Like lots of philosophers, I regularly read the open-access Notre Dame Philosophical Reviews. It’s great for staying on top of new books in philosophy, but sometimes I wish that it had more traditional journal-style formating (nice fonts, margins, and so on). To that end, I wrote a simple script to convert this-or-that NDPR review into a nicely formatted and properly named .pdf file. This script grabs a review from a specified URL, pops that into a .html, extracts information from that file, and then runs pandoc on that file to turn it into a .pdf. You’ll need both LaTeX (via MacTeX) and pandoc installed. You’ll also need Minion Pro, Myriad Pro, and Inconsolata fonts if you want the formating that I’ve specified in the pandoc conversion line, although you can remove those if you want. This script should work on both Mac and Linux, though I’ve only tested it on the former. (I’m not a programmer or an experienced scripter and so I’m sure that there are lots of ways that this could be improved, but it serves my needs just fine.)

To run this, create ndpr.sh file with the following content:

theurl=$(echo $1 | sed 's/\?.*//g')
curl --silent $theurl > tmpndpr.html
theauthor=$(cat tmpndpr.html | grep -oE 'class=.?author[^<>]*>[^<>]+' | cut -d'>' -f2 | sed 's/ /-/g')
thetitle=$(cat tmpndpr.html | grep -oE 'class=.?entry-title[^<>]*>[^<>]+' | cut -d'>' -f2 | sed 's/ /-/g' | sed 's/\:/-/g')
thedate=$(cat tmpndpr.html | grep -oE 'class=.?edition[^<>]*>[^<>]+' | cut -d'>' -f2  | sed 's/\./-/g')
filename="${thedate}_${theauthor}_${thetitle}"
sed -n "/<article/,/<\/article>/p" tmpndpr.html > $filename.html
rm tmpndpr.html
pandoc -f html $filename.html --variable mainfont="Minion Pro" --variable sansfont="Myriad Pro" --variable monofont="Inconsolata" --variable fontsize=11pt --latex-engine=xelatex -o $filename.pdf
open $filename.pdf

You may need to make the file executable (using sudo chmod +x ndpr.sh). You can then just run ./ndpr.sh <url>. For example, if I want to turn this recent review into a .pdf, I just run the following command: ./ndpr.sh http://ndpr.nd.edu/news/49455-consciousness-and-perceptual-experience-an-ecological-and-phenomenological-approach/

That will turn this:

into 2014-07-28_Thomas-Natsoulas_Consciousness-and-Perceptual-Experience--An-Ecological-and-Phenomenological-Approach.pdf:

Avatar
Clark R. Donley
PhD Candidate in Philosophy

Related