Help:Contributing/code: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
Practice what the page preaches: replace all live <code> tags with <syntaxhighlight lang="text" inline> (via update-page on MediaWiki MCP Server) |
||
| Line 29: | Line 29: | ||
<blockquote><translate> | <blockquote><translate> | ||
<!--T:12--> | <!--T:12--> | ||
You may see <tvar name="code"><code> | You may see <tvar name="code"><syntaxhighlight lang="text" inline><code>…</code></syntaxhighlight></tvar> used for inline code by others. It does not escape HTML: text such as <tvar name="lt"><syntaxhighlight lang="text" inline><</syntaxhighlight></tvar> or <tvar name="amp"><syntaxhighlight lang="text" inline>&</syntaxhighlight></tvar> inside it is read as markup. Prefer <tvar name="shi"><syntaxhighlight lang="text" inline><syntaxhighlight inline></syntaxhighlight></tvar> instead. | ||
</translate></blockquote> | </translate></blockquote> | ||
| Line 36: | Line 36: | ||
<translate> | <translate> | ||
<!--T:14--> | <!--T:14--> | ||
Any Pygments lexer alias works as the <tvar name="lang">< | Any Pygments lexer alias works as the <tvar name="lang"><syntaxhighlight lang="text" inline>lang</syntaxhighlight></tvar> attribute — use the lowercase canonical name: | ||
</translate> | </translate> | ||
<translate> | <translate> | ||
<!--T:15--> | <!--T:15--> | ||
* <tvar name="l1">< | * <tvar name="l1"><syntaxhighlight lang="text" inline>sparql</syntaxhighlight></tvar> — SPARQL queries (this wiki's query service) | ||
* <tvar name="l2">< | * <tvar name="l2"><syntaxhighlight lang="text" inline>python</syntaxhighlight></tvar>, <tvar name="l3"><syntaxhighlight lang="text" inline>javascript</syntaxhighlight></tvar>, <tvar name="l4"><syntaxhighlight lang="text" inline>bash</syntaxhighlight></tvar>, <tvar name="l5"><syntaxhighlight lang="text" inline>sql</syntaxhighlight></tvar>, <tvar name="l6"><syntaxhighlight lang="text" inline>json</syntaxhighlight></tvar>, <tvar name="l7"><syntaxhighlight lang="text" inline>wikitext</syntaxhighlight></tvar> — 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) | ||
</translate> | </translate> | ||
| Line 52: | Line 52: | ||
! <translate><!--T:17--> Option</translate> !! <translate><!--T:18--> You type</translate> !! <translate><!--T:19--> You get</translate> | ! <translate><!--T:17--> Option</translate> !! <translate><!--T:18--> You type</translate> !! <translate><!--T:19--> You get</translate> | ||
|- | |- | ||
| <translate><!--T:20--> <tvar name="o1">< | | <translate><!--T:20--> <tvar name="o1"><syntaxhighlight lang="text" inline>line</syntaxhighlight></tvar> — show line numbers</translate> | ||
| <translate><!--T:21--> <syntaxhighlight lang="sparql" line>SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}</syntaxhighlight></translate> | | <translate><!--T:21--> <syntaxhighlight lang="sparql" line>SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}</syntaxhighlight></translate> | ||
| <syntaxhighlight lang="sparql" line>SELECT ?item WHERE { | | <syntaxhighlight lang="sparql" line>SELECT ?item WHERE { | ||
| Line 58: | Line 58: | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
|- | |- | ||
| <translate><!--T:22--> <tvar name="o2">< | | <translate><!--T:22--> <tvar name="o2"><syntaxhighlight lang="text" inline>start="{int}"</syntaxhighlight></tvar> — first line number (with <tvar name="o2b"><syntaxhighlight lang="text" inline>line</syntaxhighlight></tvar>)</translate> | ||
| <translate><!--T:23--> <syntaxhighlight lang="sparql" line start="5">SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}</syntaxhighlight></translate> | | <translate><!--T:23--> <syntaxhighlight lang="sparql" line start="5">SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}</syntaxhighlight></translate> | ||
| <syntaxhighlight lang="sparql" line start="5">SELECT ?item WHERE { | | <syntaxhighlight lang="sparql" line start="5">SELECT ?item WHERE { | ||
| Line 64: | Line 64: | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
|- | |- | ||
| <translate><!--T:24--> <tvar name="o3">< | | <translate><!--T:24--> <tvar name="o3"><syntaxhighlight lang="text" inline>highlight</syntaxhighlight></tvar> — highlight specific lines (comma-separated)</translate> | ||
| <translate><!--T:25--> <syntaxhighlight lang="sparql" highlight="1,3">SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}</syntaxhighlight></translate> | | <translate><!--T:25--> <syntaxhighlight lang="sparql" highlight="1,3">SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}</syntaxhighlight></translate> | ||
| <syntaxhighlight lang="sparql" highlight="1,3">SELECT ?item WHERE { | | <syntaxhighlight lang="sparql" highlight="1,3">SELECT ?item WHERE { | ||
| Line 70: | Line 70: | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
|- | |- | ||
| <translate><!--T:26--> <tvar name="o4">< | | <translate><!--T:26--> <tvar name="o4"><syntaxhighlight lang="text" inline>inline</syntaxhighlight></tvar> — render as inline code instead of a block</translate> | ||
| <translate><!--T:27--> <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight></translate> | | <translate><!--T:27--> <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight></translate> | ||
| <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight> | | <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight> | ||
|- | |- | ||
| <translate><!--T:28--> <tvar name="o5">< | | <translate><!--T:28--> <tvar name="o5"><syntaxhighlight lang="text" inline>copy</syntaxhighlight></tvar> — adds a button that copies the block</translate> | ||
| <translate><!--T:29--> <syntaxhighlight lang="sparql" copy>SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight></translate> | | <translate><!--T:29--> <syntaxhighlight lang="sparql" copy>SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight></translate> | ||
| <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 81: | Line 81: | ||
<blockquote><translate> | <blockquote><translate> | ||
<!--T:30--> | <!--T:30--> | ||
<tvar name="i">< | <tvar name="i"><syntaxhighlight lang="text" inline>inline</syntaxhighlight></tvar> and <tvar name="c"><syntaxhighlight lang="text" inline>copy</syntaxhighlight></tvar> are mutually exclusive. | ||
</translate></blockquote> | </translate></blockquote> | ||
| Line 95: | Line 95: | ||
! <translate><!--T:33--> Markdown habit</translate> !! <translate><!--T:34--> On this wiki</translate> | ! <translate><!--T:33--> Markdown habit</translate> !! <translate><!--T:34--> On this wiki</translate> | ||
|- | |- | ||
| <translate><!--T:35--> Fenced code blocks (<tvar name="f1">< | | <translate><!--T:35--> Fenced code blocks (<tvar name="f1"><syntaxhighlight lang="text" inline>```</syntaxhighlight></tvar>)</translate> | ||
| <translate><!--T:36--> There are no fenced blocks — <tvar name="f2">< | | <translate><!--T:36--> There are no fenced blocks — <tvar name="f2"><syntaxhighlight lang="text" inline>```</syntaxhighlight></tvar> does nothing. Use <tvar name="f3"><syntaxhighlight lang="text" inline><pre></syntaxhighlight></tvar> or <tvar name="f4"><syntaxhighlight lang="text" inline><syntaxhighlight></syntaxhighlight></tvar>.</translate> | ||
|- | |- | ||
| <translate><!--T:37--> Literal <tvar name="l1">< | | <translate><!--T:37--> Literal <tvar name="l1"><syntaxhighlight lang="text" inline>[[</syntaxhighlight></tvar> and <tvar name="l2"><syntaxhighlight lang="text" inline>{{</syntaxhighlight></tvar> inside a fenced block</translate> | ||
| <translate><!--T:38--> In <tvar name="p">< | | <translate><!--T:38--> In <tvar name="p"><syntaxhighlight lang="text" inline><pre></syntaxhighlight></tvar> and <tvar name="s"><syntaxhighlight lang="text" inline><syntaxhighlight></syntaxhighlight></tvar> wiki markup is not interpreted — <tvar name="l3"><syntaxhighlight lang="text" inline>{{</syntaxhighlight></tvar>, <tvar name="l4"><syntaxhighlight lang="text" inline>[[</syntaxhighlight></tvar> and quotes appear literally, which is what you want for code.</translate> | ||
|} | |} | ||
| Line 111: | Line 111: | ||
<translate> | <translate> | ||
<!--T:41--> | <!--T:41--> | ||
* Prefer <tvar name="sh">< | * Prefer <tvar name="sh"><syntaxhighlight lang="text" inline><syntaxhighlight lang="…"></syntaxhighlight></tvar> for anything syntax-highlightable; use <tvar name="pre"><syntaxhighlight lang="text" inline><pre></syntaxhighlight></tvar> for config dumps and logs where colours add noise. | ||
</translate> | </translate> | ||
Revision as of 08:54, 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")
|
Anything inside <syntaxhighlight> or <pre></pre> will not be interpreted. They are rendered literally.
You may see
<code>…</code>used for inline code by others. 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