dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles # fast
git clone https://src.adamsgaard.dk/dotfiles.git # slow
Log | Files | Refs | README | LICENSE Back to index

cdoc (3270B)


      1 #!/bin/sh
      2 opener=xdg-open
      3 
      4 show_help() {
      5 	printf 'usage: %s [OPTIONS] FILE ...\n' "${0##*/}"
      6 	printf 'compiles each FILE to pdf. Supported input file types are:\n'
      7 	printf '   - latex (extension .tex) including dependency recursion\n'
      8 	printf '   - gnuplot (extension .gp)\n'
      9 	printf '   - mandoc (extension .[0-9])\n'
     10 	printf '   - groff/mom (extension .mom)\n'
     11 	printf 'OPTIONS are one or more of the following:\n'
     12 	printf '   -h    show this message\n'
     13 	printf '   -c    continuously recompile upon file changes (requires entr(1))\n'
     14 	printf '   -o    open output file after compiling (requires %s(1))\n' "$opener"
     15 	printf '   --    do not consider any following args as options\n'
     16 }
     17 
     18 die() {
     19 	printf '%s: error: %s\n' "${0##*/}" "$1" >&2
     20 	exit 1
     21 }
     22 
     23 regeximatch() {
     24 	printf '%s' "$1" | grep -iEq "$2"
     25 }
     26 
     27 extract_tex_includes() {
     28 	if ! test -r "$1"; then
     29 		return
     30 	fi
     31 	awk '
     32 		function firstval(s) {
     33 			if(!match(s, /%.*\{/) && match(s, /\{.*\}/))
     34 				return substr(s, RSTART+1, RLENGTH-2)
     35 		}
     36 		$0 ~ /\\input\{/ { print firstval($0) }
     37 		$0 ~ /\\include\{/ { print firstval($0) }
     38 		$0 ~ /\\includegraphics\[*.*\]*\{/ { print firstval($0) }
     39 		$0 ~ /\\addbibresource\{/ { print firstval($0) }
     40 		$0 ~ /\\bibliography\{/ && $0 !~ /%.*\{/ {
     41 			m = firstval($0)
     42 			if (m !~ /\.bib$/)
     43 				printf("%s.bib", m)
     44 			else
     45 				print m
     46 		}
     47 	' "$1"
     48 }
     49 
     50 find_dependencies() {
     51 	case "$1" in
     52 		*.tex)
     53 			printf '%s\n' "$1"
     54 			extract_tex_includes "$1"
     55 			;;
     56 		*)
     57 			die "cannot find dependencies in unknown file type $1";;
     58 	esac
     59 }
     60 
     61 run_compile() {
     62 	if [ "$continuous" = 1 ]; then
     63 		printf '%s\n' "$2" | entr -s "$1"
     64 	else
     65 		eval "$1"
     66 	fi
     67 }
     68 
     69 handle_target() {
     70 	base="$(basename "${1%.*}")"
     71 	if [ -e "$1" ]; then
     72 		case "$1" in
     73 			*.tex)
     74 				deps="$1"
     75 				deps="$(find_dependencies "$1")"
     76 				for d in $deps; do
     77 					if [ "${d##*.}" = "tex" ]; then
     78 						deps="$(printf '%s\n%s\n' "${deps}" "$(find_dependencies "$d")")"
     79 					fi
     80 				done
     81 				deps="$(printf '%s\n' "$deps" | sort | uniq)"
     82 				printf 'deps=%s\n' "$deps"
     83 				if regeximatch "$deps" '\.bib'; then
     84 					compile_string="pdflatex '$1'; bibtex '${1%.tex}'; pdflatex '$1'; pdflatex '$1'"
     85 				else
     86 					compile_string="pdflatex '$1' && pdflatex '$1'"
     87 				fi
     88 				run_compile "$compile_string" "$deps"
     89 				if [ "$open" = 1 ]; then
     90 					$opener "${1%.tex}.pdf"
     91 				fi
     92 				;;
     93 			*.gp)
     94 				run_compile "gnuplot '$1'" "$1";;
     95 			*.[0-9])
     96 				out="$(basename "${base}").pdf"
     97 				if [ "$open" = 1 ]; then
     98 					run_compile "refer -PS -e '$1' | groff -mandoc -T pdf > '$out' && $opener '$out'" "$1"
     99 				else
    100 					run_compile "refer -PS -e '$1' | groff -mandoc -T pdf > '$out'" "$1"
    101 				fi;;
    102 			*.mom)
    103 				out="$(basename "${base}").pdf"
    104 				if [ "$open" = 1 ]; then
    105 					run_compile "pdfmom '$1' > '$out' && $opener '$out'" "$1"
    106 				else
    107 					run_compile "pdfmom '$1' > '$out'" "$1"
    108 				fi;;
    109 			*)
    110 				die "File type ${1##*.} not supported";;
    111 		esac
    112 	else
    113 		die "cannot read $1"
    114 	fi
    115 }
    116 
    117 continuous=0
    118 open=0
    119 while :; do
    120 	case "$1" in
    121 		-h|-\?)
    122 			show_help
    123 			exit 0;;
    124 		-c)
    125 			continuous=1;;
    126 		-o)
    127 			open=1;;
    128 		--) # end all options
    129 			shift
    130 			break;;
    131 		-?*)
    132 			die 'unknown option specified';;
    133 		*)  # No more options
    134 			break
    135 	esac
    136 	shift
    137 done
    138 
    139 if [ $# -lt 1 ]; then
    140 	show_help
    141 	exit 1
    142 else
    143 	for f in "$@"; do
    144 		handle_target "$f"
    145 	done
    146 fi