Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
xquery version "3.1" encoding "UTF-8";
module namespace app-shared="http://xquery.weber-gesamtausgabe.de/modules/app-shared";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace mei="http://www.music-encoding.org/ns/mei";
import module namespace functx="http://www.functx.com";
import module namespace templates="http://exist-db.org/xquery/templates" at "/db/apps/shared-resources/content/templates.xql";
(:~
: Set an attribute to the value given in the $model map
:
: @author Peter Stadler
:)
declare function app-shared:set-attr($node as node(), $model as map(*), $attr as xs:string, $key as xs:string) as element() {
element {name($node)} {
$node/@*[not(name(.) = $attr)],
attribute {$attr} {$model($key)},
templates:process($node/node(), $model)
}
};
(:~
: Simply print the string value of $model($key)
:
: @author Peter Stadler
:)
declare
%templates:wrap
function app-shared:print($node as node(), $model as map(*), $key as xs:string) as xs:string? {
if ($model($key) castable as xs:string) then str:normalize-space($model($key))
else app-shared:join($node, $model, $key, '0', '')
};
(:~
: Simply print a sequence from the $model map by joining items with $separator
:
: @param $separator the separator for the string-join()
: @author Peter Stadler
:)
declare
%templates:wrap
%templates:default("max", "0")
%templates:default("separator", ", ")
function app-shared:join($node as node(), $model as map(*), $key as xs:string, $max as xs:string, $separator as xs:string) as xs:string? {
let $items :=
if($max castable as xs:integer and number($max) le 0) then $model($key)
else if($max castable as xs:integer and number($max) < count($model($key))) then (subsequence($model($key), 1, $max), '…')
else if($max castable as xs:integer and number($max) > 0) then subsequence($model($key), 1, $max)
else $model($key)
return
if (every $i in $items satisfies $i castable as xs:string) then string-join($items ! str:normalize-space(.), $separator)
else ()
};
(:~
: A non-wrapping alternative to the standard templates:each()
: Gets rid of the superfluous first list item
:
: At present, only $callbackArity=2 is supported
:
: @author Peter Stadler
:)
declare
%templates:default("max", "0")
%templates:default("callback", "0")
%templates:default("callbackArity", "2")
function app-shared:each($node as node(), $model as map(*), $from as xs:string, $to as xs:string, $max as xs:string, $callback as xs:string, $callbackArity as xs:string) as node()* {
let $items :=
if($max castable as xs:integer and $max != '0') then subsequence($model($from), 1, $max)
else $model($from)
let $callbackFunc :=
try { function-lookup(xs:QName($callback), xs:int($callbackArity)) }
catch * { core:logToFile('error', 'Failed to lookup function "' || $callback ) }
return (
for $item in $items
return
if(exists($callbackFunc)) then $callbackFunc($node, map:new(($model, map:entry($to, $item))))
else
element { node-name($node) } {
$node/@*,
templates:process($node/node(), map:new(($model, map:entry($to, $item))))
}
)
};
(:~
: Processes the node only if some $key (value) exists in $model
:
: @author Peter Stadler
:)
declare
%templates:default("wrap", "yes")
function app-shared:if-exists($node as node(), $model as map(*), $key as xs:string, $wrap as xs:string) as node()* {
if(count($model($key)) gt 0) then
if($wrap = 'yes') then
element {node-name($node)} {
$node/@*,
templates:process($node/node(), $model)
}
else templates:process($node/node(), $model)
else ()
};
(:~
: Processes the node only if some $key (value) *not* exists in $model
:
: @author Peter Stadler
:)
declare function app-shared:if-not-exists($node as node(), $model as map(*), $key as xs:string) as node()? {
if(count($model($key)) eq 0) then
element {node-name($node)} {
$node/@*,
templates:process($node/node(), $model)
}
else ()
};
(:~
: Processes the node only if some $key matches $value in $model
:
: @author Peter Stadler
:)
declare
%templates:default("wrap", "yes")
function app-shared:if-matches($node as node(), $model as map(*), $key as xs:string, $value as xs:string, $wrap as xs:string) as item()* {
if($model($key) castable as xs:string and string($model($key)) = tokenize($value, '\s+')) then
if($wrap = 'yes') then
element {node-name($node)} {
$node/@*,
templates:process($node/node(), $model)
}
else templates:process($node/node(), $model)
else ()
};
(:~
: Processes the node only if some $key *not* matches $value in $model
:
: @param $node the processed $node from the html template (a default param from the templating module)
: @param $model a map (a default param from the templating module)
: @param $key the key in $Model to look for
: @param $value the value of $key to match
: @param $wrap whether to copy the node $node to the output or just process the child nodes of $node
: @author Peter Stadler
:)
declare
%templates:default("wrap", "yes")
function app-shared:if-not-matches($node as node(), $model as map(*), $key as xs:string, $value as xs:string, $wrap as xs:string) as item()* {
if($model($key) castable as xs:string and string($model($key)) = tokenize($value, '\s+')) then ()
else if($wrap = 'yes') then
element {node-name($node)} {
$node/@*,
templates:process($node/node(), $model)
}
else templates:process($node/node(), $model)
};
declare function app-shared:order-list-items($node as node(), $model as map(*)) as element() {
element {node-name($node)} {
$node/@*,
for $child in $node/node()
let $childProcessed := templates:process($child, $model)
order by str:normalize-space($childProcessed)
return $childProcessed
}
};
(:~
: Outputs the raw value of $key, e.g. some HTML fragment
: that's not being wrapped with the $node element but replaces it.
~:)
declare function app-shared:output($node as node(), $model as map(*), $key as xs:string) as item()* {
$model($key)
};