Help:Contributing/code: Difference between revisions
Options table with live examples, Markdown comparison table, SPARQL examples; drop code-snippet vocabulary note (via update-page on MediaWiki MCP Server) |
Document inline matplotlib output (Agg backend in the Pyodide worker) (via update-page on MediaWiki MCP Server) |
||
| (24 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
Code appears on this wikibase in two | Code appears on this wikibase in two forms; this page covers both: | ||
* '''In-page code''' — written directly on a page with the block and inline methods below. This is the normal case: use it whenever the code belongs to the page it is on. | |||
* '''Semantic code snippets''' — code stored once as data, a [[Special:AddCodeSnippet|code-snippet item]], reusable and linked to what it implements. Reserve them for snippets that are likely to be reused — implementations of known algorithms such as A* — see [[#Code-snippet items|Code-snippet items]] below. | |||
== Code blocks on wiki pages == | == Code blocks on wiki pages == | ||
{| class="wikitable" style="width:100%" | {| class="wikitable" style="width:100%" | ||
! What you want !! What you type !! What you get | |||
|- | |- | ||
| Plain block — exactly as typed, no wiki markup, HTML escaped. Safe for logs and config pastes. | |||
| <syntaxhighlight lang="wikitext" copy><pre>[2026-08-19 14:32:01] wikibase.updater: entity Q42 updated</pre></syntaxhighlight> | |||
| <pre>[2026-08-19 14:32:01] wikibase.updater: entity Q42 updated</pre> | |||
|- | |- | ||
| | | Highlighted block — Pygments colours and a copy button. | ||
| < | | <pre><syntaxhighlight lang="sparql" copy>SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight></pre> | ||
| < | | <syntaxhighlight lang="sparql" copy>SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight> | ||
|- | |- | ||
| | | Plain inline code — monospace in a sentence, no colours. For short tokens with no markup-significant characters. | ||
| | | <syntaxhighlight lang="wikitext" inline><code>latestId</code></syntaxhighlight> | ||
| < | | <code>latestId</code> | ||
|- | |- | ||
| | | For highlighted inline code — monospace in a sentence, with colours. | ||
| < | | <pre><syntaxhighlight lang="python" inline>print("hello")</syntaxhighlight></pre> | ||
| < | | <syntaxhighlight lang="python" inline>print("hello")</syntaxhighlight> | ||
|} | |} | ||
== | Anything inside <syntaxhighlight lang="text" inline><syntaxhighlight></syntaxhighlight> or <syntaxhighlight lang="text" inline><pre></syntaxhighlight> is rendered literally — wiki markup and HTML are not interpreted. | ||
<blockquote> | |||
'''Don't wrap Wikimarkup content inside code tags''' — it will not be parsed. A link or template in a code tag renders as plain text: <syntaxhighlight lang="wikitext" inline>[[Special:AddCodeSnippet]]</syntaxhighlight> shows <syntaxhighlight lang="text" inline>[[Special:AddCodeSnippet]]</syntaxhighlight>, not a link. Write it as normal wikitext to get the link: [[Special:AddCodeSnippet|code-snippet items]]. | |||
</blockquote> | |||
<blockquote> | |||
Be careful with <syntaxhighlight lang="text" inline><code></syntaxhighlight>: it does not escape HTML — text such as <, > or & inside it is read as markup. Use it for short plain tokens without those characters (<code>#</code>, <code>quarto render</code>, <code>latestId</code>); it is lighter to render. Use <syntaxhighlight lang="text" inline><syntaxhighlight lang="…" inline></syntaxhighlight> when the snippet contains <, > or & (it escapes content) or when you want colours. Never use <syntaxhighlight lang="text" inline><code></syntaxhighlight> for blocks — <syntaxhighlight lang="text" inline><pre></syntaxhighlight> or <syntaxhighlight lang="text" inline><syntaxhighlight></syntaxhighlight> instead. | |||
</blockquote> | |||
== Most languages can be highlighted == | |||
Any Pygments lexer alias works as the <code>lang</code> attribute — use the lowercase canonical name: | Any Pygments lexer alias works as the <code>lang</code> attribute — use the lowercase canonical name: | ||
| Line 28: | Line 44: | ||
* 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) | ||
== | == Excellent options for <code>syntaxhighlight</code> == | ||
Every block-mode snippet carries <code>copy</code> (see the [[#House rules|house rules]]); the options below combine with it. | |||
{| class="wikitable" style="width:100%" | {| class="wikitable" style="width:100%" | ||
! Option !! You type !! You get | ! Option !! You type !! You get | ||
|- | |- | ||
| <code>line</code> — show line numbers | | <code>line</code> — show line numbers | ||
| <syntaxhighlight lang="sparql" line>SELECT ?item WHERE { | | <pre><syntaxhighlight lang="sparql" line copy>SELECT ?item WHERE { | ||
| <syntaxhighlight lang="sparql" line>SELECT ?item WHERE { | ?item wdt:P1 wd:Q6 . | ||
}</syntaxhighlight></pre> | |||
| <syntaxhighlight lang="sparql" line copy>SELECT ?item WHERE { | |||
?item wdt:P1 wd:Q6 . | ?item wdt:P1 wd:Q6 . | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
|- | |- | ||
| <code>start=" | | <code>start="5"</code> — first line number (with <code>line</code>) | ||
| <syntaxhighlight lang="sparql" line start="5">SELECT ?item WHERE { | | <pre><syntaxhighlight lang="sparql" line start="5" copy>SELECT ?item WHERE { | ||
| <syntaxhighlight lang="sparql" line start="5">SELECT ?item WHERE { | ?item wdt:P1 wd:Q6 . | ||
}</syntaxhighlight></pre> | |||
| <syntaxhighlight lang="sparql" line start="5" copy>SELECT ?item WHERE { | |||
?item wdt:P1 wd:Q6 . | ?item wdt:P1 wd:Q6 . | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
|- | |- | ||
| <code>highlight</code> — highlight specific lines (comma-separated) | | <code>highlight="1,3"</code> — highlight specific lines (comma-separated) | ||
| <syntaxhighlight lang="sparql" highlight="1,3">SELECT ?item WHERE { | | <pre><syntaxhighlight lang="sparql" highlight="1,3" copy>SELECT ?item WHERE { | ||
| <syntaxhighlight lang="sparql" highlight="1,3">SELECT ?item WHERE { | ?item wdt:P1 wd:Q6 . | ||
}</syntaxhighlight></pre> | |||
| <syntaxhighlight lang="sparql" highlight="1,3" copy>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 | | <code>inline</code> — render as inline code instead of a block | ||
| <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight> | | <pre><syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight></pre> | ||
| <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight> | | <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight> | ||
|- | |- | ||
| <code>copy</code> — | | <code>copy</code> — add a button that copies the block | ||
| <syntaxhighlight lang="sparql" copy>SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight> | | <pre><syntaxhighlight lang="sparql" copy>SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight></pre> | ||
| <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><code>inline</code> and <code>copy</code> are mutually exclusive — <code>copy</code> is for blocks, <code>inline</code> is for inline code.</blockquote> | ||
<blockquote> | |||
Blocks larger than 100 kB are not highlighted by default. | |||
</blockquote> | |||
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). | ||
== Running Python in the browser (Pyodide) == | |||
A Python block becomes '''runnable in the reader's browser''' — a '''Run''' button and an output panel below it, like a cell in a Jupyter or Quarto notebook — when you add the <code>pyodide</code> class: | |||
{| class="wikitable" style="width:100%" | |||
! What you type !! What you get | |||
|- | |||
| <pre><syntaxhighlight lang="python" class="pyodide" copy> | |||
print("hello", 1 + 1) | |||
</syntaxhighlight></pre> | |||
| <syntaxhighlight lang="python" class="pyodide" copy> | |||
print("hello", 1 + 1) | |||
</syntaxhighlight> | |||
|} | |||
* '''Runs in the reader's browser''' — no server is involved. The Python interpreter (Pyodide, CPython compiled to WebAssembly) is downloaded once and cached; press '''Run''' to execute. | |||
* '''One interpreter per page''': variables set in one block are visible in the next, so you can build up a computation across cells (define in one, use in another). The value of the last expression is shown below the output. | |||
* '''Packages''' such as <code>numpy</code>, <code>matplotlib</code>, <code>pandas</code>, <code>scipy</code> and <code>sympy</code> work: importing one loads it from this wiki's own copy on demand. | |||
* '''Plots''' from <code>matplotlib</code> appear inline: call <code>plt.show()</code> (or <code>fig.show()</code>) and each open figure is rendered below the block as a PNG, then closed — like an inline notebook cell. The output is static (no pan/zoom, animations, or interactive widgets); <code>plt.savefig(...)</code> works as usual. | |||
* The class combines with the usual options — <code>class="pyodide" copy</code>, <code>class="pyodide" line</code>, and so on. | |||
* It needs JavaScript; readers without it see ordinary highlighted code (no Run button). Readers run only the code you saved — but the code executes with their browser session, so keep the <code>pyodide</code> class for code you trust. | |||
== Differences from Markdown == | == Differences from Markdown == | ||
{| class="wikitable" | * No fenced blocks — <syntaxhighlight lang="text" inline>```</syntaxhighlight> does nothing here. Use <syntaxhighlight lang="text" inline><pre></syntaxhighlight> or <syntaxhighlight lang="text" inline><syntaxhighlight></syntaxhighlight>. | ||
* '''Every block-mode snippet include <code>copy</code>''': Use <syntaxhighlight lang="text" inline><syntaxhighlight lang="…" copy></syntaxhighlight>, so readers get a copy button. | |||
* Prefer <syntaxhighlight lang="text" inline><syntaxhighlight lang="…"></syntaxhighlight> for anything syntax-highlightable; <syntaxhighlight lang="text" inline><pre></syntaxhighlight> for config dumps and logs where colours add noise. | |||
== Code-snippet items == | |||
Most code is written in-page, with the methods above — that is the default, and it is right for one-off examples, page-specific commands and configuration dumps. A '''code-snippet item''' (a ''semantic code snippet'') stores the code instead as an [[Help:Contributing/entities|item]]: the code is kept in one place, with provenance and a semantic link to what it implements, and every embed shows that one copy. | |||
'''Normally, use in-page code.''' Reserve [[Special:AddCodeSnippet]] for snippets that are ''susceptible to be reused'' — most usefully, implementations of known algorithms or techniques (A*, quicksort, …) that several pages may want to display, or that readers should be able to find by what they implement. A snippet that belongs to a single page stays in-page. | |||
=== Adding a snippet === | |||
Open [[Special:AddCodeSnippet]] and fill the form: | |||
{| class="wikitable" | |||
! Field !! What it is | |||
|- | |||
| Label || A short title for the snippet (e.g. <code>A* search in Python</code>). | |||
|- | |- | ||
| Content || The code itself. Paste it as-is — line breaks are preserved on save and shown again when the item is edited. | |||
|- | |- | ||
| | | Programming language || The language of the code (the same Pygments list as above). | ||
|- | |- | ||
| | | Implementation of || Optional: the known algorithm, technique or concept this snippet implements — search and pick the entity (several allowed, comma-separated). This link is what makes the snippet findable by what it implements. | ||
| | |||
|- | |- | ||
| | | Attributed to / Source / Source URL / Date || Provenance, same as [[Help:Contributing/quotation|quotations]]. | ||
| | |||
|} | |} | ||
Saving creates the item and redirects to it. | |||
From the item page, click '''Copy embed code''' and paste the iframe on any page — this wiki or a third-party site. The embed always shows the item's current content: improving the snippet once improves every page that embeds it. | |||
== Editing code-snippet items == | |||
A stored code-snippet item (created via [[Special:AddCodeSnippet]]) is edited from its item page: click '''Edit content''' under the title, or go to [[Special:UpdateCodeSnippet]] with the item's id (e.g. <code>Special:UpdateCodeSnippet/Q100</code>). The code field shows the stored text exactly as entered (real line breaks); the programming-language picker carries the current value. Fields you leave blank keep their existing values. | |||
If you opened the edit form from the '''Add more''' success popup (the second button when adding), '''Update item''' returns you to the reopened [[Special:AddCodeSnippet]] form and the popup shows the updated snippet again — the fields you filled for the next item are preserved, so you can keep adding. | |||
== See also == | == See also == | ||
* [[Help:Contributing/quotation]] — adding quotations | |||
* [[Help:Contributing/math]] — adding mathematical expressions | |||
* [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 | ||
Latest revision as of 09:28, 20 November 2026
Code appears on this wikibase in two forms; this page covers both:
- In-page code — written directly on a page with the block and inline methods below. This is the normal case: use it whenever the code belongs to the page it is on.
- Semantic code snippets — code stored once as data, a code-snippet item, reusable and linked to what it implements. Reserve them for snippets that are likely to be reused — implementations of known algorithms such as A* — see Code-snippet items below.
Code blocks on wiki pages
| What you want | What you type | What you get |
|---|---|---|
| Plain block — exactly as typed, no wiki markup, HTML escaped. Safe for logs and config pastes. | <pre>[2026-08-19 14:32:01] wikibase.updater: entity Q42 updated</pre>
|
[2026-08-19 14:32:01] wikibase.updater: entity Q42 updated |
| Highlighted block — Pygments colours and a copy button. | <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
|
| Plain inline code — monospace in a sentence, no colours. For short tokens with no markup-significant characters. | <code>latestId</code>
|
latestId
|
| For highlighted inline code — monospace in a sentence, with colours. | <syntaxhighlight lang="python" inline>print("hello")</syntaxhighlight>
|
print("hello")
|
Anything inside <syntaxhighlight> or <pre> is rendered literally — wiki markup and HTML are not interpreted.
Don't wrap Wikimarkup content inside code tags — it will not be parsed. A link or template in a code tag renders as plain text:
[[Special:AddCodeSnippet]]shows[[Special:AddCodeSnippet]], not a link. Write it as normal wikitext to get the link: code-snippet items.
Be careful with
<code>: it does not escape HTML — text such as <, > or & inside it is read as markup. Use it for short plain tokens without those characters (#,quarto render,latestId); it is lighter to render. Use<syntaxhighlight lang="…" inline>when the snippet contains <, > or & (it escapes content) or when you want colours. Never use<code>for blocks —<pre>or<syntaxhighlight>instead.
Most 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)
Excellent options for syntaxhighlight
Every block-mode snippet carries copy (see the house rules); the options below combine with it.
| Option | You type | You get |
|---|---|---|
line — show line numbers
|
<syntaxhighlight lang="sparql" line copy>SELECT ?item WHERE {
?item wdt:P1 wd:Q6 .
}</syntaxhighlight>
|
SELECT ?item WHERE {
?item wdt:P1 wd:Q6 .
}
|
start="5" — first line number (with line)
|
<syntaxhighlight lang="sparql" line start="5" copy>SELECT ?item WHERE {
?item wdt:P1 wd:Q6 .
}</syntaxhighlight>
|
SELECT ?item WHERE {
?item wdt:P1 wd:Q6 .
}
|
highlight="1,3" — highlight specific lines (comma-separated)
|
<syntaxhighlight lang="sparql" highlight="1,3" copy>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 — add 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 —copyis for blocks,inlineis for inline code.
Blocks larger than 100 kB are not highlighted by default.
Full parameter reference: SyntaxHighlight extension parameters (official).
Running Python in the browser (Pyodide)
A Python block becomes runnable in the reader's browser — a Run button and an output panel below it, like a cell in a Jupyter or Quarto notebook — when you add the pyodide class:
| What you type | What you get |
|---|---|
<syntaxhighlight lang="python" class="pyodide" copy>
print("hello", 1 + 1)
</syntaxhighlight>
|
print("hello", 1 + 1)
|
- Runs in the reader's browser — no server is involved. The Python interpreter (Pyodide, CPython compiled to WebAssembly) is downloaded once and cached; press Run to execute.
- One interpreter per page: variables set in one block are visible in the next, so you can build up a computation across cells (define in one, use in another). The value of the last expression is shown below the output.
- Packages such as
numpy,matplotlib,pandas,scipyandsympywork: importing one loads it from this wiki's own copy on demand. - Plots from
matplotlibappear inline: callplt.show()(orfig.show()) and each open figure is rendered below the block as a PNG, then closed — like an inline notebook cell. The output is static (no pan/zoom, animations, or interactive widgets);plt.savefig(...)works as usual. - The class combines with the usual options —
class="pyodide" copy,class="pyodide" line, and so on. - It needs JavaScript; readers without it see ordinary highlighted code (no Run button). Readers run only the code you saved — but the code executes with their browser session, so keep the
pyodideclass for code you trust.
Differences from Markdown
- No fenced blocks —
```does nothing here. Use<pre>or<syntaxhighlight>.
- Every block-mode snippet include
copy: Use<syntaxhighlight lang="…" copy>, so readers get a copy button. - Prefer
<syntaxhighlight lang="…">for anything syntax-highlightable;<pre>for config dumps and logs where colours add noise.
Code-snippet items
Most code is written in-page, with the methods above — that is the default, and it is right for one-off examples, page-specific commands and configuration dumps. A code-snippet item (a semantic code snippet) stores the code instead as an item: the code is kept in one place, with provenance and a semantic link to what it implements, and every embed shows that one copy.
Normally, use in-page code. Reserve Special:AddCodeSnippet for snippets that are susceptible to be reused — most usefully, implementations of known algorithms or techniques (A*, quicksort, …) that several pages may want to display, or that readers should be able to find by what they implement. A snippet that belongs to a single page stays in-page.
Adding a snippet
Open Special:AddCodeSnippet and fill the form:
| Field | What it is |
|---|---|
| Label | A short title for the snippet (e.g. A* search in Python).
|
| Content | The code itself. Paste it as-is — line breaks are preserved on save and shown again when the item is edited. |
| Programming language | The language of the code (the same Pygments list as above). |
| Implementation of | Optional: the known algorithm, technique or concept this snippet implements — search and pick the entity (several allowed, comma-separated). This link is what makes the snippet findable by what it implements. |
| Attributed to / Source / Source URL / Date | Provenance, same as quotations. |
Saving creates the item and redirects to it.
From the item page, click Copy embed code and paste the iframe on any page — this wiki or a third-party site. The embed always shows the item's current content: improving the snippet once improves every page that embeds it.
Editing code-snippet items
A stored code-snippet item (created via Special:AddCodeSnippet) is edited from its item page: click Edit content under the title, or go to Special:UpdateCodeSnippet with the item's id (e.g. Special:UpdateCodeSnippet/Q100). The code field shows the stored text exactly as entered (real line breaks); the programming-language picker carries the current value. Fields you leave blank keep their existing values.
If you opened the edit form from the Add more success popup (the second button when adding), Update item returns you to the reopened Special:AddCodeSnippet form and the popup shows the updated snippet again — the fields you filled for the next item are preserved, so you can keep adding.
See also
- Help:Contributing/quotation — adding quotations
- Help:Contributing/math — adding mathematical expressions
- MediaWiki syntax 101 (official)
- SyntaxHighlight extension (official)
- Pygments lexers — full language list
- Help:Contributing — contributing to this wiki