From ef2e0329bad4b59b05e5be44b5e6aaae5c2c34b3 Mon Sep 17 00:00:00 2001 From: NikhilWanpal Date: Wed, 14 Feb 2018 11:27:56 +0530 Subject: [PATCH] Feature: generates all UMLs in the note in one go giving a performance benefit. --- render-plantuml/info.json | 4 +- render-plantuml/render-plantuml.qml | 73 +++++++++++++++++++---------- 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/render-plantuml/info.json b/render-plantuml/info.json index 3f12728..ed82ca6 100644 --- a/render-plantuml/info.json +++ b/render-plantuml/info.json @@ -3,7 +3,7 @@ "identifier": "render-plantuml", "script": "render-plantuml.qml", "authors": ["@nikhilw"], - "version": "0.0.2", + "version": "0.0.3", "minAppVersion": "17.05.7", - "description" : "This script renders any plantuml text embedded in a note with the language hint, into a uml diagram. It needs node.js (https://nodejs.org/en/download/), java (https://java.com/en/download/) and plantuml (http://plantuml.com/download) to be installed. Install node and java. download the plantuml jar and provide the full path in script settings." + "description" : "This script renders any plantuml text embedded in a note with the language hint, into a uml diagram.\n\nIt needs node.js (https://nodejs.org/en/download/), java (https://java.com/en/download/) and plantuml (http://plantuml.com/download) to be installed. Install node and java. download the plantuml jar and provide the full path in script settings.\n\nWARNING: As it needs to launch a synchronous java process to convert the diagrams to images, it adds a certain delay depending on your machine's configuration." } diff --git a/render-plantuml/render-plantuml.qml b/render-plantuml/render-plantuml.qml index 54dbcb3..2a90675 100644 --- a/render-plantuml/render-plantuml.qml +++ b/render-plantuml/render-plantuml.qml @@ -57,6 +57,46 @@ QtObject { } ]; + function extractPlantUmlText(html, plantumlSectionRegex, note) { + var plantumlFiles = []; + var index = 0; + + var match = plantumlSectionRegex.exec(html); + while (match != null) { + var matchedUml = match[1].replace(/\n/gi, "\\n"); + var filePath = workDir + "/" + note.id + "_" + (++index); + + var params = ["-e", "require('fs').writeFileSync('" + filePath + "', \"" + matchedUml + "\", 'utf8');"]; + var result = script.startSynchronousProcess("node", params, html); + + plantumlFiles.push(filePath); + + match = plantumlSectionRegex.exec(html); + } + + return plantumlFiles; + } + + function generateUmlDiagrams(html, plantumlFiles) { + var params = ["-jar", plantumlJarPath, "-o", workDir, additionalParams].concat(plantumlFiles); + var result = script.startSynchronousProcess(javaExePath, params, html); + } + + function injectDiagrams(html, plantumlSectionRegex, plantumlFiles) { + var index = 0; + var updatedHtml = html.replace(plantumlSectionRegex, function(matchedStr, g1) { + var imgElement = "
\"Wait
"; + + if (hideMarkup == "true") { + return imgElement; + } else { + return imgElement + matchedStr; + } + }); + + return updatedHtml; + } + /** * This function is called when the markdown html of a note is generated * @@ -68,32 +108,15 @@ QtObject { * @return {string} the modfied html or an empty string if nothing should be modified */ function noteToMarkdownHtmlHook(note, html) { - //var matches = html.match(/language-plantuml\"\>([\s\S]*?)<\/pre/gmi); - - var index = 0; - html = html.replace(/
([\s\S]*?)<\/pre>/gmi, function(matchedStr, g1) {
-            var matchedUml = g1.replace(/\n/gi, "\\n");
-            var filePath = workDir + "/" + note.id + "_" + (++index);
-            var plantumlFilePath = filePath + ".plantuml";
-
-            var params = ["-e", "require('fs').writeFileSync('" + plantumlFilePath + "', \"" + matchedUml + "\", 'utf8');"];
-            var result = script.startSynchronousProcess("node", params, html);
-
-            params = ["-jar", plantumlJarPath, "-o", workDir, additionalParams, plantumlFilePath];
-            result = script.startSynchronousProcess(javaExePath, params, html);
-
-            var imgElement = "
\"Wait
"; - - if (hideMarkup == "true") { - return imgElement; - } else { - return imgElement + matchedStr; - } - }); + var plantumlSectionRegex = /
([\s\S]*?)<\/pre>/gmi;
         
+        var plantumlFiles = extractPlantUmlText(html, plantumlSectionRegex, note);
+
+        if (plantumlFiles.length) {
+            generateUmlDiagrams(html, plantumlFiles);
+            return injectDiagrams(html, plantumlSectionRegex, plantumlFiles);
+        }
+
         return html;
     }
 }
-
-// Future plans:
-// TODO: Optimize image creation by combining img generation in a single java command instead of in a loop.