-
-
Save dsanson/857619 to your computer and use it in GitHub Desktop.
pd: a wrapper around pandoc that allows setting up some default options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# AUTHOR:: David Sanson | |
# URL: https://gist.github.com/857619 | |
# LICENSE: http://creativecommons.org/licenses/MIT/ | |
# VERSION: 0.2 | |
# | |
# IN PROGRESS: | |
# Support for "profiles". | |
# Removed $defaultopts_markdown2pdf---just use formatopts[pdf] instead. | |
# | |
# | |
########################################## | |
# USER SETTINGS | |
########################################## | |
# | |
# Path to Bibliography | |
# ==================== | |
# | |
# I recommend setting $PANDOC_BIB in your .bashrc or placing | |
# default.bib in $HOME/.pandoc. But you can also set the | |
# path here by uncommenting the line below: | |
# | |
# bibfile=/path/to/my/bib | |
# | |
# Default CSL | |
# =========== | |
# By default, pandoc will use the chicago-author-date style. | |
# If you want something else, set it here. | |
# | |
# csl=$HOME/.csl/apa.csl | |
# | |
# Default Options | |
# =============== | |
# | |
# If you set $defaultopts, its value will be passed to pandoc | |
# (except for PDF output, since markdown2pdf doesn't accept | |
# all the options pandoc accepts: set PDF options using | |
# $defaultopts[pdf] below). | |
# | |
defaultopts="-sS" | |
# | |
# Launch Command | |
# ============== | |
# | |
# The commandline tool that will be used to open the generated | |
# file when requested. Linux users might set this to 'xdg-open'. | |
# Windows users might set it to 'start'. | |
# | |
launcher="open" | |
# | |
# | |
# Format-specific defaults | |
# ======================== | |
# | |
# Here you can specify default options for specific output formats. | |
# These will be appended to the command line *after* the $defaultopts | |
# and so will override any conflicting values in $defaultops. | |
# | |
# NOTE: formatopts requires bash 4 or higher. If you want | |
# to use this script with generic sh or bash < 4, Comment out | |
# all lines with ending with #BASH4, and change the shebang on the | |
# first line to something like /bin/sh. | |
# | |
declare -A formatopts #BASH4 | |
formatopts[pdf]= #BASH4 | |
formatopts[native]= #BASH4 | |
formatopts[json]= #BASH4 | |
formatopts[html]="--mathjax='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=Accessible'" #BASH4 | |
formatopts[html+lhs]= #BASH4 | |
formatopts[s5]= #BASH4 | |
formatopts[slidy]= #BASH4 | |
formatopts[docbook]= #BASH4 | |
formatopts[opendocument]= #BASH4 | |
formatopts[latex]= #BASH4 | |
formatopts[latex+lhs]= #BASH4 | |
formatopts[context]= #BASH4 | |
formatopts[texinfo]= #BASH4 | |
formatopts[man]= #BASH4 | |
formatopts[markdown]= #BASH4 | |
formatopts[markdown+lhs]= #BASH4 | |
formatopts[plain]= #BASH4 | |
formatopts[rst]= #BASH4 | |
formatopts[rst+lhs]= #BASH4 | |
formatopts[mediawiki]= #BASH4 | |
formatopts[textile]= #BASH4 | |
formatopts[rtf]= #BASH4 | |
formatopts[org]= #BASH4 | |
formatopts[odt]= #BASH4 | |
formatopts[epub]= #BASH4 | |
# Profiles | |
# ======== | |
# | |
# Specify the path to a file containing "profiles". | |
# | |
profiles="$HOME/.pandoc/profiles" | |
# | |
# | |
########################################## | |
# END OF USER SETTINGS | |
########################################## | |
helpme() { | |
cat <<EOH | |
$commandname [$commandname-commands] [OPTIONS] [FILES] | |
$commandname is a general purpose wrapper around pandoc. It passes OPTIONS and | |
FILES untouched to pandoc. But before it does that, it: | |
1. Infers an output filename, using the same rules | |
used by markdown2pdf. | |
2. Parses special $commandname-commands. | |
$commandname-commands | |
=========== | |
$commandname-commands must come *before* any pandoc OPTIONS. | |
bib => --bibliography=/path/to/my/bib | |
Set the path in the USER SETTINGS section of the script. | |
<format> => -t <format> | |
<format> can be an output format that pandoc supports. If <format> is | |
"pdf", then markdown2pdf will be called instead of pandoc. | |
<profile> | |
<profile> is a key from $profiles. The format of $profiles should be | |
key1: options | |
key2: options | |
etc. | |
For example, | |
cv: --template=/path/to/cv.template --xetex | |
exam: --template=/path/to/exam.template --xetex | |
open => pandoc -o <output> && <\$launcher> <output> | |
Set the value of \$launcher(=$launcher) in USER SETTINGS. | |
no-out | |
Don't try to infer an output filename. Use this when you want to pipe | |
the output to STDOUT. This overrides "open". | |
dry | |
Print the expanded command but don't execute it. | |
help | |
Display this help. | |
USER SETTINGS | |
============= | |
Before running $commandname for the first time, you will want to edit the | |
default values in the USER SETTINGS section of the script. These include the | |
path to your bibliography file, the command to use for launching files, | |
global default options to be passed to pandoc, and format specific default | |
options. Some of these current values are: | |
\$bibfile=$bibfile | |
\$defaultopts=$defaultopts | |
\$defaultopts_markdown2pdf=$defaultopts_markdown2pdf | |
\$launcher=$launcher | |
Examples | |
======== | |
$commandname html example.markdown | |
expands to | |
pandoc -o example.html -t html $defaultopts ${formatopts[html]} example.markdown | |
The "-o example.html" is the result of inferring an output filename. "-t html" is due to | |
the "html" $commandname-command. Everything between that and "example.markdown" comes from | |
the values of \$defaultopts or \$formatopts[html]. | |
$commandname html bib example.markdown | |
expands to | |
pandoc --bibliography=<\$bibfile> -o example.html -t html $defaultopts ${formatopts[html]} example.markdown | |
Where \$bibfile is your bibliography file. | |
Pandoc options will be passed *after* any options generated by pd. So | |
$commandname html --no-wrap example.markdown | |
expands to | |
pandoc -o example.html -t html $defaultopts ${formatopts[html]} --no-wrap example.markdown | |
If you supply <format> commands, the last wins: | |
$commandname html odt pdf rtf example.markdown | |
expands to | |
pandoc -o example.rtf -t rtf $defaultopts ${formatopts[rtf]} example.markdown | |
Note that $commandname makes no attempt to parse pandoc options. This can lead to | |
weird expansions, e.g., (ignoring \$defaultopts and \$formatopts) | |
$commandname html -t rtf example.markdown | |
expands to | |
pandoc -o example.html -t html -t rtf example.markdown | |
$commandname stops looking for $commandname commands when it encounters | |
an argument that it doesn't recognize: that and all subsequent arguments are | |
passed on to pandoc untouched. So, for example, | |
$commandname rtf blah bib example.markdown | |
expands to | |
pandoc -o blah.rtf -t rtf blah bib example.markdown | |
which is probably not what you want! | |
If you run into problems, you can use the "dry" command to print out the expansion | |
without executing it. | |
Output Filename | |
=============== | |
$commandname uses the same logic and markdown2pdf to guess at reasonable | |
filename roots, and appends the extension given by the <format> command. If | |
no <format> command is given, the ".html" extension is used. If the format | |
is 'plain', then the extension 'txt' is used. | |
The "no-out" command disables this behavior. So something like | |
cat file.markdown | $commandname no-out html | tidy | |
will do what you expect. The no-out option disables the "open" command, since | |
the "open" command uses the infered filename. | |
EOH | |
} | |
########################################## | |
# Find path to bibliography | |
########################################## | |
getbibpath() { | |
if [ -n "$PANDOC_BIB" ]; then | |
bibfile="$PANDOC_BIB" | |
elif [ -f "$HOME/.pandoc/default.bib" ]; then | |
bibfile="$HOME/.pandoc/default.bib" | |
else | |
echo "You need to specify a bibliographic database." | |
echo "Either set \$PANDOC_BIB or place a file named" | |
echo "default.bib in $HOME/.pandoc or set the path" | |
echo "manually by editing $0." | |
exit | |
fi | |
} | |
commandname=`basename $0` | |
########################################## | |
# Parse Profiles File | |
########################################## | |
declare -A profile | |
while read line; do | |
key=${line%%:*} | |
profile[$key]=${line##*:} | |
done < "$profiles" | |
########################################## | |
# Parse pd commands | |
########################################## | |
profileopts="" | |
openfile=false | |
dryrun=false | |
outfile=true | |
bib=false | |
checking=true | |
while $checking | |
do | |
case $1 in | |
pdf | native | json | html | html+lhs | s5 | slidy | docbook | opendocument | latex | latex+lhs | context | texinfo | man | markdown | markdown+lhs | plain | rst | rst+lhs | mediawiki | textile | rtf | org | odt | epub) | |
format=$1 | |
if [ $format == "plain" ]; then | |
extension="txt" | |
elif [ $format == "latex" ]; then | |
extension="tex" | |
else | |
extension=$format | |
fi | |
shift 1 | |
;; | |
bib) | |
bib=true | |
shift 1 | |
;; | |
open) | |
openfile=true | |
shift 1 | |
;; | |
no-out) | |
outfile=false | |
shift 1 | |
;; | |
dry) | |
dryrun=true | |
shift 1 | |
;; | |
help) | |
helpme | |
exit | |
;; | |
*) | |
itsaprofile=false | |
for k in "${!profile[@]}"; do | |
if [ "$1" == "$k" ]; then | |
echo "match: $k" | |
profileopts="${profile[$k]}" | |
itsaprofile=true | |
fi | |
done | |
if $itsaprofile; then | |
shift 1 | |
else | |
checking=false | |
fi | |
;; | |
esac | |
done | |
echo "$profileopts" | |
################################################### | |
# Find output file (cribbed from old copies of markdown2pdf) | |
################################################### | |
SYNOPSIS="Or try \`$commandname help\` for help with the wrapper script." | |
THIS=${0##*/} | |
err () { echo "$*" | fold -s -w ${COLUMNS:-110} >&2; } | |
CONF=$(pandoc --dump-args "$@" 2>&1) || { | |
errcode=$? | |
echo "$CONF" | sed -e '/^pandoc \[OPTIONS\] \[FILES\]/,$d' >&2 | |
[ $errcode -eq 2 ] && err "$SYNOPSIS" | |
exit $errcode | |
} | |
# Supply an output file name if none was supplied on | |
# command line. | |
OUTPUT=$(echo "$CONF" | sed -ne '1p') | |
ARGS=$(echo "$CONF" | sed -e '1d') | |
if [ "$OUTPUT" = "-" ]; then | |
firstinfile="$(echo "$ARGS" | sed -ne '1p')" | |
firstinfilebase="${firstinfile%.*}" | |
destname="${firstinfilebase:-stdin}.${extension:-html}" | |
else | |
destname="$OUTPUT" | |
fi | |
########################################## | |
# Generate command line options | |
########################################## | |
cliopts="$*" | |
if $outfile; then | |
opts=" -o $destname " # add output command | |
fi | |
command="pandoc" | |
if [ -n "$format" ]; then | |
###################################### | |
# if format is pdf, run markdown2pdf | |
###################################### | |
if [ "$format" == "pdf" ]; then | |
opts="" | |
command="markdown2pdf" | |
else | |
opts="$opts $defaultopts -t $format" | |
fi | |
###################################### | |
# Apply formatopts, if there are any | |
###################################### | |
opts="$opts ${formatopts[$format]}" #BASH4 | |
###################################### | |
# Apply profile opts, if any | |
###################################### | |
opts="$opts $profileopts" | |
fi | |
###################################### | |
# Add bibliography option | |
###################################### | |
if $bib; then | |
if [ -z "$bibfile" ]; then | |
getbibpath | |
fi | |
opts="--bibliography=$bibfile --csl=$csl $opts" | |
fi | |
########################################## | |
# Run command | |
########################################## | |
# Is this a dry run? | |
if $dryrun; then | |
command="echo $command" | |
launcher="echo $launcher" | |
fi | |
$command $opts $cliopts && | |
if $openfile && $outfile; then | |
$launcher $destname | |
else | |
if [ $format != "pdf" ] && $outfile && $dryrun; then | |
echo "Would have created $destname" | |
else | |
echo "Created $destname" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment