-
Notifications
You must be signed in to change notification settings - Fork 2
/
texmacs-zotero-bibtex
138 lines (119 loc) · 4.55 KB
/
texmacs-zotero-bibtex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/guile-2.0 \
-e main -s
!#
;;
;; This program acts like "bibtex", but uses Zotero better-bibtex to
;; get the bibliography.
;;
;; Internally, src/src/Plugins/BibTeX/bibtex.cpp, effectively runs:
;;
;; cd $TEXMACS_HOME_PATH/system/bib; \
;; BIBINPUTS="$(dirname $bib_file)":$BIBINPUTS \
;; BSTINPUTS="$(dirname $bib_file)":$BSTINPUTS \
;; texmacs-zotero-bibtex temp > $TEXMACS_HOME_PATH/system/bib/temp.log
;;
;; This program is expected to read temp.aux, and produce temp.bbl
;;
;; temp.log is parsed for: "Warning--" and prints them.
;;
;; It then calls bibtex_load_bbl, and loads the temp.bbl file.
;;
;; The temp.aux file that is it's input contains:
;;
;; \bibstyle{$style}
;; \citation{bibtex_cite_key1}
;; \citation{bibtex_cite_key2}
;; \citation{bibtex_cite_key_etc}
;; \bibdata{$bib_name}
;;
;; $style is the (BibTeX) style name. It may not contain spaces.
;; $bib_name is $(basename $bib_file .bib)
;;
;; I will assume that the $bib_file is produced by
;; zotero-better-bibtex, and was used as a means of getting the cite
;; keys (for now. Later I'll implement cite as you write). This
;; program does not need to access it, since it will get everything it
;; needs from the temp.aux file and the jsonrpc call to zotero-better-bibtex.
;;
(use-modules (ice-9 receive))
(use-modules (ice-9 rdelim))
(use-modules (web client))
(use-modules (web uri))
(use-modules (web request))
(use-modules (web response))
(use-modules (json))
(define zotero-better-bibtex-schomd-uri
"http://localhost:23119/better-bibtex/schomd")
(define id -1)
(define ht (json
(object
("jsonrpc" "2.0")
("method" "bibliographybbl"))))
(hash-set! ht "params"
(list (list "placeholder")
(json (object ("style" "jm-indigobook")))))
(define (bibliographybbl-style-set! style)
(hash-set! (cadr (hash-ref ht "params")) "style" style))
(define (make-bibliographybbl-post-data citekeys)
(set! id (+ 1 id))
(hash-set! ht "id" id)
(set-car! (hash-ref ht "params") citekeys)
(scm->json-string ht))
(define (get-bibliographybbl citekeys)
(receive (result port)
(http-post
zotero-better-bibtex-schomd-uri
#:body (make-bibliographybbl-post-data citekeys)
#:streaming? #t)
(json->scm port)))
(define (parse-citekeys aux)
(let loop ((citekeys '())
(aux-str (read-line aux)))
(cond
((eof-object? aux-str) citekeys)
((string= "\\bibstyle{" aux-str 0 9 0 9)
(begin
(bibliographybbl-style-set! (substring aux-str
10
(- (string-length aux-str) 1)))
(loop citekeys (read-line aux))))
((string= "\\citation{" aux-str 0 9 0 9)
(loop (cons (substring aux-str
10
(- (string-length aux-str) 1))
citekeys)
(read-line aux)))
(#t (loop citekeys (read-line aux))))))
(define (main args)
(let* ((aux (open-input-file (string-append (cadr args) ".aux")))
(bbl (open-output-file (string-append (cadr args) ".bbl")))
(citekeys (parse-citekeys aux))
(res-ht (get-bibliographybbl citekeys))
(res-err (hash-ref res-ht "error" #f))
(res-str (hash-ref res-ht "result" "")))
(if res-err
(let ((err-str (string-append "Error:" (number->string (hash-ref res-err "code"))
":" (hash-ref res-err "message") "\n")))
(display err-str (current-error-port))
(display err-str bbl))
(begin
(display (string-append res-str) bbl)))))
;;;
;; (define test-json-string-1
;; (scm->json-string (json
;; (object
;; ("jsonrpc" "2.0")
;; ("id" 666)
;; ("method" "tmbibliography")
;; ("params" [["galloway_jr_basic_1989"
;; "galloway_jr_basic_1992"
;; "_addington_1979"
;; "_douglas_1963"]
;; (object
;; ("style" "http://juris-m.github.io/styles/jm-babyblue"))
;; ])))))
;;; "{\"id\" : 666,\"jsonrpc\" : \"2.0\",\"params\" : [[\"galloway_jr_basic_1989\", \"galloway_jr_basic_1992\", \"_addington_1979\", \"_douglas_1963\"], {\"style\" : \"http://juris-m.github.io/styles/jm-babyblue\"}],\"method\" : \"tmbibliography\"}"
;;; Local Variables:
;;; mode: scheme
;;; geiser-scheme-implementation: guile
;;; End: