Jump to content

Extension talk:SyntaxHighlight

About this board

Previous discussion was archived at Extension talk:SyntaxHighlight/Archive 2017 on 2017-03-29.

I would like to use dracula with SyntaxHightlight

2
Bibble 235 (talkcontribs)

Hi good people

Really really new to MediaWiki, the software.

I want to optional use the pygments style dracula with some of my Syntaxhighlight entries on my wiki

So far I have found the -S option in includes/Pygmentize.php but this of course changes it for all usage of SyntaxHighlight so my next approach was to add a parameter pstyle to SyntaxHighlight and choose when to use it . But I am unclear how to pass something to updateCSS.php

<syntaxhighlight lang="cpp" pstyle="dracula">

int main(int a, char* args[]) {

// Test

  return 0;

}

</syntaxhighlight>

BDavis (WMF) (talkcontribs)
Reply to "I would like to use dracula with SyntaxHightlight"
SomeArthena (talkcontribs)

Hello, I am reading everywhere it is not possible to embed links into code blocks.

However, I have seen it being done on cppreference.com, and they also use this extension, so there must be some hack or something.

I don't mind if I have to update something inside the extension.

PerfektesChaos (talkcontribs)

By definition any content than </syntaxhighlight> will be produced as regular text on wiki page.

  • However, people might post-process the result of this extension.
  • They could interact on server side with the API of the pygments software.
  • They could manipulate the generated HTML document on client side by JavaScript, identifying interesting things like URL and link those URL, or other connected formats and identified items. Every token is an HTML element equipped with a class, which makes it easy to find certain types of items, analyse this text and replace the plain text content by a HTML link to somewhere.
  • You might even try to integrate HTML link generation into your language.

Greetings

213.147.167.226 (talkcontribs)

Ah got it, so I won't get around writing my own extension.

Thanks for the elaborate answer, appreciate it. :)

BDavis (WMF) (talkcontribs)

cppreference.com is running a version of MediaWiki and SyntaxHighlight from 2013, so feature comparisons are questionable in any case. The version of SyntaxHighlight in use there would have still used GeSHi as the highlighting engine. The modern extension uses Pygments which is not only a different implementation, it is also written in a different programming language.

PerfektesChaos (talkcontribs)

Example for JavaScript postprocessing:

  • de:Hilfe:Textgestaltung/HTML
  • Find all elements (which should be descendants of the <syntaxhighlight> blocks) which match the span.nt selector.
  • Read the content of the span, create an URL to a website explaining HTML elements, linking this particular tag name. Wrap the span into an a href="" element.

Enjoy

Reply to "Links in code block"

Formatting within <syntaxhighlight>

2
Tom.Reding (talkcontribs)

Is it possible to format text within syntaxhighlight? I would like to keep this italicization of "parameter1", while syntaxhighlighting "Template name":

{{Template name|parameter1}}

  ~ Tom.Reding (talk )  11:31, 28 July 2024 (UTC)

Tacsipacsi (talkcontribs)

I don’t think so. The purpose of syntax highlighting is that it highlights syntax rather than interpreting it. How would it know what to highlight ({{) and what to interpret ('')?

By the way, if the template is really that simple (just a template name and one or a few parameters), IMHO syntax highlighting is unnecessary: it makes the code block stand out too much (color really stands out) without helping much understanding it.

Reply to "Formatting within <syntaxhighlight>"

No visual pygments with this enabled

3
Tr1ageNYC (talkcontribs)
BDavis (WMF) (talkcontribs)

The rendered HTML for your Common.css page indicates that SyntaxHighlight did not successfully run. If it had, within the <pre> child of the <div class="mw-highlight ..."> element there would be a large number of <span> elements wrapping various syntactical constructs of the CSS content.

There could be a number of issues causing this failure:

  • /path/to/extensions/SyntaxHighlight_GeSHi/pygments/pygmentize script not marked as executable
  • No python3 interpreter in the PHP runtime's PATH
  • PHP runtime configured to disable shell_exec
  • Other things I'm not thinking of right now...

None of these would prevent Special:Version from showing the extension as installed or using the version of pygments bundled in the extension. There should however be some error logging via wfWarn that says something like "Failed to invoke Pygments: ..." or "Invoking Pygments returned blank output with no error response". See Manual:How to debug#Logging for help on configuring your wiki to write debug logs to a file where you can examine them.

Tr1ageNYC (talkcontribs)

It looks like my host doesn't have luasandbox installed and won't so that is likely it.

Reply to "No visual pygments with this enabled"

Highlight lines of colour in the same block but using different colours

2
GwynethLlewelyn (talkcontribs)

Hi there!

This talk page is insanely long, so bear with me if my question has already been answered (a quick search revealed nothing).

I'm editing a MediaWiki installation where I have no access whatsoever to its configuration, not to mention its actual files. Thus, I'm looking for a user-centred solution to this apparently simple issue: how to use different colour highlights?

Let me try to explain better. With the highlight parameter, we have the option of selecting whatever lines (even blocks of lines) we wish — but the highlighting colour is always the same (default seems to be 'marker yellow').

However, in the wiki I'm editing, one of the users requires explaining how different areas of the code work together, using different colours to highlight different sections. This is actually something I have seen in several examples elsewhere (not on wikis, mind you!) and is not uncommon in certain academic environments, to attract the student's attention to how some parts of the code connect to others.

I wonder, therefore, if such a functionality is possible using the current implementation of the SyntaxHighlight extension, or if it's the kind of thing that would require either a rewriting of the code (e.g., by adding extra parameters/attributes to the <syntaxhighlight> tag), the CSS file associated with a particular page, etc. Or if this is something already built-in (Pygments, at least, doesn't mention it, so it would be something developed on top of it, at the extension level itself), how could it be triggered?

A few alternatives, of course, come to mind, namely, stopping the flow of execution inside a <syntaxhighlight> block (possibly using one of those {{#tag}} blocks which seem to be so popular to 'fix' certain aspects of the rendering procedure) so that, say, the next line gets a different background colour from the standard yellowish one. I can conceive of a few ideas on how that could be implemented, but I'm not really sure how to apply them (I need to learn a bit more about #tag, too, and see if I have access to it on the wiki I'm working). So, hopefully, someone reads this and answers with a one-liner trick to get it working :)

Thank you all in advance for whatever insights you might share!

BDavis (WMF) (talkcontribs)

It is possible to use Extension:TemplateStyles to inject CSS into a page that alters colors for content marked up by SyntaxHighlight. Template:Codesample is one example of this. hll is the CSS class applied to highlighted lines.

There is not currently any mechanism to add additional CSS classes or ids to individual lines/ranges of lines highlighted via the highlight=... attribute. This would be necessary to allow you then target each line/range of lines with a different color via CSS.

It may be possible to approximate your desired solution by placing each highlighted line/range of lines in a separate <syntaxhighlight> tag on your page.

Another very hacky solution might be using styles like .hll:has(> .linenos[data-line="7"]) { background-color: purple} assuming you are also showing line numbers in the code blocks via the line attribute. TemplateStyles does not currently support the :has() pseudo-class, so this approach would need to use site wide CSS which makes it of very limited use.

Reply to "Highlight lines of colour in the same block but using different colours"

inline with empty-string parameter after editing with Visual Editor

3
Amire80 (talkcontribs)

I created this version using wikitext editing, and it had <syntaxhighlight inline lang="perl">use strict;</syntaxhighlight>. Note the inline attribute without any parameter.

Then I edited the page using VE. I only changed the content of the tag. When I saved, inline changed to inline="". Is that correct?

The final rendering is the same, but what is the right way to store the attribute code? The documentation at Extension:SyntaxHighlight has no parameter at all.

BDavis (WMF) (talkcontribs)

The PHP code only checks to see if an "inline" attribute has been provided in the tag definition. No check is done to see if there is an associated value or not. It is relatively common to see HTML linters that get confused about bare attributes like "selected" and "checked" to recommend `attribute="attribute"` usage. There is nothing technical wrong with VE using `attribute=""` in this similar situation.

It does sound like behavior that could be considered a dirty diff from parsoid round tripping.

Tacsipacsi (talkcontribs)
Reply to "inline with empty-string parameter after editing with Visual Editor"

Is it always installed now?

2
Amire80 (talkcontribs)

The page says: "This extension comes with MediaWiki 1.21 and above".

Does this mean that I can use the <syntaxhighlight> tag in something that another extension outputs and be sure that it will work?

BDavis (WMF) (talkcontribs)
Reply to "Is it always installed now?"

</syntaxhighlight> within <syntaxhighlight>

3
One cookie (talkcontribs)

Re. the edit of 16 January 2024:

Hiya @Jdforrester (WMF): - in the edit you made, the unescaped <syntaxhighlight> tags were making the examples of the code render as the examples - I can't see a way to retain syntax highlighting while having the actual tags appear within the block of highlit code when created with normal tags (why would anyone ever need to escape characters between tags which are used to escape whole blocks of code?!) - the {{#tag:}} magic word works instead of the {{SyntaxHighlight source}} template, it looks a bit cleaner and it's how the page's other examples have done it, and like the template it will let its contents linewrap, which the raw tags don't - but I guess it looks similarly confusing in the source. Any suggestions for a better solution?

Jdforrester (WMF) (talkcontribs)
One cookie (talkcontribs)

They only got one - there was a second starting at line 267

Some lexers put pages using <syntaxhighlight> in the tracking category

5
Summary by Snowyamur9889

Forgot to set this as resolved from 8 months ago. Solution was to upgrade Pygments extension from 2.11.2 to 2.15.0.

Snowyamur9889 (talkcontribs)

Pygments lexers like "lua", "html", and "css" put pages using <syntaxhighlight> into "Category:Pages with syntax highlighting errors", despite these lexers being correct. Maybe there are other lexers causing this problem - I don't know. But the three I mentioned above are the ones I commonly use for template/module documentation.


I don't know why this happens. The wiki I contribute to runs on MediaWiki 1.39 with Extension:SyntaxHighlight version 2.0. The Pygments documentation states that these Short names: for the lexers I mentioned above are correct, so why are the pages that use them with <syntaxhighlight> placed in the tracking category?


Does anyone know?

Tacsipacsi (talkcontribs)

Does the code get highlighted? (It’s always good to share a link so that I can answer such simple questions on my own.) The error category gets added whenever the highlighting is unsuccessful – this includes not only incorrect lexer names, but also when the code is too big or calling Pygments fails for any reason.

Snowyamur9889 (talkcontribs)

The code gets highlighted, so I don't know why pages using <syntaxhighlight> end up in the tracking category. There are pages in the tracking category for this wiki that use syntax highlighting correctly but still end up categorized.

Tacsipacsi (talkcontribs)

Could you please provide a link? It’s very hard to debug without being able to see what exactly is happening.

Snowyamur9889 (talkcontribs)

I apologize for the 2-month late response User:Tacsipacsi. I tried replying sooner, but an error with Extension:StructuredDiscussions on MediaWiki was preventing me from entering a response about a month ago.


I just wanted to let you know that the lexers I defined 2 months ago as not working do work again. The pages on the wiki I contribute to no longer end up being incorrectly categorized under this extension's tracking category when using <syntaxhighlight> correctly.


This issue I had may have been fixed with a major version update to Pygments. During the time I had the categorization issue, this extension was using Pygments 2.11.2. It now uses Pygments 2.15.0 as of April 10 of this year (commit 9f9c13b).

Syntax highlight on Mediawiki without Python

2
Aoineko (talkcontribs)

Hello,

I can't install Python on my server (only running PHP/mySQL); is there a syntax highlighting solution that doesn't require Python?

I'm using Mediawiki 1.30.2.

Cavila (talkcontribs)

I don't know about MediaWiki support, but there's a couple of extensions listed at the bottom of the page that you could try out.

Reply to "Syntax highlight on Mediawiki without Python"