From e7bf5952d0729b37e677168b6e8fbd1ce58ed1a2 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 10 Aug 2014 17:28:37 +0200 Subject: First version --- point/data/html.js | 86 ++++++++++++++++++++++++++++++ point/data/markdown.js | 61 ++++++++++++++++++++++ point/data/pdf.js | 130 ++++++++++++++++++++++++++++++++++++++++++++++ point/data/renderSlide.js | 127 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 404 insertions(+) create mode 100644 point/data/html.js create mode 100644 point/data/markdown.js create mode 100644 point/data/pdf.js create mode 100644 point/data/renderSlide.js (limited to 'point/data') diff --git a/point/data/html.js b/point/data/html.js new file mode 100644 index 0000000..66448cc --- /dev/null +++ b/point/data/html.js @@ -0,0 +1,86 @@ +/* + * This file is part of "What's The Point" + * Copyright (C) 2014 Pacien TRAN-GIRARD + * + * "What's The Point" is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * "What's The Point" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +define(function () { + + var xml = { + + init: function () { + return; + }, + + getProp: function (object, dom, property, method) { + var elements = dom.getElementsByTagName(property); + if (elements.length > 0) { + object[property] = method(elements[0]); + } + return object; + }, + + getInnerText: function (object, dom, property) { + return this.getProp(object, dom, property, function (element) { + return element.textContent; + }); + }, + + getInnerHtml: function (object, dom, property) { + return this.getProp(object, dom, property, function (element) { + if (window.XMLSerializer !== undefined) { + return (new window.XMLSerializer()).serializeToString(element); + } else if (element.xml !== undefined) { + return element.xml; + } + return element.innerHTML; + }); + }, + + parseSlide: function (domSlide) { + var slide = {}; + + ["type", "title", "subtitle"].forEach(function (tag) { + xml.getInnerText(slide, domSlide, tag); + }); + + ["content", "notes"].forEach(function (tag) { + xml.getInnerHtml(slide, domSlide, tag); + }); + + return slide; + }, + + parseSlides: function (domSlides) { + var slides = []; + + for (var i = 0; i < domSlides.length; i++) { + slides.push(this.parseSlide(domSlides[i])); + } + + return slides; + }, + + parse: function (rawData, callback) { + var dom = new DOMParser().parseFromString(rawData, "text/xml"); + var domSlides = dom.getElementsByTagName("slide"); + + callback(this.parseSlides(domSlides)); + }, + }; + + return xml; + +}); diff --git a/point/data/markdown.js b/point/data/markdown.js new file mode 100644 index 0000000..38a564d --- /dev/null +++ b/point/data/markdown.js @@ -0,0 +1,61 @@ +/* + * This file is part of "What's The Point" + * Copyright (C) 2014 Pacien TRAN-GIRARD + * + * "What's The Point" is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * "What's The Point" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +requirejs.config({ + shim: { + "markdownjs": { + exports: "markdown", + }, + }, + paths: { + "markdownjs": "libs/markdownjs/markdown.min", + }, +}); + +define(["js-yaml", "markdownjs"], function (jsyaml, markdownjs) { + + var markdown = { + + init: function () { + return; + }, + + renderSlide: function (slide) { + if (slide.content !== undefined) { + slide.content = markdownjs.toHTML(slide.content); + } + if (slide.notes !== undefined) { + slide.notes = markdownjs.toHTML(slide.notes); + } + return slide; + }, + + parse: function (rawData, callback) { + var slides = []; + + jsyaml.safeLoadAll(rawData, function (slide) { + slides.push(markdown.renderSlide(slide)); + }); + + callback(slides); + }, + }; + + return markdown; + +}); diff --git a/point/data/pdf.js b/point/data/pdf.js new file mode 100644 index 0000000..f789bdf --- /dev/null +++ b/point/data/pdf.js @@ -0,0 +1,130 @@ +/* + * This file is part of "What's The Point" + * Copyright (C) 2014 Pacien TRAN-GIRARD + * + * "What's The Point" is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * "What's The Point" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +requirejs.config({ + shim: { + "pdfjs": { + exports: "PDFJS", + }, + }, + paths: { + "pdfjs": "libs/pdfjs/pdf", + }, +}); + +define(["pdfjs"], function (pdfjs) { + + var pdf = { + + init: function (settings) { + this.fileUrl = settings.data.file; + this.width = settings.dimension.width; + this.height = settings.dimension.height; + + this.slides = []; + + pdfjs.disableRange = true; + pdfjs.workerSrc = "point/libs/pdfjs/pdf.worker.js"; + }, + + makeCanvas: function () { + var canvas = document.createElement("canvas"); + canvas.width = pdf.width; + canvas.height = pdf.height; + return canvas; + }, + + clearOutline: function (context, slideWidth, slideHeight, offsetX, offsetY) { + context.clearRect(0, 0, offsetX, pdf.height); + context.clearRect(offsetX + slideWidth, 0, offsetX, pdf.height); + context.clearRect(0, 0, pdf.width, offsetY); + context.clearRect(0, offsetY + slideHeight, pdf.width, offsetY); + return context; + }, + + renderSlide: function (pdfDocument, slideIndex, callback) { + pdfDocument.getPage(slideIndex).then(function (pdfPage) { + + var viewport = pdfPage.getViewport(1); + var slideWidth = viewport.width / 2; + var slideHeight = viewport.height; + + var widthRatio = pdf.width / slideWidth; + var heightRatio = pdf.height / slideHeight; + var scale = Math.min(widthRatio, heightRatio); + slideWidth *= scale; + slideHeight *= scale; + + var offsetX = (pdf.width - slideWidth) / 2; + var offsetY = (pdf.height - slideHeight) / 2; + + var contentViewport = new pdfjs.PageViewport(pdfPage.view, scale, 0, offsetX, offsetY); + var notesViewport = new pdfjs.PageViewport(pdfPage.view, scale, 0, -slideWidth + offsetX, offsetY); + + var contentCanvas = pdf.makeCanvas(); + var contentContext = contentCanvas.getContext("2d"); + + var notesCanvas = pdf.makeCanvas(); + var notesContext = notesCanvas.getContext("2d"); + + var contentRenderContext = { + canvasContext: contentContext, + viewport: contentViewport, + }; + + var notesRenderContext = { + canvasContext: notesContext, + viewport: notesViewport, + }; + + pdfPage.render(contentRenderContext).then(function () { + pdf.clearOutline(contentContext, slideWidth, slideHeight, offsetX, offsetY); + + pdfPage.render(notesRenderContext).then(function () { + pdf.clearOutline(notesContext, slideWidth, slideHeight, offsetX, offsetY); + + pdf.registerSlide(contentCanvas, notesCanvas, pdfDocument, slideIndex, callback); + + }); + }); + }); + }, + + registerSlide: function (contentCanvas, notesCanvas, pdfDocument, slideIndex, callback) { + pdf.slides[slideIndex - 1] = { + content: contentCanvas, + notes: notesCanvas, + }; + + if (slideIndex < pdfDocument.pdfInfo.numPages) { + pdf.renderSlide(pdfDocument, slideIndex + 1, callback); + } else { + callback(pdf.slides); + } + }, + + parse: function (rawData, callback) { + pdfjs.getDocument(require.toUrl(this.fileUrl)).then(function (pdfDocument) { + pdf.renderSlide(pdfDocument, 1, callback); + }); + }, + }; + + return pdf; + +}); diff --git a/point/data/renderSlide.js b/point/data/renderSlide.js new file mode 100644 index 0000000..e149f41 --- /dev/null +++ b/point/data/renderSlide.js @@ -0,0 +1,127 @@ +/* + * This file is part of "What's The Point" + * Copyright (C) 2014 Pacien TRAN-GIRARD + * + * "What's The Point" is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * "What's The Point" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +define(function () { + + var renderSlide = { + + init: function () { + this.lastTitle = undefined; + }, + + appendNode: function (parent, tag, content, appendMethod) { + var node = document.createElement(tag); + appendMethod(node, content); + parent.appendChild(node); + return parent; + }, + + appendHtmlNode: function (parent, tag, htmlContent) { + return this.appendNode(parent, tag, htmlContent, function (parent, htmlContent) { + parent.innerHTML = htmlContent; + }); + }, + + appendObjectNode: function (parent, tag, node) { + return this.appendNode(parent, tag, node, function (parent, node) { + parent.appendChild(node); + }); + }, + + appendTextNode: function (parent, tag, text) { + return this.appendObjectNode(parent, tag, document.createTextNode(text)); + }, + + appendContentNode: function (parent, tag, content) { + if (typeof (content) === "string") { + this.appendHtmlNode(parent, tag, content); + } else { + this.appendObjectNode(parent, tag, content); + } + }, + + renderTitle: function (slideData) { + var title = slideData.title; + var subtitle = slideData.subtitle; + + if (title !== undefined) { + this.lastTitle = title; + } else { + if (subtitle !== undefined) { + title = this.lastTitle; + } else { + this.lastTitle = undefined; + } + } + + var titleNode; + if (title !== undefined || subtitle !== undefined) { + titleNode = document.createElement("s-title"); + } else { + titleNode = document.createDocumentFragment("s-title"); + } + + if (title !== undefined) { + this.appendTextNode(titleNode, "h1", title); + } + + if (subtitle !== undefined) { + this.appendTextNode(titleNode, "h2", subtitle); + } + + return titleNode; + }, + + renderBody: function (slideData) { + var bodyNode = document.createDocumentFragment(); + //var bodyNode = document.createElement("s-body"); + + if (slideData.content !== undefined) { + this.appendContentNode(bodyNode, "s-content", slideData.content); + } + + var notes = slideData.notes === undefined ? "" : slideData.notes; + this.appendContentNode(bodyNode, "s-notes", notes); + + return bodyNode; + }, + + renderSlide: function (slideData, index) { + var slide = document.createElement("p-slide"); + slide.setAttribute("id", index + 1); + slide.appendChild(this.renderTitle(slideData)); + slide.appendChild(this.renderBody(slideData)); + return slide; + }, + + renderSlides: function (slidesData) { + var slides = document.createDocumentFragment(); + for (var i = 0; i < slidesData.length; i++) { + slides.appendChild(this.renderSlide(slidesData[i], i)); + } + return slides; + }, + + render: function (slidesData, callback) { + callback(this.renderSlides(slidesData)); + }, + }; + + return renderSlide; + +}); -- cgit v1.2.3