Process variable macros line by line

This commit is contained in:
Cohee 2023-11-25 20:52:17 +02:00
parent a5c3e22833
commit 0410540066
1 changed files with 49 additions and 36 deletions

View File

@ -85,48 +85,61 @@ export function resolveVariable(name) {
return name;
}
export function replaceVariableMacros(str) {
// Replace {{getvar::name}} with the value of the variable name
str = str.replace(/{{getvar::([^}]+)}}/gi, (_, name) => {
name = name.trim();
return getLocalVariable(name);
});
export function replaceVariableMacros(input) {
const lines = input.split('\n');
// Replace {{setvar::name::value}} with empty string and set the variable name to value
str = str.replace(/{{setvar::([^:]+)::([^}]+)}}/gi, (_, name, value) => {
name = name.trim();
setLocalVariable(name, value);
return '';
});
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
// Replace {{addvar::name::value}} with empty string and add value to the variable value
str = str.replace(/{{addvar::([^:]+)::([^}]+)}}/gi, (_, name, value) => {
name = name.trim();
addLocalVariable(name, value);;
return '';
});
// Skip lines without macros
if (!line || !line.includes('{{')) {
continue;
}
// Replace {{getglobalvar::name}} with the value of the global variable name
str = str.replace(/{{getglobalvar::([^}]+)}}/gi, (_, name) => {
name = name.trim();
return getGlobalVariable(name);
});
// Replace {{setvar::name::value}} with empty string and set the variable name to value
line = line.replace(/{{setvar::([^:]+)::([^}]+)}}/gi, (_, name, value) => {
name = name.trim();
setLocalVariable(name, value);
return '';
});
// Replace {{setglobalvar::name::value}} with empty string and set the global variable name to value
str = str.replace(/{{setglobalvar::([^:]+)::([^}]+)}}/gi, (_, name, value) => {
name = name.trim();
setGlobalVariable(name, value);
return '';
});
// Replace {{getvar::name}} with the value of the variable name
line = line.replace(/{{getvar::([^}]+)}}/gi, (_, name) => {
name = name.trim();
return getLocalVariable(name);
});
// Replace {{addglobalvar::name::value}} with empty string and add value to the global variable value
str = str.replace(/{{addglobalvar::([^:]+)::([^}]+)}}/gi, (_, name, value) => {
name = name.trim();
addGlobalVariable(name, value);
return '';
});
// Replace {{addvar::name::value}} with empty string and add value to the variable value
line = line.replace(/{{addvar::([^:]+)::([^}]+)}}/gi, (_, name, value) => {
name = name.trim();
addLocalVariable(name, value);;
return '';
});
return str;
// Replace {{setglobalvar::name::value}} with empty string and set the global variable name to value
line = line.replace(/{{setglobalvar::([^:]+)::([^}]+)}}/gi, (_, name, value) => {
name = name.trim();
setGlobalVariable(name, value);
return '';
});
// Replace {{getglobalvar::name}} with the value of the global variable name
line = line.replace(/{{getglobalvar::([^}]+)}}/gi, (_, name) => {
name = name.trim();
return getGlobalVariable(name);
});
// Replace {{addglobalvar::name::value}} with empty string and add value to the global variable value
line = line.replace(/{{addglobalvar::([^:]+)::([^}]+)}}/gi, (_, name, value) => {
name = name.trim();
addGlobalVariable(name, value);
return '';
});
lines[i] = line;
}
return lines.join('\n');
}
function listVariablesCallback() {