Skip to content

Commit 398a73c

Browse files
committed
added API v1/v2 support for getEditions and getText
1 parent 8bd45d7 commit 398a73c

File tree

4 files changed

+159
-96
lines changed

4 files changed

+159
-96
lines changed

data/xql/getEditions.xql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ declare option output:media-type "application/json";
2525

2626
(: QUERY BODY ============================================================== :)
2727

28-
edition:findEditions()
28+
(: return info about editions in via v1 API:)
29+
edition:findEditions('v1')

data/xql/getText.xql

Lines changed: 5 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -5,117 +5,28 @@ xquery version "3.1";
55

66
(: IMPORTS ================================================================= :)
77

8-
import module namespace edition = "http://www.edirom.de/xquery/edition" at "../xqm/edition.xqm";
9-
10-
import module namespace eutil = "http://www.edirom.de/xquery/eutil" at "../xqm/eutil.xqm";
8+
import module namespace teitext = "http://www.edirom.de/xquery/teitext" at "../xqm/teitext.xqm";
119

1210
(: NAMESPACE DECLARATIONS ================================================== :)
1311

14-
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
15-
1612
declare namespace request = "http://exist-db.org/xquery/request";
17-
18-
declare namespace tei = "http://www.tei-c.org/ns/1.0";
19-
20-
declare namespace xhtml = "http://www.w3.org/1999/xhtml";
13+
declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
2114

2215
(: OPTION DECLARATIONS ===================================================== :)
2316

2417
declare option output:method "xhtml";
25-
2618
declare option output:media-type "text/html";
27-
2819
declare option output:omit-xml-declaration "yes";
29-
3020
declare option output:indent "yes";
3121

3222
(: QUERY BODY ============================================================== :)
3323

24+
let $edition := request:get-parameter('edition', '')
3425
let $uri := request:get-parameter('uri', '')
3526
let $idPrefix := request:get-parameter('idPrefix', '')
3627
let $term := request:get-parameter('term', '')
37-
let $path := request:get-parameter('path', '')
3828
let $page := request:get-parameter('page', '')
39-
let $doc := eutil:getDoc($uri)/root()
4029
let $contextPath := request:get-scheme()|| "://" || request:get-server-name() || ":" || request:get-server-port() || request:get-context-path()
41-
let $xslInstruction := $doc//processing-instruction(xml-stylesheet)
42-
43-
let $xslInstruction :=
44-
for $i in fn:serialize($xslInstruction, ())
45-
return
46-
if (matches($i, 'type="text/xsl"')) then
47-
(substring-before(substring-after($i, 'href="'), '"'))
48-
else
49-
()
50-
51-
let $doc :=
52-
if ($term eq '') then
53-
($doc)
54-
else
55-
($doc//tei:text[ft:query(., $term)]/ancestor::tei:TEI)
56-
57-
let $doc :=
58-
if ($term eq '') then
59-
($doc)
60-
else
61-
(util:expand($doc))
62-
63-
let $doc :=
64-
if ($page eq '') then
65-
($doc)
66-
else (
67-
let $pb1 := $doc//tei:pb[@facs eq '#' || $page]/@n
68-
let $pb2 := ($doc//tei:pb[@facs eq '#' || $page]/following::tei:pb)[1]/@n
69-
70-
return
71-
transform:transform($doc, doc('../xslt/reduceToPage.xsl'),
72-
<parameters>
73-
<param name="pb1" value="{$pb1}"/>
74-
<param name="pb2" value="{$pb2}"/>
75-
</parameters>
76-
)
77-
)
78-
79-
let $base := replace(system:get-module-load-path(), 'embedded-eXist-server', '')
80-
let $edition := request:get-parameter('edition', '')
81-
let $imageserver := edition:getPreference('image_server', $edition)
82-
83-
let $imagePrefix := edition:getPreference('image_prefix', $edition)
84-
85-
let $xsl :=
86-
if ($xslInstruction) then
87-
($xslInstruction)
88-
else
89-
('../xslt/tei/profiles/edirom-body/teiBody2HTML.xsl')
90-
91-
(:TODO introduce injection-point for tei-stylesheet parameters :)
92-
let $params := (
93-
(: parameters for Edirom-Online :)
94-
<param name="lang" value="{edition:getLanguage($edition)}"/>,
95-
<param name="docUri" value="{$uri}"/>,
96-
<param name="contextPath" value="{$contextPath}"/>,
97-
<param name="imagePrefix" value="{$imagePrefix}"/>,
98-
(: parameters for the TEI Stylesheets :)
99-
<param name="autoHead" value="false"/>,
100-
<param name="autoToc" value="false"/>,
101-
<param name="base" value="{concat($base, '/../xslt/')}"/>,
102-
<param name="documentationLanguage" value="{edition:getLanguage($edition)}"/>,
103-
<param name="footnoteBackLink" value="true"/>,
104-
<param name="numberHeadings" value="false"/>,
105-
<param name="pageLayout" value="CSS"/>
106-
)
107-
108-
let $doc := transform:transform($doc, doc($xsl), <parameters>{$params}</parameters>)
109-
110-
(: Do a second transformation to add edirom online ID prefixes for unique ID values if object is open mutiple times :)
111-
let $xsl := '../xslt/edirom_idPrefix.xsl'
112-
113-
let $params := (
114-
<param name="idPrefix" value="{$idPrefix}"/>
115-
)
116-
117-
let $doc := transform:transform($doc, doc($xsl), <parameters>{$params}</parameters>)
118-
11930

120-
return
121-
$doc
31+
return
32+
teitext:getHtmlAPI("v1", $edition, $uri, $idPrefix, $term, $page, $contextPath)

data/xqm/edition.xqm

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ module namespace edition = "http://www.edirom.de/xquery/edition";
1313
(: IMPORTS ================================================================= :)
1414

1515
import module namespace functx="http://www.functx.com";
16-
1716
import module namespace eutil = "http://www.edirom.de/xquery/eutil" at "eutil.xqm";
1817

1918
(: NAMESPACE DECLARATIONS ================================================== :)
@@ -24,6 +23,11 @@ declare namespace xlink = "http://www.w3.org/1999/xlink";
2423
declare namespace rest="http://exquery.org/ns/restxq";
2524
declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
2625

26+
27+
(: VARIABLE DECLARATIONS =================================================== :)
28+
29+
30+
2731
(: FUNCTION DECLARATIONS =================================================== :)
2832

2933

data/xqm/teitext.xqm

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import module namespace eutil="http://www.edirom.de/xquery/eutil" at "eutil.xqm"
1818
(: NAMESPACE DECLARATIONS ================================================== :)
1919

2020
declare 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

Comments
 (0)