Skip to content

Add: optional syntax highlighting with pygments#26

Merged
TrueBrain merged 3 commits intoTrueBrain:mainfrom
Qrbaker:main
May 25, 2022
Merged

Add: optional syntax highlighting with pygments#26
TrueBrain merged 3 commits intoTrueBrain:mainfrom
Qrbaker:main

Conversation

@Qrbaker
Copy link
Copy Markdown
Contributor

@Qrbaker Qrbaker commented Apr 2, 2022

This PR adds the option to render the MediaWiki <syntaxhighlight> tag. Currently it supports the "lang" and "line " options of the feature. Note that if lang is not supplied, the processor will make a best guess and highlight based on heuristics.

By default, anything downstream will not see any changed behavior. If downstream projects wish to use the new feature, they may do so by calling the prepare() method with the should_color_syntax argument set to True.

I have a companion PR ready for TrueWiki that will add this functionality and expose a setting in the .truewiki.yml file to toggle it. If toggled off, wikitexthtml converts any instances of <syntaxhighlight></syntaxhighlight> pairs to <pre> tags.

Here are some examples of what this looks like in practice:

  • Syntax highlighting with default options
    code_ex_2_nolines

  • Syntax highlighting with line option
    code_ex_1

  • Syntax highlighting with line 6800 option
    code_ex_3_anystart

I should also note that without CSS, these blocks will look identical to <pre> blocks. Pygments has the ability to both output css styling to be piped to a file, or it can render it inline with the html.

The examples above were rendered with the following CSS (applied in a second file in addition to the default wiki css in TrueWiki):
https://gist.github.com/Qrbaker/424bdd7d48b88557be1340eb208a7c6f

Copy link
Copy Markdown
Owner

@TrueBrain TrueBrain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like what you did here. Some coding-style stuff mostly. The summary of it is: name things as they are ;)

Qrbaker added a commit to Qrbaker/wikitexthtml that referenced this pull request Apr 11, 2022
@TrueBrain
Copy link
Copy Markdown
Owner

Two minor flake8 warnings left; otherwise good to go as far as I am concerned!

@Qrbaker
Copy link
Copy Markdown
Contributor Author

Qrbaker commented Apr 21, 2022

Two minor flake8 warnings left; otherwise good to go as far as I am concerned!

I removed the unused import, but I don't know if the second is fixable. Is is possible to break up the regex without ruining it?

@TrueBrain
Copy link
Copy Markdown
Owner

Two minor flake8 warnings left; otherwise good to go as far as I am concerned!

I removed the unused import, but I don't know if the second is fixable. Is is possible to break up the regex without ruining it?

(sorry for the slow replies; it is a bit busy on my side).

Hmm, I do not know :D But honestly, I am not that fuzzed the line is too long; it makes total sense to have it as a single line in this case. So I am also totally fine if you add a noqa tag to it (or what-ever the way is to make flake8 happy :P).

@TrueBrain
Copy link
Copy Markdown
Owner

Kind reminder; just a small thing left to get this merged :D

@Qrbaker
Copy link
Copy Markdown
Contributor Author

Qrbaker commented May 25, 2022

Kind reminder; just a small thing left to get this merged :D

Yeah, sorry about the wait. I did figure out that long regexps can be split using normal string literal concatenation: https://stackoverflow.com/a/8006576

@TrueBrain TrueBrain merged commit c481b51 into TrueBrain:main May 25, 2022
@TrueBrain
Copy link
Copy Markdown
Owner

Tnx again for this work! I will make a release out of this soon so we can update TrueWiki :)

@TrueBrain
Copy link
Copy Markdown
Owner

Right, released a new wikitexthtml, and bumped it in TrueWiki. @Qrbaker : you had a commit ready to enable it in TrueWiki? :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants