Commit 5c921a07 authored by MRodz's avatar MRodz Committed by mrodzis
Browse files

Take new approach to presorting (cf. #44)

parent bc6eef84
...@@ -6,100 +6,105 @@ declare namespace tei="http://www.tei-c.org/ns/1.0"; ...@@ -6,100 +6,105 @@ declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace test="http://exist-db.org/xquery/xqsuite"; declare namespace test="http://exist-db.org/xquery/xqsuite";
import module namespace console="http://exist-db.org/xquery/console"; import module namespace console="http://exist-db.org/xquery/console";
import module namespace functx="http://www.functx.com";
import module namespace simpleHelpers="http://fontane-nb.dariah.eu/teisimplehelpers" at "teisimplehelpers.xqm"; import module namespace simpleHelpers="http://fontane-nb.dariah.eu/teisimplehelpers" at "teisimplehelpers.xqm";
declare function presort:main($file as xs:string) as node()? { declare function presort:main($tei as node()*) as element(tei:text) {
let $doc := let $front := $tei//tei:front/node()
try { let $back := $tei//tei:back/node()
(doc("/db/sade-projects/textgrid/data/xml/data/" || $file)) let $body := $tei//tei:body/node()
} catch * {
(console:log("It was not possible to open the requested file " || $file))
}
let $front-covers := $doc//tei:sourceDoc/tei:surface[contains(@n, "front_cover")]
let $back-covers := $doc//tei:sourceDoc/tei:surface[contains(@n, "back_cover")]
let $content := $doc//tei:sourceDoc/tei:surface[not(contains(@n, "cover")
or matches(@n, "spine"))]
let $tei := <TEI xmlns="http://www.tei-c.org/ns/1.0"> let $tei :=
{$doc//tei:teiHeader} <text xmlns="http://www.tei-c.org/ns/1.0">
<text> <front>{presort:sort($front)}</front>
<front>{presort:sort($front-covers)}</front> <body>{presort:sort($body)}</body>
<body>{presort:sort($content)}</body> <back>{presort:sort($back)}</back>
<back>{presort:sort($back-covers)}</back>
</text> </text>
</TEI> let $store := xmldb:store("/db/apps/SADE/resources/xml/", "tei-simple-presort.xml", $tei)
let $store := xmldb:store("/db/apps/SADE/resources/xml/", "tei-simple-presort.xml", $tei)
return $tei return $tei
}; };
declare function presort:sort($nodes as node()*) as node()* { declare function presort:sort($nodes as node()*) as node()* {
for $node in $nodes for $node in $nodes
return return
typeswitch ($node) typeswitch ($node)
case text() return case text() return
$node $node
case comment() return case comment() return
() ()
case element(tei:rs) return
presort:exclude-copied($node)
default return default return
if($node/@prev and $node/@next) then if($node/@next and not($node/@prev)) then
presort:keep-node($node) (presort:apply-all-nexts($node))
else if($node/@prev) then else if($node/@prev or $node/@next) then
if(presort:should-prev-be-moved($node)) then let $prev-id := presort:get-prev-id($node)
() let $next-id := presort:get-next-id($node)
else let $prev-node := presort:find-corresp-node($node, $prev-id)
presort:keep-node($node) let $next-node := presort:find-corresp-node($node, $next-id)
else if($node/@next) then return
presort:copy-next-to-right-place($node) if($prev-node or $next-node) then
()
else
presort:keep-node($node)
else else
(console:log($node), presort:keep-node($node)) presort:keep-node($node)
};
declare function presort:should-prev-be-moved($node as node()*) as xs:boolean {
let $prev := replace($node/@prev, "#", "")
let $corresp-node := presort:find-corresp-node($node, $prev)
return
(: since we currently handle a node with @prev, the first node has to be
the corresponding node, i.e. the one with @next :)
if(presort:has-text-inbetween($corresp-node, $node)) then
true()
else
false()
}; };
declare function presort:keep-node($node as node()*) as node()* { declare function presort:keep-node($node as node()*) as node()* {
element {$node/name()} { if($node[self::text()]) then
namespace tei {"http://www.tei-c.org/ns/1.0"}, util:log-system-out("text")
else
(),
element {QName("http://www.tei-c.org/ns/1.0", $node/name())} {
$node/@*, $node/@*,
(: $node/(@* except (@prev, @next)),:) (: attrs only visible for debugging :) (: $node/(@* except (@prev, @next)), (: attrs only visible for debugging :):)
presort:sort($node/node()) presort:sort($node/node())
} }
}; };
declare function presort:copy-next-to-right-place($node as node()*) as node()* {
let $next := replace($node/@next, "#", "") declare function presort:apply-all-nexts($node as node()*) {
let $corresp-node := presort:find-corresp-node($node, $next) (: entry point :)
return if($node/@next and not($node/@prev)) then
if(not(presort:has-text-inbetween($node, $corresp-node))) then let $next-id := presort:get-next-id($node)
presort:keep-node($node) let $next-node := presort:find-corresp-node($node, $next-id)
else return
(presort:keep-node($node), presort:keep-node($corresp-node)) (presort:keep-node($node),
(: (functx:remove-attributes($node, "next"),:)
presort:apply-all-nexts($next-node))
(: exit point :)
else if(not($node/@next)) then
(: functx:remove-attributes($node, "prev"):)
presort:keep-node($node)
else
let $next-id := presort:get-next-id($node)
let $next-node := presort:find-corresp-node($node, $next-id)
return
(: (functx:remove-attributes($node, ("next", "prev")),:)
(presort:keep-node($node),
presort:apply-all-nexts($next-node))
};
declare function presort:get-next-id($node as node()) {
$node/@next => replace("#", "")
};
declare function presort:get-prev-id($node as node()) {
$node/@prev => replace("#", "")
}; };
declare function presort:find-corresp-node($node as node()*, $id as xs:string) { declare function presort:find-corresp-node($node as node()*, $id as xs:string) {
$node/ancestor::*[last()]//*[@xml:id = $id] $node/ancestor::*[last()]//*[@xml:id = $id]
}; };
declare function presort:has-text-inbetween($first-node as node()*, $second-node as declare function presort:exclude-copied($node as node()) {
node()*) { element {QName("http://www.tei-c.org/ns/1.0", $node/name())} {
if($first-node/following::text()[. << $second-node] $node/@*,
(: \w doesn't match all Unicode characters that might appear therefore we $node/node()[not(@prev or @next)]
test if the next node contains more than whitespaces :) }
[not(normalize-space(.) = "")]) then
true()
else
false()
}; };
\ No newline at end of file
...@@ -594,6 +594,7 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* { ...@@ -594,6 +594,7 @@ declare function fontaneSimple:transform($nodes as node()*) as node()* {
$node/@*, $node/@*,
(: TODO: generalize :) (: TODO: generalize :)
attribute href {fontaneSimple:make-link-to-sketch("16b00", $node/@xml:id)}, attribute href {fontaneSimple:make-link-to-sketch("16b00", $node/@xml:id)},
attribute height-in-mm {fontaneSimple:get-height-in-mm("16b00", $node/@xml:id)},
fontaneSimple:transform($nodes/node()) fontaneSimple:transform($nodes/node())
} }
else else
...@@ -950,6 +951,32 @@ $xml-id as xs:string) as xs:string { ...@@ -950,6 +951,32 @@ $xml-id as xs:string) as xs:string {
let $coordinates := replace($coordinates, "%", "") let $coordinates := replace($coordinates, "%", "")
let $img-uri := $shape-info/preceding-sibling::svg:image/@xlink:href let $img-uri := $shape-info/preceding-sibling::svg:image/@xlink:href
let $url := "https://textgridlab.org/1.0/digilib/rest/IIIF/" || $img-uri || let $url := "https://textgridlab.org/1.0/digilib/rest/IIIF/" || $img-uri ||
"/pct:" || $coordinates || "/,1000/0/default.jpg" "/pct:" || $coordinates || "/1000,/0/default.jpg"
return $url return $url
};
(:~
: @param $current-uri The URI of the current notebook, e.g. '16b00' :)
declare function fontaneSimple:get-height-in-mm($current-uri as xs:string,
$xml-id as xs:string) as xs:string {
let $tble-coll := collection("/db/sade-projects/textgrid/data/xml/tile/")
let $full-uri := "textgrid:" || $current-uri
let $file := for $res in $tble-coll return
(: REGEX is necessary to be independent from revisions :)
if($res//tei:link[matches(@targets, $full-uri || "\.[0-9]+#" || $xml-id)
and matches(@targets, "shape")]) then
$res
else
()
let $shape := $file//tei:link[matches(@targets, $full-uri || "\.[0-9]+#" || $xml-id)
and matches(@targets, "shape")]/@targets
=> substring-before(" ")
=> replace("#", "")
let $shape-info := $file//svg:*[@id = $shape]
let $height-percent := xs:double($shape-info/@height => replace("%", ""))
let $width-percent := xs:double($shape-info/@width => replace("%", ""))
let $ratio := $height-percent div $width-percent
(: print area has a width of 113mm :)
return xs:string(113 * $ratio)
}; };
\ No newline at end of file
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