commit 19e746f6fb949af4db81bfbdbd90b70ba3ee29c9
parent a25d41ff3157535738b3a68dd239b62de9645d49
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Thu, 11 Feb 2021 15:03:37 +0100
cdoc: improve dependency scanning in tex files
Diffstat:
1 file changed, 22 insertions(+), 19 deletions(-)
diff --git a/.local/bin/cdoc b/.local/bin/cdoc
@@ -24,32 +24,35 @@ regeximatch() {
printf '%s' "$1" | grep -iEq "$2"
}
-extract_tex_include() {
- d="$(grep -v ".*%.*\\${2}" "$1" |
- sed -nE "/.*\\${2}\\[*.*\\]*\\{([^}]*)\\}.*/{ s//\\1/;p; }")"
- if [ "$2" = "bibliography" ] && [ -n "$d" ]; then
- d="${d%.bib}.bib"
- fi
- if [ -n "$d" ]; then
- printf '%s\n' "$d"
+extract_tex_includes() {
+ if ! test -r "$1"; then
+ return
fi
+ awk '
+ function firstval(s) {
+ if(!match(s, /%.*\{/) && match(s, /\{.*\}/))
+ return substr(s, RSTART+1, RLENGTH-2)
+ }
+ $0 ~ /\\input\{/ { print firstval($0) }
+ $0 ~ /\\include\{/ { print firstval($0) }
+ $0 ~ /\\includegraphics\[*.*\]*\{/ { print firstval($0) }
+ $0 ~ /\\addbibresource\{/ { print firstval($0) }
+ $0 ~ /\\bibliography\{/ && $0 !~ /%.*\{/ {
+ m = firstval($0)
+ if (m !~ /\.bib$/)
+ printf("%s.bib", m)
+ else
+ print m
+ }
+ ' "$1"
}
find_dependencies() {
case "$1" in
*.tex)
printf '%s\n' "$1"
- if grep -q -e '\input{' \
- -e '\include[graphics]' \
- -e '\addbibresource{' \
- -e '\bibliography{' \
- "$1"; then
- extract_tex_include "$1" 'input'
- extract_tex_include "$1" 'include'
- extract_tex_include "$1" 'includegraphics'
- extract_tex_include "$1" 'addbibresource'
- extract_tex_include "$1" 'bibliography'
- fi;;
+ extract_tex_includes "$1"
+ ;;
*)
die "cannot find dependencies in unknown file type $1";;
esac