RealRaven2000 / SmartTemplates

Thunderbird Add-on: SmartTemplates
http://smarttemplates.quickfolders.org/
Other
25 stars 15 forks source link

Support using percent "%" sign within a text parameter, e.g. `%header.set(subject,"save 25%")%` #287

Closed RealRaven2000 closed 4 months ago

RealRaven2000 commented 5 months ago

at the moment variables fail when a %-sign is used within the parameters list. This is caused by the current regular expression that is used to recognize the contents of the parameters; see smartTemplate-overlay.js:4236

 msg = await SmartTemplate4.Util.replaceAsync(msg, /%([a-zA-Z][\w\-:=.]*)(\([^%]*\))*%/gm, replaceReservedWords);

The reason I used [^%] was that it avoided making the last match of *\))*% (a closing parenthesis followed by %) too greedy. Now I found the following replacement, using .+? - apparently this matches any characters, but as few as possible.

 msg = await SmartTemplate4.Util.replaceAsync(msg, /%([a-zA-Z][\w\-:=.]*)(\(.+?\))*%/gm, replaceReservedWords);
RealRaven2000 commented 5 months ago

Here is a test version:

smartTemplate-fx-4.5pre3.zip

I am using this in my current sale campaign for QuickFolders in order to inject a percent sign into the subject line, example:

%header.prefix(subject,"🐇 QuickFolders Frühlingsangebot - erneuern und 25% sparen - ")%


To install version above download zip file and drag the file into Thunderbird Add-ons Manager (do not extract contents, it won't install like that)

RealRaven2000 commented 5 months ago

I did a number of tweaks to the regular expression for recognizing the various smarttemplates patterns from both simple to complex. There major challenges with allowing the "%" character and matching everything within the parentheses (including parentheses for other regular expressions). The goal is that only a combination of )% within the parameter list would break the regex:

%from%
To: "%to(name)%"<%to("( %",mail)%>
%matchTextFromBody("(domainname|domain) = (\w*@){0,1}((?!:\/\/)([a-zA-Z0-9-_]+\.)*[a-zA-Z0-9][a-zA-Z0-9-_]+\.[a-zA-Z]{2,11})")%
%header.set(subject,"some text with 100%")%

(there were some problems with matching the correct 1st opening parenthesis after the smarttempaltes keyword(s). I think they are addressed now by adding a ? instead of * after the second parts (0 or one instance, with a nongreedy lookahead within the second argument (\(.+?\))?). The final pattern is:

%([a-zA-Z][\w\-:=.]*)(\(.+?\))?%

smartTemplate-fx-4.5pre57.zip


To install version above download zip file and drag the file into Thunderbird Add-ons Manager (do not extract contents, it won't install like that)

RealRaven2000 commented 4 months ago

Implemented in 4.5 released on 14/05/2024