dotfiles/scripts/.local/bin/compile
Marty Oehme b76b3ca4ca
Fix compile script
Fixed compile script to not use ifinstalled anymore.

HACK Fixed compile script to correctly pass through output targets to
RMarkdown, through the implementation is very rough currently.
It will look for additional arguments passed through and run RMarkdown
rendering once for each target. It would presumably be faster (and at
the very least more elegant) to pass all arguments through at once, but
I am not sure how to pass arguments through shell surrounded with
quotes.
2020-02-08 20:26:31 +01:00

70 lines
2.1 KiB
Bash
Executable file

#!/usr/bin/env sh
# from https://github.com/LukeSmithxyz/voidrice/blob/master/.local/bin/compiler
#
# This script will compile or run another finishing operation on a document. I
# have this script run via vim.
#
# Compiles .tex. groff (.mom, .ms), .rmd, .md. Opens .sent files as sent
# presentations. Runs scripts based on extention or shebang
#
# Expects the file to compile as first argument.
# Desired output format(s) can be specified in optional arguments following.
file=$1
if [ ! -f "$file" ]; then
echo "File does not exist."
exit 1
else
printf "file: %s\n" "$file"
fi
if [ "$#" -gt 1 ]; then
shift
formats="$*"
fi
dir=$(dirname "$file")
base="${file%.*}"
cd "$dir" || exit
textype() {
if (sed 5q "$file" | grep -i -q 'xelatex') && exist xelatex; then
command="xelatex"
elif exist pdflatex; then
command="pdflatex"
fi
$command --output-directory="$dir" "$base" &&
grep -i addbibresource "$file" >/dev/null &&
biber --input-directory "$dir" "$base" &&
$command --output-directory="$dir" "$base" &&
$command --output-directory="$dir" "$base"
}
sendtoRmd() {
printf "formats: %s\n" "$formats"
## FIXME this is horribly inefficient, should just pass all formats to R
# but R expects them as "strings" and I don't know how to quote the individual words
if [ -n "$formats" ]; then
for fmt in $formats; do
exist R critical && echo "require(rmarkdown); render('$1', c(\"$fmt\"))" | R -q --vanilla
done
else
exist R critical && echo "require(rmarkdown); render('$1')" | R -q --vanilla
fi
}
case "$file" in
*\.ms) refer -PS -e "$file" | groff -me -ms -kept -T pdf >"$base".pdf ;;
*\.mom) refer -PS -e "$file" | groff -mom -kept -T pdf >"$base".pdf ;;
*\.[0-9]) refer -PS -e "$file" | groff -mandoc -T pdf >"$base".pdf ;;
*\.[Rr]md | *\.md | *\.mkd | *\.markdown) sendtoRmd "$file" "$formats" ;;
*\.tex) textype "$file" ;;
*config.h) sudo make install ;;
*\.c) cc "$file" -o "$base" && "$base" ;;
*\.py) python "$file" ;;
*\.go) go run "$file" ;;
*\.sent) setsid sent "$file" 2>/dev/null &;;
*) sed 1q "$file" | grep "^#!/" | sed "s/^#!//" | xargs -r -I % "$file" ;;
esac