Module talk:String2
New function "label" added
I added new function "label" which capitalize only first letter for fetched wikidata labels (diff). Because Wikidata English labels generally begin with a lowercase letter (d:Help:Label#Capitalization). New function is almost same as "sentence" function except that "label" doesn't lowering the rest of text. If there are any questions or problems, feel free to report here. Thanks! --Was a bee (talk) 03:03, 17 September 2017 (UTC)
- Some objection. A "label" has multiple options. Especially: it is "free" (as in: irrelevant, not defining). If this option wants to imply "page title as a wikilink label", then change the option (parameter) name. -DePiep (talk)
- I've removed the label function as obselete in favour of ucfirst. --RexxS (talk) 15:16, 13 November 2018 (UTC)
Testing posnq
{{#invoke:String2 |posnq |This is a piece of text to be searched |ext}}
→ Script error: The function "posnq" does not exist.{{#invoke:String2 |posnq |This is a piece of text to be searched |ent}}
→ Script error: The function "posnq" does not exist.{{#invoke:String2 |posnq |This is a piece of text to be searched |"ext"}}
→ Script error: The function "posnq" does not exist.{{#invoke:String2 |posnq |This is a piece of text to be searched |"ent"}}
→ Script error: The function "posnq" does not exist.{{Posnq |This is a piece of text to be searched |piece of }}
→ Template:Posnq{{Posnq |This is a piece oftext to be searched |piece of }}
→ Template:Posnq{{Posnq |This is a piece of text to be searched |"piece of "}}
→ Template:Posnq{{Posnq |This is a piece oftext to be searched |"piece of "}}
→ Template:Posnq{{Posnq | {{start date|2018|November|30}} | bday dtstart published updated }}
→ Template:Posnq{{Posnq | 30 November 2018 | bday dtstart published updated }}
→ Template:Posnq
ucfirst bug
There is an script error on articles like 999 (album) and Casual Viewin' USA, whith an error on line 35, which originates on line 34. "%a" on line 34 did not match numbers after the pipe in my test, although it does find an letter after the pipe. As both of these articles have an number after the pipe, they both give an script error. The script needs to deal with the possibility of an number after an pipe.--Snaevar (talk) 23:13, 9 October 2019 (UTC)
- @Snaevar: I noticed many articles with that problem and I believe I have just fixed the module. Johnuniq (talk) 09:11, 10 October 2019 (UTC)
ucfirst bug, part 2
E.g. {{#invoke:String2 | ucfirst |Đorđe Balašević chronology }}
returns ĐOrđe Balašević chronology, as if the function does not realize Đ is a letter and is capitalizing O instead. Note {{ucfirst:Đorđe Balašević chronology}}
returns Đorđe Balašević chronology, as it should. Oddly, {{#invoke:String2 | ucfirst |đorđe Balašević chronology }}
works correctly (i.e. Đorđe Balašević chronology is returned). {{Infobox album}} chronology is affected, although these kinds of errors seem to be very rare. GregorB (talk) 18:24, 24 July 2020 (UTC)
- @GregorB: Almost all of the time, the standard Lua string library calls manage to cope although they only deal with single-byte character codes. Once in a while an application needs to work with UTF-8, and this is one of those cases. I've updated the ucfirst call to use the mw.ustring library which handles UTF-8 characters properly. Now we should get:
{{#invoke:String2 | ucfirst |Đorđe Balašević chronology }}
→ Đorđe Balašević chronology{{#invoke:String2 | ucfirst |đorđe Balašević chronology }}
→ Đorđe Balašević chronology
- Thanks for spotting that, and please let me know if you find any more issues. Cheers --RexxS (talk) 19:24, 24 July 2020 (UTC)
- That was super quick, thanks! GregorB (talk) 19:50, 24 July 2020 (UTC)
New function "findlast" added
Function findlast finds the last item in a list. The first unnamed parameter is the list. The second, optional unnamed parameter is the list separator (default = comma space). It returns the whole list if the separator is not found.
The list is trimmed of leading and trailing whitespace; the separator is not (so that leading or trailing spaces can be included).
One issue is that using Lua special pattern characters as the separator will probably cause problems.
Examples:
- Normal usage:
{{#invoke:String2 |findlast | 5, 932, 992,532, 6,074,702, 6,145,291}}
→ 6,145,291 - Separator not found:
{{#invoke:String2 |findlast | 5, 932, 992,532, 6,074,702, 6,145,291 |;}}
→ 5, 932, 992,532, 6,074,702, 6,145,291 - One item list:
{{#invoke:String2 |findlast | 6,074,702 }}
→ 6,074,702 - List missing
{{#invoke:String2 |findlast |}}
→ - Space as separator:
{{#invoke:String2 |findlast | 5 932 992,532 6,074,702 6,145,291 }}
→ 5 932 992,532 6,074,702 6,145,291
Any bug reports welcome. --RexxS (talk) 20:33, 19 November 2020 (UTC)
Upgrading posnq
Now supports named parameters: source, target, plain, nomatch; and UTC characters:
{{#invoke:String2 |posnq |source=This is a piece of text |target=ece}}
→ Script error: The function "posnq" does not exist.{{#invoke:String2 |posnq |source=This is a piece of text |target=%s |plain=true}}
→ Script error: The function "posnq" does not exist.{{#invoke:String2 |posnq |source=This is a piece of text |target=%s |plain=false}}
→ Script error: The function "posnq" does not exist.{{#invoke:String2 |posnq |source=This is a piece of text |target=ece |nomatch=0}}
→ Script error: The function "posnq" does not exist.{{#invoke:String2 |posnq |source=This is a piece of text |target=xyz |nomatch=0}}
→ Script error: The function "posnq" does not exist.{{#invoke:String2 |posnq |This is a piece of text |" of" |true |0}}
→ Script error: The function "posnq" does not exist.{{#invoke:String2 |posnq |This is a piece of text |" of" |true |0}}
→ Script error: The function "posnq" does not exist.{{#invoke:String2 |posnq |source=Meet at Café Nero |target=afé}}
→ Script error: The function "posnq" does not exist.
Any bug reports welcome. --RexxS (talk) 00:08, 8 December 2020 (UTC)
Added matchAny function
I have added a matchAny function to the sandbox; it takes any number of patterns and returns the index of the first which matches, if any. Demo usage at Template:Infobox animanga/Header/sandbox. Comments welcome. (I'm a new template editor so I can make this change myself if no objections.) User:GKFXtalk 19:17, 8 April 2021 (UTC)
- I'm a bit fuzzy at the moment and should not be relied on but the code looks good. I don't understand p._getParameters (please don't explain it) but my guess is that matchAll requires source=input. However, the example usage in the comment does not show that. Johnuniq (talk) 00:59, 9 April 2021 (UTC)
- Good thing you pointed out p._getParameters, I was using it wrong. Fixed the docs also. User:GKFXtalk 09:56, 9 April 2021 (UTC)
Remove upper and lower functions?
I recently noticed a rather significant issue with uppercasing and lowercasing strings in Lua: it mangles strip markers. The built-in parser functions do not. See sample (now a mock-up):
INVOKE:STRING2|UPPER.'"`UNIQ--REF-0000001E-QINU`"'
invoke:string2|lower.'"`uniq--ref-0000001f-qinu`"'
USING UC:[3]
using lc:[4]
Is there any good reason to make this feature available to wikitext? It would be highly confusing for editors and template authors to see strip markers. Otherwise this module's upper and lower functions should be removed from anything using them and replaced with the uc:/lc: parser functions. User:GKFXtalk 18:47, 15 April 2021 (UTC)
Done These functions have been removed, and the sentence function has been made strip-marker safe. User:GKFXtalk 19:36, 24 April 2021 (UTC)