Help:Contributing/code: Difference between revisions
Standardize: add copy button to block code snippets |
Standard: every block-mode syntaxhighlight must include copy (house rule); align typed examples (via update-page on MediaWiki MCP Server) |
||
| Line 11: | Line 11: | ||
|- | |- | ||
| Highlighted block — Pygments colours and a copy button. | | Highlighted block — Pygments colours and a copy button. | ||
| <pre><syntaxhighlight lang="sparql">SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight></pre> | | <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> | ||
|- | |- | ||
| Line 42: | Line 42: | ||
== Excellent options for <code>syntaxhighlight</code> == | == 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%" | ||
| Line 47: | Line 49: | ||
|- | |- | ||
| <code>line</code> — show line numbers | | <code>line</code> — show line numbers | ||
| <pre><syntaxhighlight lang="sparql" line>SELECT ?item WHERE { | | <pre><syntaxhighlight lang="sparql" line copy>SELECT ?item WHERE { | ||
?item wdt:P1 wd:Q6 . | ?item wdt:P1 wd:Q6 . | ||
}</syntaxhighlight></pre> | }</syntaxhighlight></pre> | ||
| Line 55: | Line 57: | ||
|- | |- | ||
| <code>start="5"</code> — first line number (with <code>line</code>) | | <code>start="5"</code> — first line number (with <code>line</code>) | ||
| <pre><syntaxhighlight lang="sparql" line start="5">SELECT ?item WHERE { | | <pre><syntaxhighlight lang="sparql" line start="5" copy>SELECT ?item WHERE { | ||
?item wdt:P1 wd:Q6 . | ?item wdt:P1 wd:Q6 . | ||
}</syntaxhighlight></pre> | }</syntaxhighlight></pre> | ||
| Line 63: | Line 65: | ||
|- | |- | ||
| <code>highlight="1,3"</code> — highlight specific lines (comma-separated) | | <code>highlight="1,3"</code> — highlight specific lines (comma-separated) | ||
| <pre><syntaxhighlight lang="sparql" highlight="1,3">SELECT ?item WHERE { | | <pre><syntaxhighlight lang="sparql" highlight="1,3" copy>SELECT ?item WHERE { | ||
?item wdt:P1 wd:Q6 . | ?item wdt:P1 wd:Q6 . | ||
}</syntaxhighlight></pre> | }</syntaxhighlight></pre> | ||
| Line 79: | Line 81: | ||
|} | |} | ||
<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> | ||
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). | ||
| Line 89: | Line 91: | ||
== House rules == | == House rules == | ||
* Prefer <syntaxhighlight lang="text" inline><syntaxhighlight lang="…" | * '''Every block-mode snippet must include <code>copy</code>''' — write <syntaxhighlight lang="text" inline><syntaxhighlight lang="…" copy></syntaxhighlight>, so readers get a copy button. Exceptions: <code>inline</code> blocks (<code>copy</code> is ignored there) and literal examples of the tag itself. | ||
* 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). | * Blocks larger than 100 kB lose highlighting automatically (a safety guard). | ||
* Enforcement: <code>tools/add-copy-to-syntaxhighlight.py --audit</code> in the <code>ronzz-wikibase</code> repo flags any block-mode snippet missing <code>copy</code> (exit 0 = conform). | |||
== See also == | == See also == | ||
Revision as of 09:51, 23 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 | 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.
Full parameter reference: SyntaxHighlight extension parameters (official).
Differences from Markdown
- No fenced blocks —
```does nothing here. Use<pre>or<syntaxhighlight>.
House rules
- Every block-mode snippet must include
copy— write<syntaxhighlight lang="…" copy>, so readers get a copy button. Exceptions:inlineblocks (copyis ignored there) and literal examples of the tag itself. - 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).
- Enforcement:
tools/add-copy-to-syntaxhighlight.py --auditin theronzz-wikibaserepo flags any block-mode snippet missingcopy(exit 0 = conform).
See also
- MediaWiki syntax 101 (official)
- SyntaxHighlight extension (official)
- Pygments lexers — full language list
- Help:Contributing — contributing to this wiki