Help:Contributing/code: Difference between revisions
Jump to navigation
Jump to search
Options table with live examples, Markdown comparison table, SPARQL examples; drop code-snippet vocabulary note (via update-page on MediaWiki MCP Server) |
Discourage <code> via blockquote note (does not escape HTML), recommend <syntaxhighlight inline>; make page translatable (via update-page on MediaWiki MCP Server) |
||
| Line 1: | Line 1: | ||
<languages/> | |||
<translate> | |||
<!--T:1--> | |||
Code appears on this wikibase in two places: as content on wiki pages (this page) and as data (code-snippet items created via [[Special:AddCodeSnippet]]). This page covers the former. | Code appears on this wikibase in two places: as content on wiki pages (this page) and as data (code-snippet items created via [[Special:AddCodeSnippet]]). This page covers the former. | ||
</translate> | |||
== Code blocks on wiki pages == | == <translate><!--T:2--> Code blocks on wiki pages</translate> == | ||
{| class="wikitable" style="width:100%" | {| class="wikitable" style="width:100%" | ||
|- | |- | ||
! Description !! You type !! You get | ! <translate><!--T:3--> Description</translate> !! <translate><!--T:4--> You type</translate> !! <translate><!--T:5--> You get</translate> | ||
|- | |- | ||
| Plain preformatted block — text exactly as typed, no wiki markup interpreted, HTML escaped. Safe for pasting raw code. | | <translate><!--T:6--> Plain preformatted block — text exactly as typed, no wiki markup interpreted, HTML escaped. Safe for pasting raw code.</translate> | ||
| <pre>print("hello")</pre> | | <translate><!--T:7--> <pre>print("hello")</pre></translate> | ||
| <pre>print("hello")</pre> | | <pre>print("hello")</pre> | ||
|- | |- | ||
| Syntax-highlighted block — Pygments highlighting and a copy button (top right of the block). | | <translate><!--T:8--> Syntax-highlighted block — Pygments highlighting and a copy button (top right of the block).</translate> | ||
| <syntaxhighlight lang="sparql">SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight> | | <translate><!--T:9--> <syntaxhighlight lang="sparql">SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight></translate> | ||
| <syntaxhighlight lang="sparql">SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight> | | <syntaxhighlight lang="sparql">SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight> | ||
|- | |- | ||
| Inline code — monospace within a sentence. | | <translate><!--T:10--> Inline code — monospace within a sentence.</translate> | ||
| < | | <translate><!--T:11--> <syntaxhighlight lang="python" inline>print("hello")</syntaxhighlight></translate> | ||
| < | | <syntaxhighlight lang="python" inline>print("hello")</syntaxhighlight> | ||
|} | |} | ||
== | <blockquote><translate> | ||
<!--T:12--> | |||
You may see <tvar name="code"><code><code>…</code></code></tvar> used for inline code elsewhere — in older wiki content and in MediaWiki documentation. It does not escape HTML: text such as <tvar name="lt"><code><</code></tvar> or <tvar name="amp"><code>&</code></tvar> inside it is read as markup. Prefer <tvar name="shi"><code><syntaxhighlight inline></code></tvar> instead. | |||
</translate></blockquote> | |||
== <translate><!--T:13--> Which languages can be highlighted</translate> == | |||
* <code>sparql</code> — SPARQL queries (this wiki's query service) | <translate> | ||
* <code>python</code>, <code>javascript</code>, <code>bash</code>, <code>sql</code>, <code>json</code>, <code>wikitext</code> — commonly used | <!--T:14--> | ||
Any Pygments lexer alias works as the <tvar name="lang"><code>lang</code></tvar> attribute — use the lowercase canonical name: | |||
</translate> | |||
<translate> | |||
<!--T:15--> | |||
* <tvar name="l1"><code>sparql</code></tvar> — SPARQL queries (this wiki's query service) | |||
* <tvar name="l2"><code>python</code></tvar>, <tvar name="l3"><code>javascript</code></tvar>, <tvar name="l4"><code>bash</code></tvar>, <tvar name="l5"><code>sql</code></tvar>, <tvar name="l6"><code>json</code></tvar>, <tvar name="l7"><code>wikitext</code></tvar> — commonly used | |||
* Full list: [https://pygments.org/docs/lexers/ Pygments lexers] (use the alias column) | * Full list: [https://pygments.org/docs/lexers/ Pygments lexers] (use the alias column) | ||
</translate> | |||
== Options for highlighted blocks == | == <translate><!--T:16--> Options for highlighted blocks</translate> == | ||
{| class="wikitable" style="width:100%" | {| class="wikitable" style="width:100%" | ||
|- | |- | ||
! Option !! You type !! You get | ! <translate><!--T:17--> Option</translate> !! <translate><!--T:18--> You type</translate> !! <translate><!--T:19--> You get</translate> | ||
|- | |- | ||
| <code>line</code> — show line numbers | | <translate><!--T:20--> <tvar name="o1"><code>line</code></tvar> — show line numbers</translate> | ||
| <syntaxhighlight lang="sparql" line>SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}</syntaxhighlight> | | <translate><!--T:21--> <syntaxhighlight lang="sparql" line>SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}</syntaxhighlight></translate> | ||
| <syntaxhighlight lang="sparql" line>SELECT ?item WHERE { | | <syntaxhighlight lang="sparql" line>SELECT ?item WHERE { | ||
?item wdt:P1 wd:Q6 . | ?item wdt:P1 wd:Q6 . | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
|- | |- | ||
| <code>start="{int}"</code> — first line number (with <code>line</code>) | | <translate><!--T:22--> <tvar name="o2"><code>start="{int}"</code></tvar> — first line number (with <tvar name="o2b"><code>line</code></tvar>)</translate> | ||
| <syntaxhighlight lang="sparql" line start="5">SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}</syntaxhighlight> | | <translate><!--T:23--> <syntaxhighlight lang="sparql" line start="5">SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}</syntaxhighlight></translate> | ||
| <syntaxhighlight lang="sparql" line start="5">SELECT ?item WHERE { | | <syntaxhighlight lang="sparql" line start="5">SELECT ?item WHERE { | ||
?item wdt:P1 wd:Q6 . | ?item wdt:P1 wd:Q6 . | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
|- | |- | ||
| <code>highlight</code> — highlight specific lines (comma-separated) | | <translate><!--T:24--> <tvar name="o3"><code>highlight</code></tvar> — highlight specific lines (comma-separated)</translate> | ||
| <syntaxhighlight lang="sparql" highlight="1,3">SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}</syntaxhighlight> | | <translate><!--T:25--> <syntaxhighlight lang="sparql" highlight="1,3">SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}</syntaxhighlight></translate> | ||
| <syntaxhighlight lang="sparql" highlight="1,3">SELECT ?item WHERE { | | <syntaxhighlight lang="sparql" highlight="1,3">SELECT ?item WHERE { | ||
?item wdt:P1 wd:Q6 . | ?item wdt:P1 wd:Q6 . | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
|- | |- | ||
| <code>inline</code> — render as inline code instead of a block | | <translate><!--T:26--> <tvar name="o4"><code>inline</code></tvar> — render as inline code instead of a block</translate> | ||
| <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight> | | <translate><!--T:27--> <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight></translate> | ||
| <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight> | | <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight> | ||
|- | |- | ||
| <code>copy</code> — adds a button that copies the block | | <translate><!--T:28--> <tvar name="o5"><code>copy</code></tvar> — adds a button that copies the block</translate> | ||
| <syntaxhighlight lang="sparql" copy>SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight> | | <translate><!--T:29--> <syntaxhighlight lang="sparql" copy>SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight></translate> | ||
| <syntaxhighlight lang="sparql" copy>SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight> | | <syntaxhighlight lang="sparql" copy>SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight> | ||
|} | |} | ||
<blockquote><code>inline</code> and <code>copy</code> are mutually exclusive.</blockquote> | <blockquote><translate> | ||
<!--T:30--> | |||
<tvar name="i"><code>inline</code></tvar> and <tvar name="c"><code>copy</code></tvar> are mutually exclusive. | |||
</translate></blockquote> | |||
<translate> | |||
<!--T:31--> | |||
Full parameter reference: [https://www.mediawiki.org/wiki/Extension:SyntaxHighlight#Parameters SyntaxHighlight extension parameters] (official). | Full parameter reference: [https://www.mediawiki.org/wiki/Extension:SyntaxHighlight#Parameters SyntaxHighlight extension parameters] (official). | ||
</translate> | |||
== Differences from Markdown == | == <translate><!--T:32--> Differences from Markdown</translate> == | ||
{| class="wikitable" style="width:100%" | {| class="wikitable" style="width:100%" | ||
|- | |- | ||
! | ! <translate><!--T:33--> Markdown habit</translate> !! <translate><!--T:34--> On this wiki</translate> | ||
|- | |- | ||
| | | <translate><!--T:35--> Fenced code blocks (<tvar name="f1"><code>```</code></tvar>)</translate> | ||
| < | | <translate><!--T:36--> There are no fenced blocks — <tvar name="f2"><code>```</code></tvar> does nothing. Use <tvar name="f3"><code><pre></code></tvar> or <tvar name="f4"><code><syntaxhighlight></code></tvar>.</translate> | ||
|- | |- | ||
| Literal <code>[[</code> and <code>{{</code> inside a fenced block | | <translate><!--T:37--> Literal <tvar name="l1"><code>[[</code></tvar> and <tvar name="l2"><code>{{</code></tvar> inside a fenced block</translate> | ||
| In <code><pre></code> and <code><syntaxhighlight></code> wiki markup is not interpreted — <code>{{</code>, <code>[[</code> and quotes appear literally, which is what you want for code. | | <translate><!--T:38--> In <tvar name="p"><code><pre></code></tvar> and <tvar name="s"><code><syntaxhighlight></code></tvar> wiki markup is not interpreted — <tvar name="l3"><code>{{</code></tvar>, <tvar name="l4"><code>[[</code></tvar> and quotes appear literally, which is what you want for code.</translate> | ||
|} | |} | ||
<translate> | |||
<!--T:39--> | |||
Blocks larger than 100 kB lose highlighting automatically (a safety guard). | Blocks larger than 100 kB lose highlighting automatically (a safety guard). | ||
</translate> | |||
== House rules for code content == | == <translate><!--T:40--> House rules for code content</translate> == | ||
* Prefer <code><syntaxhighlight lang="…"></code> for anything syntax-highlightable; use <code><pre></code> for config dumps and logs where colours add noise. | <translate> | ||
<!--T:41--> | |||
* Prefer <tvar name="sh"><code><syntaxhighlight lang="…"></code></tvar> for anything syntax-highlightable; use <tvar name="pre"><code><pre></code></tvar> for config dumps and logs where colours add noise. | |||
</translate> | |||
== See also == | == <translate><!--T:42--> See also</translate> == | ||
<translate> | |||
<!--T:43--> | |||
* [https://www.mediawiki.org/wiki/Help:Formatting MediaWiki syntax 101] (official) | * [https://www.mediawiki.org/wiki/Help:Formatting MediaWiki syntax 101] (official) | ||
* [https://www.mediawiki.org/wiki/Extension:SyntaxHighlight SyntaxHighlight extension] (official) | * [https://www.mediawiki.org/wiki/Extension:SyntaxHighlight SyntaxHighlight extension] (official) | ||
* [https://pygments.org/docs/lexers/ Pygments lexers] — full language list | * [https://pygments.org/docs/lexers/ Pygments lexers] — full language list | ||
* [[Help:Contributing]] — contributing to this wiki | * [[Help:Contributing]] — contributing to this wiki | ||
</translate> | |||
Revision as of 08:18, 19 August 2026
Code appears on this wikibase in two places: as content on wiki pages (this page) and as data (code-snippet items created via Special:AddCodeSnippet). This page covers the former.
Code blocks on wiki pages
| Description | You type | You get |
|---|---|---|
| Plain preformatted block — text exactly as typed, no wiki markup interpreted, HTML escaped. Safe for pasting raw code. | <pre>print("hello")</pre> | print("hello")
|
| Syntax-highlighted block — Pygments highlighting and a copy button (top right of the block). | <syntaxhighlight lang="sparql">SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight> | SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5
|
| Inline code — monospace within a sentence. | <syntaxhighlight lang="python" inline>print("hello")</syntaxhighlight> | print("hello")
|
You may see
<code>…</code>used for inline code elsewhere — in older wiki content and in MediaWiki documentation. It does not escape HTML: text such as<or&inside it is read as markup. Prefer<syntaxhighlight inline>instead.
Which languages can be highlighted
Any Pygments lexer alias works as the lang attribute — use the lowercase canonical name:
sparql— SPARQL queries (this wiki's query service)python,javascript,bash,sql,json,wikitext— commonly used- Full list: Pygments lexers (use the alias column)
Options for highlighted blocks
| Option | You type | You get |
|---|---|---|
line — show line numbers
|
<syntaxhighlight lang="sparql" line>SELECT ?item WHERE { ?item wdt:P1 wd:Q6 . }</syntaxhighlight> |
SELECT ?item WHERE {
?item wdt:P1 wd:Q6 .
}
|
start="{int}" — first line number (with line)
|
<syntaxhighlight lang="sparql" line start="5">SELECT ?item WHERE { ?item wdt:P1 wd:Q6 . }</syntaxhighlight> |
SELECT ?item WHERE {
?item wdt:P1 wd:Q6 .
}
|
highlight — highlight specific lines (comma-separated)
|
<syntaxhighlight lang="sparql" highlight="1,3">SELECT ?item WHERE { ?item wdt:P1 wd:Q6 . }</syntaxhighlight> |
SELECT ?item WHERE {
?item wdt:P1 wd:Q6 .
}
|
inline — render as inline code instead of a block
|
<syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight> | LIMIT
|
copy — adds a button that copies the block
|
<syntaxhighlight lang="sparql" copy>SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight> | SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5
|
inlineandcopyare mutually exclusive.
Full parameter reference: SyntaxHighlight extension parameters (official).
Differences from Markdown
| Markdown habit | On this wiki |
|---|---|
Fenced code blocks (```)
|
There are no fenced blocks — ``` does nothing. Use <pre> or <syntaxhighlight>.
|
Literal [[ and {{ inside a fenced block
|
In <pre> and <syntaxhighlight> wiki markup is not interpreted — {{, [[ and quotes appear literally, which is what you want for code.
|
Blocks larger than 100 kB lose highlighting automatically (a safety guard).
House rules for code content
- Prefer
<syntaxhighlight lang="…">for anything syntax-highlightable; use<pre>for config dumps and logs where colours add noise.
See also
- MediaWiki syntax 101 (official)
- SyntaxHighlight extension (official)
- Pygments lexers — full language list
- Help:Contributing — contributing to this wiki