Module talk:Template invocation: Difference between revisions

Content deleted Content added
 
(13 intermediate revisions by 5 users not shown)
Line 32:
The code seems somewhat broken to me. I would fix it but omg it's used in so many places and I haven't got a clue why it is invoked in articles. For a lol, see [[Module talk:Template invocation/testcases]] which apparently has been displaying "Error: attempt to compare string with number" for a long time. [[User:Johnuniq|Johnuniq]] ([[User talk:Johnuniq|talk]]) 03:42, 20 May 2020 (UTC)
:{{ping|Andrybak}} OK I got courageous and fixed the module anyway. Please check it out. [[User:Johnuniq|Johnuniq]] ([[User talk:Johnuniq|talk]]) 04:05, 20 May 2020 (UTC)
::A mystery explained with a mystery: the reason the module is used at [[Alan Garner]] is that it uses {{tl|postnominals}} and even "{{tlf|postnominals}}" (with no parameters) calls [[Module:Template invocation]]. [[User:Johnuniq|Johnuniq]] ([[User talk:Johnuniq|talk]]) 04:50, 20 May 2020 (UTC)
 
== Editor-hostile wikitext ==
 
{{editprotected|answered=yes}}
The module is currently producing "[[WP:BOTDICT#editor-hostile wikitext / editor-friendly wikitext|editor-hostile wikitext]]" by not adding spaces between the parameters. Please add a space at line [[Module:Template_invocation#L-116]]:
: <code>ret[#ret + 1] = ' ' .. seps.pipe</code>
(an alternative solution by changing <code>pipe = '|',</code> to <code>pipe = ' |',</code> at [[Module:Template_invocation#L-81]] would probably be less robust, since <code>pipe</code> is used for unnamed parameters as well, but some such templates don't trim whitespace from their arguments). — [[User:Mikhail Ryazanov|Mikhail Ryazanov]] ([[User talk:Mikhail Ryazanov|talk]]) 20:56, 9 February 2024 (UTC)
: {{Done}} [[User:Pppery|* Pppery *]] [[User talk:Pppery|<sub style="color:#800000">it has begun...</sub>]] 23:23, 9 February 2024 (UTC)
:: {{thank you}}, seems to work better now. I've updated the test cases accordingly. — [[User:Mikhail Ryazanov|Mikhail Ryazanov]] ([[User talk:Mikhail Ryazanov|talk]]) 00:25, 11 February 2024 (UTC)
{{od|2}}
I've stumbled upon a minor glitch caused by [[Special:Diff/1205562670]]. At [[Special:Permalink/1234259862#Notifications|Template:Myprefs/testcases#Notifications]], the wikitext is shown with an extra space before the pipe in {{para|check}}:
 
<pre>
{{Myprefs|Notifications|Notify me about these events |check=Page links}}
</pre>
instead of:
<pre>
{{Myprefs|Notifications|Notify me about these events|check=Page links}}
</pre>
 
This is significant, because for unnamed positional parameters the whitespace isn't stripped. Compare: <code><nowiki>{{tl|x1}}</nowiki></code>→{{tl|x1}} vs <code><nowiki>{{tl| x1 }}</nowiki></code>→{{tl| x1 }}. The extraneous space might confuse editors who are copy-pasting from testcases.
 
I propose to fix it by not adding a space for the first named parameter. A hacky implementation via variable <code>maybeSpace</code> in sandbox: [[Special:Diff/1205562670/1234263149]]. Tested at [[Special:Diff/1234262869]]. —⁠[[User:Andrybak|andrybak]] ([[User talk:Andrybak|talk]]) 12:44, 13 July 2024 (UTC)
 
:Courtesy pings: [[User:Pppery|Pppery]], [[User:Mikhail Ryazanov|Mikhail Ryazanov]]. —⁠[[User:Andrybak|andrybak]] ([[User talk:Andrybak|talk]]) 12:47, 13 July 2024 (UTC)
:: Fine with me. [[User:Pppery|* Pppery *]] [[User talk:Pppery|<sub style="color:#800000">it has begun...</sub>]] 15:46, 13 July 2024 (UTC)
:: {{Hidden ping|andrybak}}It's a tolerable workaround, but in principle, templates that use positional parameters must use {{tlx|trim}} in cases where leading/trailing spaces don't have any functional purpose (besides readability) but can break the result. — [[User:Mikhail Ryazanov|Mikhail Ryazanov]] ([[User talk:Mikhail Ryazanov|talk]]) 20:23, 13 July 2024 (UTC)
 
== Title clash in a few edge cases ==
 
{{editprotected|answered=yes}}
 
Currently, the "name" function returns the wrong result in three edge cases. Two of these are relatively straightforward to fix, but the third is more tricky:
# A page name like "Template:User:Theknightwho" should not return "User:Theknightwho", but instead should remain "Template:User:Theknightwho".
# The function should throw an error if it receives a title with an interwiki prefix (e.g. {{fr:foo}}) or one which starts with "#" (e.g. {{#foo}}), since both of these generate valid title objects, but neither can be used with template invocations.
# (Harder to fix): when a template name overlaps with that of a magic word, the "Template:" prefix is required in order to disambiguate it: e.g. <nowiki>{{Template:!}}</nowiki> or <nowiki>{{Template:PAGENAME:foo}}</nowiki>.
I've done a full implementation at [[wikt:Module:template parser#L-218]], but it relies on loading data for point 3, which might not be desirable to import to Wikipedia. However, points 1 and 2 are pretty simple to deal with. I've drafted a new version of the function below, incorporating fixes for both of them:
<syntaxhighlight lang=lua>
function p.name(title)
if type(title) == 'string' then
title = mw.title.new(title)
if not title or #title.prefixedText == 0 or #title.interwiki > 0 then
error("invalid title in parameter #1 of function 'name'", 2)
end
elseif type(title) ~= 'table' or type(title.getContent) ~= 'function' then
error("parameter #1 of function 'name' must be a string or a mw.title object", 2)
end
if title.namespace == 10 then
local text = title.text
local check = mw.title.new(text, 10)
-- Exclude the prefix, unless we have something like "Template:Category:Foo", which can't be abbreviated to "Category:Foo".
return check and mw.title.equals(title, check) and text or title.prefixedText
elseif title.namespace == 0 then
return ':' .. title.prefixedText
else
return title.prefixedText
end
end
</syntaxhighlight>
[[User:Theknightwho|Theknightwho]] ([[User talk:Theknightwho|talk]]) 20:07, 31 August 2024 (UTC)
: {{done}} and you should consider [[WP:Requests for permissions/Template editor|running for template editor here]] since you seem to know what you are doing. [[User:Pppery|* Pppery *]] [[User talk:Pppery|<sub style="color:#800000">it has begun...</sub>]] 20:22, 5 September 2024 (UTC)