-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjournal-meta.sch
215 lines (199 loc) · 6.8 KB
/
journal-meta.sch
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
; ,open utils - for string-position
; ,open extended-ports - for make-string-input-port
; ,open signals
; Generate metadata (originally for Google Scholar, now for Rod Page)
(define $articles (element-constructor 'articles))
(define $article (element-constructor 'article))
(define $front (element-constructor 'front))
(define $journal-meta (element-constructor 'journal-meta))
(define $journal-title (element-constructor 'journal-title))
(define $abbrev-journal-title (element-constructor 'abbrev-journal-title))
(define $issn (element-constructor 'issn))
(define $publisher (element-constructor 'publisher))
(define $publisher-name (element-constructor 'publisher-name))
(define $article-meta (element-constructor 'article-meta))
(define $title-group (element-constructor 'title-group))
(define $article-title (element-constructor 'article-title))
(define $trans-title (element-constructor 'trans-title))
(define $contrib-group (element-constructor 'contrib-group))
(define $contrib (element-constructor 'contrib))
(define $name (element-constructor 'name))
(define $surname (element-constructor 'surname))
(define $given-names (element-constructor 'given-names))
(define $suffix (element-constructor 'suffix))
(define $pub-date (element-constructor 'pub-date))
(define $year (element-constructor 'year))
(define $volume (element-constructor 'volume))
(define $issue (element-constructor 'issue))
(define $fpage (element-constructor 'fpage))
(define $lpage (element-constructor 'lpage))
(define $self-uri (element-constructor 'self-uri))
; article-id -- no DOI's yet.
; day month
; Attributes
(define $contrib-type= (attribute-constructor 'contrib-type))
(define $pub-type= (attribute-constructor 'pub-type))
(define $xlink:href= (attribute-constructor 'xlink:href))
(define (write-articles-metadata volumes build-dir)
(call-with-output-file
(path->filename build-dir "journal_meta.xml")
(lambda (port)
(let ((journal-metadata (journal-metadata)))
(display "<?xml version='1.0' encoding='UTF-8'?>" port)
(xml-end-of-line port)
(display "<articles>" port)
(xml-end-of-line port)
(for-each (lambda (volnum)
(for-each (lambda (article)
(if (pair? (article-authors article))
(begin (write-item
(article-metadata article journal-metadata)
port)
(xml-end-of-line port)
(xml-end-of-line port))))
(or (get-volume-toc volnum) '()))
(xml-end-of-line port))
volumes)
(display "</articles>" port)
(newline port)))))
(define (write-volume-metadata volnum outfile)
(call-with-output-file outfile
(lambda (port)
(let ((journal-metadata (journal-metadata)))
(display "<?xml version='1.0' encoding='UTF-8'?>" port)
(xml-end-of-line port)
(display "<articles>" port)
(xml-end-of-line port)
(for-each (lambda (article)
(if (pair? (article-authors article))
(begin (write-item
(article-metadata article journal-metadata)
port)
(xml-end-of-line port)
(xml-end-of-line port))))
(or (get-volume-toc volnum) '()))
(display "</articles>" port)
(newline port)))))
(define (journal-metadata)
($journal-meta
($journal-title "Psyche: A Journal of Entomology")
($abbrev-journal-title "Psyche")
($issn "0033-2615")
($publisher
($publisher-name "Cambridge Entomological Club"))))
(define (article-metadata article journal-metadata)
($article
($front
journal-metadata
($article-meta
($title-group
($article-title (convert-markup (article-title article))))
($contrib-group
(map (lambda (auth)
(call-with-values
(lambda () (parse-author-name auth))
(lambda (surname given-names suffix)
(if (not given-names)
(warn "No given names for this author"
auth
article))
($contrib ($contrib-type= "author")
($name
($surname surname)
(if given-names
($given-names given-names)
'())
(if suffix
($suffix suffix)
'())
)))))
(article-authors article)))
($pub-date ($pub-type= "pub")
($year (article-year article)))
($volume (article-volume article))
($issue (article-issue article))
(if (article-pages article)
(list ($fpage (car (article-pages article)))
(let ((last (cadr (article-pages article))))
(if (integer? last)
($lpage last)
'())))
'())
(let ((path (path-to-landing-page article)))
(if path
($self-uri ($xlink:href= (string-append "http://psyche.entclub.org/"
(path->string path))))
'()))))))
(define (parse-author-name auth)
(let ((auth (explode-item auth)))
(call-with-values (lambda () (split-from-end auth #\,))
(lambda (first+last suffix)
(call-with-values (lambda () (split-from-end first+last #\space))
(lambda (first last)
(values first
last
(if (and (pair? suffix)
(eq? (car suffix) #\space))
(cdr suffix)
suffix))))))))
; Author name is either a string or a list of XML items - strings,
; entities, and elements.
; To do parsing we need to explode the strings into their constituent
; characters.
(define (explode-item item)
(cond ((string? item)
(string->list item))
((list? item)
(apply append
(map (lambda (item)
(cond ((string? item)
(string->list item))
((list? item) item)
(else (list item))))
item)))
(else (list item))))
(define (split-from-end z separator)
(let loop ((y '())
(z (reverse z)))
(if (null? z)
(values y #f)
(if (eq? (car z) separator)
(values y
(reverse (cdr z)))
(loop (cons (car z) y)
(cdr z))))))
; Strip tags but not entities
(define (convert-markup item)
(cond ((list? item)
(map convert-markup item))
((and (element? item)
(eq? (element-type item) 'i))
(make-element 'italic
(element-attributes item)
(element-content item)))
((procedure? item)
(let ((port (make-string-output-port)))
(item port)
(let ((s (string-output-port-output port)))
(list->string
(let gobble1 ((z (string->list s)))
(if (null? z)
z
(if (char=? (car z) #\<)
(let gobble2 ((z (cdr z)))
(if (null? z)
z
(if (char=? (car z) #\>)
(gobble1 (cdr z))
(gobble2 (cdr z)))))
(cons (car z)
(gobble1 (cdr z))))))))))
;; TBD: introduce entities where needed: < > &
(else item)))
(define (allow-entities arg)
(if (string? arg)
(let ((exploded (string->list arg)))
(if (memq #\& exploded)
(lambda (port) (display arg port))
arg))
arg))