2018-05-18 16:53:07 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/parsers/binaryparser.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: parser
|
|
|
|
|
2020-01-31 09:13:28 +00:00
|
|
|
The binary parser parses a binary tiddler into a warning message and download link
|
2018-05-18 16:53:07 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var BINARY_WARNING_MESSAGE = "$:/core/ui/BinaryWarning";
|
2020-01-31 09:13:28 +00:00
|
|
|
var EXPORT_BUTTON_IMAGE = "$:/core/images/export-button";
|
2018-05-18 16:53:07 +00:00
|
|
|
|
|
|
|
var BinaryParser = function(type,text,options) {
|
2020-01-31 09:13:28 +00:00
|
|
|
// Transclude the binary data tiddler warning message
|
|
|
|
var warn = {
|
|
|
|
type: "element",
|
|
|
|
tag: "p",
|
|
|
|
children: [{
|
|
|
|
type: "transclude",
|
|
|
|
attributes: {
|
|
|
|
tiddler: {type: "string", value: BINARY_WARNING_MESSAGE}
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
// Create download link based on binary tiddler title
|
|
|
|
var link = {
|
|
|
|
type: "element",
|
|
|
|
tag: "a",
|
2018-05-18 16:53:07 +00:00
|
|
|
attributes: {
|
2020-01-31 09:13:28 +00:00
|
|
|
title: {type: "indirect", textReference: "!!title"},
|
|
|
|
download: {type: "indirect", textReference: "!!title"}
|
|
|
|
},
|
|
|
|
children: [{
|
|
|
|
type: "transclude",
|
|
|
|
attributes: {
|
|
|
|
tiddler: {type: "string", value: EXPORT_BUTTON_IMAGE}
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
// Set the link href to external or internal data URI
|
|
|
|
if(options._canonical_uri) {
|
|
|
|
link.attributes.href = {
|
|
|
|
type: "string",
|
|
|
|
value: options._canonical_uri
|
|
|
|
};
|
|
|
|
} else if(text) {
|
|
|
|
link.attributes.href = {
|
|
|
|
type: "string",
|
|
|
|
value: "data:" + type + ";base64," + text
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// Combine warning message and download link in a div
|
|
|
|
var element = {
|
|
|
|
type: "element",
|
|
|
|
tag: "div",
|
|
|
|
attributes: {
|
|
|
|
class: {type: "string", value: "tc-binary-warning"}
|
|
|
|
},
|
|
|
|
children: [warn, link]
|
|
|
|
}
|
|
|
|
this.tree = [element];
|
2018-05-18 16:53:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports["application/octet-stream"] = BinaryParser;
|
|
|
|
|
|
|
|
})();
|
|
|
|
|