1
0
Fork 0
mirror of https://github.com/marty-oehme/scripts.git synced 2024-12-22 07:58:08 +00:00

Feature: generates all UMLs in the note in one go giving a performance benefit.

This commit is contained in:
NikhilWanpal 2018-02-14 11:27:56 +05:30
parent d0c23ca5d1
commit ef2e0329ba
2 changed files with 50 additions and 27 deletions

View file

@ -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 (<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

@ -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 = "<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
*
@ -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(/<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);
params = ["-jar", plantumlJarPath, "-o", workDir, additionalParams, plantumlFilePath];
result = script.startSynchronousProcess(javaExePath, params, html);
var imgElement = "<div><img src=\"file://" + filePath + ".png?t=" + +(new Date()) + "\" alt=\"Wait for it..\"/></div>";
if (hideMarkup == "true") {
return imgElement;
} else {
return imgElement + 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: Optimize image creation by combining img generation in a single java command instead of in a loop.