Help:Contributing/code: Difference between revisions
Rewrite per cheatsheet style: strip translation markup, terse, real-world examples, practice the <code> carve-out (via update-page on MediaWiki MCP Server) |
|||
| Line 1: | Line 1: | ||
Code appears on this wikibase in two places: as content on wiki pages and as data ([[Special:AddCodeSnippet|code-snippet items]]). This page covers the former. | |||
== Code blocks on wiki pages == | |||
== | |||
{| class="wikitable" style="width:100%" | {| class="wikitable" style="width:100%" | ||
! What you want !! You type !! 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> | |||
| <pre>[2026-08-19 14:32:01] wikibase.updater: entity Q42 updated</pre> | |||
|- | |- | ||
| | | Highlighted block — Pygments colours and a copy button. | ||
| <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> | | <syntaxhighlight lang="sparql">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. | ||
| | | <code>latestId</code> | ||
| <code>latestId</code> | | <code>latestId</code> | ||
|- | |- | ||
| | | Highlighted inline code — monospace in a sentence, with colours. | ||
| | | <syntaxhighlight lang="python" inline>print("hello")</syntaxhighlight> | ||
| <syntaxhighlight lang="python" inline>print("hello")</syntaxhighlight> | | <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 | <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. | |||
Be careful with | </blockquote> | ||
== | == Which 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>sparql</code> — SPARQL queries (this wiki's query service) | |||
* <code>python</code>, <code>javascript</code>, <code>bash</code>, <code>sql</code>, <code>json</code>, <code>wikitext</code> — 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) | ||
== | == Options for highlighted blocks == | ||
The options below are shown on this query — "items that are people": | |||
<syntaxhighlight lang="sparql"> | |||
SELECT ?item WHERE { | |||
?item wdt:P1 wd:Q6 . | |||
} | |||
</syntaxhighlight> | |||
{| class="wikitable" style="width:100%" | {| class="wikitable" style="width:100%" | ||
! Option !! You type !! You get | |||
|- | |- | ||
| <code>line</code> — show line numbers | |||
| <syntaxhighlight lang="sparql" line>SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}</syntaxhighlight> | |||
| < | |||
| | |||
| <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="5"</code> — first line number (with <code>line</code>) | ||
| | | <syntaxhighlight lang="sparql" line start="5">SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}</syntaxhighlight> | ||
| <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="1,3"</code> — highlight specific lines (comma-separated) | ||
| | | <syntaxhighlight lang="sparql" highlight="1,3">SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}</syntaxhighlight> | ||
| <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 | ||
| | | <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight> | ||
| <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight> | | <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight> | ||
|- | |- | ||
| < | | <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> | ||
| <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>< | <blockquote><code>inline</code> and <code>copy</code> are mutually exclusive.</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). | ||
== | == Differences from Markdown == | ||
< | * 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>. | ||
< | * <syntaxhighlight lang="text" inline>[[</syntaxhighlight> and <syntaxhighlight lang="text" inline>{{</syntaxhighlight> inside <syntaxhighlight lang="text" inline><pre></syntaxhighlight> or <syntaxhighlight lang="text" inline><syntaxhighlight></syntaxhighlight> stay literal — which is what you want for code. | ||
</ | |||
== | == House rules == | ||
* 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. | |||
* Blocks larger than 100 kB lose highlighting automatically (a safety guard). | |||
* Prefer | |||
== | == See also == | ||
* [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 | ||
Revision as of 17:04, 19 August 2026
Code appears on this wikibase in two places: as content on wiki pages and as data (code-snippet items). This page covers the former.
Code blocks on wiki pages
| What you want | You type | 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">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
|
| 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.
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.
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
The options below are shown on this query — "items that are people":
SELECT ?item WHERE {
?item wdt:P1 wd:Q6 .
}
| 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="5" — 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="1,3" — 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 — 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.
Full parameter reference: SyntaxHighlight extension parameters (official).
Differences from Markdown
- No fenced blocks —
```does nothing here. Use<pre>or<syntaxhighlight>. [[and{{inside<pre>or<syntaxhighlight>stay literal — which is what you want for code.
House rules
- Prefer
<syntaxhighlight lang="…">for anything syntax-highlightable;<pre>for config dumps and logs where colours add noise. - Blocks larger than 100 kB lose highlighting automatically (a safety guard).
See also
- MediaWiki syntax 101 (official)
- SyntaxHighlight extension (official)
- Pygments lexers — full language list
- Help:Contributing — contributing to this wiki