Skip to content

Commit c84732e

Browse files
committed
feat: generate docs on install, nicer URLs
- generate documentation in the finish step of the package installation - add cache headers to most routes that could benefit from client-side caching - the html part is dropped from all URLs - /index.html -> / - /view.html -> /view - /ajax.html -> /query - /browse.html -> /browse - reliably set the URL on all rendered HTML pages - move all page templates to /templates/pages
1 parent 72c28f4 commit c84732e

File tree

13 files changed

+73
-53
lines changed

13 files changed

+73
-53
lines changed

src/main/xar-resources/controller.xq

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,66 @@ import module namespace login="http://exist-db.org/xquery/login" at "resource:or
44

55
declare namespace json="http://www.json.org";
66

7-
declare variable $exist:root external;
87
declare variable $exist:prefix external;
98
declare variable $exist:controller external;
109
declare variable $exist:path external;
1110
declare variable $exist:resource external;
1211

12+
declare variable $local:method := lower-case(request:get-method());
13+
declare variable $local:is-get := $local:method eq 'get';
14+
declare variable $local:user := login:set-user("org.exist.login", (), false());
15+
16+
declare function local:render-view($view as xs:string) {
17+
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
18+
<forward url="templates/pages/{$view}.html"/>
19+
<view>
20+
<forward url="{$exist:controller}/modules/view.xq">
21+
<set-attribute name="$exist:prefix" value="{$exist:prefix}"/>
22+
<set-attribute name="$exist:controller" value="{$exist:controller}"/>
23+
</forward>
24+
</view>
25+
</dispatch>
26+
};
27+
1328
if ($exist:path eq '') then (
1429
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
1530
<redirect url="{concat(request:get-uri(), '/')}"/>
1631
</dispatch>
17-
) else if ($exist:path eq "/") then (
18-
(: forward root path to index.xql :)
32+
) else if ($local:is-get and $exist:path eq "/") then (
33+
local:render-view('index')
34+
) else if ($local:is-get and $exist:path eq "/view") then (
35+
local:render-view('view')
36+
) else if ($local:is-get and $exist:path eq "/browse") then (
37+
local:render-view('browse')
38+
) else if ($local:method eq 'post' and $exist:path eq "/query") then (
39+
local:render-view('query')
40+
) else if ($local:is-get and $exist:path eq "/login") then (
41+
try {
42+
util:declare-option("exist:serialize", "method=json"),
43+
<status>
44+
<user>{request:get-attribute("org.exist.login.user")}</user>
45+
<isAdmin json:literal="true">{ sm:is-dba(request:get-attribute("org.exist.login.user")) }</isAdmin>
46+
</status>
47+
} catch * {
48+
response:set-status-code(401),
49+
<status>{$err:description}</status>
50+
}
51+
) else if ($local:is-get and matches($exist:path, ".+\.md$")) then (
1952
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
20-
<redirect url="index.html"/>
53+
<forward url="{$exist:controller}{$exist:path}">
54+
<set-header name="Cache-Control" value="max-age=73600; must-revalidate;"/>
55+
</forward>
2156
</dispatch>
22-
) else if (ends-with($exist:resource, ".html")) then (
23-
let $loggedIn := login:set-user("org.exist.login", (), true())
24-
return
25-
(: the html page is run through view.xql to expand templates :)
26-
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
27-
<view>
28-
<forward url="{$exist:controller}/modules/view.xq">
29-
<set-attribute name="$exist:prefix" value="{$exist:prefix}"/>
30-
<set-attribute name="$exist:controller" value="{$exist:controller}"/>
31-
</forward>
32-
</view>
33-
</dispatch>
34-
) else if (ends-with($exist:resource, ".md")) then (
57+
) else if ($local:is-get and matches($exist:path, "/resources/(css|fonts|images|scripts|svg|css)/.+")) then (
3558
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
36-
<forward url="{$exist:controller}{$exist:path}" />
59+
<forward url="{$exist:controller}{$exist:path}">
60+
<set-header name="Cache-Control" value="max-age=73600; must-revalidate;"/>
61+
</forward>
3762
</dispatch>
38-
) else if (matches($exist:path, "/resources/(css|fonts|images|scripts|svg|css)/.+")) then (
63+
) else if ($local:is-get and $exist:path = "/regenerate") then (
3964
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
40-
<forward url="{$exist:controller}{$exist:path}" />
65+
<forward url="{$exist:controller}/modules/regenerate.xq" />
4166
</dispatch>
42-
) else if ($exist:resource eq "login") then (
43-
let $loggedIn := login:set-user("org.exist.login", (), true())
44-
return
45-
try {
46-
util:declare-option("exist:serialize", "method=json"),
47-
<status>
48-
<user>{request:get-attribute("org.exist.login.user")}</user>
49-
<isAdmin json:literal="true">{ sm:is-dba(request:get-attribute("org.exist.login.user")) }</isAdmin>
50-
</status>
51-
} catch * {
52-
response:set-status-code(401),
53-
<status>{$err:description}</status>
54-
}
55-
) else if ($exist:path = "/regenerate") then (
56-
let $loggedIn := login:set-user("org.exist.login", (), false())
57-
return
58-
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
59-
<forward url="{$exist:controller}/modules/regenerate.xq" />
60-
</dispatch>
6167
) else (
6268
response:set-status-code(404),
6369
<data>Not Found</data>

src/main/xar-resources/finish.xq

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import module namespace generate = "http://exist-db.org/apps/fundocs/generate" at "modules/generate.xqm";
2+
3+
generate:fundocs(),
4+
util:log("info", "Finished generating function documentation for fundocs")

src/main/xar-resources/modules/app.xqm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ function app:print-module(
117117
<div class="module-head">
118118
<div class="module-head-inner row">
119119
<div class="col-md-1 hidden-xs">
120-
<a href="view.html?uri={$uri}&amp;location={$location}&amp;details=true"
120+
<a href="view?uri={$uri}&amp;location={$location}&amp;details=true"
121121
class="module-info-icon"><span class="glyphicon glyphicon-info-sign"/></a>
122122
</div>
123123
<div class="col-md-11 col-xs-12">
124-
<h3><a href="view.html?uri={$uri}&amp;location={$location}&amp;details=true">{ $uri }</a></h3>
124+
<h3><a href="view?uri={$uri}&amp;location={$location}&amp;details=true">{ $uri }</a></h3>
125125
{
126126
if (empty($location)) then (
127127
) else if (starts-with($location, '/db')) then (
@@ -223,7 +223,7 @@ function app:print-function(
223223
"&amp;function=" || $function-name || "&amp;arity=" || $arity ||
224224
(if ($location) then ("&amp;location=" || $location) else "#")
225225
return
226-
<a href="view.html{$query}" class="extended-docs btn btn-primary">
226+
<a href="view{$query}" class="extended-docs btn btn-primary">
227227
<span class="glyphicon glyphicon-info-sign"></span> Read more</a>
228228
)
229229
}
@@ -339,7 +339,7 @@ function app:showmodules(
339339
($extensions and app:is-extension($uri, $location))
340340
) then (
341341
<tr>
342-
<td><a href="view.html?uri={$uri}&amp;location={$location}#">{$uri}</a></td>
342+
<td><a href="view?uri={$uri}&amp;location={$location}#">{$uri}</a></td>
343343
<td>{$location}</td>
344344
</tr>
345345
) else ()

src/main/xar-resources/modules/config.xqm

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ xquery version "3.1";
66
:)
77
module namespace config="http://exist-db.org/xquery/apps/config";
88

9-
import module namespace templates="http://exist-db.org/xquery/html-templating";
10-
9+
declare namespace templates="http://exist-db.org/xquery/html-templating";
1110
declare namespace repo="http://exist-db.org/xquery/repo";
1211
declare namespace expath="http://expath.org/ns/pkg";
1312

@@ -29,6 +28,11 @@ declare variable $config:app-root :=
2928
substring-before($modulePath, "/modules")
3029
;
3130

31+
(:~
32+
: Calculate the base URL set on all HTML pages
33+
:)
34+
declare variable $config:base := request:get-context-path() || substring-after($config:app-root, "/db") || "/";
35+
3236
declare variable $config:app-data := $config:app-root || "/data";
3337

3438
declare variable $config:ext-doc := $config:app-data || "/docs";
@@ -66,6 +70,10 @@ declare %templates:wrap function config:app-title($node as node(), $model as map
6670
$config:expath-descriptor/expath:title/text()
6771
};
6872

73+
declare %templates:wrap function config:base-url ($node as node(), $model as map(*)) as attribute(href) {
74+
attribute href { $config:base }
75+
};
76+
6977
declare function config:app-meta($node as node(), $model as map(*)) as element()* {
7078
<meta xmlns="http://www.w3.org/1999/xhtml" name="description" content="{$config:repo-descriptor/repo:description/text()}"/>,
7179
for $author in $config:repo-descriptor/repo:author
@@ -99,4 +107,4 @@ declare function config:app-info($node as node(), $model as map(*)) {
99107
<td>{ request:get-attribute("$exist:controller") }</td>
100108
</tr>
101109
</table>
102-
};
110+
};

src/main/xar-resources/resources/scripts/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ document.addEventListener("DOMContentLoaded", function () {
1919
const formData = new FormData(form);
2020
formData.append("action", "search");
2121

22-
fetch("ajax.html", {
22+
fetch("query", {
2323
method: "POST",
2424
body: new URLSearchParams(formData),
2525
})

src/main/xar-resources/templates/page.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<title data-template="config:app-title">App Title</title>
44
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
55
<meta data-template="config:app-meta" />
6+
<base data-template="config:base-url" />
67
<link rel="shortcut icon" href="resources/images/exist_icon_16x16.ico" />
78
<link rel="stylesheet" type="text/css" href="resources/css/bootstrap.min.css" />
89
<link rel="stylesheet" type="text/css" href="resources/css/atom-one-dark.min.css" />
@@ -72,7 +73,7 @@
7273
<a href="#" class="nav-link dropdown-toggle" data-bs-toggle="dropdown">Documentation</a>
7374
<ul class="dropdown-menu">
7475
<li><a class="dropdown-item" href="{exist-documentation}">Documentation</a></li>
75-
<li><a class="dropdown-item" href="{fundocs}/index.html">Function
76+
<li><a class="dropdown-item" href="{fundocs}/">Function
7677
Library</a></li>
7778
<li><a class="dropdown-item"
7879
href="https://exist-db.org/exist/apps/wiki/blogs/eXist/">News &amp;

src/main/xar-resources/browse.html renamed to src/main/xar-resources/templates/pages/browse.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<article class="col-12-md">
44
<h1>XQuery Function Documentation</h1>
55
<p>
6-
<a href="index.html">
6+
<a href="">
77
<i class="glyphicon glyphicon-chevron-left"/> Back to Search Page</a>
88
</p>
99
<form class="form-inline">
File renamed without changes.

src/main/xar-resources/index.html renamed to src/main/xar-resources/templates/pages/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ <h1>XQuery Function Documentation</h1>
2828
</div>
2929
<div class="btn-toolbar">
3030
<div class="btn-group me-2" role="group" aria-label="browse action">
31-
<a id="browse" class="btn btn-primary" href="browse.html?extensions=true">
31+
<a id="browse" class="btn btn-primary" href="browse?extensions=true">
3232
<span class="glyphicon glyphicon-globe"></span> Browse</a>
3333
</div>
3434
<div class="btn-group me-2" role="group" aria-label="generate action">

0 commit comments

Comments
 (0)