🐛 extend regex for macro parsing

- extend the regex to include "." to parse decimal values for
the parameter Dialog of the macro

Fixes #76
This commit is contained in:
thelastWallE 2021-12-28 16:53:13 +01:00
parent 8c2d8d054e
commit 4fc646598a
1 changed files with 16 additions and 16 deletions

View File

@ -4,38 +4,38 @@
// it under the terms of the GNU Affero General Public License as // it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the // published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version. // License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details. // GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
$(function() { $(function() {
function KlipperMacroDialogViewModel(parameters) { function KlipperMacroDialogViewModel(parameters) {
var self = this; var self = this;
self.parameters = ko.observableArray(); self.parameters = ko.observableArray();
self.interpolatedCmd; self.interpolatedCmd;
self.macro; self.macro;
self.macroName = ko.observable(); self.macroName = ko.observable();
var paramObjRegex = /{(.*?)}/g; var paramObjRegex = /{(.*?)}/g;
var keyValueRegex = /(\w*)\s*:\s*([\w\s°"|]*)/g; var keyValueRegex = /(\w*)\s*:\s*([\w\s°"|\.]*)/g;
self.process = function(macro) { self.process = function(macro) {
self.macro = macro.macro(); self.macro = macro.macro();
self.macroName(macro.name()); self.macroName(macro.name());
var matches = self.macro.match(paramObjRegex); var matches = self.macro.match(paramObjRegex);
var params = []; var params = [];
for (var i=0; i < matches.length; i++) { for (var i=0; i < matches.length; i++) {
var obj = {}; var obj = {};
var res = keyValueRegex.exec(matches[i]); var res = keyValueRegex.exec(matches[i]);
while (res != null) { while (res != null) {
if("options" == res[1]) { if("options" == res[1]) {
obj["options"] = res[2].trim().split("|"); obj["options"] = res[2].trim().split("|");
@ -44,32 +44,32 @@ $(function() {
} }
res = keyValueRegex.exec(matches[i]); res = keyValueRegex.exec(matches[i]);
} }
if(!("label" in obj)) { if(!("label" in obj)) {
obj["label"] = "Input " + (i+1); obj["label"] = "Input " + (i+1);
} }
if(!("unit" in obj)) { if(!("unit" in obj)) {
obj["unit"] = ""; obj["unit"] = "";
} }
if("default" in obj) { if("default" in obj) {
obj["value"] = obj["default"]; obj["value"] = obj["default"];
} }
params.push(obj); params.push(obj);
} }
self.parameters(params); self.parameters(params);
} }
self.executeMacro = function() { self.executeMacro = function() {
var i=-1; var i=-1;
function replaceParams(match) { function replaceParams(match) {
i++; i++;
return self.parameters()[i]["value"]; return self.parameters()[i]["value"];
} }
// Use .split to create an array of strings which is sent to // Use .split to create an array of strings which is sent to
// OctoPrint.control.sendGcode instead of a single string. // OctoPrint.control.sendGcode instead of a single string.
expanded = self.macro.replace(paramObjRegex, replaceParams) expanded = self.macro.replace(paramObjRegex, replaceParams)
expanded = expanded.split(/\r\n|\r|\n/); expanded = expanded.split(/\r\n|\r|\n/);