transform2teisimple.xqm 25.4 KB
Newer Older
MRodz's avatar
MRodz committed
1
2
3
xquery version "3.1";

(:~ 
Michelle Rodzis's avatar
Michelle Rodzis committed
4
5
6
7
 : This modules handles the conversion of the Fontante-TEI/XML into TEI simplePrint
 : for the edited text. The resulting TEI simplePrint is the basis for the "Editerter
 : Text" (edited text) view on the website and the book and represent the latest 
 : layer of text.
MRodz's avatar
MRodz committed
8
9
10
 : 
 : @author Michelle Rodzis
 : @version 0.1
Michelle Rodzis's avatar
Michelle Rodzis committed
11
 : @since TODO
MRodz's avatar
MRodz committed
12
13
14
15
 :)

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

MRodz's avatar
MRodz committed
16

MRodz's avatar
MRodz committed
17
18
19
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace test="http://exist-db.org/xquery/xqsuite";

20
import module namespace config="http://textgrid.de/ns/SADE/config" at "../config/config.xqm";
MRodz's avatar
MRodz committed
21
import module namespace console="http://exist-db.org/xquery/console";
MRodz's avatar
MRodz committed
22
import module namespace functx = "http://www.functx.com";
MRodz's avatar
MRodz committed
23
24
25
26
27
28
29

(:~ 
 : The main function initiates the transformation of a given notebook.
 : 
 : TODO: adapt to several input files?
 : 
 : :)
30
declare function fontane-simple:main($file as xs:string) as xs:string? {
MRodz's avatar
MRodz committed
31
32
33
34
35
36
37
  let $doc := 
    try {
      (doc("/db/sade-projects/textgrid/data/xml/data/" || $file))
    } catch * {
      (console:log("It was not possible to open the requested file " || $file))
    }
    
MRodz's avatar
Fix #23    
MRodz committed
38
39
    let $front-covers := $doc//tei:sourceDoc/tei:surface[contains(@n, "front_cover")]
    let $back-covers := $doc//tei:sourceDoc/tei:surface[contains(@n, "back_cover")]
MRodz's avatar
MRodz committed
40
    let $content := $doc//tei:sourceDoc/tei:surface[not(contains(@n, "cover") 
MRodz's avatar
MRodz committed
41
        or matches(@n, "spine"))]
MRodz's avatar
MRodz committed
42
    
43
    let $tei := <TEI xmlns="http://www.tei-c.org/ns/1.0">
MRodz's avatar
MRodz committed
44
        {$doc//tei:teiHeader}
MRodz's avatar
MRodz committed
45
46
47
48
49
        <text>
            <front>{fontane-simple:transform($front-covers)}</front>
            <body>{fontane-simple:transform($content)}</body>
            <back>{fontane-simple:transform($back-covers)}</back>
        </text>
MRodz's avatar
MRodz committed
50
      </TEI>
51
    return xmldb:store("/db/apps/SADE/resources/xml/", "tei-simple.xml", $tei)
MRodz's avatar
MRodz committed
52
53
};

MRodz's avatar
MRodz committed
54
55
56
57
58
59
60
61
62
(:~ 
 : Recursivly iterates the passed nodes and converts them according to the 
 : requirements for the "Edierter Text". 
 : 
 : The requirements are part of the encoding documentation, c.f.
 : https://fontane-nb.dariah.eu/doku.html.
 : 
 : @author Michelle Rodzis
 : @param $nodes
Michelle Rodzis's avatar
Michelle Rodzis committed
63
 : @return $node()* a TEI simplePrint element
MRodz's avatar
MRodz committed
64
65
 :)
declare function fontane-simple:transform($nodes as node()*) as node()* {
MRodz's avatar
MRodz committed
66
67
68
    for $node in $nodes
      return
        typeswitch ($node)
MRodz's avatar
MRodz committed
69
70
71
72
73
74
75
        case text() return
            local:prepare-text($node)
        
        case element(tei:lb) return
            if($node[@break = "keepHyphen"]) then
                ()
            else
Michelle Rodzis's avatar
Michelle Rodzis committed
76
                local:copy-element($node)
MRodz's avatar
MRodz committed
77
78
79
80
81
                
        case element(tei:g) return
            if($node[@ref = "#vds"]) then
                ()
            else if($node/@ref ="#rth" or $node/@ref ="#hb") then
Michelle Rodzis's avatar
Michelle Rodzis committed
82
                local:copy-element($node)
MRodz's avatar
MRodz committed
83
84
            else
                fontane-simple:transform($node/node())
85
                
MRodz's avatar
MRodz committed
86
87
88
89
90
91
        case element(tei:del) return
            if($node/parent::tei:restore) then
                fontane-simple:transform($node/node())
            else if($node/descendant::tei:restore) then
                fontane-simple:transform($node/descendant::tei:restore)
            else
MRodz's avatar
MRodz committed
92
                ()
93
                
MRodz's avatar
MRodz committed
94
95
96
97
98
99
        case element(tei:restore) return
            if(count($node/child::*) = 1 and $node/child::tei:del
            and $node/ancestor::tei:del) then
                ()
            else
                fontane-simple:transform($node/node())
100
                
MRodz's avatar
MRodz committed
101
102
103
        case element(tei:retrace) return
            if($node/@rend) then
                element tei:seg {
Michelle Rodzis's avatar
Michelle Rodzis committed
104
                    attribute rendition {$node/@rend},
MRodz's avatar
MRodz committed
105
106
107
108
                    fontane-simple:transform($node/node())
                }
            else
                fontane-simple:transform($node/node())
109
110
                
        case element(tei:add) return
111
112
            if($node/@type = "edited_text" 
            or $node/child::tei:seg[@type = "multiphrase"]) then
Michelle Rodzis's avatar
Michelle Rodzis committed
113
                local:copy-element($node)
MRodz's avatar
MRodz committed
114
115
            else if($node/@cause ="catchword" or $node/@cause ="unclear") then
                ()
MRodz's avatar
MRodz committed
116
117
            else if($node/@rend ="|") then
                fontane-simple:transform($node/node())
118
119
120
121
122
123
124
125
126
127
128
129
            else if(not($node/@xml:id)) then
                fontane-simple:transform($node/node())
            else if(fontane-simple:is-transposed($node)) then
                let $corresp := //tei:metamark[matches(@target, $node/@xml:id)]
                return
                    (fontane-simple:transform($corresp/node()),
                    fontane-simple:transform($node/node()))
            else
                fontane-simple:transform($node/node())
                
        case element(tei:addSpan) return
            if($node/@type = "edited_text") then
Michelle Rodzis's avatar
Michelle Rodzis committed
130
                local:copy-element($node)
131
132
133
            else
                ()       
                
134
135
        case element(tei:fw) return
            ()
136
                
137
138
139
        case element(tei:line) return
            if(count($node/*) = 1 and $node/child::tei:fw) then
                ()
MRodz's avatar
MRodz committed
140
            else if($node/@type = "heading") then
Michelle Rodzis's avatar
Michelle Rodzis committed
141
                local:make-head($node)
142
143
144
145
            else if(not($node/@type = "item")) then
                fontane-simple:transform($node/node())
            else if($node/@type = "item" and not($node/@xml:id)) then
                element tei:item {
146
                    fontane-simple:transform($node/node())
147
148
149
                }
            else if($node/@type = "item" 
            and fontane-simple:is-transposed($node)) then
MRodz's avatar
MRodz committed
150
                ()            
151
152
            else
                fontane-simple:transform($node/node())
153
                        
154
        case element(tei:handShift) return
Michelle Rodzis's avatar
Michelle Rodzis committed
155
            (: tei:local:copy-element($node)ft without @new denote the duct of the current 
156
157
            writer or a change of writing medium :)
            if($node/@new and (fontane-simple:is-hand-contemporary($node/@new) or
MRodz's avatar
MRodz committed
158
159
160
            ($node/@new = "#Friedrich_Fontane" 
                and $node/ancestor::tei:surface[@type = "label"])) 
            or not($node/@new))
161
                then
Michelle Rodzis's avatar
Michelle Rodzis committed
162
                    local:copy-element($node)
163
164
                else
                    ()
MRodz's avatar
MRodz committed
165
166
167
168
169
170
                
(:                if($node/following-sibling::*[1][count(./*) = 1 :)
(:                    and child::tei:fw or child::tei:stamp]):)
(:                    then:)
(:                        ():)
(:                    else:)
Michelle Rodzis's avatar
Michelle Rodzis committed
171
(:                        element tei:local:copy-element($node)ft {:)
MRodz's avatar
MRodz committed
172
173
(:                            $node/@*:)
(:                        }:)
174
175
        case element(tei:stamp) return
            ()
176
                
177
178
179
180
181
        case element(tei:seg) return
            if(count($node/*) = 1 and 
            ($node/child::tei:stamp or $node/child::tei:metamark[@function = "caret"]))
                then
                    ()
MRodz's avatar
MRodz committed
182
            else if($node/@type = "heading") then
Michelle Rodzis's avatar
Michelle Rodzis committed
183
                local:make-head($node)
184
185
186
            else if(matches($node/@style, "underline") 
            and not(matches($node/@style, "vertical-align"))) then
                fontane-simple:transform($node/node())
187
            else if($node/@style and local:has-valid-text($node)) then
MRodz's avatar
MRodz committed
188
                element{QName("http://www.tei-c.org/ns/1.0", $node/name())}{
Michelle Rodzis's avatar
Michelle Rodzis committed
189
                    attribute rendition {fontane-simple:filter-rendition($node/@style)},
MRodz's avatar
MRodz committed
190
191
192
                    $node/(@* except @style),
                    fontane-simple:transform($node/node())
                }                   
193
            else if($node/@type = "initials" or $node/@type = "monogram" 
MRodz's avatar
MRodz committed
194
            or $node/@type = "multiphrase")
195
                then
Michelle Rodzis's avatar
Michelle Rodzis committed
196
                    local:copy-element($node)
197
198
            else if($node/@type = "auction_number" 
            or $node/@type = "cancel"
MRodz's avatar
MRodz committed
199
200
            or $node/@type = "abort"
            or $node/@function ="unknown")
201
202
203
204
                then
                    ()
            else
                fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
205
206
                    
        case element(tei:hi) return
Michelle Rodzis's avatar
Michelle Rodzis committed
207
            local:copy-element($node)
208
209
        
        case element(tei:anchor) return
Michelle Rodzis's avatar
Michelle Rodzis committed
210
            local:copy-element($node)
211
                        
212
        case element(tei:surface) return 
MRodz's avatar
MRodz committed
213
            if(matches($node/@n, "cover")) then
214
                local:make-pb-with-type($node/@n)
215
            else if(local:is-page($node)
MRodz's avatar
MRodz committed
216
217
            and $node/@type = "clipping") then
                (local:make-pb($node),
MRodz's avatar
MRodz committed
218
219
                (if(not($node/@subtype = "Kalenderblatt"
                or $node/@subtype = "Zeitungsausschnitt_Fragment")) then
220
                    fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
221
222
                else
                    ()))
223
            else if(local:is-page($node)) then
224
225
                (local:make-pb($node),
                fontane-simple:transform($node/node()))
226
            else if($node/@type = "label" and
227
228
229
230
            (contains($node/@subtype, "Fontane")
            or contains($node/@subtype, "Hersteller"))    
            ) then
                local:make-div($node)
MRodz's avatar
MRodz committed
231
232
            else
                ()
MRodz's avatar
MRodz committed
233
234
235
236
237
        
        case element(tei:milestone) return
            if($node/@unit = "illustration") then
                ()
            else
Michelle Rodzis's avatar
Michelle Rodzis committed
238
                local:copy-element($node)
239
240
                    
        case element(tei:gap) return
Michelle Rodzis's avatar
Michelle Rodzis committed
241
            local:copy-element($node)
242
                
243
244
245
246
247
248
        case element(tei:metamark) return
            if($node/@function = "integrate" 
            or $node/@function = "authorial_note") then
                element tei:ab {
                    $node/@*
                }
249
250
251
252
253
254
            else if($node/@function = "placeholder" 
            or $node/@function ="etc."
            or $node/@function ="caret"
            or $node/@function ="footnotes"
            or $node/@function ="ellipsis"
            or $node/@function = "paragraph") then
MRodz's avatar
MRodz committed
255
256
257
                element tei:ab {
                    attribute type {$node/@function},
                    fontane-simple:transform($node/node())
258
                }            
259
260
            else
                ()
261
262
263
264
265
        
        (: TODO: check if correct :)
        case element(tei:surplus) return
            ()
            
MRodz's avatar
MRodz committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
        case element(tei:zone) return
            if(matches($node/@style, "border-style:solid") 
            and not(matches($node/@style, "border-radius"))
            and not($node/@rend = "border-style:house")) then
                element tei:div {
                    attribute type {"frame"},
                    fontane-simple:transform($node/node())
                }
            else if(matches($node/@rend, "border-bottom-style:brace")) then
                (fontane-simple:transform($node/node()),
                element tei:ab {
                    attribute type {"bottom-brace"}
                })                    
            else if($node/@type = "cancel") then
                ()
            else if($node/@type = "marked_off") then
                element tei:seg {
                    $node/@type,
                    $node/@xml:id,
                    fontane-simple:transform($node/node())
                }
MRodz's avatar
MRodz committed
287
288
289
290
291
292
293
            else if($node/@type = "highlighted") then
                if($node/child::tei:zone[@type = "highlighted"]) then
                    fontane-simple:transform($node/node())
                else
                    element tei:hi {
                        attribute type {"vertical-mark"},
                        fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
                    }
            else if($node/@type = "illustration") then
                if(not($node//tei:figure/parent::tei:del)) then
                    element {QName("http://www.tei-c.org/ns/1.0", "ab")}{
                        (if($node/child::tei:zone[@type = "illustration"]) then
                            attribute type {"composed-sketch"}
                        else
                            (attribute type {"sketch"},
                            if($node/parent::tei:zone[@type = "illustration"]) then
                                attribute rendition {"margin-left:" || $node/@ulx 
                                || "cm; " || "margin-top:" || $node/@uly || "cm"}
                            else
                                ()
                            )
                        ),
                        fontane-simple:transform($node/node())
                    }
                else 
                    ()
            else if($node/parent::tei:zone/@type = "illustration") then
                element {QName("http://www.tei-c.org/ns/1.0", "seg")}{
                    attribute type {"caption"},
                    attribute rendition {"margin-left:" || $node/@ulx || "cm; " 
                        || "margin-top:" || $node/@uly || "cm"},
                    fontane-simple:transform($node/node())
                }
MRodz's avatar
MRodz committed
320
            else if($node/@type = "heading") then
Michelle Rodzis's avatar
Michelle Rodzis committed
321
                local:make-head($node)                 
MRodz's avatar
MRodz committed
322
323
            else if($node/@type = "list" or $node/@type = "item") then
                element {QName("http://www.tei-c.org/ns/1.0", $node/@type)}{
324
                    $node/(@* except (@type, @ulx, @uly, @lrx, @lry, @rotate)),
MRodz's avatar
MRodz committed
325
326
327
328
329
330
331
                    fontane-simple:transform($node/node())
                }            
            else if($node/@type = "legend") then
                element {QName("http://www.tei-c.org/ns/1.0", "div")}{
                    $node/@*,
                    fontane-simple:transform($node/node())
                }            
332
333
334
335
            else if(not($node/@xml:id)) then
                fontane-simple:transform($node/node())
            else if($node/@xml:id and fontane-simple:is-transposed($node)) then
                ()
MRodz's avatar
MRodz committed
336
337
            else
                fontane-simple:transform($node/node())
338
                
MRodz's avatar
MRodz committed
339
        case element(tei:figure) return
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
            if(count($node/child::*) = 1 and $node/child::tei:figDesc) then
                (: genealogy lines probably shouldn't be displayed, but I still have to 
                check that. in case they should be serialized, I leave the code :)
(:                if(matches($node/descendant::tei:ref, "Stammbaumverbindungslinie")) then:)
(:                    element tei:seg {:)
(:                        $node/@*,:)
(:                        fontane-simple:transform($node/node()):)
(:                    }:)
(:                else if(matches($node/descendant::tei:ref, "Schlusslinie")):)
                if(matches($node/descendant::tei:ref, "Schlusslinie"))
                    then
                        element tei:ab {
                            switch ($node/descendant::tei:ref)
                                case "horizontale einfache Schlusslinie" return 
                                    attribute type {"long-end-line"}                                    
                                case "Schlusslinie; horizontale Halbschleife von links oben nach rechts" return 
                                    attribute type {"long-end-line"}
                                case "horizontale einfache Schlusslinie (gewellt)" return 
                                    attribute type {"long-end-line-wavy"}
                                case "Schlusslinien; horizontale Schleife von links oben nach rechts unten" return 
                                    attribute type {"bottom-brace-short"}
                                default return 
                                    attribute type {"end-line"}  
                        }
                else if(matches($node/descendant::tei:ref, "Absatzlinie")
                (: in case of double paragraph lines the single lines are 
                encoded with "oberer" resp. "unterer Teil", but we only 
                serialize the encoding for the upper line :)
                and not(matches($node/descendant::tei:ref, "unterer Teil"))
                and not(matches($node/descendant::tei:figDesc, "unsicher"))) then
                    element tei:ab {
                        if(matches($node/descendant::tei:ref, "doppelt"))
                            then
                                attribute type {"short-paragraph-line-double"}
                            else
                                attribute type {"short-paragraph-line"}
                    }
MRodz's avatar
MRodz committed
377
378
379
380
                else if($node/parent::tei:zone[@type = "illustration"]) then
                    element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
                        $node/@*
                    }
381
                else
MRodz's avatar
MRodz committed
382
383
                    ()
            else
Michelle Rodzis's avatar
Michelle Rodzis committed
384
                local:copy-element($node)
385
                    
MRodz's avatar
MRodz committed
386
        case element(tei:note) return
387
388
            if($node/@type = "authorial" 
            and not($node/@subtype = "footnote")) then
MRodz's avatar
MRodz committed
389
390
                ()
            else 
Michelle Rodzis's avatar
Michelle Rodzis committed
391
                local:copy-element($node)
MRodz's avatar
MRodz committed
392
393
394
395
396
397
398
399
400
                
        case element(tei:certainty) return
            element {QName("http://www.tei-c.org/ns/1.0", "note")}{
                attribute type {"editorial"},
                attribute subtype {"certainty"},
                $node/@cert,
                $node/@target,
                fontane-simple:transform($node/node())
            }
MRodz's avatar
MRodz committed
401
                    
MRodz's avatar
MRodz committed
402
        case element(tei:figDesc) return
Michelle Rodzis's avatar
Michelle Rodzis committed
403
            local:copy-element($node)
404
            
MRodz's avatar
MRodz committed
405
        case element(tei:ref) return
Michelle Rodzis's avatar
Michelle Rodzis committed
406
            local:copy-element($node)
MRodz's avatar
MRodz committed
407
408
        
        case element(tei:space) return
Michelle Rodzis's avatar
Michelle Rodzis committed
409
            local:copy-element($node)
MRodz's avatar
MRodz committed
410
411
            
        case element(tei:choice) return
Michelle Rodzis's avatar
Michelle Rodzis committed
412
            local:copy-element($node)
MRodz's avatar
MRodz committed
413
414
            
        case element(tei:abbr) return
Michelle Rodzis's avatar
Michelle Rodzis committed
415
            local:copy-element($node)
MRodz's avatar
MRodz committed
416
417
            
        case element(tei:expan) return
Michelle Rodzis's avatar
Michelle Rodzis committed
418
            local:copy-element($node)        
MRodz's avatar
MRodz committed
419
420
            
        case element(tei:rs) return
Michelle Rodzis's avatar
Michelle Rodzis committed
421
            local:copy-element($node)        
MRodz's avatar
MRodz committed
422
423
            
        case element(tei:date) return
424
            element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
425
426
427
                (if($node/@when-iso) then
                    attribute when {$node/@when-iso}
                else 
428
429
430
431
432
                    ()), 
                (if($node/@from-iso) then
                    attribute from {$node/@from-iso}
                else 
                    ()),                    
433
434
435
436
                (if($node/@to-iso) then
                    attribute to {$node/@to-iso}
                else 
                    ()),
437
                $node/(@* except (@when-iso, @to-iso, @from-iso)),
438
439
                fontane-simple:transform($node/node())
            }
440
441
442
443
444
445
            
        case element(tei:ptr) return
            let $target-id := substring-after($node/@target, "#")
            let $target := $node/ancestor::tei:TEI//*[matches(@xml:id, $target-id)]
            let $stripped-target := fontane-simple:strip-element($target)
            return fontane-simple:transform($stripped-target)
MRodz's avatar
MRodz committed
446
            
MRodz's avatar
MRodz committed
447
        default return
448
            fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
449
450
451
};

(: TODO: adapt to general XML :)
Michelle Rodzis's avatar
Michelle Rodzis committed
452
453
454
455
456
457
458
459
460
461
462
(:~
 : Determines whether the passed tei:handShift is contemporary or not. For the 
 : edited text we only need to consider contemporary hands or additions of 
 : Friedrich Fontane if they occur on labels and on the backside of calendar 
 : pages.
 : 
 : TODO: check if we reach backside of calendar pages at all
 : @author Michelle Rodzis
 : @param $hand the tei:handShift/@new to be checked
 : @return xs:boolean
 : :)
463
declare function fontane-simple:is-hand-contemporary($hand as xs:string?) as xs:boolean {
MRodz's avatar
MRodz committed
464
465
466
467
468
469
470
471
472
    let $hand := replace($hand, "#", "")
    let $file := doc("/db/sade-projects/textgrid/data/xml/data/16b00.xml")
    let $handNote := $file//tei:handNote[@xml:id = $hand]
    return 
        if($handNote/@script = "contemporary")
            then
                true()
            else
                false()
473
474
};

Michelle Rodzis's avatar
Michelle Rodzis committed
475
476


477
478
479
480
481
482
483
484
485
486
487
488
489
490
declare function fontane-simple:is-transposed($node as node()) as xs:boolean {
    let $root := $node/ancestor::tei:TEI
    return
        if($root//tei:ptr[contains(@target, $node/@xml:id)]) then
            true()
        else
            false()
};

declare function fontane-simple:strip-element($node as node()) {
    element {$node/name()} {
        $node/@type,
        fontane-simple:transform($node/node())
    }
MRodz's avatar
MRodz committed
491
492
};

Michelle Rodzis's avatar
Michelle Rodzis committed
493
494
495
496
497
498
499
500
501
502
503
504

(:~
 : Performs a couple of processing steps on a text node:
 : 
 : 1.   for the edited text only hyphens that are marked with
 :      <tei:lb break="keepHyphen"/> should be displayed
 : 2.   round s (ſ) is normalized to s
 : 
 : @author Michelle Rodzis
 : @param $node the current text node
 : @return text() the formatted text
 : :)
505
declare function local:prepare-text($node as text()) as text()? {
Michelle Rodzis's avatar
Michelle Rodzis committed
506
507
    if(local:is-valid-text($node) 
    and not(normalize-space($node) = "")) then
508
509
510
511
512
513
514
515
        let $cleared-hyphen := 
            if(ends-with($node, "-") and not(local:keep-hyphen($node))) then
                text {functx:substring-before-last($node, "-")}
            else if(ends-with($node, "⸗") and not(local:keep-hyphen($node))) then
                text {functx:substring-before-last($node, "⸗")}
            else
                $node
        let $cleared-round-s := replace($cleared-hyphen, "ſ", "s")
Michelle Rodzis's avatar
Michelle Rodzis committed
516
(:        let $normalized := normalize-space($cleared-round-s):)
517
518
519
        return text {$cleared-round-s}
    else
        ()
MRodz's avatar
MRodz committed
520
521
};

Michelle Rodzis's avatar
Michelle Rodzis committed
522
523
524
525
526
527
528
529

(:~
 : Checks if a hyphen should be kept for the edited text or not. 
 : 
 : @author Michelle Rodzis
 : @param $node the current text node
 : @return xs:boolean
 : :)
MRodz's avatar
MRodz committed
530
531
532
533
534
535
536
537
declare function local:keep-hyphen($node as text()) as xs:boolean {
    if($node/parent::tei:line/following-sibling::*[1][self::tei:lb[@break = "keepHyphen"]])
        then
            true()
    else
        false()
};

Michelle Rodzis's avatar
Michelle Rodzis committed
538
539
540
541
542
543
544
545
(:~
 : Takes a given element over as is since it is compliant to TEI simplePrint.
 : 
 : @author Michelle Rodzis
 : @param $node the current text node
 : @return node() a copy of the current node
 : :)
declare function local:copy-element($node as node()) as node() {
MRodz's avatar
MRodz committed
546
547
548
549
    element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
        $node/@*,
        fontane-simple:transform($node/node())
    }
MRodz's avatar
MRodz committed
550
551
};

552

MRodz's avatar
MRodz committed
553
declare function local:has-valid-text($node as node()) as xs:boolean {
Michelle Rodzis's avatar
Michelle Rodzis committed
554
    let $text-nodes := $node/descendant::text()[not(normalize-space(.) = "")]
555
556
557
558
559
560
    let $results := 
        for $text-node in $text-nodes
            return 
                local:is-valid-text($text-node)
    return 
        if(functx:is-value-in-sequence(true(), $results)) then
Michelle Rodzis's avatar
Michelle Rodzis committed
561
            true()
562
563
564
565
566
567
568
569
570
        else
            false()
};


declare function local:is-valid-text($node as text()) as xs:boolean {
    let $current-hand := $node/preceding::tei:handShift[@new][1]/@new
    return
    if(fontane-simple:is-hand-contemporary($current-hand)
MRodz's avatar
MRodz committed
571
572
    or ($node[ancestor::tei:surface[@type = "label"]]
        and matches($current-hand, "Friedrich_Fontane"))
Michelle Rodzis's avatar
Michelle Rodzis committed
573
574
    or $node/ancestor::*[@type = "edited_text"]
    and not(normalize-space($node) = "")) then
575
576
577
        true()
    else 
        false()
MRodz's avatar
MRodz committed
578
579
};

Michelle Rodzis's avatar
Michelle Rodzis committed
580

581
582
583
584
declare function local:is-page($node as node()) as xs:boolean {
    matches($node/@n, "[0-9IVXMCD]{1,7}[rv]{1}")
};

Michelle Rodzis's avatar
Michelle Rodzis committed
585
586
587
588
589
590
591
(:~
 : Creates a tei:pb.
 : 
 : @author Michelle Rodzis
 : @param $node the current tei:surface node
 : @return element(tei:pb)
 : :)
MRodz's avatar
MRodz committed
592
593
594
595
declare function local:make-pb($node as node()) as element(tei:pb) {
    element {QName("http://www.tei-c.org/ns/1.0", "pb")}{
        $node/@n
    }
MRodz's avatar
MRodz committed
596
597
};

Michelle Rodzis's avatar
Michelle Rodzis committed
598

599
600
601
602
603
604
605
declare function local:make-pb-with-type($type as xs:string) as element(tei:pb) {
    element {QName("http://www.tei-c.org/ns/1.0", "pb")}{
        attribute type {$type}
    }
};


Michelle Rodzis's avatar
Michelle Rodzis committed
606
607
608
609
610
611
612
613
(:~
 : Creates a tei:head.
 : 
 : @author Michelle Rodzis
 : @param $node the current tei:line, tei:zone or tei:seg node
 : @return element(tei:head)
 : :)
declare function local:make-head($node as node()) as element(tei:head) {
MRodz's avatar
MRodz committed
614
615
    element tei:head {
        (if($node/descendant::tei:seg[matches(@style, "font-size")]) then
Michelle Rodzis's avatar
Michelle Rodzis committed
616
            attribute type {local:get-font-size($node)}
MRodz's avatar
MRodz committed
617
618
619
620
621
622
623
624
        else 
            attribute type {"default"}
        ),
        $node/@subtype,
        fontane-simple:transform($node/node())
    }
};

Michelle Rodzis's avatar
Michelle Rodzis committed
625
626
627
628
629
630
631
632

(:~
 : Creates a tei:div.
 : 
 : @author Michelle Rodzis
 : @param $node the current tei:surface node
 : @return element(tei:div)
 : :)
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
declare function local:make-div($node as node()) as element(tei:div)? {
    if(local:has-valid-text($node)) then
        element tei:div{
            $node/(@* except (@facs, @n, @attachment, @subtype, @ulx, @uly, @lrx, @lry, @points)),
            fontane-simple:transform($node/node())
        }
    else
        ()
};


declare function local:make-div-with-type($node as node(), $type as xs:string) 
as element(tei:div)? {
    if(local:has-valid-text($node)) then
        element tei:div{
            attribute type {$type},
            $node/(@* except (@facs, @n, @attachment, @subtype, @ulx, @uly, @lrx, @lry, @points)),
            fontane-simple:transform($node/node())
        }    
    else
        ()
654
655
};

Michelle Rodzis's avatar
Michelle Rodzis committed
656
657
658
659
660
661
662
663
664

(:~
 : Retrieves the font size from the @style of a tei:seg.
 : 
 : @author Michelle Rodzis
 : @param $node the current tei:line, tei:zone or tei:seg node with @type = heading
 : @return xs:string the font size value
 : :)
declare function local:get-font-size($node as node()) as xs:string {
MRodz's avatar
MRodz committed
665
666
667
    (: example for style: "font-size:large; letter-spacing:0.2cm; text-decoration:underline" :)
    let $tmp := substring-after($node/descendant-or-self::tei:seg[matches(@style, "font-size")]/@style, "font-size:")
    return substring-before($tmp, ";")
Michelle Rodzis's avatar
Michelle Rodzis committed
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
};


declare function fontane-simple:filter-rendition($seg-styles as xs:string) as xs:string* {
    let $styles := tokenize($seg-styles, " ")
    let $relevant-styles :=
        for $style in $styles
        return 
            if(matches($style, "font")
            or matches($style, "align")
            or matches($style, "spacing")
            or matches($style, "line-through")) then
                $style
            else
                ()
    return string-join($relevant-styles, " ")    
MRodz's avatar
MRodz committed
684
};