mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-27 03:57:21 +00:00
Extend new parser mechanism to determine parser based on content type
And add an image parser and a plain text parser
This commit is contained in:
parent
c078edf468
commit
d6e531e87c
43
core/modules/parsers/imageparser.js
Normal file
43
core/modules/parsers/imageparser.js
Normal file
@ -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;
|
||||
|
||||
})();
|
||||
|
31
core/modules/parsers/textparser.js
Normal file
31
core/modules/parsers/textparser.js
Normal file
@ -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;
|
||||
|
||||
})();
|
||||
|
@ -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;
|
||||
|
||||
})();
|
||||
|
||||
|
@ -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 = {};
|
||||
|
Loading…
Reference in New Issue
Block a user