summaryrefslogtreecommitdiff
path: root/skribe/src/bigloo/bib.bgl
blob: 6b0f7ddee5cde339337b409489dd21de8c81e034 (about) (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
;*=====================================================================*/
;*    serrano/prgm/project/skribe/src/bigloo/bib.bgl                   */
;*    -------------------------------------------------------------    */
;*    Author      :  Manuel Serrano                                    */
;*    Creation    :  Fri Dec  7 06:12:29 2001                          */
;*    Last change :  Tue Nov  2 17:14:02 2004 (serrano)                */
;*    Copyright   :  2001-04 Manuel Serrano                            */
;*    -------------------------------------------------------------    */
;*    Skribe Bibliography                                              */
;*    -------------------------------------------------------------    */
;*    Implementation: @label bib@                                      */
;*    bigloo: @path ../common/bib.scm@                                 */
;*=====================================================================*/

;*---------------------------------------------------------------------*/
;*    The module                                                       */
;*---------------------------------------------------------------------*/
(module skribe_bib
   
   (include "new.sch")
   
   (import  skribe_types
	    skribe_lib
	    skribe_resolve
	    skribe_eval
	    skribe_read)
   
   (export  (bib-table?::bool ::obj)
	    (make-bib-table ::bstring) 
	    (default-bib-table)
	    (bib-load! ::obj ::bstring ::obj)
	    (bib-add! ::obj . entries)
	    (resolve-bib ::obj ::obj)
	    (resolve-the-bib ::obj ::obj ::procedure ::obj ::symbol ::pair-nil)
	    (bib-sort/authors::pair-nil ::pair-nil)
	    (bib-sort/idents::pair-nil ::pair-nil)
	    (bib-sort/dates::pair-nil ::pair-nil)))

;*---------------------------------------------------------------------*/
;*    bib-table? ...                                                   */
;*---------------------------------------------------------------------*/
(define (bib-table? obj)
   (hashtable? obj))

;*---------------------------------------------------------------------*/
;*    *bib-table* ...                                                  */
;*---------------------------------------------------------------------*/
(define *bib-table* #f)

;*---------------------------------------------------------------------*/
;*    make-bib-table ...                                               */
;*---------------------------------------------------------------------*/
(define (make-bib-table ident)
   (make-hashtable))

;*---------------------------------------------------------------------*/
;*    default-bib-table ...                                            */
;*---------------------------------------------------------------------*/
(define (default-bib-table)
   (if (not *bib-table*)
       (set! *bib-table* (make-bib-table "default-bib-table")))
   *bib-table*)

;*---------------------------------------------------------------------*/
;*    bib-parse-error ...                                              */
;*---------------------------------------------------------------------*/
(define (bib-parse-error entry)
   (if (epair? entry)
       (match-case (cer entry)
	  ((at ?fname ?pos ?-)
	   (error/location "parse-biblio"
			   "bibliography syntax error"
			   entry
			   fname
			   pos))
	  (else
	   (error 'bib-parse "bibliography syntax error" entry)))
       (error 'bib-parse "bibliography syntax error" entry)))

;*---------------------------------------------------------------------*/
;*    bib-duplicate ...                                                */
;*---------------------------------------------------------------------*/
(define (bib-duplicate ident from old)
   (let ((ofrom (markup-option old 'from)))
      (skribe-warning 2
		      'bib
		      (format "Duplicated bibliographic entry ~a'.\n" ident)
		      (if ofrom
			  (format " Using version of `~a'.\n" ofrom)
			  "")
		      (if from
			  (format " Ignoring version of `~a'." from)
			  " Ignoring redefinition."))))
   
;*---------------------------------------------------------------------*/
;*    parse-bib ...                                                    */
;*---------------------------------------------------------------------*/
(define (parse-bib table port)
   (if (not (bib-table? table))
       (skribe-error 'parse-bib "Illegal bibliography table" table)
       (let ((from (input-port-name port)))
	  (let loop ((entry (skribe-read port)))
	     (if (not (eof-object? entry))
		 (match-case entry
		    (((and (? symbol?) ?kind) (and (? symbol?) ?ident) . ?fds)
		     (let* ((ident (symbol->string ident))
			    (old (hashtable-get table ident)))
			(if old
			    (bib-duplicate ident from old)
			    (hashtable-put! table
					    ident
					    (make-bib-entry kind
							    ident
							    fds
							    from))))
		     (loop (skribe-read port)))
		    (((and (? symbol?) ?kind) (and (? string?) ?ident) . ?fds)
		     (let ((old (hashtable-get table ident)))
			(if old
			    (bib-duplicate ident from old)
			    (hashtable-put! table
					    ident
					    (make-bib-entry kind
							    ident
							    fds
							    from))))
		     (loop (skribe-read port)))
		    (else
		     (bib-parse-error entry))))))))

;*---------------------------------------------------------------------*/
;*    bib-add! ...                                                     */
;*---------------------------------------------------------------------*/
(define (bib-add! table . entries)
   (if (not (bib-table? table))
       (skribe-error 'bib-add! "Illegal bibliography table" table)
       (for-each (lambda (entry)
		    (match-case entry
		       (((and (? symbol?) ?kind) (and (? symbol?) ?ident) . ?fs)
			(let* ((ident (symbol->string ident))
			       (old (hashtable-get table ident)))
			   (if old
			       (bib-duplicate ident #f old)
			       (hashtable-put! table
					       ident
					       (make-bib-entry kind
							       ident fs #f)))))
		       (((and (? symbol?) ?kind) (and (? string?) ?ident) . ?fs)
			(let ((old (hashtable-get table ident)))
			   (if old
			       (bib-duplicate ident #f old)
			       (hashtable-put! table
					       ident
					       (make-bib-entry kind
							       ident fs #f)))))
		       (else
			(bib-parse-error entry))))
		 entries)))