Skip to content

Commit

Permalink
add URL rewriting and controller logic for svg
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstadler committed Dec 21, 2022
1 parent 772f142 commit ba8127e
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion controller.xql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ declare variable $exist:root external;
: Content Negotiation
: Evaluate Accept header and resource suffix to serve appropriate media type
:
: @return ('html' | 'json' | 'tei' | 'png')
: @return ('html' | 'json' | 'tei' | 'png' | 'svg')
~:)
declare function local:media-type() as xs:string {
let $suffix := substring-after($exist:resource, '.')
Expand All @@ -32,13 +32,15 @@ declare function local:media-type() as xs:string {
else if(matches($suffix, '^json$', 'i')) then 'json'
else if(matches($suffix, '^js(onp)?$', 'i')) then 'jsonp'
else if(matches($suffix, '^png$', 'i')) then 'png'
else if(matches($suffix, '^svg$', 'i')) then 'svg'

(: Accept header follows if no suffix is given :)
else if($accepted-content-types[1] = ('text/html', 'application/xhtml+xml')) then 'html'
else if($accepted-content-types[1] = ('application/xml', 'application/tei+xml')) then 'tei'
else if($accepted-content-types[1] = ('application/json')) then 'json'
else if($accepted-content-types[1] = ('application/javascript')) then 'jsonp'
else if($accepted-content-types[1] = ('image/png', 'application/png', 'application/x-png')) then 'png'
else if($accepted-content-types[1] = ('image/svg+xml')) then 'svg'

(: if nothing matches fall back to TEI-XML :)
else 'tei'
Expand All @@ -62,6 +64,7 @@ declare function local:dispatch() {
case 'json' return local:return-json($char)
case 'jsonp' return local:return-jsonp($char)
case 'png' return local:dispatch-image($char)
case 'svg' return local:dispatch-svgGlyph($char)
default return local:error()
else local:error()
};
Expand Down Expand Up @@ -147,6 +150,21 @@ declare function local:dispatch-image($char as element(tei:char)?) as element(ex
else local:error()
};

(:~
: Return an SVG XML file of a single char
~:)
declare function local:dispatch-svgGlyph($char as element(tei:char)?) as element(exist:dispatch) {
let $codepoint := substring($char/tei:mapping[@type='smufl'], 3)
return
if($codepoint) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/resources/images/{$codepoint}.svg">
<cache-control cache="yes"/>
</forward>
</dispatch>
else local:error()
};

(:~
: Return the 404 error page
~:)
Expand Down

0 comments on commit ba8127e

Please sign in to comment.