Help:Contributing/code: Difference between revisions
Jump to navigation
Jump to search
Discourage <code> via blockquote note (does not escape HTML), recommend <syntaxhighlight inline>; make page translatable (via update-page on MediaWiki MCP Server) |
mNo edit summary |
||
| Line 20: | Line 20: | ||
| <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> | ||
|- | |- | ||
| <translate><!--T:10--> | | <translate><!--T:10--> Syntax-highlighted inline code — monospace within a sentence.</translate> | ||
| <translate><!--T:11--> <syntaxhighlight lang="python" inline>print("hello")</syntaxhighlight></translate> | | <translate><!--T:11--> <syntaxhighlight lang="python" inline>print("hello")</syntaxhighlight></translate> | ||
| <syntaxhighlight lang="python" inline>print("hello")</syntaxhighlight> | | <syntaxhighlight lang="python" inline>print("hello")</syntaxhighlight> | ||
Revision as of 08:43, 19 August 2026
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")
|
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
|
inlineandcopyare 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
- MediaWiki syntax 101 (official)
- SyntaxHighlight extension (official)
- Pygments lexers — full language list
- Help:Contributing — contributing to this wiki