1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Get rid of the wikivocabulary object

This commit is contained in:
Jeremy Ruston 2012-12-26 22:02:59 +00:00
parent cbc08fcc1c
commit c703fb1267
8 changed files with 49 additions and 84 deletions

View File

@ -444,6 +444,25 @@ $tw.modules.applyMethods = function(moduleType,targetObject) {
return targetObject;
};
/*
Return an array of classes created from the modules of a specified type. Each module should export the properties to be added to those of the optional base class
*/
$tw.modules.createClassesFromModules = function(moduleType,subType,baseClass) {
var classes = {};
$tw.modules.forEachModuleOfType(moduleType,function(title,moduleExports) {
if(!subType || moduleExports.types[subType]) {
var newClass = function() {};
if(baseClass) {
newClass.prototype = new baseClass();
newClass.prototype.constructor = baseClass;
}
$tw.utils.extend(newClass.prototype,moduleExports);
classes[moduleExports.name] = newClass;
}
});
return classes;
};
/////////////////////////// Barebones tiddler object
/*

View File

@ -27,13 +27,6 @@ Command.prototype.execute = function() {
if(this.params.length < 2) {
return "Missing filename";
}
if(!this.commander.wiki.vocabulary) {
this.commander.wiki.new_initParsers();
}
var self = this,
fs = require("fs"),
path = require("path"),

View File

@ -26,9 +26,18 @@ Attributes are stored as hashmaps of the following objects:
/*global $tw: false */
"use strict";
var WikiParser = function(vocabulary,type,text,options) {
var WikiParser = function(type,text,options) {
this.wiki = options.wiki;
this.vocabulary = vocabulary;
// Initialise the classes if we don't have them already
if(!this.pragmaRuleClasses) {
WikiParser.prototype.pragmaRuleClasses = $tw.modules.createClassesFromModules("wikirule","pragma",$tw.WikiRuleBase);
}
if(!this.blockRuleClasses) {
WikiParser.prototype.blockRuleClasses = $tw.modules.createClassesFromModules("wikirule","block",$tw.WikiRuleBase);
}
if(!this.inlineRuleClasses) {
WikiParser.prototype.inlineRuleClasses = $tw.modules.createClassesFromModules("wikirule","inline",$tw.WikiRuleBase);
}
// Save the parse text
this.type = type || "text/vnd.tiddlywiki";
this.source = text || "";
@ -38,10 +47,10 @@ var WikiParser = function(vocabulary,type,text,options) {
// Initialise the things that pragma rules can change
this.macroDefinitions = {}; // Hash map of macro definitions
// Instantiate the pragma parse rules
this.pragmaRules = this.instantiateRules(this.vocabulary.pragmaRuleClasses,"pragma",0);
this.pragmaRules = this.instantiateRules(this.pragmaRuleClasses,"pragma",0);
// Instantiate the parser block and inline rules
this.blockRules = this.instantiateRules(this.vocabulary.blockRuleClasses,"block",0);
this.inlineRules = this.instantiateRules(this.vocabulary.inlineRuleClasses,"inline",0);
this.blockRules = this.instantiateRules(this.blockRuleClasses,"block",0);
this.inlineRules = this.instantiateRules(this.inlineRuleClasses,"inline",0);
// Parse any pragmas
this.parsePragmas();
// Parse the text into inline runs or blocks

View File

@ -20,11 +20,15 @@ var WidgetRenderer = function(renderTree,renderContext,parseTreeNode) {
this.renderTree = renderTree;
this.renderContext = renderContext;
this.parseTreeNode = parseTreeNode;
// Widget classes
if(!this.widgetClasses) {
WidgetRenderer.prototype.widgetClasses = $tw.modules.createClassesFromModules("widget",null,$tw.WidgetBase);
}
// Compute our attributes
this.attributes = {};
this.computeAttributes();
// Create the widget object
var WidgetClass = this.renderTree.parser.vocabulary.widgetClasses[this.parseTreeNode.tag];
var WidgetClass = this.widgetClasses[this.parseTreeNode.tag];
if(WidgetClass) {
this.widget = new WidgetClass();
this.widget.init(this);

View File

@ -18,6 +18,11 @@ Create a render tree object for a parse tree
var WikiRenderTree = function(parser,options) {
this.parser = parser;
this.wiki = options.wiki;
// Hashmap of the renderer classes
if(!this.rendererClasses) {
WikiRenderTree.prototype.rendererClasses = $tw.modules.applyMethods("wikirenderer");
}
};
/*
@ -48,7 +53,7 @@ WikiRenderTree.prototype.createRenderers = function(renderContext,parseTreeNodes
Create a renderer node for a parse tree node
*/
WikiRenderTree.prototype.createRenderer = function(renderContext,parseTreeNode) {
var RenderNodeClass = this.parser.vocabulary.rendererClasses[parseTreeNode.type];
var RenderNodeClass = this.rendererClasses[parseTreeNode.type];
return new RenderNodeClass(this,renderContext,parseTreeNode);
};

View File

@ -13,7 +13,6 @@ Test the new parser
"use strict";
var testNewParser = function() {
$tw.wiki.new_initParsers();
var templateTitle = "$:/templates/NewPageTemplate";
var parser = $tw.wiki.new_parseTiddler(templateTitle);
console.log("parsetree after execution",parser);

View File

@ -356,11 +356,6 @@ exports.clearCache = function(title) {
}
};
exports.new_initParsers = function() {
// Create a default vocabulary
this.vocabulary = new $tw.WikiVocabulary({wiki: this});
};
/*
Parse a block of text of a specified MIME type
type: content type of text to be parsed
@ -370,7 +365,11 @@ Options include:
parseAsInline: if true, the text of the tiddler will be parsed as an inline run
*/
exports.new_parseText = function(type,text,options) {
return this.vocabulary.parseText(type,text,options);
options = options || {};
return new $tw.WikiParser(type,text,{
parseAsInline: options.parseAsInline,
wiki: this.wiki
});
};
/*

View File

@ -1,63 +0,0 @@
/*\
title: $:/core/modules/parsers/wikiparser/wikivocabulary.js
type: application/javascript
module-type: global
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var WikiVocabulary = function(options) {
this.wiki = options.wiki;
// Hashmaps of the various parse rule classes
this.pragmaRuleClasses = this.createClassesFromModules("wikirule","pragma",$tw.WikiRuleBase);
this.blockRuleClasses = this.createClassesFromModules("wikirule","block",$tw.WikiRuleBase);
this.inlineRuleClasses = this.createClassesFromModules("wikirule","inline",$tw.WikiRuleBase);
// Hashmap of the various renderer classes
this.rendererClasses = $tw.modules.applyMethods("wikirenderer");
// Hashmap of the available widgets
this.widgetClasses = this.createClassesFromModules("widget",null,$tw.WidgetBase);
};
/*
Return an array of classes created from the modules of a specified type. Each module should export the properties to be added to those of the optional base class
*/
WikiVocabulary.prototype.createClassesFromModules = function(moduleType,subType,baseClass) {
var classes = {};
$tw.modules.forEachModuleOfType(moduleType,function(title,moduleExports) {
if(!subType || moduleExports.types[subType]) {
var newClass = function() {};
if(baseClass) {
newClass.prototype = new baseClass();
newClass.prototype.constructor = baseClass;
}
$tw.utils.extend(newClass.prototype,moduleExports);
classes[moduleExports.name] = newClass;
}
});
return classes;
};
/*
Parse a block of text of a specified MIME type
type: content type of text to be parsed
text: text
options: see below
Options include:
parseAsInline: if true, the text of the tiddler will be parsed as an inline run
*/
WikiVocabulary.prototype.parseText = function(type,text,options) {
options = options || {};
return new $tw.WikiParser(this,type,text,{
parseAsInline: options.parseAsInline,
wiki: this.wiki
});
};
exports.WikiVocabulary = WikiVocabulary;
})();