1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-10-01 00:10:45 +00:00

Replace the bitmap and SVG parsers with a single image parser

This commit is contained in:
Jeremy Ruston 2012-01-24 18:08:59 +00:00
parent 2cd0e681ea
commit bd5de61156
3 changed files with 51 additions and 90 deletions

View File

@ -1,45 +0,0 @@
/*\
title: js/BitmapParser.js
Compiles bitmap images into JavaScript functions that render them in HTML
\*/
(function(){
/*jslint node: true */
"use strict";
var utils = require("./Utils.js");
var BitmapRenderer = function(handlerCode) {
/*jslint evil: true */
this.handler = eval(handlerCode);
};
BitmapRenderer.prototype.render = function(tiddler,store) {
return this.handler(tiddler,store,utils);
};
// The parse tree is degenerate
var BitmapParseTree = function() {
this.dependencies = [];
};
BitmapParseTree.prototype.compile = function(type) {
if(type === "text/html") {
return new BitmapRenderer("(function (tiddler,store,utils) {return '<img src=\"data:' + tiddler.type + ';base64,' + tiddler.text + '\">';})");
} else {
return null;
}
};
var BitmapParser = function() {
};
BitmapParser.prototype.parse = function() {
return new BitmapParseTree();
};
exports.BitmapParser = BitmapParser;
})();

51
js/ImageParser.js Normal file
View File

@ -0,0 +1,51 @@
/*\
title: js/ImageParser.js
Compiles images into JavaScript functions that render them in HTML
\*/
(function(){
/*jslint node: true */
"use strict";
var utils = require("./Utils.js");
var ImageRenderer = function(handlerCode) {
/*jslint evil: true */
this.handler = eval(handlerCode);
};
ImageRenderer.prototype.render = function(tiddler,store) {
return this.handler(tiddler,store,utils);
};
// The parse tree is degenerate
var ImageParseTree = function(type,text) {
this.type = type;
this.text = text;
this.dependencies = [];
};
ImageParseTree.prototype.compile = function(type) {
if(type === "text/html") {
if(this.type === "image/svg+xml") {
return new ImageRenderer("(function (tiddler,store,utils) {return '<img src=\"data:" + this.type + "," + utils.stringify(encodeURIComponent(this.text)) + "\">';})");
} else {
return new ImageRenderer("(function (tiddler,store,utils) {return '<img src=\"data:" + this.type + ";base64," + this.text + "\">';})");
}
} else {
return null;
}
};
var ImageParser = function() {
};
ImageParser.prototype.parse = function(type,text) {
return new ImageParseTree(type,text);
};
exports.ImageParser = ImageParser;
})();

View File

@ -1,45 +0,0 @@
/*\
title: js/SVGParser.js
Compiles SVG images into JavaScript functions that render them in HTML
\*/
(function(){
/*jslint node: true */
"use strict";
var utils = require("./Utils.js");
var SVGRenderer = function(handlerCode) {
/*jslint evil: true */
this.handler = eval(handlerCode);
};
SVGRenderer.prototype.render = function(tiddler,store) {
return this.handler(tiddler,store,utils);
};
// The parse tree is degenerate
var SVGParseTree = function() {
this.dependencies = [];
};
SVGParseTree.prototype.compile = function(type) {
if(type === "text/html") {
return new SVGRenderer("(function (tiddler,store,utils) {return '<img src=\"data:' + tiddler.type + ',' + encodeURIComponent(tiddler.text) + '\">';})");
} else {
return null;
}
};
var SVGParser = function() {
};
SVGParser.prototype.parse = function() {
return new SVGParseTree();
};
exports.SVGParser = SVGParser;
})();