Merge pull request #24 from nikhilw/master

Features: option to hide uml text, pass additional params to platuml and java optimisations
This commit is contained in:
Patrizio Bekerle 2018-02-14 07:11:15 +01:00 committed by GitHub
commit 21cc381bdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 27 deletions

View File

@ -3,7 +3,7 @@
"identifier": "render-plantuml",
"script": "render-plantuml.qml",
"authors": ["@nikhilw"],
"version": "0.0.1",
"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 (<a href=\"https://nodejs.org/en/download/\">https://nodejs.org/en/download/</a>), java (<a href=\"https://java.com/en/download/\">https://java.com/en/download/</a>) and plantuml (<a href=\"http://plantuml.com/download\">http://plantuml.com/download</a>) 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 (<a href=\"https://nodejs.org/en/download/\">https://nodejs.org/en/download/</a>), java (<a href=\"https://java.com/en/download/\">https://java.com/en/download/</a>) and plantuml (<a href=\"http://plantuml.com/download\">http://plantuml.com/download</a>) 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."
}

View File

@ -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,9 +40,63 @@ 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": ""
}
];
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 = "<div><img src=\"file://" + plantumlFiles[index++] + ".png?t=" + +(new Date()) + "\" alt=\"Wait for it..\"/></div>";
if (hideMarkup == "true") {
return imgElement;
} else {
return imgElement + matchedStr;
}
});
return updatedHtml;
}
/**
* This function is called when the markdown html of a note is generated
*
@ -52,31 +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(/<pre><code class=\"language-plantuml\"\>([\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);
// script.log(additionalPumlParams);
params = ["-jar", plantumlJarPath, "-o", workDir, " ", plantumlFilePath];
result = script.startSynchronousProcess(javaExePath, params, html); //["-jar", plantumlJarPath, "-o", workDir, filePath]
return "<div><img src=\"file://" + filePath + ".png\" alt=\"Wait for it..\"/></div>" + matchedStr;
});
var plantumlSectionRegex = /<pre><code class=\"language-plantuml\"\>([\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: 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.