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

Finalize first draft for presorting

parent 15b872a5
......@@ -40,26 +40,66 @@ declare function presort:sort($nodes as node()*) as node()* {
typeswitch ($node)
case text() return
$node
case comment() return
()
default return
if($node/@prev and $node/@next) then
presort:keep-node($node)
else if($node/@prev) then
()
if(presort:should-prev-be-moved($node)) then
()
else
presort:keep-node($node)
else if($node/@next) then
()
presort:copy-next-to-right-place($node)
else
presort:keep-node($node)
(console:log($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()* {
element {$node/name()} {
namespace tei {"http://www.tei-c.org/ns/1.0"},
$node/@*,
(: $node/(@* except (@prev, @next)),:) (: attrs only visible for debugging :)
presort:sort($node/node())
}
};
declare function presort:copy-next-to-right-place($node as node()*) {
()
declare function presort:copy-next-to-right-place($node as node()*) as node()* {
let $next := replace($node/@next, "#", "")
let $corresp-node := presort:find-corresp-node($node, $next)
return
if(not(presort:has-text-inbetween($node, $corresp-node))) then
presort:keep-node($node)
else
(presort:keep-node($node), presort:keep-node($corresp-node))
};
declare function presort:find-corresp-node($node as node()*, $id as xs:string) {
$node/ancestor::*[last()]//*[@xml:id = $id]
};
declare function presort:has-text-inbetween($first-node as node()*, $second-node as
node()*) {
if($first-node/following::text()[. << $second-node]
(: \w doesn't match all Unicode characters that might appear therefore we
test if the next node contains more than whitespaces :)
[not(normalize-space(.) = "")]) then
true()
else
false()
};
\ No newline at end of file
......@@ -38,30 +38,39 @@ declare
presort:sort($node)
};
(: @next nodes :)
declare
%test:name("Establishing right order with @next nodes")
%test:args("<handShift xmlns:tei=""http://www.tei-c.org/ns/1.0"" next=""#some-test-id""/>")
%test:assertEmpty
(: case 1: two elements referencing each other also stand right next to each
other, i.e. there's no text inbetween. then nothing should happen :)
%test:args("<text xmlns:tei=""http://www.tei-c.org/ns/1.0""><zone xmlns:tei=""http://www.tei-c.org/ns/1.0"" type=""item""><line>in das <rs next=""#C07_1r_b"" ref=""plc:Kloster_EF"" xml:id=""C07_1r_a"" type=""direct"">Auguſtinerkloſter</rs></line><line><rs ref=""plc:Kloster_EF"" prev=""#C07_1r_a"" xml:id=""C07_1r_b"" type=""direct"">zu Erfurt</rs></line></zone></text>")
%test:assertEquals("<text xmlns:tei=""http://www.tei-c.org/ns/1.0""><zone xmlns:tei=""http://www.tei-c.org/ns/1.0"" type=""item""><line>in das <rs next=""#C07_1r_b"" ref=""plc:Kloster_EF"" xml:id=""C07_1r_a"" type=""direct"">Auguſtinerkloſter</rs></line><line><rs ref=""plc:Kloster_EF"" prev=""#C07_1r_a"" xml:id=""C07_1r_b"" type=""direct"">zu Erfurt</rs></line></zone></text>")
(: case 2: two elements referencing each other are seperated by an arbitrary
amout of text. then they should be sorted into the right order :)
%test:args("<text xmlns:tei=""http://www.tei-c.org/ns/1.0""><zone><line next=""#C07_1r_13"" xml:id=""C07_1r_q"">2. Luther, <hi>Eisleben</hi><date when-iso=""1483"" type=""asynchronous"">1483</date></line><line>Some Text</line></zone><zone prev=""#C07_1r_q"" type=""item"" xml:id=""C07_1r_13""><handShift medium=""brown_ink thin_pen""/><line>Ein Stück ſeines Mantels und ſei</line></zone></text>")
%test:assertEquals("<text xmlns:tei=""http://www.tei-c.org/ns/1.0""><zone><line next=""#C07_1r_13"" xml:id=""C07_1r_q"">2. Luther, <hi>Eisleben</hi><date when-iso=""1483"" type=""asynchronous"">1483</date></line><zone prev=""#C07_1r_q"" type=""item"" xml:id=""C07_1r_13""><handShift medium=""brown_ink thin_pen""/><line>Ein Stück ſeines Mantels und ſei</line></zone><line>Some Text</line></zone></text>")
function presort-test:next-nodes($node as element(*)) {
presort:sort($node)
};
(: @prev nodes :)
declare
%test:name("Omit @prev nodes")
%test:args("<handShift xmlns:tei=""http://www.tei-c.org/ns/1.0"" medium=""pencil"" script=""Latf""/>")
%test:assertEquals("<handShift xmlns:tei=""http://www.tei-c.org/ns/1.0"" medium=""pencil"" script=""Latf""/>")
%test:args("<seg xmlns:tei=""http://www.tei-c.org/ns/1.0"" prev=""#some_id""><stamp xmlns:tei=""http://www.tei-c.org/ns/1.0"">STAATSBIBLIOTHEK •BERLIN•</stamp></seg>")
%test:assertEmpty
%test:args("<text xmlns:tei=""http://www.tei-c.org/ns/1.0""><seg xml:id=""some_id"" next=""#id""/><seg xml:id=""id"" prev=""#some_id""><stamp xmlns:tei=""http://www.tei-c.org/ns/1.0"">STAATSBIBLIOTHEK •BERLIN•</stamp></seg></text>")
%test:assertEquals("<text xmlns:tei=""http://www.tei-c.org/ns/1.0""><seg xml:id=""some_id"" next=""#id""/><seg xml:id=""id"" prev=""#some_id""><stamp xmlns:tei=""http://www.tei-c.org/ns/1.0"">STAATSBIBLIOTHEK •BERLIN•</stamp></seg></text>")
%test:args("<seg xmlns:tei=""http://www.tei-c.org/ns/1.0"" prev=""#some_id"" next=""#some_other_id""><stamp xmlns:tei=""http://www.tei-c.org/ns/1.0"">STAATSBIBLIOTHEK •BERLIN•</stamp></seg>")
%test:assertEquals("<seg xmlns:tei=""http://www.tei-c.org/ns/1.0"" prev=""#some_id"" next=""#some_other_id""><stamp xmlns:tei=""http://www.tei-c.org/ns/1.0"">STAATSBIBLIOTHEK •BERLIN•</stamp></seg>")
%test:args("<seg xmlns:tei=""http://www.tei-c.org/ns/1.0"" prev=""#some_id"" next=""#some_other_id""><stamp prev=""#id"" xmlns:tei=""http://www.tei-c.org/ns/1.0"">STAATSBIBLIOTHEK •BERLIN•</stamp></seg>")
%test:assertEquals("<seg xmlns:tei=""http://www.tei-c.org/ns/1.0"" prev=""#some_id"" next=""#some_other_id""/>")
%test:args("<text xmlns:tei=""http://www.tei-c.org/ns/1.0""><seg xml:id=""some_id"" next=""#id""/><seg xmlns:tei=""http://www.tei-c.org/ns/1.0"" xml:id=""id"" prev=""#some_id"" next=""#some_other_id""><stamp prev=""#id"" xmlns:tei=""http://www.tei-c.org/ns/1.0"">STAATSBIBLIOTHEK •BERLIN•</stamp></seg><seg xml:id=""some_other_id""/></text>")
%test:assertEquals("<text xmlns:tei=""http://www.tei-c.org/ns/1.0""><seg xml:id=""some_id"" next=""#id""/><seg xmlns:tei=""http://www.tei-c.org/ns/1.0"" xml:id=""id"" prev=""#some_id"" next=""#some_other_id""><stamp prev=""#id"" xmlns:tei=""http://www.tei-c.org/ns/1.0"">STAATSBIBLIOTHEK •BERLIN•</stamp></seg><seg xml:id=""some_other_id""/></text>")
function presort-test:omit-prev-nodes($node as element(*)) {
presort:sort($node)
};
Supports Markdown
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