Template talk:Code

Latest comment: 1 month ago by SilverLocust in topic Template-protected edit request on 4 March 2024

Template-protected edit request on 4 March 2024 edit

Update template to match this edit I made to Template:Code/Sandbox. The specific change to line 1, and is as follows, compared to the current version of Template:Code (disregard the mangled noinclude in the left/old one, it's an issue with Template:TextDiff, new version is on the right anyway):

{{#tag:syntaxhighlight|{{{code|{{{1}}}}}}|lang={{{lang|{{{2|text}}}}}}|class={{{class|}}}|id={{{id|}}}|style={{{style|}}}|inline=1}}<noinclude>
+
{{#tag:{{#ifeq:{{{lang|{{{2|text}}}}}}|text|code|syntaxhighlight}}|{{{code|{{{1}}}}}}|lang={{{lang|{{{2|text}}}}}}|class={{{class|}}}|id={{{id|}}}|style={{{style|}}}|inline=1}}<noinclude>

The change has been tested in Template:Code/testcases, and produces the expected output.

Purpose of change: makes this template use #tag:code instead of #tag:syntaxhighlight when lang/param 2 is either undefined or has been defined as 'text', but still uses #tag:syntaxhighlight in all other cases. #tag:syntaxhighlight is a WP:EXPENSIVE parser function, and current template behaviour always uses #tag:syntaxhighlight, even when unnecessary (as 'lang=text' has no syntax highlighting) - but, this change ensures that the non-expensive #tag:code will be used instead when syntax highlighting is not necessary. 🔥HOTm̵̟͆e̷̜̓s̵̼̊s̸̜̃🔥 (talkedits) 20:01, 4 March 2024 (UTC)Reply

It needs some additional work first. There are differences in markup behavior:
plain ''ABC'' <b>bold</b> {{mdash}} {{PAGENAME}}
{{code}} plain ''ABC'' <b>bold</b> Code
{{code/sandbox}} plain ''ABC'' <b>bold</b> Code
<syntaxhighlight inline> plain ''ABC'' <b>bold</b> {{mdash}} {{PAGENAME}}
<code><nowiki> plain ''ABC'' <b>bold</b> {{mdash}} {{PAGENAME}}
<code> plain ABC bold Code
See this template's documentation about how syntaxhighlight works like code plus nowiki (except that with this template inner templates/magic words expand first). SilverLocust 💬 08:54, 5 March 2024 (UTC)Reply
{{code}} plain ''ABC'' <b>bold</b> Code
{{code/sandbox2}} plain ABC bold Code
@HotMess: I have made a new version in Template:Code/sandbox2 that may be correct — but it could use more checking. [Edit: Now that my changes have been moved to /sandbox, I have put HotMess's original version in /sandbox2.] SilverLocust 💬 10:16, 5 March 2024 (UTC)Reply
@SilverLocust I've incorporated those changes into Template:Code/sandbox, and updated Template:Code/testcases with those additional checks (and a couple extra checks) - and it appears to be functioning correctly now. 🔥HOTm̵̟͆e̷̜̓s̵̼̊s̸̜̃🔥 (talkedits) 13:13, 5 March 2024 (UTC)Reply
@HotMess: There is also a difference for how they treat html codes.
&ndash; &ndash;
lang=something
{{code}} &ndash; &ndash;
{{code/sandbox}} &ndash;
{{code/sandbox2}}
(your original request)
&ndash;
<syntaxhighlight inline> &ndash; &ndash;
<code><nowiki>
<code>
For context about the need for the change, is there a page/situation where you encountered an issue because of the current template? (It is, of course, possible to have an issue by just using the template 501 times, or on a page with 500 other expensive calls, but I want to know how much this has been an issue.) SilverLocust 💬 23:43, 5 March 2024 (UTC)Reply
Well, the context behind this change was seeing Comparison of programming languages (basic instructions) in Category:Pages with too many expensive parser function calls (as this template is used 663 times within that page). I noticed that the current behaviour of this template upon reaching the WP:EXPENSIVE limit is to just present the code within regular <code>...</code> blocks, so, y'know, figured that it would make sense to default to <code>...</code> unless the expensive syntax-highlighting functionality is desired. Genuinely have no idea how to replicate the 'not needing to escape & within HTML character entities' functionality of mw:SyntaxHighlight though. 🔥HOTm̵̟͆e̷̜̓s̵̼̊s̸̜̃🔥 (talkedits) 12:35, 6 March 2024 (UTC)Reply
I have substituted most all of those with {{subst:codenowiki}}. That should keep that page under 500 hopefully forever. (I also then removed extra nowiki tags, though there may be some nowiki over- or under-inclusion errors left on the page that I haven't caught.) SilverLocust 💬 14:04, 6 March 2024 (UTC)Reply