From c703fb126743d8f9668c74b9363ac5bc0aefb3df Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Wed, 26 Dec 2012 22:02:59 +0000 Subject: [PATCH] Get rid of the wikivocabulary object --- core/boot.js | 19 ++++++ core/modules/commands/newsavetiddler.js | 7 --- core/modules/parsers/wikiparser/wikiparser.js | 19 ++++-- core/modules/rendertree/renderers/widget.js | 6 +- core/modules/rendertree/wikirendertree.js | 7 ++- core/modules/testnewwikiparser.js | 1 - core/modules/wiki.js | 11 ++-- core/modules/wikivocabulary.js | 63 ------------------- 8 files changed, 49 insertions(+), 84 deletions(-) delete mode 100644 core/modules/wikivocabulary.js diff --git a/core/boot.js b/core/boot.js index fc55de5e4..f2578570c 100644 --- a/core/boot.js +++ b/core/boot.js @@ -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 /* diff --git a/core/modules/commands/newsavetiddler.js b/core/modules/commands/newsavetiddler.js index 6a2fbde67..003a51a18 100644 --- a/core/modules/commands/newsavetiddler.js +++ b/core/modules/commands/newsavetiddler.js @@ -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"), diff --git a/core/modules/parsers/wikiparser/wikiparser.js b/core/modules/parsers/wikiparser/wikiparser.js index eae4c6c4e..374ad861b 100644 --- a/core/modules/parsers/wikiparser/wikiparser.js +++ b/core/modules/parsers/wikiparser/wikiparser.js @@ -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 diff --git a/core/modules/rendertree/renderers/widget.js b/core/modules/rendertree/renderers/widget.js index 7ab3f5284..f31b2a029 100644 --- a/core/modules/rendertree/renderers/widget.js +++ b/core/modules/rendertree/renderers/widget.js @@ -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); diff --git a/core/modules/rendertree/wikirendertree.js b/core/modules/rendertree/wikirendertree.js index ce33161f3..c9624379b 100644 --- a/core/modules/rendertree/wikirendertree.js +++ b/core/modules/rendertree/wikirendertree.js @@ -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); }; diff --git a/core/modules/testnewwikiparser.js b/core/modules/testnewwikiparser.js index a94db74cf..f82d25d51 100644 --- a/core/modules/testnewwikiparser.js +++ b/core/modules/testnewwikiparser.js @@ -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); diff --git a/core/modules/wiki.js b/core/modules/wiki.js index 5c22f8992..e6b8b3d93 100644 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -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 + }); }; /* diff --git a/core/modules/wikivocabulary.js b/core/modules/wikivocabulary.js deleted file mode 100644 index 84a53a19f..000000000 --- a/core/modules/wikivocabulary.js +++ /dev/null @@ -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; - -})(); -