Jump to content

User:Bjohas/Syntax highlighting discussion

From mediawiki.org

urlencode:<syntaxhighlight>...</syntaxhighlight>

[edit]

Hello!

I'm trying to pass a code snippet to a template. The code snippet uses <syntaxhighlight>...</syntaxhighlight>. I would then link to urlencode that code snippet, but urlencode:<syntaxhighlight>...</syntaxhighlight> doesn't work - presumably because syntaxhighlight somehow pulls the text out of the processing flow? Any ideas how this could be achieved? I.e. something like:

Template:ShowCodeMakeUrl

{{{1}}}
myserver?q={{uriencode:{{{1}}} }}

which is called as

{{ShowCodeMakeUrl|<syntaxhighlight>
...
</syntaxhighlight>}}

It works fine in terms of displaying the code, but urlencode doesn't handle the <syntaxhighlight>... Thanks! Bjohas (talk) 13:56, 5 May 2017 (UTC) (Also see: https://www.mediawiki.org/wiki/Topic:Tpz21dqvqgkokhfl)

  • I did not fully understand your plan.
    • What is a “code snippet” and where is the problem?
  • Yes, your assumption is right: syntaxhighlight pulls the content out of the source text around.
    • The parser will put the stuff between <syntaxhighlight> tags aside and transclude it later into the HTML document.
    • It is not possible to wrap something into <syntaxhighlight> tags first and process the content later. The other way around works.
  • I fail to guess your expectation, is there a link supposed to be clickable, or shall the URL be displayed as code?
    • Note that for security reasons no external server must contribute code, but internal pages only and may be transcluded.
  • As far I read your intention try something like that:
    • Displaying generated URL, simply use nowiki, why coloured syntaxhighlight?
{{#tag:nowiki|{{ShowCodeMakeUrl|...}}}}
  • A clickable link with coloured syntaxhighlight as linktext, got passed a pipe-escaped code snippet, but no line breaks:
  • Template programming of ShowCodeMakeUrl:
[http://example.org?q={{uriencode:{{{1}}}}} {{#tag:syntaxhighlight|{{{1}}}|lang="php"|inline=1}}]
  • Transcluded with:
{{ShowCodeMakeUrl|way[highway][name="6th Avenue"];node(w)->.n1;way[highway][name="West 23rd Street"];node(w)->.n2;node.n1.n2;out meta;}}
Greetings --PerfektesChaos (talk) 03:46, 6 May 2017 (UTC)
I'll try to summarize all requirements we have:
- Source code (actually "Overpass QL" language) is passed to template as a parameter
- Source code is rendered with syntax highlighting, preserving any new line characters
- An additional icon with hyperlink is generated, hyperlink includes source code as URL parameter. All newline characters need to be preserved!
- If you click on the hyperlink, you'll see exactly the same source code as shown on the wiki page.
See this page with the template in action: https://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_API_by_Example
Bjohas wanted to find out some way to get rid of the sometimes insane escaping, like ({{((}}bbox{{))}}) instead of ({{bbox}}). This would have the advantage, that a user can just copy & paste some source code as a template parameter and doesn't have to bother with escaping.
For even more details: see https://github.com/tyrasd/overpass-turbo/issues/288
Mmd-osm (talk) 06:51, 6 May 2017 (UTC)
Template:OverpassTurboExample is already using the means I suggested above.
  • Note: source is called syntaxhighlight today.
  • Therefore, {{#tag:source|{{{query}}}|lang=c is already present like I proposed {{#tag:syntaxhighlight|{{{1}}}|lang= above.
  • Template programming functionality is as best as possible yet.
Your real problem is easy escaping of {{bbox}} since those curly brackets are colliding with Wiki syntax.
  • The solution is quite easy:
{{OverpassTurboExample|query=<nowiki>node["amenity"="post_box"]({{bbox}});</nowiki>}}
  • The enclosing nowiki-tags when providing the query= parameter will be stripped off before their content is passed as {{{query}}} inside the programming.
  • However, they will prevent {{bbox}} getting evaluated too early. It is intended that this should happen in advance usually. When returning, their remainders won’t get expanded any longer.
  • Should work. Enjoy.
  • Entire code should be able to be wrapped this way without touching anything inside.
The basic idea when this thread has been opened was fine: enclosing the program code into something to prevent from evaluation.
  • However, syntaxhighlight is a very powerful thing that does a lot of analysis and produces sophisticated output with error messages and highlighting. That is parked outside the wikitext and merged later when the HTML document is composed.
  • 15 years old nowiki has the same disabling effect, but on low level, just hiding its content once from parsing, and bye.
Greetings --PerfektesChaos (talk) 17:01, 6 May 2017 (UTC)
Thanks a lot for the detailed feedback! We have tried the nowiki approach before. Unfortunately, it has one major shortcoming: the source code is no longer passed on to the URL parameter: https://wiki.openstreetmap.org/wiki/User:Mmd/TurboExample - so our initial requirement If you click on the hyperlink, you'll see exactly the same source code as shown on the wiki page. is no longer fulfilled using this approach. Not sure if there's some way to get around this limitation? Mmd-osm (talk) 07:29, 7 May 2017 (UTC)
  • Sorry, my approach has the same pitfall like syntaxhighlight, since nowiki also gets some kind of escaping and is not available for a parser function. I never noticed that yet.
  • Then, the only way to relieve your burden a bit is as follows:
    • Create an auxilary template:bracketed (or any other name).
    • Implement that as follows:
      <onlyinclude>{{<nowiki />{{{1}}}<nowiki />}}</onlyinclude>
    • Transclude
{{OverpassTurboExample|query=node["amenity"="post_box"]({{bracketed|bbox}});}}
  • At least slightly more readable.
  • The last remainig syntax with no extra template would be:
{{OverpassTurboExample|query=node["amenity"="post_box"]({<nowiki />{bbox}<nowiki />});}}

Good luck --PerfektesChaos (talk) 15:02, 7 May 2017 (UTC)

Many thanks for the suggestions - that's helpful! The developer of https://github.com/tyrasd/overpass-turbo has included an 'escaping mechanism' that essentially does this https://github.com/bjohas/mediawikiencode. Many thanks. Bjohas (talk) 10:43, 8 May 2017 (UTC)