diff --git a/core/modules/parsers/imageparser.js b/core/modules/parsers/imageparser.js new file mode 100644 index 000000000..a82d58de3 --- /dev/null +++ b/core/modules/parsers/imageparser.js @@ -0,0 +1,43 @@ +/*\ +title: $:/core/modules/parsers/imageparser.js +type: application/javascript +module-type: newparser + +The image parser parses an image into an embeddable HTML element + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +var ImageParser = function(type,text,options) { + var element = "img", + src; + if(type === "application/pdf" || type === ".pdf") { + src = "data:application/pdf;base64," + text; + element = "embed"; + } else if(type === "image/svg+xml" || type === ".svg") { + src = "data:image/svg+xml," + encodeURIComponent(text); + } else { + src = "data:" + type + ";base64," + text; + } + this.tree = [{ + type: "element", + tag: element, + attributes: { + "src": {type: "string", value: src} + } + }]; +}; + +exports["image/svg+xml"] = ImageParser; +exports["image/jpg"] = ImageParser; +exports["image/jpeg"] = ImageParser; +exports["image/png"] = ImageParser; +exports["image/gif"] = ImageParser; +exports["application/pdf"] = ImageParser; + +})(); + diff --git a/core/modules/parsers/textparser.js b/core/modules/parsers/textparser.js new file mode 100644 index 000000000..3179cc406 --- /dev/null +++ b/core/modules/parsers/textparser.js @@ -0,0 +1,31 @@ +/*\ +title: $:/core/modules/parsers/textparser.js +type: application/javascript +module-type: newparser + +The plain text parser processes blocks of source text into a degenerate parse tree consisting of a single text node + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +var TextParser = function(type,text,options) { + this.tree = [{ + type: "element", + tag: "pre", + children: [{ + type: "text", + text: text + }] + }]; +}; + +exports["text/plain"] = TextParser; +exports["text/html"] = TextParser; +exports["application/javascript"] = TextParser; + +})(); + diff --git a/core/modules/parsers/wikiparser/wikiparser.js b/core/modules/parsers/wikiparser/wikiparser.js index 374ad861b..1cc465dd1 100644 --- a/core/modules/parsers/wikiparser/wikiparser.js +++ b/core/modules/parsers/wikiparser/wikiparser.js @@ -1,7 +1,7 @@ /*\ title: $:/core/modules/parsers/wikiparser/wikiparser.js type: application/javascript -module-type: global +module-type: newparser The wiki text parser processes blocks of source text into a parse tree. @@ -348,7 +348,7 @@ WikiParser.prototype.amendRules = function(type,names) { processRuleArray(this.inlineRules); } -exports.WikiParser = WikiParser; +exports["text/vnd.tiddlywiki"] = WikiParser; })(); diff --git a/core/modules/wiki.js b/core/modules/wiki.js index e6b8b3d93..7c9244596 100644 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -366,9 +366,21 @@ Options include: */ exports.new_parseText = function(type,text,options) { options = options || {}; - return new $tw.WikiParser(type,text,{ + // Select a parser + var Parser = this.newparsers[type]; + if(!Parser && $tw.config.fileExtensionInfo[type]) { + Parser = this.newparsers[$tw.config.fileExtensionInfo[type].type]; + } + if(!Parser) { + Parser = this.newparsers[options.defaultType || "text/vnd.tiddlywiki"]; + } + if(!Parser) { + return null; + } + // Return the parser instance + return new Parser(type,text,{ parseAsInline: options.parseAsInline, - wiki: this.wiki + wiki: this }); }; @@ -411,6 +423,16 @@ exports.new_renderTiddler = function(outputType,title) { }; exports.initParsers = function(moduleType) { + // Install the new parser modules + $tw.wiki.newparsers = {}; + var self = this; + $tw.modules.forEachModuleOfType("newparser",function(title,module) { + for(var f in module) { + if($tw.utils.hop(module,f)) { + $tw.wiki.newparsers[f] = module[f]; // Store the parser class + } + } + }); // Install the parser modules moduleType = moduleType || "parser"; $tw.wiki.parsers = {};