etTransfo.xqm 21.1 KB
Newer Older
mrodzis's avatar
mrodzis committed
1
2
3
4
5
6
7
8
9
xquery version "3.1";

(:~
 : This module is responsible for pulling the whole transformation/serialization
 : of the edited text together.
 :
 : It creates all the single HTML files for each notebook as well as a complete
 : intermediate format TEI/XML that is used for the print output.
 :
mrodzis's avatar
mrodzis committed
10
11
 : This module is used in production and is called by tgconnect.
 :
mrodzis's avatar
mrodzis committed
12
13
14
15
16
17
 : @author Michelle Weidling
 : @version 1.0
 :)

module namespace etTransfo="http://fontane-nb.dariah.eu/etTransfo";

18
declare namespace err="http://www.w3.org/2005/xqt-errors";
mrodzis's avatar
mrodzis committed
19
20
21
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace xi="http://www.w3.org/2001/XInclude";

mrodzis's avatar
mrodzis committed
22
import module namespace abbrev-index="http://fontane-nb.dariah.eu/abbrev-index" at "abbrev-index.xqm";
mrodzis's avatar
mrodzis committed
23
24
25
26
27
28
import module namespace config="http://textgrid.de/ns/SADE/config" at "../../config/config.xqm";
import module namespace fontaneSimple="http://fontane-nb.dariah.eu/teisimple" at "tei2teisimple.xqm";
import module namespace fsort="http://fontane-nb.dariah.eu/sort" at "sort.xqm";
import module namespace functx = "http://www.functx.com";
import module namespace prepCom="http://fontane-nb.dariah.eu/prepCom" at "prepcom.xqm";
import module namespace simple2xhtml="http://fontane-nb.dariah.eu/simple2xhtml" at "simple2xhtml.xqm";
29
import module namespace tidy ="http://fontane-nb.dariah.eu/tidy" at "tidy.xqm";
mrodzis's avatar
mrodzis committed
30
31
32

declare variable $etTransfo:cases :=
    (
33
34
35
36
37
(:    "3qtcz.xml", (: case C :):)
(:    "3qtqv.xml", (: case A :):)
(:    "3qtqw.xml", (: case B :):)
    "3qtqx.xml" (: case D :)
(:    "3qtqz.xml" (: case E :):)
mrodzis's avatar
mrodzis committed
38
39
    );

mrodzis's avatar
mrodzis committed
40
declare variable $etTransfo:coll := "/db/apps/SADE/modules/fontane/edited-text/";
mrodzis's avatar
mrodzis committed
41

mrodzis's avatar
mrodzis committed
42
43

(:~
mrodzis's avatar
mrodzis committed
44
 : The main function. It is called by tgconnect.
mrodzis's avatar
mrodzis committed
45
46
47
48
49
50
 :
 : @author Michelle Weidling
 : @return An XHTML page for each notebook in /db/sade-projects/textgrid/data/print/xhtml
 : @return An intermediate TEI/XML with all notebooks in it in /db/sade-projects/textgrid/data/print/xml/fontane-full.xml
 :)
declare function etTransfo:complete() {
mrodzis's avatar
mrodzis committed
51
52
    etTransfo:assure-coll-available("print"),
    etTransfo:assure-coll-available("log"),
mrodzis's avatar
mrodzis committed
53
54
55
56
57
58
59
    for $case in $etTransfo:cases return
        (etTransfo:create-case($case),
        etTransfo:create-htmls($case)),

    try {
        etTransfo:create-print-tei()
    } catch * {
mrodzis's avatar
mrodzis committed
60
        error(QName("error", "ETTRANSFO02"), "An error occurred while creating the whole TEI base for print.")
61
    },
62
    etTransfo:tidy-logs(),
mrodzis's avatar
mrodzis committed
63
64
65
    try {
        etTransfo:report-errors()
    } catch * {
mrodzis's avatar
mrodzis committed
66
        error(QName("error", "ETTRANSFO15"), "An error occurred while reporting errors.")
mrodzis's avatar
mrodzis committed
67
    }
mrodzis's avatar
mrodzis committed
68
69
};

mrodzis's avatar
mrodzis committed
70

71
72
(:~
 : A function to trigger the creation of the edited text for a single notebook.
mrodzis's avatar
mrodzis committed
73
74
 : Mainly used for parallel creation of the notebooks but can also be used for
 : debugging.
75
 :
mrodzis's avatar
mrodzis committed
76
77
 : @param $uri The notebook's URI, e.g. "16b00"
 :)
78
declare function etTransfo:transform-single-nb($uri as xs:string) as xs:string* {
mrodzis's avatar
mrodzis committed
79
80
    let $assure-dir-available := etTransfo:assure-coll-available("print")
    let $assure-dir-available := etTransfo:assure-coll-available("log")
81

82
83
    let $log := util:log-system-out("Start creating edited text for " || $uri)
    let $log := etTransfo:create-log($uri)
84

85
86
87
88
89
90
91
92
93
    let $tei := doc($config:data-root || "/data/" || $uri || ".xml")//tei:TEI
    let $updated-notebook :=
        element tei:TEI {
            attribute id {$uri},
            $tei/@*,
            $tei/*
        }

    return
mrodzis's avatar
mrodzis committed
94
95
96
97
98
99
100
101
        (
          try {
              etTransfo:transform-tei($updated-notebook, $log)
          } catch * {
              etTransfo:add-log-entry($log, "ETTRANSFO08: Couldn't transform TEI.")
          },
          etTransfo:tidy-logs()
        )
102
103
};

mrodzis's avatar
mrodzis committed
104
105

(:~
mrodzis's avatar
mrodzis committed
106
107
108
 : Resolves all XIncludes in a given case file and copies all notebooks belonging
 : to a case into one XML file.
 :
mrodzis's avatar
mrodzis committed
109
110
 : This is mainly used to be able to serialize one case at a time which is
 : a project requirement.
mrodzis's avatar
mrodzis committed
111
112
113
114
115
116
117
118
119
120
121
122
 :
 : @author Michelle Weidling
 : @param $showcase The filename of a showcase, e.g. "12345.xml"
 : @return The full path to the created showcase with all notebooks in it.
 :)
declare function etTransfo:create-case($showcase as xs:string) as xs:string {
    let $doc :=
        try {
            doc("/db/sade-projects/textgrid/data/xml/data/" || $showcase)
        } catch * {
            error(QName("error", "ETTRANSFO03"), "An error occured while opening " || $showcase || ".")
        }
123
124
125
126

    let $log := etTransfo:create-log($doc//tei:title[1] => replace(" ", "-"))

    let $log := util:log-system-out("Start creating case " || $showcase || " (" || $doc//tei:title[1] || ").")
mrodzis's avatar
mrodzis committed
127
    let $new-filename := $doc//tei:title[1] => replace(" ", "-") || ".xml"
mrodzis's avatar
mrodzis committed
128
    let $assure-dir-available := etTransfo:assure-coll-available("print")
mrodzis's avatar
mrodzis committed
129
130
131
132
133
134
135
136
137

    let $create-new-file := xmldb:store($config:data-root || "/print/xml/", $new-filename, $doc/*)

    let $xis := doc($create-new-file)//xi:include
    let $update :=
        for $xi in $xis
            let $uri := $xi/@href/string()
            let $tei-referred-to :=
            try {
mrodzis's avatar
mrodzis committed
138
                doc($config:data-root || "/data/" || $uri || ".xml")/tei:TEI
mrodzis's avatar
mrodzis committed
139
            } catch * {
140
                etTransfo:add-log-entry($log, "ETTRANSFO04: Couldn't find the node tei:TEI of  " || $uri || ".")
mrodzis's avatar
mrodzis committed
141
142
            }
            let $updated-notebook :=
mrodzis's avatar
mrodzis committed
143
144
145
146
                element tei:TEI {
                  attribute id {$uri},
                  $tei-referred-to/@*,
                  $tei-referred-to/*
mrodzis's avatar
mrodzis committed
147
                }
mrodzis's avatar
mrodzis committed
148
149
150
151
152
153
154
155

            return update replace $xi with $updated-notebook
    return
        $create-new-file
};


(:~
mrodzis's avatar
mrodzis committed
156
 : Returns all notebooks of a showcase
mrodzis's avatar
mrodzis committed
157
158
159
160
161
 :
 : @author Michelle Weidling
 : @param $showcase The filename of a showcase, e.g. "12345.xml"
 : @return All notebooks of a showcase in-memory, i.e. a sequence of tei:TEI
 :)
162
declare function etTransfo:get-teis($showcase as xs:string) as element(tei:TEI)* {
mrodzis's avatar
mrodzis committed
163
    let $doc := doc($config:data-root || "/data/" || $showcase)
mrodzis's avatar
mrodzis committed
164
    let $summary-file-name := $doc//tei:title[1] => replace(" ", "-") || ".xml"
mrodzis's avatar
mrodzis committed
165
    let $log := doc($etTransfo:coll || "logs/" || $doc//tei:title[1] => replace(" ", "-") || "-log.xml")
166
    return
mrodzis's avatar
mrodzis committed
167
        try {
168
            doc($config:data-root || "/print/xml/" || $summary-file-name)//tei:TEI
mrodzis's avatar
mrodzis committed
169
        } catch * {
170
            etTransfo:add-log-entry($log, "ETTRANSFO05: Summary file for case " || $showcase || "couldn't be opened.")
mrodzis's avatar
mrodzis committed
171
172
173
174
175
176
177
178
179
180
181
        }
};


(:~
 : Creates the intermediate format of a given case and transforms each notebook of a showcase to a XHTML page.
 :
 : @author Michelle Weidling
 : @param $showcase The filename of a showcase, e.g. "12345.xml"
 : @return One or more strings indicating the location where the XHTML(s) have been stored to.
 :)
182
declare function etTransfo:create-htmls($showcase as xs:string) as xs:string* {
mrodzis's avatar
mrodzis committed
183
184
185
    let $teis := etTransfo:get-teis($showcase)

    for $tei in $teis return
186
        let $log := util:log-system-out("Start creating edited text for " || $tei//tei:idno[@type = "TextGrid"])
mrodzis's avatar
mrodzis committed
187
188
        let $log := etTransfo:create-log(substring-after($tei//tei:idno[@type = "TextGrid"], "textgrid:"))
        return
189
190
191
192
193
            try {
                etTransfo:transform-tei($tei, $log)
            } catch * {
                etTransfo:add-log-entry($log, "ETTRANSFO08: Couldn't transform TEI. ")
            }
mrodzis's avatar
mrodzis committed
194
195
};

mrodzis's avatar
mrodzis committed
196
197
198
199
200

(:~
 : Handles the transformation to the intermediate format and the XHTML
 : representation.
 :
mrodzis's avatar
mrodzis committed
201
202
203
 : The whole transformation is a five step process in order to keep different
 : steps apart semantically and logically.
 :
mrodzis's avatar
mrodzis committed
204
 : @param $tei The current notebook as element(tei:TEI)
mrodzis's avatar
mrodzis committed
205
 : @param $log The absolute path of the log file belonging to the current notebook
mrodzis's avatar
mrodzis committed
206
 :)
mrodzis's avatar
mrodzis committed
207
declare function etTransfo:transform-tei($tei as element(tei:TEI), $log as xs:string) {
208
209
210
211
    let $sorted :=
        try {
            fsort:main($tei, $log)
        } catch * {
mrodzis's avatar
mrodzis committed
212
213
            etTransfo:add-log-entry($log, "ETTRANSFO09: Error while sorting this notebook. Reason: 
" ||
            concat("[", $err:line-number, ": ", $err:column-number, "] Error ", $err:code, ": ", $err:description))
214
215
216
217
218
219
        }

    let $prepare-comment :=
        try {
            prepCom:main($sorted, $tei/@id)
        } catch * {
220
221
            etTransfo:add-log-entry($log, "ETTRANSFO10: Error while preparing commentary for this notebook. Reason: 
" ||
            concat("[", $err:line-number, ": ", $err:column-number, "] Error ", $err:code, ": ", $err:description))
222
223
224
225
226
227
        }

    let $transform-to-interform :=
        try {
            fontaneSimple:main($prepare-comment, $tei/@id, $log)
        } catch * {
228
229
            etTransfo:add-log-entry($log, "ETTRANSFO11: Error while transforming this notebook to an intermediate format.                Reason: 
" ||
            concat("[", $err:line-number, ": ", $err:column-number, "] Error ", $err:code, ": ", $err:description))
230
231
232
233
        }

    let $tidy-interform :=
        try {
234
            tidy:main($transform-to-interform, $tei/@id)
235
        } catch * {
236
237
            etTransfo:add-log-entry($log, "ETTRANSFO12: Error while tidying up the intermediate format for this notebook.                Reason: 
" ||
            concat("[", $err:line-number, ": ", $err:column-number, "] Error ", $err:code, ": ", $err:description))
238
239
240
241
242
243
        }

    return
        try {
            simple2xhtml:main($tidy-interform, $tei/@id)
        } catch * {
244
245
            etTransfo:add-log-entry($log, "ETTRANSFO13: Error while creating XHTML for this notebook. Reason: 
" ||
            concat("[", $err:line-number, ": ", $err:column-number, "] Error ", $err:code, ": ", $err:description))
246
        }
mrodzis's avatar
mrodzis committed
247
248
249
250
};


(:~
mrodzis's avatar
mrodzis committed
251
 : Assembles the intermediate format version of each notebook and copies it into one file.
mrodzis's avatar
mrodzis committed
252
 : Furthermore, additional data needed for the print is appended.
mrodzis's avatar
mrodzis committed
253
 :
mrodzis's avatar
mrodzis committed
254
255
256
 : The resulting file is mainly needed for serializing the print.
 : Attention: In case you change the file name, update the PERL script in the print repo as well.
 :
mrodzis's avatar
mrodzis committed
257
 : @author Michelle Weidling
mrodzis's avatar
mrodzis committed
258
 : @return A string indicating the location where the full TEI/XML of the edition was stored to, i.e. "/db/sade-projects/textgrid/data/xml/print/xml/fontane-full.xml"
mrodzis's avatar
mrodzis committed
259
260
261
 :)
declare function etTransfo:create-print-tei() as xs:string {
    let $xmls := etTransfo:get-all-xmls()
262
    let $log := etTransfo:create-log("general-print-creation")
mrodzis's avatar
mrodzis committed
263
264
265
    let $complete-print-file :=
        element {QName("http://www.tei-c.org/ns/1.0", "teiCorpus")} {
            $xmls,
mrodzis's avatar
mrodzis committed
266
267
268
            try {
                etTransfo:get-literature()
            } catch * {
mrodzis's avatar
mrodzis committed
269
                etTransfo:add-log-entry($log, "ETTRANSFO06: An error occurred while creating the bibliography.")
mrodzis's avatar
mrodzis committed
270
271
272
273
274
            },

            try {
                abbrev-index:main()
            } catch * {
mrodzis's avatar
mrodzis committed
275
                etTransfo:add-log-entry($log, "ETTRANSFO07: An error occurred while creating the index of abbreviations.")
mrodzis's avatar
mrodzis committed
276
277
278
279
280
            },

            try {
                etTransfo:create-overall-toc()
            } catch * {
mrodzis's avatar
mrodzis committed
281
                etTransfo:add-log-entry($log, "ETTRANSFO10: An error occurred while creating the overall TOC..")
mrodzis's avatar
mrodzis committed
282
            }
mrodzis's avatar
mrodzis committed
283
284
285
286
287
        }
    return
        xmldb:store($config:data-root || "/print/xml/", "fontane-full.xml", $complete-print-file)
};

mrodzis's avatar
mrodzis committed
288

mrodzis's avatar
mrodzis committed
289
290
291
292
(:~
 : An auxiliary function. Returns all final intermediate format TEI/XML files.
 :
 : @author Michelle Weidling
mrodzis's avatar
mrodzis committed
293
 : @return All available final intermediate format TEI/XML files as document nodes
mrodzis's avatar
mrodzis committed
294
 :)
mrodzis's avatar
mrodzis committed
295
declare function etTransfo:get-all-xmls() as node()+ {
mrodzis's avatar
mrodzis committed
296
297
    for $res in collection($config:data-root || "/print/xml/")
        [contains(base-uri(.), ".xml")
mrodzis's avatar
mrodzis committed
298
        and not(contains(substring-after(base-uri(.), "/print/xml/"), "-"))]
mrodzis's avatar
mrodzis committed
299
300
301
302
303
304
305
306
307
308
309
    order by $res/tei:TEI/@key1, number($res/tei:TEI/@key2)
    return
       $res
};


(:~
 : Creates a summary tei:div with all information needed for the print
 : bibliography.
 :
 : @author Michelle Weidling
mrodzis's avatar
mrodzis committed
310
 : @return A tei:div containing all bibliographic sections of the print
mrodzis's avatar
mrodzis committed
311
312
313
314
315
 :)
declare function etTransfo:get-literature() as element(tei:div) {
    element {QName("http://www.tei-c.org/ns/1.0", "text")} {
        attribute type {"bibliography"},

mrodzis's avatar
mrodzis committed
316
        element tei:div {
mrodzis's avatar
mrodzis committed
317
            attribute n {"Fontanes_Werke"},
mrodzis's avatar
mrodzis committed
318
319
320
321
322
            local:make-bib-div("Erstausgaben"),
            local:make-bib-div("Werkausgaben"),
            local:make-bib-div("Briefausgaben"),
            local:make-bib-div("Postume_Einzelausgaben"),
            local:make-bib-div("Populaere_postume_Einzelausgaben")
mrodzis's avatar
mrodzis committed
323
        },
mrodzis's avatar
mrodzis committed
324
325
326
327
328
329
        local:make-bib-div("Fontanes_Quellen"),
        local:make-bib-div("Allgemeine_Nachschlagewerke"),
        local:make-bib-div("Nachschlagewerke_der_Fontane-Forschung"),
        local:make-bib-div("Forschungsliteratur")
    }
};
mrodzis's avatar
mrodzis committed
330
331


mrodzis's avatar
mrodzis committed
332
(:~
mrodzis's avatar
mrodzis committed
333
 : An auxiliary function to create the tei:div elements/sections in the bibliography.
mrodzis's avatar
mrodzis committed
334
335
 :
 : @author Michelle Weidling
mrodzis's avatar
mrodzis committed
336
337
 : @param $identifier An identifier for a subtype of scholarly literature, e.g. "Briefausgaben"
 : @return A tei:div containing all bibliographic information of the literature's subtype
mrodzis's avatar
mrodzis committed
338
339
 :)
declare function local:make-bib-div($identifier as xs:string) as element(tei:div) {
mrodzis's avatar
mrodzis committed
340
    element {QName("http://www.tei-c.org/ns/1.0", "div")} {
mrodzis's avatar
mrodzis committed
341
342
        attribute n {$identifier},
        etTransfo:make-bib-entries($identifier)
mrodzis's avatar
mrodzis committed
343
344
345
    }
};

mrodzis's avatar
mrodzis committed
346
347


mrodzis's avatar
mrodzis committed
348
(:~
mrodzis's avatar
mrodzis committed
349
 : Creates a tei:div containing all the scholarly literature ("Forschungsliteratur")
mrodzis's avatar
mrodzis committed
350
351
352
353
354
 : of a kind that's listed in the respective index. Examples for this are
 : "Allgemeine Nachschlagewerke" and "Werkausgaben"
 :
 : @author Michelle Weidling
 : @param $type The type of a tei:listBibl, e.g. "Forschungsliteratur"
mrodzis's avatar
mrodzis committed
355
 : @return A tei:div containing the scribal abbreviation as well as the full
mrodzis's avatar
mrodzis committed
356
357
358
 : bibliographic reference
 :)
declare function etTransfo:make-bib-entries($type as xs:string) as element(tei:div)* {
mrodzis's avatar
mrodzis committed
359
360
    (: 25547.xml is the file that contains all bibliographical information :)
    for $entry in doc($config:data-root || "/data/25547.xml")//tei:listBibl[@type = $type]//tei:bibl
mrodzis's avatar
mrodzis committed
361
362
363
364
365
366
367
368
369
        let $abbr :=
            if($entry/tei:abbr) then
                $entry/tei:abbr
            else
                $entry/tei:choice/tei:abbr
        let $full-ref := $entry/tei:choice/tei:expan
        let $bib-no := count($entry/ancestor::tei:bibl)

        return
mrodzis's avatar
mrodzis committed
370
            element {QName("http://www.tei-c.org/ns/1.0", "div")} {
mrodzis's avatar
mrodzis committed
371
                attribute type {"entry"},
mrodzis's avatar
mrodzis committed
372
                element {QName("http://www.tei-c.org/ns/1.0", "seg")} {
mrodzis's avatar
mrodzis committed
373
374
375
376
                    attribute type {"abbr"},
                    $abbr
                },
                if($full-ref) then
mrodzis's avatar
mrodzis committed
377
                    element {QName("http://www.tei-c.org/ns/1.0", "seg")} {
mrodzis's avatar
mrodzis committed
378
379
380
                        attribute type {"full"},
                        $full-ref
                    }
mrodzis's avatar
mrodzis committed
381
382
383
384
385
386
387
388

                else if($entry/tei:ptr[@target]) then
                    let $target-id := replace($entry/tei:ptr/@target, "#", "")
                    let $referenced-entry := $entry/ancestor::tei:body//tei:bibl[@xml:id = $target-id]
                    let $same-as := $referenced-entry/tei:choice/tei:abbr/string()
                    let $same-as-loc := $referenced-entry/ancestor::tei:listBibl[1]/@type/string()

                    return
mrodzis's avatar
mrodzis committed
389
390
391
392
393
394
395
396
397
398
                        (
                          element {QName("http://www.tei-c.org/ns/1.0", "seg")} {
                              attribute type {"same-as"},
                              $same-as
                          },
                          element {QName("http://www.tei-c.org/ns/1.0", "seg")} {
                              attribute type {"same-as-loc"},
                              $same-as-loc
                          }
                        )
mrodzis's avatar
mrodzis committed
399
400
401
402

                else
                    (),
                if($bib-no gt 0) then
mrodzis's avatar
mrodzis committed
403
                    element {QName("http://www.tei-c.org/ns/1.0", "seg")} {
mrodzis's avatar
mrodzis committed
404
405
406
407
408
409
                        attribute type {"sub-entry"},
                        $bib-no
                    }
                else
                    ()
            }
mrodzis's avatar
mrodzis committed
410
};
mrodzis's avatar
mrodzis committed
411
412


mrodzis's avatar
mrodzis committed
413
414
415
416
417
418
419
(:~
 : Creates a log file for a given notebook.
 :
 : @author Michelle Weidling
 : @param $uri The notebook's URI, e.g. "12345.xml"
 : @return The location of the stored log-file, e.g. "/db/apps/sade/modules/fontane/edited-text/logs/12345-log.xml"
 :)
mrodzis's avatar
mrodzis committed
420
declare function etTransfo:create-log($uri as xs:string) as xs:string {
mrodzis's avatar
mrodzis committed
421
  let $assure-dir-available := etTransfo:assure-coll-available("logs")
mrodzis's avatar
mrodzis committed
422
  let $log-name := $uri || "-log.xml"
mrodzis's avatar
mrodzis committed
423
  return xmldb:store($etTransfo:coll || "logs", $log-name, <log/>)
mrodzis's avatar
mrodzis committed
424
425
426
};


mrodzis's avatar
mrodzis committed
427
428
429
430
(:~
 : Makes sure a requested collection is available.
 :
 : @author Michelle Weidling
mrodzis's avatar
mrodzis committed
431
 : @param $flag A string indicating the target collection
mrodzis's avatar
mrodzis committed
432
433
434
435
436
437
438
 : @return The location of the log collection, "/db/apps/sade/modules/fontane/edited-text/logs/"
 :)
declare function etTransfo:assure-coll-available($flag as xs:string) {
    switch ($flag)
        case "logs" return
            if(xmldb:collection-available($etTransfo:coll || "logs")) then
                $etTransfo:coll || "logs"
439
            else
mrodzis's avatar
mrodzis committed
440
                xmldb:create-collection($etTransfo:coll, "logs")
441
442
443
444
445
446
447
448
449
450

        case "print" return
            if(xmldb:collection-available($config:data-root || "/print/")) then
                ()
            else
                (xmldb:create-collection($config:data-root, "/print"),
                xmldb:create-collection($config:data-root || "/print/", "xml"),
                xmldb:create-collection($config:data-root || "/print/", "xhtml"))
        default return ()
};
451

mrodzis's avatar
mrodzis committed
452
453
454
455
456

(:~
 : Adds a log entry to a given log file.
 :
 : @author Michelle Weidling
mrodzis's avatar
mrodzis committed
457
 : @param $log-file The path to the log file, e.g. "/db/apps/sade/modules/fontane/edited-text/logs/12345-log.xml"
mrodzis's avatar
mrodzis committed
458
459
460
 : @param $message The message of the log entry
 : @return The location of the log collection, "/db/apps/sade/modules/fontane/edited-text/logs/"
 :)
461
462
463
declare function etTransfo:add-log-entry($log-file as xs:string,
$message as xs:string) as empty-sequence() {
  let $entry := <LogEntry timestamp="{util:system-time()}">{$message}</LogEntry>
mrodzis's avatar
mrodzis committed
464
465
  return
    update insert $entry into doc($log-file)/*
466
467
468
};


469
(:~
mrodzis's avatar
mrodzis committed
470
 : Removes all log files that don't contain any information.
mrodzis's avatar
mrodzis committed
471
 :
mrodzis's avatar
mrodzis committed
472
 : @author Michelle Weidling
473
 :)
mrodzis's avatar
mrodzis committed
474
475
declare function etTransfo:tidy-logs() as item()* {
    for $log in collection($etTransfo:coll || "logs/") return
476
477
478
479
480
        if($log//LogEntry) then
            ()
        else
            let $uri := substring-after(base-uri($log), "logs/")
            return
mrodzis's avatar
mrodzis committed
481
                xmldb:remove($etTransfo:coll || "logs/", $uri)
482
};
483

mrodzis's avatar
mrodzis committed
484
(:~
mrodzis's avatar
mrodzis committed
485
486
 : Prints all errors found during the transformation to stdout.
 :
mrodzis's avatar
mrodzis committed
487
488
 : @author Michelle Weidling
 :)
489
declare function etTransfo:report-errors() as item()* {
mrodzis's avatar
mrodzis committed
490
    let $log-path := $etTransfo:coll || "logs/"
491
    return
mrodzis's avatar
mrodzis committed
492
        if(normalize-space(string-join(xmldb:get-child-resources($log-path), "")) = "") then
493
494
495
496
497
498
499
500
501
502
503
504
505
506
            util:log-system-out("No errors found while creating the edited text! Yay!")
        else
            (
                util:log-system-out("Errors detected in the following notebooks:"),
                for $log in collection($log-path) return
                    (
                        util:log-system-out("*&#8195;" ||
                        substring-after(base-uri($log), "logs/")
                        => substring-before("-log")),
                        util:log-system-out($log)
                    ),
                util:log-system-out("Total: " || count(xmldb:get-child-resources($log-path)) || " notebook(s).")
            )
};
mrodzis's avatar
mrodzis committed
507
508
509


(:~
mrodzis's avatar
mrodzis committed
510
 : Creates the overall TOC by collecting information from all "Überblickskommentare".
mrodzis's avatar
mrodzis committed
511
 : This info is needed for a separate kind of index in the book.
mrodzis's avatar
mrodzis committed
512
 :
mrodzis's avatar
mrodzis committed
513
514
 : This feature is still in development and has to be extended to work with all
 : notebooks, not just E1.
mrodzis's avatar
mrodzis committed
515
 :
mrodzis's avatar
mrodzis committed
516
517
 : @author Michelle Weidling
 :)
mrodzis's avatar
mrodzis committed
518
519
declare function etTransfo:create-overall-toc() as element(tei:div) {
    let $nbs := collection($config:data-root || "/data")
mrodzis's avatar
mrodzis committed
520
    let $items := $nbs//tei:TEI[descendant::tei:title = "Notizbuch E1"]/tei:teiHeader//tei:msContents/tei:ab/tei:list[@type="editorial"]/tei:item
mrodzis's avatar
mrodzis committed
521
522
    let $sortPattern := "^\W+"

mrodzis's avatar
mrodzis committed
523
    return element {QName("http://www.tei-c.org/ns/1.0", "div")} {
mrodzis's avatar
mrodzis committed
524
525
        attribute type {"overall-toc"},
        let $segs :=
mrodzis's avatar
mrodzis committed
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
          for $doc in $nbs
          return
              let $nb-name := $doc//tei:fileDesc/tei:titleStmt/tei:title[1]/substring-after(., " ")
              return
                  for $item in $doc//tei:msContents/tei:ab/tei:list[@type="editorial"]//tei:item return
                      let $first-ref := $item//tei:ref[@type = "first"]/@target/string()
                      let $last-ref := $item//tei:ref[@type = "last"]/@target/string()
                      let $refs :=
                          for $ref in ($first-ref, $last-ref) return
                              let $tokens := tokenize($ref, " ")
                              return $tokens[2]
                      return
                            element {QName("http://www.tei-c.org/ns/1.0", "seg")} {
                              attribute type {$nb-name},
                              attribute source {
                                  let $string := for $ref in $refs return
                                      substring-after($ref, "@xml:id='")
                                      => substring-before("']")
                                  return
                                      string-join($string, " ")
                              },
                              normalize-space(functx:substring-before-last($item, ")") || ")")
                          }
mrodzis's avatar
mrodzis committed
549
550
551
552
553
        return for $seg in $segs
            order by replace($seg, $sortPattern, "") => substring(1, 1) => upper-case()
            return $seg
    }
};