transform2teisimple.xqm 25.8 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
155
156
157
            if($node/@new) then
                if(fontane-simple:is-hand-valid($node)
                and not(fontane-simple:is-prev-valid-hand-same($node))) then
Michelle Rodzis's avatar
Michelle Rodzis committed
158
                    local:copy-element($node)
159
160
                else
                    ()
161
162
163
164
165
166
167
168
            else
                (: tei:local:copy-element($node)ft without @new denote the duct 
                of the current writer or a change of writing medium :)
                if(fontane-simple:is-hand-valid($node/preceding::tei:handShift[@new][1])) then
                    local:copy-element($node)
                else
                    ()

169
170
        case element(tei:stamp) return
            ()
171
                
172
173
174
175
176
        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
177
            else if($node/@type = "heading") then
Michelle Rodzis's avatar
Michelle Rodzis committed
178
                local:make-head($node)
179
180
181
            else if(matches($node/@style, "underline") 
            and not(matches($node/@style, "vertical-align"))) then
                fontane-simple:transform($node/node())
182
            else if($node/@style and local:has-valid-text($node)) then
MRodz's avatar
MRodz committed
183
                element{QName("http://www.tei-c.org/ns/1.0", $node/name())}{
Michelle Rodzis's avatar
Michelle Rodzis committed
184
                    attribute rendition {fontane-simple:filter-rendition($node/@style)},
MRodz's avatar
MRodz committed
185
186
187
                    $node/(@* except @style),
                    fontane-simple:transform($node/node())
                }                   
188
            else if($node/@type = "initials" or $node/@type = "monogram" 
MRodz's avatar
MRodz committed
189
            or $node/@type = "multiphrase")
190
                then
Michelle Rodzis's avatar
Michelle Rodzis committed
191
                    local:copy-element($node)
192
193
            else if($node/@type = "auction_number" 
            or $node/@type = "cancel"
MRodz's avatar
MRodz committed
194
195
            or $node/@type = "abort"
            or $node/@function ="unknown")
196
197
198
199
                then
                    ()
            else
                fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
200
201
                    
        case element(tei:hi) return
Michelle Rodzis's avatar
Michelle Rodzis committed
202
            local:copy-element($node)
203
204
        
        case element(tei:anchor) return
Michelle Rodzis's avatar
Michelle Rodzis committed
205
            local:copy-element($node)
206
                        
207
        case element(tei:surface) return 
MRodz's avatar
MRodz committed
208
            if(matches($node/@n, "cover")) then
209
                local:make-pb-with-type($node/@n)
210
            else if(local:is-page($node)
MRodz's avatar
MRodz committed
211
212
            and $node/@type = "clipping") then
                (local:make-pb($node),
MRodz's avatar
MRodz committed
213
214
                (if(not($node/@subtype = "Kalenderblatt"
                or $node/@subtype = "Zeitungsausschnitt_Fragment")) then
215
                    fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
216
217
                else
                    ()))
218
            else if(local:is-page($node)) then
219
220
                (local:make-pb($node),
                fontane-simple:transform($node/node()))
221
            else if($node/@type = "label" and
222
223
224
225
            (contains($node/@subtype, "Fontane")
            or contains($node/@subtype, "Hersteller"))    
            ) then
                local:make-div($node)
MRodz's avatar
MRodz committed
226
227
            else
                ()
MRodz's avatar
MRodz committed
228
229
230
231
232
        
        case element(tei:milestone) return
            if($node/@unit = "illustration") then
                ()
            else
Michelle Rodzis's avatar
Michelle Rodzis committed
233
                local:copy-element($node)
234
235
                    
        case element(tei:gap) return
Michelle Rodzis's avatar
Michelle Rodzis committed
236
            local:copy-element($node)
237
                
238
239
240
241
242
243
        case element(tei:metamark) return
            if($node/@function = "integrate" 
            or $node/@function = "authorial_note") then
                element tei:ab {
                    $node/@*
                }
244
245
246
247
248
249
            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
250
251
252
                element tei:ab {
                    attribute type {$node/@function},
                    fontane-simple:transform($node/node())
253
                }            
254
255
            else
                ()
256
257
258
259
260
        
        (: TODO: check if correct :)
        case element(tei:surplus) return
            ()
            
MRodz's avatar
MRodz committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
        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
282
283
284
285
286
287
288
            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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
                    }
            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
315
            else if($node/@type = "heading") then
Michelle Rodzis's avatar
Michelle Rodzis committed
316
                local:make-head($node)                 
MRodz's avatar
MRodz committed
317
318
            else if($node/@type = "list" or $node/@type = "item") then
                element {QName("http://www.tei-c.org/ns/1.0", $node/@type)}{
319
                    $node/(@* except (@type, @ulx, @uly, @lrx, @lry, @rotate)),
MRodz's avatar
MRodz committed
320
321
322
323
324
325
326
                    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())
                }            
327
328
329
330
            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
331
332
            else
                fontane-simple:transform($node/node())
333
                
MRodz's avatar
MRodz committed
334
        case element(tei:figure) return
335
336
337
338
339
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
            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
372
373
374
375
                else if($node/parent::tei:zone[@type = "illustration"]) then
                    element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
                        $node/@*
                    }
376
                else
MRodz's avatar
MRodz committed
377
378
                    ()
            else
Michelle Rodzis's avatar
Michelle Rodzis committed
379
                local:copy-element($node)
380
                    
MRodz's avatar
MRodz committed
381
        case element(tei:note) return
382
383
            if($node/@type = "authorial" 
            and not($node/@subtype = "footnote")) then
MRodz's avatar
MRodz committed
384
385
                ()
            else 
Michelle Rodzis's avatar
Michelle Rodzis committed
386
                local:copy-element($node)
MRodz's avatar
MRodz committed
387
388
389
390
391
392
393
394
395
                
        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
396
                    
MRodz's avatar
MRodz committed
397
        case element(tei:figDesc) return
Michelle Rodzis's avatar
Michelle Rodzis committed
398
            local:copy-element($node)
399
            
MRodz's avatar
MRodz committed
400
        case element(tei:ref) return
Michelle Rodzis's avatar
Michelle Rodzis committed
401
            local:copy-element($node)
MRodz's avatar
MRodz committed
402
403
        
        case element(tei:space) return
Michelle Rodzis's avatar
Michelle Rodzis committed
404
            local:copy-element($node)
MRodz's avatar
MRodz committed
405
406
            
        case element(tei:choice) return
Michelle Rodzis's avatar
Michelle Rodzis committed
407
            local:copy-element($node)
MRodz's avatar
MRodz committed
408
409
            
        case element(tei:abbr) return
Michelle Rodzis's avatar
Michelle Rodzis committed
410
            local:copy-element($node)
MRodz's avatar
MRodz committed
411
412
            
        case element(tei:expan) return
Michelle Rodzis's avatar
Michelle Rodzis committed
413
            local:copy-element($node)        
MRodz's avatar
MRodz committed
414
415
            
        case element(tei:rs) return
Michelle Rodzis's avatar
Michelle Rodzis committed
416
            local:copy-element($node)        
MRodz's avatar
MRodz committed
417
418
            
        case element(tei:date) return
419
            element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
420
421
422
                (if($node/@when-iso) then
                    attribute when {$node/@when-iso}
                else 
423
424
425
426
427
                    ()), 
                (if($node/@from-iso) then
                    attribute from {$node/@from-iso}
                else 
                    ()),                    
428
429
430
431
                (if($node/@to-iso) then
                    attribute to {$node/@to-iso}
                else 
                    ()),
432
                $node/(@* except (@when-iso, @to-iso, @from-iso)),
433
434
                fontane-simple:transform($node/node())
            }
435
436
437
438
439
440
            
        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
441
            
MRodz's avatar
MRodz committed
442
        default return
443
            fontane-simple:transform($node/node())
MRodz's avatar
MRodz committed
444
445
446
};

(: TODO: adapt to general XML :)
Michelle Rodzis's avatar
Michelle Rodzis committed
447
448
449
450
451
452
453
454
455
456
457
(:~
 : 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
 : :)
458
declare function fontane-simple:is-hand-contemporary($hand as xs:string?) as xs:boolean {
MRodz's avatar
MRodz committed
459
460
461
462
463
464
465
466
467
    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()
468
469
};

Michelle Rodzis's avatar
Michelle Rodzis committed
470
471


472
473
474
475
476
477
478
479
480
481
482
483
484
485
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
486
487
};

Michelle Rodzis's avatar
Michelle Rodzis committed
488
489
490
491
492
493
494
495
496
497
498
499

(:~
 : 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
 : :)
500
declare function local:prepare-text($node as text()) as text()? {
Michelle Rodzis's avatar
Michelle Rodzis committed
501
502
    if(local:is-valid-text($node) 
    and not(normalize-space($node) = "")) then
503
504
505
506
507
508
509
510
        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
511
(:        let $normalized := normalize-space($cleared-round-s):)
512
513
514
        return text {$cleared-round-s}
    else
        ()
MRodz's avatar
MRodz committed
515
516
};

Michelle Rodzis's avatar
Michelle Rodzis committed
517
518
519
520
521
522
523
524

(:~
 : 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
525
526
527
528
529
530
531
532
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
533
534
535
536
537
538
539
540
(:~
 : 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
541
542
543
544
    element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
        $node/@*,
        fontane-simple:transform($node/node())
    }
MRodz's avatar
MRodz committed
545
546
};

547

MRodz's avatar
MRodz committed
548
declare function local:has-valid-text($node as node()) as xs:boolean {
Michelle Rodzis's avatar
Michelle Rodzis committed
549
    let $text-nodes := $node/descendant::text()[not(normalize-space(.) = "")]
550
551
552
553
554
555
    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
556
            true()
557
558
559
560
561
562
563
564
565
        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
566
567
    or ($node[ancestor::tei:surface[@type = "label"]]
        and matches($current-hand, "Friedrich_Fontane"))
Michelle Rodzis's avatar
Michelle Rodzis committed
568
569
    or $node/ancestor::*[@type = "edited_text"]
    and not(normalize-space($node) = "")) then
570
571
572
        true()
    else 
        false()
MRodz's avatar
MRodz committed
573
574
};

Michelle Rodzis's avatar
Michelle Rodzis committed
575

576
577
578
579
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
580
581
582
583
584
585
586
(:~
 : Creates a tei:pb.
 : 
 : @author Michelle Rodzis
 : @param $node the current tei:surface node
 : @return element(tei:pb)
 : :)
MRodz's avatar
MRodz committed
587
588
589
590
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
591
592
};

Michelle Rodzis's avatar
Michelle Rodzis committed
593

594
595
596
597
598
599
600
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
601
602
603
604
605
606
607
608
(:~
 : 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
609
610
    element tei:head {
        (if($node/descendant::tei:seg[matches(@style, "font-size")]) then
Michelle Rodzis's avatar
Michelle Rodzis committed
611
            attribute type {local:get-font-size($node)}
MRodz's avatar
MRodz committed
612
613
614
615
616
617
618
619
        else 
            attribute type {"default"}
        ),
        $node/@subtype,
        fontane-simple:transform($node/node())
    }
};

Michelle Rodzis's avatar
Michelle Rodzis committed
620
621
622
623
624
625
626
627

(:~
 : Creates a tei:div.
 : 
 : @author Michelle Rodzis
 : @param $node the current tei:surface node
 : @return element(tei:div)
 : :)
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
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
        ()
649
650
};

Michelle Rodzis's avatar
Michelle Rodzis committed
651
652
653
654
655
656
657
658
659

(:~
 : 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
660
661
662
    (: 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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
};


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, " ")    
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
};


declare function fontane-simple:is-hand-valid($node as element(tei:handShift)) 
as xs:boolean {
    if(fontane-simple:is-hand-contemporary($node/@new)
    or ($node/ancestor::tei:surface[@type = "label"]
        and matches($node/@new, "Friedrich_Fontane"))
    ) then
        true()
    else
        false()
};


declare function fontane-simple:is-prev-valid-hand-same($node as element(tei:handShift)) 
as xs:boolean {
    let $prev-valid := $node/preceding::tei:handShift[fontane-simple:is-hand-valid(.)][1]
    return 
        if($prev-valid
        and functx:sequence-deep-equal($node/@*, $prev-valid/@*)) then
            true()
        else
            false()
MRodz's avatar
MRodz committed
703
};