Topic on Extension talk:SyntaxHighlight

Expanding templates/preprocess wikicode before syntaxhighlight?

8
Summary by 50.71.69.60

IP is trying to do something that SH doesn't have the functionality to do due to the nature of the implementation.

50.71.69.60 (talkcontribs)

Hi,

I'm using several nested templates within SyntaxHighlight v2.0 on MediaWiki v.1.33.3 to try to create a shallow documentation generator. So far, I have an template feeding inputs into another template that calls syntaxhighlight. My problem is this: I'm trying to create links within a syntaxhighlighted code block that allows for external links, by calling another template within it. Is this possible within this extension? I've been trying to play around with it for a few hours now, to no avail.

Thanks.

PerfektesChaos (talkcontribs)

Basically, you can do that and it is done already on wikis by template transclusion stack.

You will need syntax {{#tag:syntaxhighlight|1=code|lang=xyz}} and {{!}} to escape pipes. Good luck if double }} occur in your program code at the same level. Content and sections of other pages may be transcluded.

Basically the feature is dedicated to illustrate encyclopedic articles by static code snippets. A very sophisticated dynamic transclusion system might require advanced knowledge of wiki programming.

50.71.69.60 (talkcontribs)

Thanks for the response. My knowledge of MediaWiki's inner mechanics is very superficial, I mostly use templates and the odd Lua script. I would link my existing template if I didn't trip the abuse filter. I should have been a bit more specific on the implementation - rereading it now, it's kind of vague (or I'm not understanding how to implement what I want)

Just to make sure I understand, given the following (omitting more complicated parts to just get to the crux of what I'm trying to do):

  • a "link" template that takes an input and returns something like [whateverurlhere?search={{{1}}} {{{1}}}]
  • a template that has feeds one or more of the above to syntaxhighlight

would create the following, given an input of test

{{#tag:syntaxhighlight|[whateverurlhere?search=test test]|lang=c#}}

How would I escape the provided first parameter to be processed as wikitext, instead of appearing verbatim? Apologies if I'm being dense, or going about this the wrong way.

PerfektesChaos (talkcontribs)

If you are using Lua anyway, I would recommend to use frame:callParserFunction( "syntaxhighlight", { [1] = "printf(%"Hello world%")", lang = "c#" } )

For template syntax, the part in between will be evaluated wrt double or triple brackets.

  • You might try to escape by <nowiki> if brackets indicate a URL.
  • However, that URL would not be linked, since it has single brackets and those are passed as parameter 1= to syntaxhighlight and that is hiding it from being evaluated.
  • You may put other wikitext as 1=code parameter, e.g. template transclusions or transcluding sections from other pages.
  • The major problem is if there is a pipe symbol | occurring in your c# code, e.g. bitwise or / || or. Those need to be escaped by {{!}} otherwise it will terminate that source code parameter if present at the same level like the #tag: function. If transcluded from somewhere else, as parameter value or result of any other thing, it does no harm.
  • Value of {{{1}}} will be shown as current value (being replaced by current content).
  • The inner parameter value of 1=code is evaluated first, until next | or }} visible at the same level, then #tag: will be evaluated with that string.
  • Pipes within complete template transclusion or complete internal link are assigned to those wrapping elements, they do not influence #tag: evaluation.
Pppery (talkcontribs)

I don't think you understand what the IP is asking; they want to post-process the output of <syntaxhighlight> to, for example, add links to it, which I don't think there is any supported way of doing that, although I can think of a very hacky way.

50.71.69.60 (talkcontribs)

Just to provide context before I'm able to start hacking away at it - here is a snippet of the template.

{{Code/Pre|lang={{{lang|}}}|{{#if:{{{modifier|}}}|{{{modifier|}}} |}}{{#if:{{{keywords|}}}|{{{keywords|}}} |}}{{#if:{{{type|class}}}|{{API/ref|{{{type|class}}}}}{{#if:{{{generic|}}}|| |}}|}}{{#if:{{{generic|}}}|<{{{generic|}}}> |}}{{#if:{{{name|}}}|{{{name|}}} |}}{{#if:{{{parent|}}}|: {{API/ref|{{{parent|}}}}}|}}{{#if:{{{parentGeneric|}}}|| |}}

Aside from potential refactoring and code shortening (trying to get this to work before anything else), Code/pre is a template containing the SwitchHighlight call and API/ref is a template containing a switch statement with a link to a search query for external docs, or an internal link to a page (which is where I currently am in trying to add hyperlinks using wikitext). I'm trying to get the type identifiers to be searchable for faster navigation between documentation pages. All the nowiki tags are for syntax purposes and will be replaced later. Based on what I understood from PerfektesChaos, the URL can't be linked in the way I am attempting to do it as it all becomes evaluated by a template.

PerfektesChaos (talkcontribs)

The URL wouldn’t be clickable since it is displayed as text, and whatever you are feeding into syntaxhighlight is shown as source code rather than active element. That is the rationale behind syntaxhighlight. If you want to get a clickable URL you need to place an external link out of the syntaxhighlight region, e.g. using a table.

An entirely different approach would be a JavaScript gadget which is to be started by some miracle when that page is displayed, and which could read any element content, find some hidden URL and equip any element with a listener for a click.

50.71.69.60 (talkcontribs)

Alright. Thanks for the help, I'll look for another avenue to try to get this functionality implemented.

Reply to "Expanding templates/preprocess wikicode before syntaxhighlight?"