Commit d5122ebb authored by MRodz's avatar MRodz Committed by mrodzis
Browse files

Restructure helper module to reduce complexity

parent 8e2eb2d6
......@@ -13,7 +13,6 @@ xquery version "3.1";
module namespace fontaneSimple="http://fontane-nb.dariah.eu/teisimple";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace test="http://exist-db.org/xquery/xqsuite";
......@@ -81,13 +80,13 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
if($node[@break = "keepHyphen"]) then
()
else
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
case element(tei:g) return
if($node[@ref = "#vds"]) then
()
else if($node/@ref ="#rth" or $node/@ref ="#hb") then
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
else
fontaneSimple:transform($node/node())
......@@ -118,7 +117,7 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
case element(tei:add) return
if($node/@type = "edited_text"
or $node/child::tei:seg[@type = "multiphrase"]) then
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
else if($node/@cause ="catchword" or $node/@cause ="unclear") then
()
else if($node/@rend ="|") then
......@@ -135,7 +134,7 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
case element(tei:addSpan) return
if($node/@type = "edited_text") then
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
else
()
......@@ -146,12 +145,12 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
if(count($node/*) = 1 and $node/child::tei:fw) then
()
else if($node/@type = "heading") then
simpleHelpers:make-head($node)
fontaneSimple:make-head($node)
else if(simpleHelpers:has-valid-style($node)
or matches($node/@rendition, "black_letter")
or matches($node/@rendition, "roman")) then
simpleHelpers:make-seg-with-rendition($node)
fontaneSimple:make-seg-with-rendition($node)
else if(not($node/@type = "item")) then
fontaneSimple:transform($node/node())
......@@ -166,7 +165,7 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
fontaneSimple:transform($node/node())
case element(tei:handShift) return
simpleHelpers:enhance-handshift($node)
fontaneSimple:enhance-handshift($node)
case element(tei:stamp) return
()
......@@ -178,7 +177,7 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
()
else if($node/@type = "heading") then
simpleHelpers:make-head($node)
fontaneSimple:make-head($node)
else if(matches($node/@style, "underline")
and not(matches($node/@style, "vertical-align"))) then
......@@ -187,14 +186,14 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
else if(simpleHelpers:has-valid-style($node)
or matches($node/@rendition, "black_letter")
or matches($node/@rendition, "roman")) then
simpleHelpers:make-seg-with-rendition($node)
fontaneSimple:make-seg-with-rendition($node)
else if($node/@type = "initials"
or $node/@type = "monogram"
or $node/@type = "multiphrase"
or $node/@xml:lang)
then
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
else if($node/@type = "auction_number"
or $node/@type = "cancel"
......@@ -207,7 +206,7 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
fontaneSimple:transform($node/node())
case element(tei:hi) return
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
(: TODO if $node/@type = "highlighted" then make
a hi[@type = "vertical-mark"] in the second stage of creating the
......@@ -215,32 +214,32 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
case element(tei:mod) return
if($node/@type = "highlighted"
and simpleHelpers:is-hand-contemporary($node/@hand)) then
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
else
fontaneSimple:transform($node/node())
case element(tei:anchor) return
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
case element(tei:surface) return
if(matches($node/@n, "cover")) then
simpleHelpers:make-pb-with-type($node/@n)
fontaneSimple:make-pb-with-type($node/@n)
else if(simpleHelpers:is-page($node)
and $node/@type = "clipping") then
(simpleHelpers:make-pb($node),
(fontaneSimple:make-pb($node),
(if(not($node/@subtype = "Kalenderblatt"
or $node/@subtype = "Zeitungsausschnitt_Fragment")) then
fontaneSimple:transform($node/node())
else
()))
else if(simpleHelpers:is-page($node)) then
(simpleHelpers:make-pb($node),
(fontaneSimple:make-pb($node),
fontaneSimple:transform($node/node()))
else if($node/@type = "label" and
(contains($node/@subtype, "Fontane")
or contains($node/@subtype, "Hersteller"))
) then
simpleHelpers:make-div($node)
fontaneSimple:make-div($node)
else
()
......@@ -252,10 +251,10 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
(: else if($node/@unit = "pararaph") then:)
(: fontaneSimple:make-paragraph($node):)
else
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
case element(tei:gap) return
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
case element(tei:metamark) return
if($node/@function = "integrate"
......@@ -344,7 +343,7 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
}
else if($node/@type = "heading") then
simpleHelpers:make-head($node)
fontaneSimple:make-head($node)
else if($node/@type = "list" or $node/@type = "item") then
element {QName("http://www.tei-c.org/ns/1.0", $node/@type)}{
......@@ -366,7 +365,7 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
else if(simpleHelpers:has-valid-style($node)
or matches($node/@rendition, "black_letter")
or matches($node/@rendition, "roman")) then
simpleHelpers:make-seg-with-rendition($node)
fontaneSimple:make-seg-with-rendition($node)
else if(not($node/@xml:id)) then
fontaneSimple:transform($node/node())
......@@ -423,14 +422,14 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
else
()
else
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
case element(tei:note) return
if($node/@type = "authorial"
and not($node/@subtype = "footnote")) then
()
else
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
case element(tei:certainty) return
element {QName("http://www.tei-c.org/ns/1.0", "note")}{
......@@ -442,25 +441,25 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
}
case element(tei:figDesc) return
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
case element(tei:ref) return
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
case element(tei:space) return
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
case element(tei:choice) return
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
case element(tei:abbr) return
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
case element(tei:expan) return
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
case element(tei:rs) return
simpleHelpers:copy-element($node)
fontaneSimple:copy-element($node)
case element(tei:date) return
element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
......@@ -475,7 +474,7 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
(if($node/@to-iso) then
attribute to {$node/@to-iso}
else
()),
()),
(if($node/@notAfter-iso) then
attribute notAfter {$node/@notAfter-iso}
else
......@@ -490,3 +489,124 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
default return
fontaneSimple:transform($node/node())
};
(:~
: 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 fontaneSimple:copy-element($node as node()) as node() {
element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
$node/@*,
fontaneSimple:transform($node/node())
}
};
(:~
: Creates a tei:head with an @rendition and a font-size, if available.
:
: @author Michelle Rodzis
: @param $node the current tei:line, tei:zone or tei:seg node
: @return element(tei:head)
: :)
declare function fontaneSimple:make-head($node as node()) as element(tei:head) {
element tei:head {
(if($node/descendant::tei:seg[matches(@style, "font-size")]) then
attribute type {simpleHelpers:get-font-size($node)}
else
attribute type {"default"}
),
$node/@subtype,
(if($node/@rendition) then
$node/@rendition
else
()),
fontaneSimple:transform($node/node())
}
};
(:~
: Creates a tei:div.
:
: @author Michelle Rodzis
: @param $node the current tei:surface node
: @return element(tei:div)
: :)
declare function fontaneSimple:make-div($node as element(tei:surface))
as element(tei:div) {
element tei:div{
$node/(@* except (@facs, @n, @attachment, @subtype, @ulx, @uly, @lrx, @lry, @points)),
fontaneSimple:transform($node/node())
}
};
(:~
: Creates a new tei:seg with all relevant rendition information.
:
: @author Michelle Rodzis
: @param $node the current tei:zone, tei:line, or tei:seg element
: @return element(tei:seg)
:)
declare function fontaneSimple:make-seg-with-rendition($node as element(*))
as element(tei:seg) {
element tei:seg {
attribute rendition {simpleHelpers:filter-rendition($node)},
$node/(@* except (@style, @rendition)),
fontaneSimple:transform($node/node())
}
};
(:~
: Creates a tei:pb.
:
: @author Michelle Rodzis
: @param $node the current tei:surface node
: @return element(tei:pb)
:)
declare function fontaneSimple:make-pb($node as element(tei:surface))
as element(tei:pb) {
element {QName("http://www.tei-c.org/ns/1.0", "pb")}{
$node/@n
}
};
(:~
: Creates a tei:pb with a type instead of a @n representing the pagination.
: This function is mainly used to separate the inner and outer book covers from
: each other.
:
: @author Michelle Rodzis
: @param $node the current tei:surface node
: @return element(tei:pb)
:)
declare function fontaneSimple: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}
}
};
declare function fontaneSimple:enhance-handshift($node as element(tei:handShift))
as element(tei:handShift) {
let $prev-hand := $node/preceding::tei:handShift[@new][1]
return
element tei:handShift {
$node/@*,
if(not($node/@new)) then
attribute new {$prev-hand/@new}
else
(),
if(not($node/@script)) then
attribute script {""}
else
(),
if(not($node/@medium)) then
attribute medium {""}
else
()
}
};
......@@ -13,7 +13,6 @@ module namespace simpleHelpers="http://fontane-nb.dariah.eu/teisimplehelpers";
declare namespace tei="http://www.tei-c.org/ns/1.0";
import module namespace console="http://exist-db.org/xquery/console";
import module namespace fontaneSimple="http://fontane-nb.dariah.eu/teisimple" at "tei2teisimple.xqm";
import module namespace functx = "http://www.functx.com";
import module namespace tidySimple ="http://fontane-nb.dariah.eu/tidysimple" at "tidysimple.xqm";
......@@ -115,28 +114,10 @@ declare function simpleHelpers:keep-hyphen($node as text()) as xs:boolean {
false()
};
(:~
: 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 simpleHelpers:copy-element($node as node()) as node() {
element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
$node/@*,
fontaneSimple:transform($node/node())
}
};
declare function simpleHelpers:copy-second-stage-element($node as node())
as node() {
element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
$node/@*,
tidySimple:tidy($node/node())
}
};
(:~
......@@ -227,44 +208,6 @@ as element(tei:pb) {
}
};
(:~
: Creates a tei:head with an @rendition and a font-size, if available.
:
: @author Michelle Rodzis
: @param $node the current tei:line, tei:zone or tei:seg node
: @return element(tei:head)
: :)
declare function simpleHelpers:make-head($node as node()) as element(tei:head) {
element tei:head {
(if($node/descendant::tei:seg[matches(@style, "font-size")]) then
attribute type {simpleHelpers:get-font-size($node)}
else
attribute type {"default"}
),
$node/@subtype,
(if($node/@rendition) then
$node/@rendition
else
()),
fontaneSimple:transform($node/node())
}
};
(:~
: Creates a tei:div.
:
: @author Michelle Rodzis
: @param $node the current tei:surface node
: @return element(tei:div)
: :)
declare function simpleHelpers:make-div($node as element(tei:surface))
as element(tei:div) {
element tei:div{
$node/(@* except (@facs, @n, @attachment, @subtype, @ulx, @uly, @lrx, @lry, @points)),
fontaneSimple:transform($node/node())
}
};
(:~
: Retrieves the font size from the @style of a tei:seg.
:
......@@ -359,7 +302,7 @@ declare function simpleHelpers:filter-rendition($node as node()) as xs:string* {
: @param $node the current tei:handShift element
: @return xs:boolean
:)
declare function simpleHelpers:is-hand-valid($hands as xs:string*,
declare function simpleHelpers:is-hand-valid($hands as xs:string*,
$node as element(tei:handShift)) as xs:boolean {
let $current-hand := replace($node/@new, "#", "")
return
......@@ -374,12 +317,12 @@ $node as element(tei:handShift)) as xs:boolean {
declare function simpleHelpers:belongs-to-valid-hand($hands as xs:string*,
declare function simpleHelpers:belongs-to-valid-hand($hands as xs:string*,
$node as element(*)*) as xs:boolean {
(: in some cases elements like tei:front or tei:body don't have a preceeding
tei:handShift because the initial pages are empty. in these cases we want to
preserve the element and therefore set a valid tei:handShift :)
let $prev-hand :=
let $prev-hand :=
if($node/preceding::tei:handShift[1]) then
$node/preceding::tei:handShift[1]
else
......@@ -394,7 +337,7 @@ $node as element(*)*) as xs:boolean {
(:~
: Checks if the previous handShift is the same as the current handShift. Two
: Checks if the previous handShift is the same as the current handShift. Two
: handShifts are the same if they have the same attributes.
:
: @author Michelle Rodzis
......@@ -417,9 +360,9 @@ as xs:boolean {
};
declare function simpleHelpers:is-prev-valid-hand-same($hands as xs:string,
declare function simpleHelpers:is-prev-valid-hand-same($hands as xs:string,
$node as element(tei:handShift)) as xs:boolean {
let $prev-hand := $node/preceding::tei:handShift[simpleHelpers:is-hand-valid($hands, $node)][1]
let $prev-hand := $node/preceding::tei:handShift[simpleHelpers:is-hand-contemporary(.)][1]
return
(: since we can't take the order of the attributes for granted we can't
use functx:sequence-deep-equal :)
......@@ -443,21 +386,7 @@ as element(tei:section) {
<div/>
};:)
(:~
: Creates a new tei:seg with all relevant rendition information.
:
: @author Michelle Rodzis
: @param $node the current tei:zone, tei:line, or tei:seg element
: @return element(tei:seg)
:)
declare function simpleHelpers:make-seg-with-rendition($node as element(*))
as element(tei:seg) {
element tei:seg {
attribute rendition {simpleHelpers:filter-rendition($node)},
$node/(@* except (@style, @rendition)),
fontaneSimple:transform($node/node())
}
};
(:~
: Returns all nodes between a tei:milestone and its corresponding tei:anchor
......@@ -475,7 +404,7 @@ declare function simpleHelpers:get-xml-chunk($node as node()) as node()* {
};
declare function simpleHelpers:enhance-handshift($node as element(tei:handShift))
declare function simpleHelpers:enhance-handshift($node as element(tei:handShift))
as element(tei:handShift) {
let $prev-hand := $node/preceding::tei:handShift[@new][1]
return
......@@ -484,7 +413,7 @@ as element(tei:handShift) {
if(not($node/@new)) then
attribute new {$prev-hand/@new}
else
(),
(),
if(not($node/@script)) then
attribute script {""}
else
......@@ -497,13 +426,13 @@ as element(tei:handShift) {
};
declare function simpleHelpers:clear-handshift($node as element(tei:handShift))
declare function simpleHelpers:clear-handshift($node as element(tei:handShift))
as element(tei:handShift) {
element tei:handShift {
if($node/@new = "") then
()
else
$node/@new,
$node/@new,
if($node/@script = "") then
()
else
......@@ -513,4 +442,4 @@ as element(tei:handShift) {
else
$node/@medium
}
};
\ No newline at end of file
};
......@@ -19,18 +19,16 @@ declare namespace test="http://exist-db.org/xquery/xqsuite";
import module namespace config="http://textgrid.de/ns/SADE/config" at "../../config/config.xqm";
import module namespace console="http://exist-db.org/xquery/console";
import module namespace fontaneSimple="http://fontane-nb.dariah.eu/teisimple" at "tei2teisimple.xqm";
import module namespace functx = "http://www.functx.com";
import module namespace simpleHelpers="http://fontane-nb.dariah.eu/teisimplehelpers" at "teisimplehelpers.xqm";
declare variable $tidySimple:valid-hands :=
declare variable $tidySimple:valid-hands :=
for $res in collection("/db/sade-projects/textgrid/data/xml/data")
return
$res//tei:handNote[@script = "contemporary"]/@xml:id/string();
declare function tidySimple:main() {
declare function tidySimple:main() as xs:string* {
let $doc :=
try {
(doc($config:app-root || "/resources/xml/tei-simple-tmp.xml"))
......@@ -40,12 +38,12 @@ declare function tidySimple:main() {
let $text := $doc//tei:text
let $tidied-text := tidySimple:tidy($text)
let $final-tei:=
let $final-tei:=
<TEI xmlns="http://www.tei-c.org/ns/1.0">
{$doc//tei:teiHeader}
{$tidied-text}
</TEI>
return xmldb:store("/db/apps/SADE/resources/xml/", "tei-simple.xml", $final-tei)
};
......@@ -57,13 +55,41 @@ declare function tidySimple:tidy($nodes as node()*) as node()* {
$node
case element(tei:handShift) return
if(simpleHelpers:is-hand-valid($tidySimple:valid-hands, $node)
and not(simpleHelpers:is-prev-hand-same($node))) then
simpleHelpers:clear-handshift($node)
and not(simpleHelpers:is-prev-valid-hand-same($tidySimple:valid-hands, $node))) then
tidySimple:clear-handshift($node)
else
()
default return
if(simpleHelpers:belongs-to-valid-hand($tidySimple:valid-hands, $node)) then
simpleHelpers:copy-second-stage-element($node)
tidySimple:copy-second-stage-element($node)
else
()
};
\ No newline at end of file
};
declare function tidySimple:copy-second-stage-element($node as node())
as node() {
element {QName("http://www.tei-c.org/ns/1.0", $node/name())}{
$node/@*,
tidySimple:tidy($node/node())
}
};
declare function tidySimple:clear-handshift($node as element(tei:handShift))
as element(tei:handShift) {
element tei:handShift {
if($node/@new = "") then
()
else
$node/@new,
if($node/@script = "") then
()
else
$node/@script,
if($node/@medium = "") then
()
else
$node/@medium
}
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment