#lang racket ;; Zotero inspired bibliography tooling ;; Really trying to import and make sense of the zotero sxml stuff tbh (require "hacky-autobib.rkt" scriblib/footnote scribble/base scribble/core sxml "record-utils.rkt") (provide zotbib-sxmlish zotbib-bib-entry) ;;; Zotbib structure ;;; ================ (struct zotbib (sxmlish bib-entry)) (define (record-sxml->bib-entry sxml) (define the-authors (map sxml-onlychild (sxml-body (or (record-ref (or (record-ref sxml 'contributors) '(contributors)) 'authors) '(authors))))) (make-bib #:title (record-ref-onlychild (record-ref sxml 'titles) 'title) ;; I'm sorry this is so gnarly, trying to not block morgan from ;; getting to work :P #:author (match the-authors ['() #f] [_ (apply authors the-authors)]) #:date (match (record-ref (or (record-ref sxml 'dates) '(dates)) 'year) [(list 'year the-date) the-date] [_ #f]) #:record sxml)) (define-syntax-rule (define-bibitem id entries ...) (begin (define id (let ([sxml `(record entries ...)]) (zotbib sxml (record-sxml->bib-entry sxml)))) (provide id))) (provide define-bibitem) (define-cite ~cite-orig citet-orig generate-bibliography #:cite-author cite-author-orig #:cite-year cite-year-orig) (define (cite zotbib [page #f]) (make-element (make-style #f '()) (format-citation-body zotbib page))) (define (citet zotbib) (citet-orig (zotbib-bib-entry zotbib))) ;; (define (cite-author zotbib) ;; (cite-author-orig (zotbib-bib-entry zotbib))) ;; (define (cite-year zotbib) ;; (cite-year-orig (zotbib-bib-entry zotbib))) (define (footnote-author-name author) (match author [(list 'author author-name) (if (string-contains? author-name ",") (first (string-split author-name ",")) author-name)])) (define (format-authors zotbib) (match (record-sub-ref (zotbib-sxmlish zotbib) 'contributors 'authors) [(list author-one) (footnote-author-name author-one)] [(list author-one author-two) (format "~a and ~a" (footnote-author-name author-one) (footnote-author-name author-two))] ;; even more... [(cons author-one _) (format "~a et al." (footnote-author-name author-one))])) (define (format-citation-body zotbib page) `(,(format-authors zotbib) " " ,(cite-year-orig (zotbib-bib-entry zotbib)) ,(if page `(", " ,(match page [(? number?) (number->string page)] [(? string?) page])) null))) (define-syntax-rule (define-cite-footnote footnote-id footnote-part-id cite-footnote-id) (begin #;(define-footnote footnote-id footnote-part-id) (define footnote-id note) (define footnote-part-id void) (define (cite-footnote-id zotbib [page #f]) (unless (zotbib? zotbib) (error (format "Hey Morgan, I think you mean @cite-footnote[~a] not @cite-footnote{~a}" zotbib zotbib))) (apply footnote-id (cons (format-citation-body zotbib page) '(".")))))) (define (generate-chapter-bibliography) (when (equal? (environment-variables-ref (current-environment-variables) #"CHAPTER_BIBLIOGRAPHY") #"true") (generate-bibliography))) (provide cite citet ;; cite-author cite-year generate-bibliography generate-chapter-bibliography define-cite-footnote) (provide (all-from-out racket))