1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-05 09:36:18 +00:00

Use variable definition in the job/sitemap/route text instead of from var-* fields

This commit is contained in:
jeremy@jermolene.com 2021-04-10 17:42:35 +01:00
parent 9c62bd8030
commit 184083ad1a
7 changed files with 107 additions and 55 deletions

View File

@ -60,17 +60,15 @@ PublishingJob.prototype.publish = function(callback) {
if(this.jobTiddler && this.jobTiddler.fields.enabled === "yes") {
// Get the list of tiddlers to be exported, defaulting to all non-system tiddlers
this.exportList = this.publisherHandler.wiki.filterTiddlers(this.jobTiddler.fields["export-filter"] || "[!is[system]]");
// Get the job variables
this.jobVariables = this.jobTiddler.getFieldStrings({prefix: "var-"});
// Get publisher
this.publisher = this.getPublisher(this.jobTiddler.fields.publisher);
if(this.publisher) {
// Get the sitemap
this.sitemap = new $tw.Sitemap({
this.sitemap = new $tw.Sitemap(this.jobTiddler.fields.sitemap,{
wiki: this.publisherHandler.wiki,
variables: this.publishVariables
});
this.sitemap.load(this.jobTiddler.fields.sitemap);
this.sitemap.load();
// Get the output operations
this.operations = this.sitemap.getAllFileDetails(this.exportList);
// Display the progress modal

View File

@ -58,34 +58,8 @@ function Server(options) {
// console.log("Loading server route " + title);
self.addAuthenticator(authenticatorDefinition.AuthenticatorClass);
});
// Load route handlers from sitemap if present, or just load all route modules
if(this.variables.sitemap) {
this.sitemap = new $tw.Sitemap({
wiki: this.wiki,
variables: {}
});
this.sitemap.load(this.variables.sitemap);
$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) {
self.addRoute(routeDefinition);
});
}
// Load route handlers
this.addRouteHandlers();
// Initialise the http vs https
this.listenOptions = null;
this.protocol = "http";
@ -135,6 +109,38 @@ Server.prototype.addAuthenticator = function(AuthenticatorClass) {
}
};
Server.prototype.addRouteHandlers = function() {
var self = this;
// Load route handlers from sitemap if present, or just load all route modules
if(this.variables.sitemap) {
this.sitemap = new $tw.Sitemap(this.variables.sitemap,{
wiki: this.wiki,
variables: {}
});
this.sitemap.load();
$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) {
self.addRoute(routeDefinition);
});
}
};
Server.prototype.findMatchingRoute = function(request,state,options) {
options = options || {};
for(var t=0; t<this.routes.length; t++) {

View File

@ -12,8 +12,9 @@ Sitemaps are used for static publishing and web serving
/*global $tw: false */
"use strict";
function Sitemap(options) {
function Sitemap(sitemapTitle,options) {
options = options || {};
this.sitemapTitle = sitemapTitle;
this.wiki = options.wiki;
this.routes = [];
this.variables = $tw.utils.extend({},options.variables);
@ -22,11 +23,8 @@ function Sitemap(options) {
Sitemap.prototype.load = function(sitemapTitle) {
var self = this;
// Get the sitemap
var sitemapTiddler = this.wiki.getTiddler(sitemapTitle);
var sitemapTiddler = this.wiki.getTiddler(this.sitemapTitle);
if(sitemapTiddler) {
// Get the sitemap variables
$tw.utils.extend(this.variables,sitemapTiddler.getFieldStrings({prefix: "var-"}));
console.log("sitemap variables",this.variables)
// Collect each route
$tw.utils.each(sitemapTiddler.fields.list,function(routeTitle) {
var routeTiddler = self.wiki.getTiddler(routeTitle);
@ -34,6 +32,7 @@ Sitemap.prototype.load = function(sitemapTitle) {
// Convert the path into a regexp and an array of {field:,function:} for each capture group
var regexpurgatedParameterisedPath = self.regexpurgateParameterisedPath(routeTiddler.fields["route-path"]);
self.routes.push({
title: routeTitle,
params: routeTiddler.getFieldStrings({prefix: "route-"}),
variables: routeTiddler.getFieldStrings({prefix: "var-"}),
regexp: regexpurgatedParameterisedPath.regexp,
@ -42,7 +41,6 @@ Sitemap.prototype.load = function(sitemapTitle) {
}
});
}
console.log("routes",self.routes)
};
Sitemap.prototype.renderRoute = function(title,route) {
@ -57,12 +55,45 @@ Sitemap.prototype.renderRoute = function(title,route) {
};
break;
case "render":
var text = this.wiki.renderTiddler("text/plain",route.params.template,{
variables: $tw.utils.extend({},this.variables,route.variables,{currentTiddler: title})
});
var parser = {
tree: [
{
"type": "importvariables",
"attributes": {
"tiddler": {
"name": "tiddler",
"type": "string",
"value": this.sitemapTitle,
}
},
"tag": "$importvariables",
"isBlock": false,
"children": [
{
"type": "importvariables",
"attributes": {
"tiddler": {
"name": "tiddler",
"type": "string",
"value": route.title,
}
},
"tag": "$importvariables",
"isBlock": false,
"children": this.wiki.parseTiddler(route.params.template,{parseAsInline: true}).tree
}
]
}
]
},
widgetNode = this.wiki.makeWidget(parser,{
variables: $tw.utils.extend({},this.variables,{currentTiddler: title})
}),
container = $tw.fakeDocument.createElement("div");
widgetNode.render(container,null);
return {
path: this.resolveParameterisedPath(route.params.path,title),
text: text,
text: container.textContent,
type: route.params["output-type"] || "text/html"
};
break;
@ -112,6 +143,16 @@ Sitemap.prototype.getServerRoutes = function() {
}
}
})
// Check that the tiddler passes the route filter
if(route.params["tiddler-filter"]) {
if(!title) {
return null;
}
var routeTiddlers = self.wiki.filterTiddlers(route.params["tiddler-filter"],null,self.wiki.makeTiddlerIterator([title]));
if(routeTiddlers.indexOf(title) === -1) {
return null;
}
}
// Return the rendering or raw tiddler
return self.renderRoute(title,route);
}

View File

@ -40,10 +40,8 @@ ImportVariablesWidget.prototype.execute = function(tiddlerList) {
var widgetPointer = this;
// Got to flush all the accumulated variables
this.variables = new this.variablesConstructor();
// Get our parameters
this.filter = this.getAttribute("filter");
// Compute the filter
this.tiddlerList = tiddlerList || this.wiki.filterTiddlers(this.filter,this);
this.tiddlerList = tiddlerList || this.getTiddlerList();
// Accumulate the <$set> widgets from each tiddler
$tw.utils.each(this.tiddlerList,function(title) {
var parser = widgetPointer.wiki.parseTiddler(title);
@ -86,7 +84,6 @@ ImportVariablesWidget.prototype.execute = function(tiddlerList) {
}
}
});
if (widgetPointer != this) {
widgetPointer.parseTreeNode.children = this.parseTreeNode.children;
} else {
@ -94,13 +91,21 @@ ImportVariablesWidget.prototype.execute = function(tiddlerList) {
}
};
ImportVariablesWidget.prototype.getTiddlerList = function() {
var filter = this.getAttribute("filter"),
title = this.getAttribute("tiddler");
return (filter && this.wiki.filterTiddlers(filter,this)) || (title && [title]) || [];
};
/*
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
*/
ImportVariablesWidget.prototype.refresh = function(changedTiddlers) {
// Recompute our attributes and the filter list
var changedAttributes = this.computeAttributes(),
tiddlerList = this.wiki.filterTiddlers(this.getAttribute("filter"),this);
filter = this.getAttribute("filter"),
title = this.getAttribute("tiddler"),
tiddlerList = this.getTiddlerList();
// Refresh if the filter has changed, or the list of tiddlers has changed, or any of the tiddlers in the list has changed
function haveListedTiddlersChanged() {
var changed = false;
@ -111,7 +116,7 @@ ImportVariablesWidget.prototype.refresh = function(changedTiddlers) {
});
return changed;
}
if(changedAttributes.filter || !$tw.utils.isArrayEqual(this.tiddlerList,tiddlerList) || haveListedTiddlersChanged()) {
if(changedAttributes.filter || changedAttributes.tiddler || !$tw.utils.isArrayEqual(this.tiddlerList,tiddlerList) || haveListedTiddlersChanged()) {
// Compute the filter
this.removeChildDomNodes();
this.execute(tiddlerList);

View File

@ -3,6 +3,7 @@ caption: Static site map
description: The original TiddlyWiki 5 static file layout
tags: $:/tags/SiteMap
list: $:/core/routes/StaticSite/Index $:/core/routes/StaticSite/HTML $:/core/routes/StaticSite/Images $:/core/routes/StaticSite/Styles
var-tv-wikilink-template-filter: [slugify[]addsuffix[.html]]
var-tv-image-template-filter: [slugify[]addprefix[../images/]addsuffix<extension>]
var-tv-wikilink-template: $title_slugify$.html
\define tv-wikilink-template()
$title_slugify$.html
\end

View File

@ -33,7 +33,7 @@ A ''publishing job'' describes a self-contained publishing operation. Jobs are d
* ''publisher'' -- the name of the publisher module to be used
* ''&lt;publisher-name&gt;-&lt;parameter-name&gt;'' -- parameters required by the publisher module
* ''sitemap'' -- title of the site map tiddler to be used
* ''var-&lt;variable-name&gt;'' -- custom variables to be provided to the output templates
* ''text'' -- optional variable/macro definitions as sequential `\define` pragmas or nested `<$set>` widgets (the same format as global macros)
A ''site map'' describes the layout and types of files in a publishing job. Site maps are defined as configuration tiddlers with the following fields:
@ -42,7 +42,7 @@ A ''site map'' describes the layout and types of files in a publishing job. Site
* ''description'' -- longer human readable description for the sitemap
* ''tags'' -- `$:/tags/SiteMap`
* ''list'' -- list of titles of routes making up this sitemap
* ''var-&lt;variable-name&gt;'' -- custom variables to be provided to the output templates
* ''text'' -- optional variable/macro definitions as sequential `\define` pragmas or nested `<$set>` widgets (the same format as global macros)
A ''route'' describes how a group of one or more files is to be created during the export. Routes are defined as configuration tiddlers with the following fields:
@ -55,7 +55,7 @@ A ''route'' describes how a group of one or more files is to be created during t
* ''route-type'' which can be set to "raw" or "render":
** ''"raw"'' indicates that the raw tiddler is to be saved, without any rendering
** ''"render"'' indicates that the tiddler is to be rendered through a specified template
* ''var-&lt;variable-name&gt;'' -- custom variables to be provided to the output template
* ''text'' -- optional variable/macro definitions as sequential `\define` pragmas or nested `<$set>` widgets (the same format as global macros)
The route tiddler filter is passed the tiddlers resulting from the job export filter. In order to respect the restrictions of the job export filter, route filters must be carefully constructed to ensure they pull their titles from the incoming list.

View File

@ -1,6 +1,6 @@
caption: importvariables
created: 20140612142500000
modified: 20180928150043777
modified: 20210410110618869
tags: Widgets
title: ImportVariablesWidget
type: text/vnd.tiddlywiki
@ -21,6 +21,7 @@ The content of the importvariables widget is the scope within which the imported
|!Attribute |!Description |
|filter |[[Tiddler filter|Filters]] defining the tiddlers from which macro definitions will be imported |
|tiddler |<<.from-version "5.1.24">> Optional title of a single tiddler from which macro definitions will be imported (only used if the ''filter'' attribute is missing or blank) |
! Global Macros