dotfiles/scripts/.local/bin/compile

60 lines
1.9 KiB
Text
Raw Normal View History

2019-12-03 22:12:44 +00:00
#!/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
2020-02-03 21:10:11 +00:00
#
# Expects the file to compile as first argument.
# Desired output format(s) can be specified in optional arguments following.
2019-12-03 22:12:44 +00:00
file=$(readlink -f "$1")
2020-02-03 21:10:11 +00:00
printf "file: %s\n" "$file"
if [ "$#" -gt 1 ]; then
shift
formats="$*"
fi
2019-12-03 22:12:44 +00:00
dir=$(dirname "$file")
base="${file%.*}"
cd "$dir" || exit
textype() {
if (sed 5q "$file" | grep -i -q 'xelatex') && ifinstalled xelatex; then
command="xelatex"
elif ifinstalled pdflatex; then
command="pdflatex"
fi
2019-12-03 22:12:44 +00:00
$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() {
2020-02-03 21:10:11 +00:00
# v removed for too much magic that can break; simply input pdf_document if you want it
# formats=$(echo "$formats" | perl -pe 's/([\w-]+)(?<!_document)\b/"\1_document"/gm' | tr ' ' ',')
printf "formats: %s\n" "$formats"
ifinstalled R && echo "require(rmarkdown); render('$1', c($formats))" | R -q --vanilla
}
2019-12-03 22:12:44 +00:00
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 ;;
2020-02-03 21:10:11 +00:00
*\.[Rr]md | *\.md | *\.mkd | *\.markdown) sendtoRmd "$file" "$formats" ;;
2019-12-03 22:12:44 +00:00
*\.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