Help:Contributing/code: Difference between revisions

From Wikibase
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>
| &lt;pre&gt;print("hello")&lt;/pre&gt;
| <translate><!--T:7--> &lt;pre&gt;print("hello")&lt;/pre&gt;</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>
| &lt;syntaxhighlight lang="sparql"&gt;SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5&lt;/syntaxhighlight&gt;
| <translate><!--T:9--> &lt;syntaxhighlight lang="sparql"&gt;SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5&lt;/syntaxhighlight&gt;</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>
| &lt;code&gt;print("hello")&lt;/code&gt;
| <translate><!--T:11--> &lt;syntaxhighlight lang="python" inline&gt;print("hello")&lt;/syntaxhighlight&gt;</translate>
| <code>print("hello")</code>
| <syntaxhighlight lang="python" inline>print("hello")</syntaxhighlight>
|}
|}


== Which languages can be highlighted ==
<blockquote><translate>
<!--T:12-->
You may see <tvar name="code"><code>&lt;code&gt;…&lt;/code&gt;</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>&lt;</code></tvar> or <tvar name="amp"><code>&amp;</code></tvar> inside it is read as markup. Prefer <tvar name="shi"><code>&lt;syntaxhighlight inline&gt;</code></tvar> instead.
</translate></blockquote>


Any Pygments lexer alias works as the <code>lang</code> attribute — use the lowercase canonical name:
== <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>
| &lt;syntaxhighlight lang="sparql" line&gt;SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}&lt;/syntaxhighlight&gt;
| <translate><!--T:21--> &lt;syntaxhighlight lang="sparql" line&gt;SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}&lt;/syntaxhighlight&gt;</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>
| &lt;syntaxhighlight lang="sparql" line start="5"&gt;SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}&lt;/syntaxhighlight&gt;
| <translate><!--T:23--> &lt;syntaxhighlight lang="sparql" line start="5"&gt;SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}&lt;/syntaxhighlight&gt;</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>
| &lt;syntaxhighlight lang="sparql" highlight="1,3"&gt;SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}&lt;/syntaxhighlight&gt;
| <translate><!--T:25--> &lt;syntaxhighlight lang="sparql" highlight="1,3"&gt;SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}&lt;/syntaxhighlight&gt;</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>
| &lt;syntaxhighlight lang="sparql" inline&gt;LIMIT&lt;/syntaxhighlight&gt;
| <translate><!--T:27--> &lt;syntaxhighlight lang="sparql" inline&gt;LIMIT&lt;/syntaxhighlight&gt;</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>
| &lt;syntaxhighlight lang="sparql" copy&gt;SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5&lt;/syntaxhighlight&gt;
| <translate><!--T:29--> &lt;syntaxhighlight lang="sparql" copy&gt;SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5&lt;/syntaxhighlight&gt;</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%"
|-
|-
! Markdown habit !! On this wiki
! <translate><!--T:33--> Markdown habit</translate> !! <translate><!--T:34--> On this wiki</translate>
|-
| Fenced code blocks (<code>```</code>)
| There are no fenced blocks — <code>```</code> does nothing. Use <code>&lt;pre&gt;</code> or <code>&lt;syntaxhighlight&gt;</code>.
|-
|-
| Inline code (<code>`code`</code>)
| <translate><!--T:35--> Fenced code blocks (<tvar name="f1"><code>```</code></tvar>)</translate>
| <code>&lt;code&gt;</code> is raw HTML: the text inside is not escaped. Type <code>&amp;lt;</code> for a literal <code>&lt;</code>, <code>&amp;gt;</code> for <code>&gt;</code>, <code>&amp;amp;</code> for <code>&amp;</code>.
| <translate><!--T:36--> There are no fenced blocks — <tvar name="f2"><code>```</code></tvar> does nothing. Use <tvar name="f3"><code>&lt;pre&gt;</code></tvar> or <tvar name="f4"><code>&lt;syntaxhighlight&gt;</code></tvar>.</translate>
|-
|-
| Literal <code>&#91;&#91;</code> and <code>&#123;&#123;</code> inside a fenced block
| <translate><!--T:37--> Literal <tvar name="l1"><code>&#91;&#91;</code></tvar> and <tvar name="l2"><code>&#123;&#123;</code></tvar> inside a fenced block</translate>
| In <code>&lt;pre&gt;</code> and <code>&lt;syntaxhighlight&gt;</code> wiki markup is not interpreted — <code>&#123;&#123;</code>, <code>&#91;&#91;</code> and quotes appear literally, which is what you want for code.
| <translate><!--T:38--> In <tvar name="p"><code>&lt;pre&gt;</code></tvar> and <tvar name="s"><code>&lt;syntaxhighlight&gt;</code></tvar> wiki markup is not interpreted — <tvar name="l3"><code>&#123;&#123;</code></tvar>, <tvar name="l4"><code>&#91;&#91;</code></tvar> and quotes appear literally, which is what you want for code.</translate>
|}
|}


<translate>
<!--T:39-->
Blocks larger than 100&nbsp;kB lose highlighting automatically (a safety guard).
Blocks larger than 100&nbsp;kB lose highlighting automatically (a safety guard).
</translate>


== House rules for code content ==
== <translate><!--T:40--> House rules for code content</translate> ==


* Prefer <code>&lt;syntaxhighlight lang="…"&gt;</code> for anything syntax-highlightable; use <code>&lt;pre&gt;</code> for config dumps and logs where colours add noise.
<translate>
<!--T:41-->
* Prefer <tvar name="sh"><code>&lt;syntaxhighlight lang="…"&gt;</code></tvar> for anything syntax-highlightable; use <tvar name="pre"><code>&lt;pre&gt;</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

Languages: English · français · Esperanto


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

inline and copy are 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