From 74def9e08070c477778b445297c84d6f1fe64433 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Thu, 8 Dec 2016 16:42:53 +0000 Subject: [PATCH] Separate the PDF parser from the image parser It was a bit of a hack, and made it harder to customise PDF presentation --- core/modules/parsers/imageparser.js | 9 +------- core/modules/parsers/pdfparser.js | 33 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 core/modules/parsers/pdfparser.js diff --git a/core/modules/parsers/imageparser.js b/core/modules/parsers/imageparser.js index 709c50ad9..b1cadd296 100644 --- a/core/modules/parsers/imageparser.js +++ b/core/modules/parsers/imageparser.js @@ -21,14 +21,8 @@ var ImageParser = function(type,text,options) { src; if(options._canonical_uri) { element.attributes.src = {type: "string", value: options._canonical_uri}; - if(type === "application/pdf" || type === ".pdf") { - element.tag = "embed"; - } } else if(text) { - if(type === "application/pdf" || type === ".pdf") { - element.attributes.src = {type: "string", value: "data:application/pdf;base64," + text}; - element.tag = "embed"; - } else if(type === "image/svg+xml" || type === ".svg") { + if(type === "image/svg+xml" || type === ".svg") { element.attributes.src = {type: "string", value: "data:image/svg+xml," + encodeURIComponent(text)}; } else { element.attributes.src = {type: "string", value: "data:" + type + ";base64," + text}; @@ -42,7 +36,6 @@ exports["image/jpg"] = ImageParser; exports["image/jpeg"] = ImageParser; exports["image/png"] = ImageParser; exports["image/gif"] = ImageParser; -exports["application/pdf"] = ImageParser; exports["image/x-icon"] = ImageParser; })(); diff --git a/core/modules/parsers/pdfparser.js b/core/modules/parsers/pdfparser.js new file mode 100644 index 000000000..95d74ef4b --- /dev/null +++ b/core/modules/parsers/pdfparser.js @@ -0,0 +1,33 @@ +/*\ +title: $:/core/modules/parsers/pdfparser.js +type: application/javascript +module-type: parser + +The PDF parser embeds a PDF viewer + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +var ImageParser = function(type,text,options) { + var element = { + type: "element", + tag: "embed", + attributes: {} + }, + src; + if(options._canonical_uri) { + element.attributes.src = {type: "string", value: options._canonical_uri}; + } else if(text) { + element.attributes.src = {type: "string", value: "data:application/pdf;base64," + text}; + } + this.tree = [element]; +}; + +exports["application/pdf"] = ImageParser; + +})(); +