Skip to content

Instantly share code, notes, and snippets.

@robotanarchy
Forked from michih57/pygmentize
Last active August 29, 2015 14:11
Show Gist options
  • Save robotanarchy/16ca1d06d26e306c3062 to your computer and use it in GitHub Desktop.
Save robotanarchy/16ca1d06d26e306c3062 to your computer and use it in GitHub Desktop.
add global flag to regex
#!/bin/bash
#
# This script caches the pygmentize output.
# Original script by michih57
#
# Modifications by robotanarchy:
# - remove real_pygmentize variable
# - store hash in 'out' folder
# - replace '\$' with '$' to fix syntax highlighting in editors
# - also cache style information
# - add .cache extension (useful for gitignore)
#
# Example usage (add this to your preamble):
# \usepackage[outputdir=out]{minted}
# \renewcommand{\MintedPygmentize}{scripts/pygmentize.sh}
# \usemintedstyle{manni}
#
outputdir="out"
# get the output file, if any
take_outfile=0
for arg in $@; do
[ $take_outfile -eq 1 ] && outfile=$arg && break
[ $arg == -o ] && take_outfile=1
done
# when called without an output file, minted expects
# the output on stdout (this gets used for style information).
# We'll use a hash of the commandline in that case.
if [ "$outfile" == "" ]; then
hash="$outputdir/$( echo "$*" | md5sum | cut -d' ' -f1 ).cache"
[ -f "$hash" ] || pygmentize $* > $hash
cat $hash
exit 0
fi
# Otherwise: the last parameter is the input file
infile=${@: -1}
# compute the hash value of the input
hash="$outputdir/$( md5sum $infile | cut -d' ' -f1 ).cache"
if [ -f "$hash" ]; then
# we have cached output from pygmentize
cp "$hash" "$outfile"
else
# fix syntax highlighting issues with '$'
sed --in-place -e's~\\\$~\$~g' "$infile"
pygmentize $*
cp "$outfile" "$hash"
fi
@robotanarchy
Copy link
Author

Note: minted 2.0 has cache support built-in. It's still useful for me to replace \$ with $.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment