#lang racket (require sxml racket/pretty "record-utils.rkt") ;;; The actual data ;;; =============== (define bib-sxml (call-with-input-file "My Library.xml" (lambda (p) (ssax:xml->sxml p '())))) (define records (match bib-sxml [(list '*TOP* (list '*PI*' xml _ ...) (list 'xml (list 'records records ...))) records])) (define ref-types (for/fold ([ref-types (set)]) ([record records]) (match (sxml-prop-ref (record-ref record 'ref-type) 'name) [#f ref-types] [(? string? name) (set-add ref-types name)]))) ;; Appears to be: ;; (set "Web Page" "Blog" "Book Section" "Book" "Thesis" ;; "Conference Paper" "Journal Article") (define ref-type-strings->symbols #hash(("Web Page" . web-page) ("Blog" . blog) ("Book Section" . book-section) ("Book" . book) ("Thesis" . thesis) ("Conference Paper" . conference-paper) ("Journal Article" . journal-article))) (define (pretty-print-record record entry-num [out-port (current-output-port)]) (define mungers (list (lambda (r) (record-set r 'ref-type (sxml-prop-ref (record-ref (car records) 'ref-type) 'name))) (lambda (r) (record-delete r 'source-app)) (lambda (r) (record-delete r 'database)))) (define munged-record (for/fold ([record record]) ([munger mungers]) (munger record))) (define to-print `(define-bibitem ,(string->symbol (format "entry-name-~a" entry-num)) ,@(cdr munged-record))) (define record-pprinted (call-with-output-string (lambda (p) (pretty-print to-print p 1)))) ;; kludge in the indentation we actually want (define better-indented-record (regexp-replace #rx"\\(define-bibitem\n (.+)\n" record-pprinted "(define-bibitem \\1\n")) (display better-indented-record out-port)) (define (write-records filename [records records]) (call-with-output-file filename (lambda (p) (display "#lang s-exp \"zotbib.rkt\" (provide ~cite citet cite-author cite-year generate-bibliography)\n\n" p) (for ([record records] [i (in-naturals)]) (pretty-print-record record i p) (newline p))) #:exists 'replace))