~charles/dotfiles

42ae9b59869483135b3070a0f00deb773c85adb0 — Charles Daniels 2 months ago 936715f
add new-reference
4 files changed, 37 insertions(+), 6 deletions(-)

A overlay.full/bin/bib2yaml
A overlay.full/bin/new-reference
M utils/README.md
M utils/url2bib/url2bib.py
A overlay.full/bin/bib2yaml => overlay.full/bin/bib2yaml +5 -0
@@ 0,0 1,5 @@
#!/bin/sh

# Converts BibTeX files on standard in to YAML format, using pandoc.

pandoc /dev/stdin -s -f biblatex -t markdown | grep -v -- '---'

A overlay.full/bin/new-reference => overlay.full/bin/new-reference +25 -0
@@ 0,0 1,25 @@
#!/bin/sh

set -e
set -u

# create a new "reference" zettlekasten from a URL

# We do this up front in case it fails, that way we don't create spurious
# zettles.
BIB="$(url2bib "$1" | bib2yaml)"

ZETTLE="$(zettle new)"

function finish() {
	rm -f "$ZETTLE.tmp"
}
trap finish EXIT


awk '$1 == "---" {n++} (n < 2) {print($0)}' < "$ZETTLE" > "$ZETTLE.tmp"
printf '%s\n' "$BIB" >> "$ZETTLE.tmp"
printf 'tags:\n  - reference\n---\n\n' >> "$ZETTLE.tmp"
mv "$ZETTLE.tmp" "$ZETTLE"

echo "$ZETTLE"

M utils/README.md => utils/README.md +1 -0
@@ 23,3 23,4 @@ build artifacts.
* `zettle`: manage a library of Zettelkasten
* `disavow`: detach a command from the terminal
* `query-webpage`: run XPath queries against webpages
* `url2bib`: generate BibTeX files from URLs by parsing web content

M utils/url2bib/url2bib.py => utils/url2bib/url2bib.py +6 -6
@@ 85,14 85,14 @@ def bib2bibtex(bib):

    key = keybase + keydate

    s = "@online {{{}}} {{\n".format(key)
    s += "\ttitle = {{{}}}\n".format(bib["title"])
    s += "\turldate = {{{}}}\n".format(bib["urldate"].strftime("%Y-%m-%d"))
    s += "\turl = {{{}}}\n".format(bib["url"])
    s = "@online {{{},\n".format(key)
    s += "\ttitle = {{{}}},\n".format(bib["title"])
    s += "\turldate = {{{}}},\n".format(bib["urldate"].strftime("%Y-%m-%d"))
    s += "\turl = {{{}}},\n".format(bib["url"])
    if "authors" in bib:
        s += "\tauthor = {{{}}}\n".format(" and ".join(bib["authors"]))
        s += "\tauthor = {{{}}},\n".format(" and ".join(bib["authors"]))
    if "date" in bib:
        s += "\tdate = {{{}}}\n".format(bib["date"].strftime("%Y-%m-%d"))
        s += "\tdate = {{{}}},\n".format(bib["date"].strftime("%Y-%m-%d"))
        s += "\tyear = {{{}}}\n".format(bib["date"].year)
    s += "}\n"