Skip to content

Commit

Permalink
allow prefix and suffix with double braces
Browse files Browse the repository at this point in the history
  • Loading branch information
TomW1605 committed Aug 14, 2024
1 parent 5c5bd46 commit 813d299
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions client/src/pages/printing/printing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,35 @@ function applyTextFormatting(text: string): JSX.Element[] {
});
return elements;
}

export function renderLabelContents(template: string, spool: ISpool): JSX.Element {
// Find all {tags} in the template string and loop over them
let result = template.replace(/\{(.*?)\}/g, function (_, tag) {
return getTagValue(tag, spool);
// let matches = [...template.matchAll(/(?:{(.*?))?{(.*?)}(.*?)(?:}(.*?))?/gs)];
let matches = [...template.matchAll(/{(?:[^}{]|{[^}{]*})*}/gs)];
// console.log(matches){(?:[^}{]|{[^}{]*})*}
let label_text = template;
matches.forEach((match) => {
// console.log(match)
if ((match[0].match(/{/g)||[]).length == 1) {
let tag = match[0].replace(/[{}]/g, "");
// console.log(tag)
let tagValue = getTagValue(tag, spool)
label_text = label_text.replace(match[0], tagValue);
}
else if ((match[0].match(/{/g)||[]).length == 2) {
let structure = match[0].match(/{(.*?){(.*?)}(.*?)}/);
if (structure != null) {
const tag = structure[2];
let tagValue = getTagValue(tag, spool);
if (tagValue == "?") {
label_text = label_text.replace(match[0], "");
} else {
label_text = label_text.replace(match[0], structure[1] + tagValue + structure[3]);
}
}
}
});

// Split string on \n into individual lines
return <>{applyTextFormatting(result)}</>;
return <>{applyTextFormatting(label_text)}</>;
}

0 comments on commit 813d299

Please sign in to comment.