Topic on Project:Support desk

Background highlighting inside a designated code snippet?

6
208.167.254.13 (talkcontribs)

Is there a way to background highlight certain words/characters within a code snippet?  I've been using <pre> and <nowiki> for defining code snippets but they don't allow code to be parsed. I have a lot of code to display but need to bring attention only to small portions of it for the reader's convenience.

Ciencia Al Poder (talkcontribs)

There's no easy way to do that. Most you can do is start each line with at least one space. This will use monospace font similar to <pre> but will allow markup to be parsed. You'll probably need to use <nowiki> when needed to prevent special markup on the code from being parsed.

87.123.33.161 (talkcontribs)

Highlighting single characters in preformatted text contradicts the principle of preformatting. Since it's preformatted, there should not be other styles for that text.

Another option is installing and using the Extension:SyntaxHighlight, which provides syntax highlighting for complete languages, e.g. for PHP or SQL code.

121.214.39.65 (talkcontribs)

False. <pre> just means "don't mess with the white-space"; you're allowed to have HTML inside to add additional formatting. In fact the W3C even uses a <pre> tag with a <code> tag inside it to mark up pre-formatted blocks of code as an example.

121.214.39.65 (talkcontribs)

You can use the comment hack to make pre tags be interpreted as plain HTML rather than an extension tag, which allows HTML and wikitext inside.

<pre<!---->>
Text
<span style=background:lightblue>'''Highlighted''' text</span>
</pre>

<pre> Text Highlighted text </pre>

121.214.39.65 (talkcontribs)

However it appears to not work with VisualEditor. It does work on a real wiki page though.

Reply to "Background highlighting inside a designated code snippet?"