@@ -18,6 +18,9 @@ import module namespace eutil="http://www.edirom.de/xquery/eutil" at "eutil.xqm"
1818(: NAMESPACE DECLARATIONS ================================================== :)
1919
2020declare namespace tei="http://www.tei-c.org/ns/1.0" ;
21+ declare namespace rest="http://exquery.org/ns/restxq" ;
22+ declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization" ;
23+ declare namespace xhtml="http://www.w3.org/1999/xhtml" ;
2124
2225(: FUNCTION DECLARATIONS =================================================== :)
2326
@@ -44,3 +47,147 @@ declare function teitext:getLabel($uri as xs:string, $edition as xs:string) as x
4447 eutil:getLocalizedTitle (eutil:getDoc ($uri), edition:getLanguage ($edition))
4548
4649};
50+
51+ (:~
52+ API endpoint for TEI as HTML output
53+ :
54+ : @param $apiVersion The API version to decide on output format
55+ : @param $uri The URI of the document to process
56+ : @param $idPrefix An ID prefix to add to all unique ID values in the document
57+ : @param $term An optional search term to filter the text by
58+ : @param $page An optional page to filter the text by
59+ : @param $contextPath The context path of the server to build absolute URLs for images and links
60+ : @return HTML for the text
61+ :)
62+ declare
63+ %rest:GET
64+ %rest:path("/{$apiVersion}/tei/html" )
65+ %rest:query-param("edition" , "{$edition}" , "" )
66+ %rest:query-param("uri" , "{$uri}" , "" )
67+ %rest:query-param("idPrefix" , "{$idPrefix}" , "" )
68+ %rest:query-param("term" , "{$term}" , "" )
69+ %rest:query-param("page" , "{$page}" , "" )
70+ %rest:query-param("contextPath" , "{$contextPath}" , "" )
71+ %rest:query-param("lang" , "{$lang}" , "" )
72+ %rest:produces("text/html" )
73+ %output:media-type("text/html" )
74+ %output:method("html" )
75+ function teitext:getHtmlAPI ($apiVersion as xs:string, $edition as xs:string*, $uri as xs:string*, $idPrefix as xs:string*, $term as xs:string*,
76+ $page as xs:string*, $contextPath as xs:string*, $lang as xs:string*) as element () {
77+
78+ let $doc := teitext:getHTML ($edition, $uri, $idPrefix, $term, $page, $contextPath, $lang)
79+ let $body := $doc//xhtml:body
80+
81+ return
82+
83+ (: for API v1 return body/* wrapped in div :)
84+ if ($apiVersion eq "v1" ) then
85+ element div {
86+ for $attribute in $body/@*
87+ return
88+ $attribute,
89+ for $node in $body/node ()
90+ return
91+ $node
92+ }
93+ else
94+
95+ (: for API v2 return complete $doc :)
96+ if ($apiVersion eq "v2" ) then
97+ ($doc)
98+
99+ (: otherwise :)
100+ else
101+ element p {
102+ "Invalid API version: " || $apiVersion
103+ }
104+
105+ };
106+
107+ (:~
108+ : Returns HTML for TEI text
109+ :
110+ : @param $source The URIs of the Text's document to process
111+ : @return HTML
112+ :)
113+ declare function teitext:getHTML (
114+ $edition as xs:string*,
115+ $uri as xs:string*,
116+ $idPrefix as xs:string*,
117+ $term as xs:string*,
118+ $page as xs:string*,
119+ $contextPath as xs:string*,
120+ $lang as xs:string*) as element () {
121+
122+ (: get preferences :)
123+ let $imagePrefix := edition:getPreference ('image_prefix' , $edition)
124+ let $lang := if ($lang != "" ) then $lang else edition:getLanguage ($edition)
125+
126+ (: get TEI from $uri :)
127+ let $doc := eutil:getDoc ($uri)/root ()
128+
129+ (: get XSLTs from processing instruction :)
130+ let $xslInstruction := $doc//processing-instruction (xml-stylesheet)
131+ let $xslInstruction :=
132+ for $i in fn:serialize ($xslInstruction, ())
133+ return
134+ if (matches ($i, 'type="text/xsl"' )) then
135+ (substring-before (substring-after ($i, 'href="' ), '"' ))
136+ else
137+ ()
138+
139+ (: get TEI root by $term if provided :)
140+ let $doc :=
141+ if ($term eq '' ) then
142+ ($doc)
143+ else
144+ ($doc//tei:text [ft:query (., $term)]/ancestor::tei:TEI)
145+
146+ (: apply util:expand if $term is provided :)
147+ let $doc :=
148+ if ($term eq '' ) then
149+ ($doc)
150+ else
151+ (util:expand ($doc))
152+
153+ let $base := replace (system:get-module-load-path (), 'embedded-eXist-server' , '' )
154+
155+ let $xsl :=
156+ if ($xslInstruction) then
157+ ($xslInstruction)
158+ else
159+ ('../xslt/tei/profiles/edirom-body/teiBody2HTML.xsl' )
160+
161+ (:TODO introduce injection-point for tei-stylesheet parameters :)
162+ let $params := (
163+ (: parameters for Edirom-Online :)
164+ <param name = "lang" value = "{$lang} " />,
165+ <param name = "docUri" value = "{$uri} " />,
166+ <param name = "contextPath" value = "{$contextPath} " />,
167+ <param name = "imagePrefix" value = "{$imagePrefix} " />,
168+ (: parameters for the TEI Stylesheets :)
169+ <param name = "autoHead" value = "false" />,
170+ <param name = "autoToc" value = "false" />,
171+ <param name = "base" value = "{concat ($base, '/../xslt/' )} " />,
172+ <param name = "documentationLanguage" value = "{$lang} " />,
173+ <param name = "footnoteBackLink" value = "true" />,
174+ <param name = "numberHeadings" value = "false" />,
175+ <param name = "pageLayout" value = "CSS" />
176+ )
177+
178+ let $doc := transform:transform ($doc, doc ($xsl), <parameters>{$params}</parameters>)
179+
180+ (: Do a second transformation to add edirom online ID prefixes for unique ID values if object is open mutiple times :)
181+ let $xsl := '../xslt/edirom_idPrefix.xsl'
182+
183+ let $params := (
184+ <param name = "idPrefix" value = "{$idPrefix} " />
185+ )
186+
187+ let $doc := transform:transform ($doc, doc ($xsl), <parameters>{$params}</parameters>)
188+
189+ return
190+ $doc
191+
192+
193+ };
0 commit comments