Commit debccf72 authored by Mathias Goebel's avatar Mathias Goebel 🎠
Browse files

modularized and non-repetitive code

parent 039b6640
......@@ -6,6 +6,7 @@ xquery version "3.1";
: @see https://fontane-nb.dariah.eu/register.html
:)
module namespace fontaneregister="http://fontane-nb.dariah.eu/register";
import module namespace console="http://exist-db.org/xquery/console";
declare namespace ore="http://www.openarchives.org/ore/terms/";
declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
......@@ -14,7 +15,6 @@ declare namespace tgmd="http://textgrid.info/namespaces/metadata/core/2010";
declare namespace xhtml="http://www.w3.org/1999/xhtml";
declare variable $fontaneregister:metaCollection := '/db/sade-projects/textgrid/data/xml/meta/';
declare variable $fontaneregister:get-entity := request:get-parameter('e', '');
declare variable $fontaneregister:get-notebook := request:get-parameter('nb', 'all');
......@@ -34,7 +34,8 @@ declare function local:resolve-name($uri as xs:string) {
: @param – $id the id to search for
: @param – $node the document or xml fragment
: :)
declare function local:check-active($id as xs:string, $node as element(*)+) as xs:boolean {
declare function local:check-active($id as xs:string, $node as element(*)+)
as xs:boolean {
$node//@xml:id = $id
};
......@@ -48,26 +49,167 @@ declare function local:check-entity($node as node()+) as xs:string? {
else ()
};
(:~
: creates a generic xhtml:class attribute mandatory for every entry
: :)
declare function local:entry-class($id as xs:string)
as attribute(class){
attribute class {
"registerEintrag",
if($id = $fontaneregister:get-entity)
then "fhighlighted"
else ()
}
};
(:~
: creates a list for label or name :)
declare function local:variant($variant as element()*)
as element(xhtml:li)? {
if(not(exists($variant))) then () else
element xhtml:li {
attribute class { "variant" },
"Im Text: ",
element xhtml:ul {
for $var at $pos in $variant
return
element xhtml:li {
string($var) || (if($pos = count($variant)) then () else ",")
}
}
}
};
(:~
: transform tei:idno to xhtml:li
: TODO: declare element(tei:idno), currently wrong encoding with tei:label
: :)
declare function local:idno($idnos as element()*)
as element(xhtml:li)*{
for $idno in $idnos
let $str := string($idno)
return
element xhtml:li {
attribute class {"idno"},
string($idno/@type) || ": " || $str,
" ",
element xhtml:a {
attribute href { string($idno/@xml:base) || $str },
<xhtml:i title="Link zu externer Ressource" class="fa fa-external-link"></xhtml:i>
}
}
};
(:~
: transformation from tei:note to xhtml:li
: :)
declare function local:note($note as element(tei:note)*)
as element(xhtml:li)*{
for $n in $note
return
element xhtml:li {
attribute class { "tei-note" },
$n/text()
}
};
(:~
: a list item with a list of links
: :)
declare function local:links($link-map as map())
as element(xhtml:li){
element xhtml:li {
attribute class { "links" },
if(count(map:keys($link-map)) lt 2) then 'In Notizbuch' else 'In den Notizbüchern',
element xhtml:ul {
local:link($link-map)
}
}
};
(:~
: create list items containing links
: :)
declare function local:link($map as map()*)
as element(xhtml:li)*{
for $tguri in map:keys($map)
let $resolve := local:resolve-name($tguri)
return
element xhtml:li {
attribute class { "nb-" || matches($resolve, "Notizbuch") => string() },
$resolve || ": ",
element xhtml:ul {
attribute class {"link-list-horizontal"},
for $link in $map($tguri)
let $page := $link/@target => substring-after("surface[@n='") => substring-before("']//")
return
element xhtml:li {
element xhtml:a {
attribute href {
"edition.html?id=%2Fxml%2Fdata%2F" || $tguri
|| ".xml&amp;page=" || $page
},
$page
}
}
}
}
};
(:~
: creates a xhtml list item containing serialized code.
: @param $node – a single element
: :)
declare function local:li-code($node as element(*)) {
element {"xhtml:li"} {
attribute {"class"} {"register-code"},
element { "xhtml:span" } {
attribute { "class" } {"registerCode"},
element xhtml:li {
attribute class {"register-code"},
element xhtml:span {
attribute class {"registerCode"},
<xhtml:i title="Zeige XML" class="fa fa-code"></xhtml:i>
},
element {"xhtml:pre"} {
element {"xhtml:code"} {
attribute {"class"}{"html"},
element xhtml:pre {
element xhtml:code {
attribute class {"html"},
serialize($node) => replace("[ ]{2,}", " ")
}
}
}
};
(:~
: generic function for entries with tei:note/tei:ptr.
: @return link to the corresponding index entry
: :)
declare function local:pointer($entry as element())
as element(xhtml:li){
let $label-main := ($entry/*)[1]
let $target := $entry/tei:note/tei:ptr/@target => substring-after('#')
let $target-node := $entry/ancestor::tei:TEI//tei:*[@xml:id = $target]
return
(: item :)
element xhtml:li {
attribute class {
"registerEintrag",
if( $entry >> $target-node )
then "up"
else "down"
},
element xhtml:span {
attribute class { "label-main" },
$label-main/text()
},
element xhtml:ul {
element xhtml:li {
attribute class { "pointer" },
"siehe: ",
element xhtml:a {
attribute href { "?e=" || $target },
string(($target-node/tei:*)[1])
}
}
}
}
};
(:~
: Main function to create a complete index, contains some template specific
: code.
......@@ -129,7 +271,8 @@ return
(:~
: parser for listEvents/event entities
: :)
declare function fontaneregister:events($eventDoc as element(tei:listEvent)) as element(xhtml:div) {
declare function fontaneregister:events($eventDoc as element(tei:listEvent))
as element(xhtml:div) {
let $eventDoc-uri := tokenize($eventDoc/base-uri(), '/|\.')[8]
return
(: main tab :)
......@@ -153,7 +296,8 @@ return
</div>
};
declare function local:prepare-event($e as element(tei:event)) as element(xhtml:li) {
declare function local:prepare-event($e as element(tei:event))
as element(xhtml:li) {
let $id := $e/@xml:id
let $where := substring-after($e/@where, ":")
let $label-main := ($e/tei:label)[1]
......@@ -172,71 +316,50 @@ declare function local:prepare-event($e as element(tei:event)) as element(xhtml:
return
(: item :)
element { "xhtml:li" } {
attribute { "id" } { string($id) },
attribute { "class" } {
"registerEintrag",
if($id = $fontaneregister:get-entity)
then "fhighlighted"
else ()
},
element xhtml:li {
attribute id { string($id) },
local:entry-class($id),
(: link to this entry :)
element { "xhtml:span" } {
attribute { "class" } {"registerLink"},
element { "xhtml:a" } {
attribute { "href" } {
element xhtml:span {
attribute class {"registerLink"},
element xhtml:a {
attribute href {
"register.html?e=" || $id
},
<xhtml:i title="Link zu diesem Eintrag" class="fa fa-link"></xhtml:i>
}
},
element { "xhtml:span" } {
attribute { "class" } { "label-main" },
element xhtml:span {
attribute class { "label-main" },
$label-main/text()
},
element { "xhtml:ul" } {
element xhtml:ul {
(: variant :)
if(not($variant)) then () else
element { "xhtml:li" } {
attribute { "class" } { "variant" },
"Im Text: ",
element { "xhtml:ul" } {
for $var in $variant
return
element { "xhtml:li" } {
string($var)
}
}
},
local:variant($variant),
(: secondary label :)
for $l in $label-alt
return
element { "xhtml:li" } {
attribute { "class" } {"label-" || string($l/@type)},
element xhtml:li {
attribute class {"label-" || string($l/@type)},
$l/text(),
if($l/@type = "variant")
then ()
else (
"&#160;",
element { "xhtml:a" } {
attribute { "href" } { string($l/@xml:base) || string($l) },
element xhtml:a {
attribute href { string($l/@xml:base) || string($l) },
<xhtml:i title="Link zu externer Ressource" class="fa fa-external-link"></xhtml:i>
}
)
},
(: note :)
for $n in $note
return
element { "xhtml:li" } {
attribute { "class" } { "tei-note" },
$n/text()
},
local:note($note),
(: where :)
if($where = "") then () else
element { "xhtml:li" } {
attribute { "class" } { "where" },
element { "xhtml:a" } {
attribute { "href" } { "?e="|| $where },
element xhtml:li {
attribute class { "where" },
element xhtml:a {
attribute href { "?e="|| $where },
<xhtml:i class="fa fa-map-marker" aria-hidden="true"></xhtml:i>,
"&#160;",
$where
......@@ -246,53 +369,22 @@ return
if(not(exists($bibl))) then () else
let $bibl-id := $bibl/tei:ptr/@target => substring-after(":")
return
element { "xhtml:li" } {
attribute { "class" } { "bibl" },
element { "xhtml:a" } {
attribute { "href" } { "literaturvz.html?id=" || $bibl-id },
element xhtml:li {
attribute class { "bibl" },
element xhtml:a {
attribute href { "literaturvz.html?id=" || $bibl-id },
<xhtml:i class="fa fa-book" aria-hidden="true"></xhtml:i>,
"&#160;",
$bibl-id
}
},
(: links :)
element {"xhtml:li"} {
attribute {"class"} {"links"},
if(count(map:keys($link-map)) lt 2) then 'In Notizbuch' else 'In den Notizbüchern',
element {"xhtml:ul"} {
local:links-event($link-map)
}
},
local:links($link-map),
local:li-code($e)
}
}
};
declare function local:links-event($map as map()) {
for $tguri in map:keys($map)
let $resolve := local:resolve-name($tguri)
return
element { "xhtml:li" } {
attribute { "class" } { "nb-" || matches($resolve, "Notizbuch") => string() },
$resolve || ": ",
element { "xhtml:ul" } {
attribute {"class"} {"link-list-horizontal"},
for $link in $map($tguri)
let $page := $link/@target => substring-after("surface[@n='") => substring-before("']//")
return
element { "xhtml:li" } {
element { "xhtml:a" } {
attribute { "href" } {
"edition.html?id=%2Fxml%2Fdata%2F" || $tguri
|| ".xml&amp;page=" || $page
},
$page
}
}
}
}
};
(:~
: parser for tei:listOrg
: :)
......@@ -342,86 +434,59 @@ declare function local:prepare-org($org as element(tei:org)) as element(xhtml:li
return
(: item :)
element { "xhtml:li" } {
attribute { "id" } { string($id) },
attribute { "class" } {
"registerEintrag",
if($id = $fontaneregister:get-entity)
then "fhighlighted"
else ()
},
element xhtml:li {
attribute id { string($id) },
local:entry-class($id),
(: link to this entry :)
element { "xhtml:span" } {
attribute { "class" } {"registerLink"},
element { "xhtml:a" } {
attribute { "href" } {
element xhtml:span {
attribute class {"registerLink"},
element xhtml:a {
attribute href {
"register.html?e=" || $id
},
<xhtml:i title="Link zu diesem Eintrag" class="fa fa-link"></xhtml:i>
}
},
element { "xhtml:span" } {
attribute { "class" } { "label-main" },
element xhtml:span {
attribute class { "label-main" },
$label-main/text()
},
element { "xhtml:ul" } {
element xhtml:ul {
(: variant :)
if(not($variant)) then () else
element { "xhtml:li" } {
attribute { "class" } { "variant" },
"Im Text: ",
element { "xhtml:ul" } {
for $var in $variant
return
element { "xhtml:li" } {
string($var)
}
}
},
local:variant($variant),
(: secondary label :)
for $l in $label-alt
return
element { "xhtml:li" } {
attribute { "class" } {"label-" || string($l/@type)},
element xhtml:li {
attribute class {"label-" || string($l/@type)},
$l/text(),
if($l/@type = "variant")
then ()
else (
"&#160;",
element { "xhtml:a" } {
attribute { "href" } { string($l/@xml:base) || string($l) },
element xhtml:a {
attribute href { string($l/@xml:base) || string($l) },
<xhtml:i title="Link zu externer Ressource" class="fa fa-external-link"></xhtml:i>
}
)
},
(: note :)
for $n in $note
return
element { "xhtml:li" } {
attribute { "class" } { "tei-note" },
$n/text()
},
local:note($note),
(: bibl :)
if(not(exists($bibl))) then () else
let $bibl-id := $bibl/tei:ptr/@target => substring-after(":")
return
element { "xhtml:li" } {
attribute { "class" } { "bibl" },
element { "xhtml:a" } {
attribute { "href" } { "literaturvz.html?id=" || $bibl-id },
element xhtml:li {
attribute class { "bibl" },
element xhtml:a {
attribute href { "literaturvz.html?id=" || $bibl-id },
<xhtml:i class="fa fa-book" aria-hidden="true"></xhtml:i>,
"&#160;",
$bibl-id
}
},
(: links :)
element {"xhtml:li"} {
attribute {"class"} {"links"},
if(count(map:keys($link-map)) lt 2) then 'In Notizbuch' else 'In den Notizbüchern',
element {"xhtml:ul"} {
local:links-event($link-map)
}
},
local:links($link-map),
local:li-code($org)
}
}
......@@ -456,38 +521,11 @@ return
};
declare function local:prepare-person($pers as element(*)+) as element(xhtml:li) {
let $label-main := $pers/tei:persName[1]
return
(: check if the item points to another entity :)
if( not(exists($pers/@xml:id)) )
then
(: pointer :)
let $target := $pers/tei:note/tei:ptr/@target => substring-after('#')
let $target-node := $pers/ancestor::tei:TEI//tei:*[@xml:id = $target]
return
(: item :)
element { "xhtml:li" } {
attribute { "class" } {
"registerEintrag",
if( $pers >> $target-node )
then "up"
else "down"
},
element { "xhtml:span" } {
attribute { "class" } { "label-main" },
$label-main/text()
},
element { "xhtml:ul" } {
element { "xhtml:li" } {
attribute { "class" }{ "pointer" },
"siehe: ",
element { "xhtml:a" } {
attribute { "href" } { "?e=" || $target },
string($target-node/tei:persName[1])
}
}
}
}
local:pointer($pers)
else
(: entity :)
let $id := $pers/@xml:id
......@@ -512,74 +550,48 @@ else
return
(: item :)
element { "xhtml:li" } {
attribute { "id" } { string($id) },
attribute { "class" } {
"registerEintrag",
if($id = $fontaneregister:get-entity)
then "fhighlighted"
else ()
},
element xhtml:li {
attribute id { string($id) },
local:entry-class($id),
(: link to this entry :)
element { "xhtml:span" } {
attribute { "class" } {"registerLink"},
element { "xhtml:a" } {
attribute { "href" } {
element xhtml:span {
attribute class {"registerLink"},
element xhtml:a {
attribute href {
"register.html?" ||
"e=" || $id
},
<xhtml:i title="Link zu diesem Eintrag" class="fa fa-link"></xhtml:i>
}
},
element { "xhtml:span" } {
attribute { "class" } { "label-main" },
element xhtml:span {
attribute class { "label-main" },
$label-main/text()
},
element { "xhtml:ul" } {
element xhtml:ul {
(: variant :)
if(not($variant)) then () else
element { "xhtml:li" } {
attribute { "class" } { "variant" },
"Im Text: ",
element { "xhtml:ul" } {
for $var in $variant
return
element { "xhtml:li" } {
string($var)
}
}
},
local:variant($variant),
(: secondary label :)
for $l in $label-alt
return
element { "xhtml:li" } {
attribute { "class" } {"label-" || string($l/@type)},
element xhtml:li {
attribute class {"label-" || string($l/@type)},
$l/text(),
if($l/@type = "variant")
then ()
else (
"&#160;",
element { "xhtml:a" } {
attribute { "href" } { string($l/@xml:base) || string($l) },
element xhtml:a {
attribute href { string($l/@xml:base) || string($l) },
<xhtml:i title="Link zu externer Ressource" class="fa fa-external-link"></xhtml:i>
}
)
},
(: idno :)
for $idno in $idnos
return
element { "xhtml:li" } {
attribute { "class" } {"label-" || string($idno/@type)},
$idno/text(),
"&#160;",
element { "xhtml:a" } {
attribute { "href" } { string($idno/@xml:base) || string($idno) },
<xhtml:i title="Link zu externer Ressource" class="fa fa-external-link"></xhtml:i>
}
},
local:idno($idnos),
(: birth, death :)
if( not($birth or $death) ) then () else
element { "xhtml:li" } {
element xhtml:li {
if($birth) then ('&#8727;', $birth/text()) else (),
if( $birth and $death ) then " " else (),
if($death) then ('✝', $death/text()) else()
......@@ -587,31 +599,26 @@ else
(: occupation :)
for $o in $occupation
return
element { "xhtml:li" } {
attribute { "class" } { "occupation" },
element xhtml:li {
attribute class { "occupation" },
$o/text()
},
for $s in $state
return
element { "xhtml:li" } {
attribute { "class" } { "state" },
element xhtml:li {
attribute class { "state" },
$s/text()
},
(: note :)
for $n in $note
return
element { "xhtml:li" } {
attribute { "class" } { "tei-note" },
$n/text()
},
local:note($note),
(: bibl :)
for $b in $bibl
let $bibl-id := $b/tei:ptr/@target => substring-after(":")
return
element { "xhtml:li" } {
attribute { "class" } { "bibl" },
element { "xhtml:a" } {
attribute { "href" } { "literaturvz.html?id=" || $bibl-id },
element xhtml:li {
attribute class { "bibl" },
element xhtml:a {
attribute href { "literaturvz.html?id=" || $bibl-id },
<xhtml:i class="fa fa-book" aria-hidden="true"></xhtml:i>,
"&#160;",
$bibl-id ||
......@@ -621,13 +628,7 @@ else
}
},
(: links :)
element {"xhtml:li"} {
attribute {"class"} {"links"},