1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-10-01 08:20:46 +00:00

Finish refactoring of server <-> sitemap interface

Continuing c4cdb1ed8c
This commit is contained in:
jeremy@jermolene.com 2021-04-10 13:51:07 +01:00
parent 02d390a673
commit b7419dec3a
2 changed files with 22 additions and 32 deletions

View File

@ -65,9 +65,24 @@ function Server(options) {
variables: {}
});
this.sitemap.load(this.variables.sitemap);
this.addRoutes(this.sitemap.getServerRoutes());
$tw.utils.each(this.sitemap.getServerRoutes(),function(routeInfo) {
self.addRoute({
method: "GET",
path: routeInfo.regexp,
handler: function(request,response,state) {
var fileDetails = routeInfo.handler(state.params);
if(fileDetails) {
response.writeHead(200, {"Content-Type": fileDetails.type});
response.end(fileDetails.text,fileDetails.isBase64 ? "base64" : "utf8");
} else {
response.writeHead(404);
response.end();
}
}
});
});
} else {
$tw.modules.forEachModuleOfType("route", function(title,routeDefinition) {
$tw.modules.forEachModuleOfType("route",function(title,routeDefinition) {
self.addRoute(routeDefinition);
});
}
@ -104,13 +119,6 @@ Server.prototype.get = function(name) {
return this.variables[name];
};
Server.prototype.addRoutes = function(routes) {
var self = this;
$tw.utils.each(routes,function(route) {
self.addRoute(route);
});
};
Server.prototype.addRoute = function(route) {
this.routes.push(route);
};

View File

@ -87,21 +87,20 @@ Sitemap.prototype.getAllFileDetails = function(exportTiddlers) {
/*
Returns an array of server routes {method:, path:, handler:}
Returns an array of server routes {regexp:, handler:}
*/
Sitemap.prototype.getServerRoutes = function() {
var self = this,
output = [];
$tw.utils.each(this.routes,function(route) {
output.push({
method: "GET",
path: route.regexp,
handler: function(request,response,state) {
regexp: route.regexp,
handler: function(params) {
// Locate the tiddler identified by the capture groups, if any
var title = null,
nextParam = 0;
$tw.utils.each(route.captureGroups,function(captureGroup) {
var param = state.params[nextParam++];
var param = params[nextParam++];
if(captureGroup.field === "title") {
switch(captureGroup.function) {
case "slugify":
@ -114,24 +113,7 @@ Sitemap.prototype.getServerRoutes = function() {
}
})
// Return the rendering or raw tiddler
switch(route.params.type) {
case "render":
response.writeHead(200,{"Content-Type": route.params["output-type"] || "text/html"});
response.end(self.wiki.renderTiddler("text/plain",route.params.template,{
variables: $tw.utils.extend({},self.variables,route.variables,{currentTiddler: title})
}));
break;
case "raw":
var tiddler = title && self.wiki.getTiddler(title);
if(tiddler) {
response.writeHead(200, {"Content-Type": tiddler.fields.type || "text/vnd.tiddlywiki"});
response.end(self.wiki.getTiddlerText(title),($tw.config.contentTypeInfo[tiddler.fields.type] || {encoding: "utf8"}).encoding);
} else {
response.writeHead(404);
response.end();
}
break;
}
return self.renderRoute(title,route);
}
});
});