Help:Contributing/code: Difference between revisions

From Wikibase
Jump to navigation Jump to search
mNo edit summary
Practice what the page preaches: replace all live <code> tags with <syntaxhighlight lang="text" inline> (via update-page on MediaWiki MCP Server)
Line 29: Line 29:
<blockquote><translate>
<blockquote><translate>
<!--T:12-->
<!--T:12-->
You may see <tvar name="code"><code>&lt;code&gt;&lt;/code&gt;</code></tvar> used for inline code by others. 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.
You may see <tvar name="code"><syntaxhighlight lang="text" inline><code>…</code></syntaxhighlight></tvar> used for inline code by others. It does not escape HTML: text such as <tvar name="lt"><syntaxhighlight lang="text" inline><</syntaxhighlight></tvar> or <tvar name="amp"><syntaxhighlight lang="text" inline>&</syntaxhighlight></tvar> inside it is read as markup. Prefer <tvar name="shi"><syntaxhighlight lang="text" inline><syntaxhighlight inline></syntaxhighlight></tvar> instead.
</translate></blockquote>
</translate></blockquote>


Line 36: Line 36:
<translate>
<translate>
<!--T:14-->
<!--T:14-->
Any Pygments lexer alias works as the <tvar name="lang"><code>lang</code></tvar> attribute — use the lowercase canonical name:
Any Pygments lexer alias works as the <tvar name="lang"><syntaxhighlight lang="text" inline>lang</syntaxhighlight></tvar> attribute — use the lowercase canonical name:
</translate>
</translate>


<translate>
<translate>
<!--T:15-->
<!--T:15-->
* <tvar name="l1"><code>sparql</code></tvar> — SPARQL queries (this wiki's query service)
* <tvar name="l1"><syntaxhighlight lang="text" inline>sparql</syntaxhighlight></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
* <tvar name="l2"><syntaxhighlight lang="text" inline>python</syntaxhighlight></tvar>, <tvar name="l3"><syntaxhighlight lang="text" inline>javascript</syntaxhighlight></tvar>, <tvar name="l4"><syntaxhighlight lang="text" inline>bash</syntaxhighlight></tvar>, <tvar name="l5"><syntaxhighlight lang="text" inline>sql</syntaxhighlight></tvar>, <tvar name="l6"><syntaxhighlight lang="text" inline>json</syntaxhighlight></tvar>, <tvar name="l7"><syntaxhighlight lang="text" inline>wikitext</syntaxhighlight></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>
</translate>
Line 52: Line 52:
! <translate><!--T:17--> Option</translate> !! <translate><!--T:18--> You type</translate> !! <translate><!--T:19--> You get</translate>
! <translate><!--T:17--> Option</translate> !! <translate><!--T:18--> You type</translate> !! <translate><!--T:19--> You get</translate>
|-
|-
| <translate><!--T:20--> <tvar name="o1"><code>line</code></tvar> — show line numbers</translate>
| <translate><!--T:20--> <tvar name="o1"><syntaxhighlight lang="text" inline>line</syntaxhighlight></tvar> — show line numbers</translate>
| <translate><!--T:21--> &lt;syntaxhighlight lang="sparql" line&gt;SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}&lt;/syntaxhighlight&gt;</translate>
| <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 {
Line 58: Line 58:
}</syntaxhighlight>
}</syntaxhighlight>
|-
|-
| <translate><!--T:22--> <tvar name="o2"><code>start="{int}"</code></tvar> — first line number (with <tvar name="o2b"><code>line</code></tvar>)</translate>
| <translate><!--T:22--> <tvar name="o2"><syntaxhighlight lang="text" inline>start="{int}"</syntaxhighlight></tvar> — first line number (with <tvar name="o2b"><syntaxhighlight lang="text" inline>line</syntaxhighlight></tvar>)</translate>
| <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>
| <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 {
Line 64: Line 64:
}</syntaxhighlight>
}</syntaxhighlight>
|-
|-
| <translate><!--T:24--> <tvar name="o3"><code>highlight</code></tvar> — highlight specific lines (comma-separated)</translate>
| <translate><!--T:24--> <tvar name="o3"><syntaxhighlight lang="text" inline>highlight</syntaxhighlight></tvar> — highlight specific lines (comma-separated)</translate>
| <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>
| <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 {
Line 70: Line 70:
}</syntaxhighlight>
}</syntaxhighlight>
|-
|-
| <translate><!--T:26--> <tvar name="o4"><code>inline</code></tvar> — render as inline code instead of a block</translate>
| <translate><!--T:26--> <tvar name="o4"><syntaxhighlight lang="text" inline>inline</syntaxhighlight></tvar> — render as inline code instead of a block</translate>
| <translate><!--T:27--> &lt;syntaxhighlight lang="sparql" inline&gt;LIMIT&lt;/syntaxhighlight&gt;</translate>
| <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>
|-
|-
| <translate><!--T:28--> <tvar name="o5"><code>copy</code></tvar> — adds a button that copies the block</translate>
| <translate><!--T:28--> <tvar name="o5"><syntaxhighlight lang="text" inline>copy</syntaxhighlight></tvar> — adds a button that copies the block</translate>
| <translate><!--T:29--> &lt;syntaxhighlight lang="sparql" copy&gt;SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5&lt;/syntaxhighlight&gt;</translate>
| <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>
Line 81: Line 81:
<blockquote><translate>
<blockquote><translate>
<!--T:30-->
<!--T:30-->
<tvar name="i"><code>inline</code></tvar> and <tvar name="c"><code>copy</code></tvar> are mutually exclusive.
<tvar name="i"><syntaxhighlight lang="text" inline>inline</syntaxhighlight></tvar> and <tvar name="c"><syntaxhighlight lang="text" inline>copy</syntaxhighlight></tvar> are mutually exclusive.
</translate></blockquote>
</translate></blockquote>


Line 95: Line 95:
! <translate><!--T:33--> Markdown habit</translate> !! <translate><!--T:34--> On this wiki</translate>
! <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:35--> Fenced code blocks (<tvar name="f1"><syntaxhighlight lang="text" inline>```</syntaxhighlight></tvar>)</translate>
| <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>
| <translate><!--T:36--> There are no fenced blocks — <tvar name="f2"><syntaxhighlight lang="text" inline>```</syntaxhighlight></tvar> does nothing. Use <tvar name="f3"><syntaxhighlight lang="text" inline><pre></syntaxhighlight></tvar> or <tvar name="f4"><syntaxhighlight lang="text" inline><syntaxhighlight></syntaxhighlight></tvar>.</translate>
|-
|-
| <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>
| <translate><!--T:37--> Literal <tvar name="l1"><syntaxhighlight lang="text" inline>[[</syntaxhighlight></tvar> and <tvar name="l2"><syntaxhighlight lang="text" inline>{{</syntaxhighlight></tvar> inside a fenced block</translate>
| <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:38--> In <tvar name="p"><syntaxhighlight lang="text" inline><pre></syntaxhighlight></tvar> and <tvar name="s"><syntaxhighlight lang="text" inline><syntaxhighlight></syntaxhighlight></tvar> wiki markup is not interpreted — <tvar name="l3"><syntaxhighlight lang="text" inline>{{</syntaxhighlight></tvar>, <tvar name="l4"><syntaxhighlight lang="text" inline>[[</syntaxhighlight></tvar> and quotes appear literally, which is what you want for code.</translate>
|}
|}


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



Revision as of 08:54, 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
Syntax-highlighted inline code — monospace within a sentence. <syntaxhighlight lang="python" inline>print("hello")</syntaxhighlight> print("hello")

Anything inside <syntaxhighlight> or <pre></pre> will not be interpreted. They are rendered literally.

You may see <code>…</code> used for inline code by others. 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