-
Notifications
You must be signed in to change notification settings - Fork 6
/
shell-file.xq
246 lines (238 loc) · 12.9 KB
/
shell-file.xq
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
xquery version "3.0";
import module namespace release = "http://history.state.gov/ns/xquery/release" at "modules/release.xql";
import module namespace frus = "http://history.state.gov/ns/xquery/frus" at "modules/frus.xql";
import module namespace console="http://exist-db.org/xquery/console";
declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method "html5";
declare option output:media-type "text/html";
declare function local:generate-shell($vol-ids) {
let $volumes := collection('/db/apps/frus/bibliography')/volume[@id = $vol-ids]
let $shell-files :=
for $vol in $volumes
return
<TEI xmlns="http://www.tei-c.org/ns/1.0" xml:id="{$vol/@id}">
<teiHeader>
<fileDesc>
<titleStmt>
<title type="complete">{normalize-space($vol/*:title[@type='complete'])}</title>
<title type="series">{$vol/*:title[@type='series']/string()}</title>
<title type="sub-series">{$vol/*:title[@type='sub-series']/string()}</title>
<title type="volume-number">{normalize-space($vol/*:title[@type='volume-number'])}</title>
<title type="volume">{normalize-space($vol/*:title[@type='volume'])}</title>
{
if ($vol/*:editor[@role='primary'][. ne '']) then
$vol/*:editor[@role='primary'][. ne ''] ! <editor role="{./@role}">{./string()}</editor>
else
<editor role="primary">???</editor>
,
if ($vol/*:editor[@role='general'][. ne '']) then
$vol/*:editor[@role='general'][. ne ''] ! <editor role="{./@role}">{./string()}</editor>
else
<editor role="general">???</editor>
}
</titleStmt>
<publicationStmt>
<publisher>United States Government Publishing Office</publisher>
<pubPlace>Washington</pubPlace>
<date>{($vol/*:published-year[. ne '']/string(), '???')[1]}</date>
<idno type="dospubno"></idno>
<idno type="isbn-10">{$vol/*:isbn10/string()}</idno>
<idno type="isbn-13">{normalize-space($vol/*:isbn13)}</idno>
<idno type="frus">{$vol/@id/string()}</idno>
<idno type="oclc">{$vol/*:location[@loc='worldcat']/string()}</idno>
</publicationStmt>
<sourceDesc><p>Released in {($vol/*:published-year[. ne '']/string(), '???')[1]}</p></sourceDesc>
</fileDesc>
<revisionDesc>
<change>{format-date(current-date(), '[Y]-[M01]-[D01]')}: Created TEI shell</change>
</revisionDesc>
</teiHeader>
<text>
<front>
<titlePage>
<docTitle>
<titlePart type="series">{$vol/*:title[@type='series']/string()}</titlePart>
<titlePart type="subseries">{$vol/*:title[@type='sub-series']/string()}</titlePart>
<titlePart type="volumeno">{normalize-space($vol/*:title[@type='volume-number'])}</titlePart>
<titlePart type="volumetitle">{$vol/*:title[@type='volume']/string()}</titlePart>
</docTitle>
<byline>
<hi rend="italic">Editor{
if (count($vol/*:editor[@role='primary'][. ne '']) gt 1) then 's' else ''
}</hi>: {
if ($vol/*:editor[@role="primary"][. ne '']) then
($vol/*:editor[@role="primary"][. ne ''] ! <name>{./string()}</name>)
else
<name>???</name>
}
</byline>
<byline>
<hi rend="italic">General Editor</hi>: <name>{($vol/*:editor[@role="general"][. ne '']/string(), '???')[1]}</name>
</byline>
<docImprint>
<publisher>United States Government Publishing Office</publisher>
<pubPlace>Washington</pubPlace>
<docDate>{($vol/*:published-year[. ne '']/string(), '???')[1]}</docDate>
DEPARTMENT OF STATE O<hi rend="smallcaps">ffice</hi>
<hi rend="smallcaps">of</hi>
<hi rend="smallcaps">the</hi> H<hi rend="smallcaps">istorian</hi> F<hi
rend="smallcaps">oreign</hi> S<hi rend="smallcaps">ervice</hi>
I<hi rend="smallcaps">nstitute</hi> For sale by the Superintendent of Documents,
U.S. Government Publishing Office Internet: bookstore.gpo.gov Phone: toll free
(866) 512-1800; DC area (202) 512-1800 Fax: (202) 512-2250 Mail: Stop IDCC,
Washington, DC 20402-0001</docImprint>
</titlePage>
<div xml:id="pressrelease" type="section">
<head>Press Release</head>
<p rend="right">
<hi rend="strong">Office of the Historian<lb/> Foreign Service Institute<lb/>
United States Department of State<lb/>??? ???, {($vol/*:published-year/string(), '???')[1]}</hi>
</p>
{
let $paragraphs := $vol/*:summary/*:p[. ne '']
return
if (exists($paragraphs)) then
for $p in $vol/*:summary/*:p[. ne '']
return
<p>{$p/string()}</p>
else
<p>???</p>
}
<p>This volume was compiled and edited by {
(
let $editors := $vol/*:editor[@role="primary"][. ne '']
let $editor-count := count($editors)
let $first-editors := subsequence($editors, 1, $editor-count - 1)
let $last-editor := $editors[last()]
return
string-join(
(
string-join(
$first-editors,
", "
),
$last-editor
),
", and "
)
,
'???'
)[1]
}. The volume and this
press release are available on the Office of the Historian website at <ref
target="https://history.state.gov/historicaldocuments/{$vol/@id}"
>https://history.state.gov/historicaldocuments/{$vol/@id/string()}</ref>. Copies
of the volume will be available for purchase from the U.S. Government Publishing
Office online at <ref target="http://bookstore.gpo.gov"
>http://bookstore.gpo.gov</ref> (GPO S/N ???; ISBN {(normalize-space($vol/*:isbn13), '???')[1]}), or by calling
toll-free 1-866-512-1800 (D.C. area 202-512-1800). For further information,
contact <ref target="mailto:history@state.gov">history@state.gov</ref>. </p>
</div>
<!--<div xml:id="preface" type="section"/>
<div xml:id="sources" type="section"/>
<div xml:id="terms" type="section"/>
<div xml:id="persons" type="section"/>-->
</front>
<body/>
<back/>
</text>
</TEI>
return
try {
if (count($shell-files) gt 1) then
let $zip-filename := 'frus-shell-files-' || format-date(current-date(), '[Y]-[M01]-[D01]') || '.zip'
let $entries :=
for $shell-file in $shell-files
let $filename := $shell-file/@xml:id || '.xml'
return
<entry name="{$filename}" type="xml">{$shell-file}</entry>
return
(
response:set-header("Content-Disposition", concat("attachment; filename=", $zip-filename))
,
response:stream-binary(compression:zip($entries, false()), 'application/zip', $zip-filename)
,
<p class="bg-success text-success">{ count($shell-files) } shell files included in { $zip-filename }.</p>
)
else
let $filename := $shell-files/@xml:id || '.xml'
let $serialization-parameters :=
<output:serialization-parameters>
<output:method>xml</output:method>
<output:indent>yes</output:indent>
</output:serialization-parameters>
return
(
response:set-header("Content-Disposition", concat("attachment; filename=", $filename))
,
response:stream($shell-files, "method=xml indent=yes")
,
<p class="bg-success text-success">Generated shell file for { $filename }.</p>
)
,
string-join($shell-files/@xml:id ! (. || '.xml'), '; ')
} catch * {
<p class="bg-danger">There was an unexpected problem. {concat($err:code, ": ", $err:description, ' (', $err:module, ' ', $err:line-number, ':', $err:column-number, ')')}</p>
}
};
declare function local:form($volumes as xs:string*) {
<form action="{request:get-url()}">
<div class="form-group">
<label for="volumes" class="control-label">Volume IDs</label>
<div>
<textarea name="volumes" id="volumes" class="form-control" rows="6">{$volumes}</textarea>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Generate Shell Files</button>
<a class="btn btn-default" href="{request:get-url()}" role="button">Clear</a>
</div>
</form>
};
declare function local:validate($vol-ids as xs:string*) {
for $vol-id in $vol-ids
return
if (collection('/db/apps/frus/bibliography')/volume[@id = $vol-ids]) then ()
else $vol-id
};
let $titles := ('Release', 'Shell File Helper')
let $new-volumes := request:get-parameter('volumes', ())
let $body :=
<div>
<h2>{$titles[2]}</h2>
{
if ($new-volumes) then
(
local:form($new-volumes)
,
let $vol-ids :=
for $vol-id in tokenize($new-volumes, '\s+')[. ne '']
order by $vol-id
return $vol-id
let $invalid-ids := local:validate($vol-ids)
return
if (empty($invalid-ids)) then
<div>
<h2>Results</h2>
{local:generate-shell($vol-ids)}
</div>
else
<div class="bg-danger">
<p>The following volume ID(s) are invalid. Please correct the following and resubmit.</p>
<ul>{
for $vol-id in $invalid-ids
return
<li>{$vol-id}</li>
}</ul>
</div>
)
else
(
local:form(()),
<p>Please enter volume IDs, one per line. (Click <a href="?volumes=frus1989-92v01">here</a> to try generating a shell file for frus1989-92v01.)
If you enter a single volume ID, a single XML file will download; if you enter multiple, the files will be compressed and downloaded as a .zip file.</p>
)
}
</div>
return
release:wrap-html($titles, $body)