Skip to content

Instantly share code, notes, and snippets.

@tjluoma
Created February 6, 2015 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjluoma/49b28ef83709346b063f to your computer and use it in GitHub Desktop.
Save tjluoma/49b28ef83709346b063f to your computer and use it in GitHub Desktop.
Converts a given Markdown file to a PDF and then prints it
#!/bin/zsh -f
# Purpose: Convert a Markdown file to a PDF and then print it
#
# From: Tj Luo.ma
# Mail: luomat at gmail dot com
# Web: http://RhymesWithDiploma.com
# Date: 2015-02-05
# This is the `basename` of the script as a variable
NAME="$0:t:r"
# This is the path to the file in your Byword iCloud folder
FILE="$HOME/Library/Mobile Documents/N39PJFAFEV~com~metaclassy~byword/Documents/Three Things.txt"
# This is the path to the PDF which will be created
PDF="$HOME/Desktop/$NAME.ThreeThings.pdf"
####|####|####|####|####|####|####|####|####|####|####|####|####|####|####
#
# You should not have to edit anything below this line
#
#
# po.sh is another shell script I wrote to send myself push notifications.
# if it is not installed, this script will just skip it, so you can ignore that.
# If you want to learn more about it, go to
# https://github.com/tjluoma/po.sh
LAST_SUM_FILE="$HOME/Desktop/.$NAME.lastsum.txt"
[[ ! -e "$LAST_SUM_FILE" ]] && touch "$LAST_SUM_FILE"
################################################################################
# This block is all just standard stuff that I use for logging.
zmodload zsh/datetime
TIME=$(strftime "%Y-%m-%d-at-%H.%M.%S" "$EPOCHSECONDS")
HOST=`hostname -s`
HOST="$HOST:l"
LOG="$HOME/Library/Logs/metalog/$NAME/$HOST/$TIME.log"
[[ -d "$LOG:h" ]] || mkdir -p "$LOG:h"
[[ -e "$LOG" ]] || touch "$LOG"
function timestamp { strftime "%Y-%m-%d at %H:%M:%S" "$EPOCHSECONDS" }
function log { echo "$NAME [`timestamp`]: $@" | tee -a "$LOG" }
################################################################################
#
# This makes sure that the 'pandoc' command is installed
#
if ((! $+commands[pandoc] ))
then
MSG="$NAME: pandoc is required but not found in $PATH"
log "$MSG"
(( $+commands[po.sh] )) && po.sh "$MSG"
exit 0
fi
#
################################################################################
#
# This checks to make sure the file actually exists
#
if [ ! -e "$FILE" ]
then
log "$FILE not found. Make sure you set the \$FILE variable in $0"
exit 0
fi
################################################################################
#
# This checks to make sure the file is not empty
#
if [ ! -s "$FILE" ]
then
log "$FILE is 0 bytes"
exit 0
fi
################################################################################
#
# This checks the current md5 checksum of the file against its checksum
# the last time that it was printed via this script.
#
CURRENT_SUM=`md5 -q "$FILE"`
LAST_SUM=`tr -dc '[0-9A-z]' < "$LAST_SUM_FILE"`
if [[ "$CURRENT_SUM" == "$LAST_SUM" ]]
then
MSG="$FILE:t has not changed since it was last printed"
log "$MSG"
(( $+commands[po.sh] )) && po.sh "$MSG"
exit 0
fi
################################################################################
#
# This is where the PDF gets made
#
if [ -e "$PDF" ]
then
# A copy of the PDF already exists. Let's move it to the trash
# and rename it with a unique name, just in case we need to recover it later
mv -vf "$PDF" "$HOME/.Trash/$PDF:t:r.$EPOCHSECONDS.pdf"
fi
# This is where the Markdown file becomes a PDF
pandoc \
-r markdown_mmd \
-t latex \
-o "$PDF" \
"$FILE"
################################################################################
#
# This is where the PDF gets printed
#
# I assume that your Mac is already setup to print the printer you want to use.
# See `man lpr` if you need more. This worked for me.
lpr "$PDF"
# did `lpr` exit successfully?
EXIT="$?"
if [ "$EXIT" = "0" ]
then
# if `lpr` reported success, log it, and update the checksum for next time
log "Printed $PDF successfully"
(( $+commands[po.sh] )) && po.sh "Printed $PDF:t"
echo -n "$CURRENT_SUM" > "$LAST_SUM_FILE"
else
# if `lpr` reported an error, log it and do NOT update the check sum for next time
log "Failed to print $PDF successfully"
(( $+commands[po.sh] )) && po.sh "FAILED to print $PDF:t"
fi
exit 0
#
#EOF
################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment