Help:Contributing/code: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
Options table with live examples, Markdown comparison table, SPARQL examples; drop code-snippet vocabulary note (via update-page on MediaWiki MCP Server) |
||
| Line 12: | Line 12: | ||
|- | |- | ||
| Syntax-highlighted block — Pygments highlighting and a copy button (top right of the block). | | Syntax-highlighted block — Pygments highlighting and a copy button (top right of the block). | ||
| <syntaxhighlight lang=" | | <syntaxhighlight lang="sparql">SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight> | ||
| <syntaxhighlight lang=" | | <syntaxhighlight lang="sparql">SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight> | ||
|- | |- | ||
| Inline code — monospace within a sentence. | | Inline code — monospace within a sentence. | ||
| Line 24: | Line 24: | ||
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: | ||
* <code>sparql</code> — SPARQL queries (this | * <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 | * <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 == | == Options for highlighted blocks == | ||
{| 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 { | |||
?item wdt:P1 wd:Q6 . | |||
}</syntaxhighlight> | |||
|- | |||
| <code>start="{int}"</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 { | |||
?item wdt:P1 wd:Q6 . | |||
}</syntaxhighlight> | |||
|- | |||
| <code>highlight</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 { | |||
?item wdt:P1 wd:Q6 . | |||
}</syntaxhighlight> | |||
|- | |||
| <code>inline</code> — render as inline code instead of a block | |||
| <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight> | |||
| <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight> | |||
|- | |||
| <code>copy</code> — adds 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> | |||
|} | |||
<blockquote><code>inline</code> and <code>copy</code> are mutually exclusive.</blockquote> | <blockquote><code>inline</code> and <code>copy</code> are mutually exclusive.</blockquote> | ||
| Line 44: | Line 67: | ||
== Differences from Markdown == | == Differences from Markdown == | ||
{| class="wikitable" style="width:100%" | |||
|- | |||
! Markdown habit !! On this wiki | |||
|- | |||
| Fenced code blocks (<code>```</code>) | |||
| There are no fenced blocks — <code>```</code> does nothing. Use <code><pre></code> or <code><syntaxhighlight></code>. | |||
|- | |||
| Inline code (<code>`code`</code>) | |||
| <code><code></code> is raw HTML: the text inside is not escaped. Type <code>&lt;</code> for a literal <code><</code>, <code>&gt;</code> for <code>></code>, <code>&amp;</code> for <code>&</code>. | |||
|- | |||
| Literal <code>[[</code> and <code>{{</code> inside a fenced block | |||
| In <code><pre></code> and <code><syntaxhighlight></code> wiki markup is not interpreted — <code>{{</code>, <code>[[</code> 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 == | == House rules for code content == | ||
Revision as of 07:31, 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
|
| Inline code — monospace within a sentence. | <code>print("hello")</code> | print("hello")
|
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>.
|
Inline code (`code`)
|
<code> is raw HTML: the text inside is not escaped. Type < for a literal <, > for >, & for &.
|
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