From d0c23ca5d156f1e2b4d5e1edaeca5813a14227a8 Mon Sep 17 00:00:00 2001 From: NikhilWanpal Date: Tue, 13 Feb 2018 15:16:01 +0530 Subject: [PATCH 1/2] Feature: pass additional params to plantuml jar. Feature: Hide plantuml markup in preview. BugFix: removed dev specifc default path --- render-plantuml/info.json | 2 +- render-plantuml/render-plantuml.qml | 51 +++++++++++++++++++---------- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/render-plantuml/info.json b/render-plantuml/info.json index ec9531a..3f12728 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.1", + "version": "0.0.2", "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." } diff --git a/render-plantuml/render-plantuml.qml b/render-plantuml/render-plantuml.qml index 5ba50d6..54dbcb3 100644 --- a/render-plantuml/render-plantuml.qml +++ b/render-plantuml/render-plantuml.qml @@ -15,6 +15,8 @@ QtObject { property string javaExePath; property string plantumlJarPath; property string workDir; + property string hideMarkup; + property string additionalParams; // register your settings variables so the user can set them in the script settings property variant settingsVariables: [ @@ -30,7 +32,7 @@ QtObject { "name": "Path to plantuml jar", "description": "Please enter absolute path to plantuml jar file:", "type": "file", - "default": "/home/nikhil/softs/plantuml/plantuml.jar" + "default": "/opt/softs/plantuml/plantuml.jar" }, { "identifier": "workDir", @@ -38,6 +40,20 @@ QtObject { "description": "Please enter a path to be used as working directory i.e. temporary directory for file creation:", "type": "file", "default": "/tmp" + }, + { + "identifier": "hideMarkup", + "name": "Hide plantuml markup", + "description": "Enable if you wish to hide plantuml markup in preview.", + "type": "boolean", + "default": false + }, + { + "identifier": "additionalParams", + "name": "Additional Params (Advanced)", + "description": "Enter any additional parameters you wish to pass to plantuml. This can potentially cause unexpected behaviour:", + "type": "string", + "default": "" } ]; @@ -52,22 +68,27 @@ 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 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 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);
-        	
-//			script.log(additionalPumlParams);
-        	params = ["-jar", plantumlJarPath, "-o", workDir, " ", plantumlFilePath];
-        	result = script.startSynchronousProcess(javaExePath, params, html); //["-jar", plantumlJarPath, "-o", workDir, filePath]
-        	
-        	return "
\"Wait
" + matchedStr; + 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; + } }); return html; @@ -75,8 +96,4 @@ QtObject { } // Future plans: -// TODO: Allow for passingin addtional parameters to plantuml. -// TODO: Allow for replacing the markup in the rendered preview with image instead of keeping both. // TODO: Optimize image creation by combining img generation in a single java command instead of in a loop. - - From ef2e0329bad4b59b05e5be44b5e6aaae5c2c34b3 Mon Sep 17 00:00:00 2001 From: NikhilWanpal Date: Wed, 14 Feb 2018 11:27:56 +0530 Subject: [PATCH 2/2] 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.