diff options
| author | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2026-06-06 12:02:41 -0400 |
|---|---|---|
| committer | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2026-06-06 12:02:41 -0400 |
| commit | eabf1f6d74dac497ce31e3e2f441cfa25e9f74f2 (patch) | |
| tree | 626d64c3574cfbc7cc38eae6d142ef22b21cf59b /bib-convert | |
| parent | 8351a1da3f56cde9939b934bc5533a95aff1c95e (diff) | |
| download | bibutils-eabf1f6d74dac497ce31e3e2f441cfa25e9f74f2.tar.gz | |
Initial implementation (only a few years later!)
This is pure Claude. I'd written out the plan for
this suite of scripts eons ago, but never found the
time to actual do it. Remembered it this morning,
pointed Claude at the README, and had something
that appears to work in minutes.
caveat emptor: the design is mine, but the code is
purely LLM generated at this point.
Diffstat (limited to 'bib-convert')
| -rwxr-xr-x | bib-convert | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/bib-convert b/bib-convert new file mode 100755 index 0000000..ef4c0b0 --- /dev/null +++ b/bib-convert @@ -0,0 +1,56 @@ +#!/bin/sh +# bib-convert - convert between bibtex and refer database formats +# +# usage: bib-convert [-b | -r] [file] (stdin if no file given) +# -b force refer -> bibtex +# -r force bibtex -> refer +# +# Without a flag the direction is detected from the input: text whose +# first record starts with @ is taken as bibtex, with % as refer. + +usage() { + printf 'usage: bib-convert [-b | -r] [file]\n' >&2 + exit 2 +} + +if [ -n "$BIBUTILS_LIB" ]; then + LIB=$BIBUTILS_LIB +elif [ -d "$(dirname "$0")/lib" ]; then + LIB=$(dirname "$0")/lib +else + LIB=/usr/local/share/bibutils +fi + +bibkey=$(dirname "$0")/bib-key +[ -x "$bibkey" ] || bibkey=bib-key + +mode=auto +while getopts br opt; do + case $opt in + b) mode=tobib ;; + r) mode=toref ;; + *) usage ;; + esac +done +shift $((OPTIND - 1)) +[ $# -le 1 ] || usage + +tmp=$(mktemp) || exit 1 +trap 'rm -f "$tmp"' EXIT INT TERM +cat "$@" > "$tmp" + +if [ "$mode" = auto ]; then + first=$(awk 'NF { sub(/^[ \t]+/, ""); print substr($0, 1, 1); exit }' "$tmp") + case $first in + @) mode=toref ;; + %) mode=tobib ;; + *) printf 'bib-convert: cannot detect input format\n' >&2; exit 1 ;; + esac +fi + +if [ "$mode" = toref ]; then + exec awk -f "$LIB/bib-parse.awk" -f "$LIB/bib-canon.awk" \ + -f "$LIB/bib2ref.awk" "$tmp" +else + awk -f "$LIB/ref2bib.awk" "$tmp" | "$bibkey" +fi |