Help:Syntax Highlighting: Difference between revisions

From Rosetta Code
Content added Content deleted
(Removed xfeeds tag)
m (→‎Supported languages: left a word out)
 
(40 intermediate revisions by 27 users not shown)
Line 1: Line 1:
This wiki supports syntax highlighting in code examples using the [http://sourceforge.net/projects/geshi/ GeSHi] project, at the time of writing this version 1.0.7.22.
Rosetta Code supports syntax highlighting of code using [https://pygments.org/ Pygments].


== How to highlight ==
(''FIXME'': How can one find out which version is currently really installed? [[Special:Version]] doesn't seem to list it).
Prefix your code with <code>&lt;syntaxhighlight lang="foo"&gt;</code> (where ''foo'' is the language to use for highlighting) and suffix it with <code>&lt;/syntaxhighlight&gt;</code>. Be sure not to insert extra space between the tags and your code. For example, here's how you'd highlight a C program:

At the moment, the wiki is in transition from old style to new style highlighting; old style highlighting will still be valid for some time.

== New style ==

Code examples are enclosed by '''&lt;code&gt;''' tags. The '''lang''' argument selects the language used for highlighting. Example:


<pre>
<pre>
<syntaxhighlight lang="c">int main (int argc, char* argv[])
<code lang="C">
int main (int argc, char* argv[])
{
{
return 0;
return 0;
}</syntaxhighlight>
}
</code>
</pre>
</pre>


That markup would be rendered thus:
looks like


<syntaxhighlight lang="c">int main (int argc, char* argv[])
<code lang="C">
{
int main (int argc, char* argv[])
return 0;
{
}</syntaxhighlight>
return 0;
}
</code>


== Supported languages ==
A list of supported languages, and their corresponding PHP files for GeSHi, can be found here:
Make sure to use the standard identifier for your language even if Pygments doesn't know how to highlight it yet. If your language isn't in the list, make up an identifier (consisting entirely of lowercase alphanumeric ASCII characters; make it short yet unambiguous) and add it to the list. That way, if your language is supported in a future release of Pygments, Rosetta Code can immediately benefit.
http://geshi.svn.sourceforge.net/viewvc/geshi/tags/RELEASE_1_0_7_22/geshi-1.0.X/src/geshi/


Also, if it hasn't been done yet, add the identifier to your language's category page. Use the "tags" parameter in the [[Template:Language|language template]] to specify it (e.g. "tags=foo" for the example above). If you find that your language already has a tag listed there that isn't in the Village Pump post, add it to the post as well.
Ideally, one should also use the &lt;code&gt;-tags with an appropriate lang argument for languages that are not yet supported, because that will avoid additional editing if these languages will become supported in a future release of GeSHi.


If you'd like to, it's not difficult to write a new language definition for Pygments. See [https://pygments.org/docs/lexerdevelopment/ Write your own lexer] for more on adding support for a new language.
It's in principle possible to write additional PHP files for unsupported languages and include them. Please contact one of the administrators for details.


<div style="background-color:#eee;padding:1em;>
== Old style ==
'''''Note:''' There will likely be significant lag between a new lexer being added to Pygments, and the new syntax highlighting being available on Rosetta Code.''


''Once the new lexer is added to Pygments, it won't be available until Pygments has a release.''
Code examples are enclosed in tags named after the language, as in


''We would then need to wait for MediaWiki to bump the Pygments version in the package and release a new version.''
<pre>
<c>
int main (int argc, char* argv[])
{
return 0;
}
</c>
</pre>


'''''Then''' we would need to wait for Miraheze to update to the version of MediaWiki software that includes the updated Pygments package.''
That renders as


''A multiple month delay is expected, a several year delay is not out of the question.''</div>
<c>

int main (int argc, char* argv[])
===Recommended language tag usage===
{

return 0;
''Rather than trying to keep disparate lists in sync, refer directly to the official Pygments documentation.''
}
</c>


[https://pygments.org/languages/ Official language list]
The available tags are (from [[Special:Version]]):


In general, the language parameter should be the lowercase language name bounded by double quotes.
<nowiki>
<actionscript-french>, <actionscript>, <ada>, <apache>, <applescript>, <asm>, <asp>, <bash>, <blitzbasic>, <caddcl>, <cadlisp>, <c_mac>, <c>, <cpp>, <csharp>, <css>, <delphi>, <diff>, <dos>, <d>, <eiffel>, <freebasic>, <gml>, <html4strict>, <ini>, <inno>, <java>, <javascript>, <lisp>, <lua>, <matlab>, <mpasm>, <mysql>, <nsis>, <objc>, <ocaml-brief>, <ocaml>, <oobas>, <oracle8>, <pascal>, <perl>, <php-brief>, <php>, <python>, <qbasic>, <ruby>, <scheme>, <sdlbasic>, <smarty>, <sql>, <vbnet>, <vb>, <vhdl>, <visualfoxpro>, <xml>
</nowiki>

Latest revision as of 23:55, 3 May 2023

Rosetta Code supports syntax highlighting of code using Pygments.

How to highlight

Prefix your code with <syntaxhighlight lang="foo"> (where foo is the language to use for highlighting) and suffix it with </syntaxhighlight>. Be sure not to insert extra space between the tags and your code. For example, here's how you'd highlight a C program:

<syntaxhighlight lang="c">int main (int argc, char* argv[])
{
  return 0;
}</syntaxhighlight>

That markup would be rendered thus:

int main (int argc, char* argv[])
{
  return 0;
}

Supported languages

Make sure to use the standard identifier for your language even if Pygments doesn't know how to highlight it yet. If your language isn't in the list, make up an identifier (consisting entirely of lowercase alphanumeric ASCII characters; make it short yet unambiguous) and add it to the list. That way, if your language is supported in a future release of Pygments, Rosetta Code can immediately benefit.

Also, if it hasn't been done yet, add the identifier to your language's category page. Use the "tags" parameter in the language template to specify it (e.g. "tags=foo" for the example above). If you find that your language already has a tag listed there that isn't in the Village Pump post, add it to the post as well.

If you'd like to, it's not difficult to write a new language definition for Pygments. See Write your own lexer for more on adding support for a new language.

Note: There will likely be significant lag between a new lexer being added to Pygments, and the new syntax highlighting being available on Rosetta Code.

Once the new lexer is added to Pygments, it won't be available until Pygments has a release.

We would then need to wait for MediaWiki to bump the Pygments version in the package and release a new version.

Then we would need to wait for Miraheze to update to the version of MediaWiki software that includes the updated Pygments package.

A multiple month delay is expected, a several year delay is not out of the question.

Recommended language tag usage

Rather than trying to keep disparate lists in sync, refer directly to the official Pygments documentation.

Official language list

In general, the language parameter should be the lowercase language name bounded by double quotes.