Help:Contributing/code: Difference between revisions

From Wikibase
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="python">print("hello")</syntaxhighlight>
| <syntaxhighlight lang="sparql">SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight>
| <syntaxhighlight lang="python">print("hello")</syntaxhighlight>
| <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 wiki’s query service)
* <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)
The same language vocabulary is used by code-snippet items: a code item’s language is a Pygments language item, so the <code>lang</code> attribute on wiki pages and the language item in the data layer stay consistent.


== Options for highlighted blocks ==
== Options for highlighted blocks ==


* <code>line</code> — show line numbers
{| class="wikitable" style="width:100%"
* <code>start="{int}"</code> — first line number (with <code>line</code>)
|-
* <code>highlight</code> — highlight specific lines (comma-separated)
! Option !! You type !! You get
* <code>inline</code> — render as inline code instead of a block
|-
* The <code>copy</code> attribute adds a button link that copies the content to the clipboard when clicked.
| <code>line</code> — show line numbers
| &lt;syntaxhighlight lang="sparql" line&gt;SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}&lt;/syntaxhighlight&gt;
| <syntaxhighlight lang="sparql" line>SELECT ?item WHERE {
?item wdt:P1 wd:Q6 .
}</syntaxhighlight>
|-
| <code>start="{int}"</code> — first line number (with <code>line</code>)
| &lt;syntaxhighlight lang="sparql" line start="5"&gt;SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}&lt;/syntaxhighlight&gt;
| <syntaxhighlight lang="sparql" line start="5">SELECT ?item WHERE {
?item wdt:P1 wd:Q6 .
}</syntaxhighlight>
|-
| <code>highlight</code> — highlight specific lines (comma-separated)
| &lt;syntaxhighlight lang="sparql" highlight="1,3"&gt;SELECT ?item WHERE {<br/>?item wdt:P1 wd:Q6 .<br/>}&lt;/syntaxhighlight&gt;
| <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
| &lt;syntaxhighlight lang="sparql" inline&gt;LIMIT&lt;/syntaxhighlight&gt;
| <syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight>
|-
| <code>copy</code> adds a button that copies the block
| &lt;syntaxhighlight lang="sparql" copy&gt;SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5&lt;/syntaxhighlight&gt;
| <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 ==


* There are no fenced code blocks — ``` does nothing. Use <code>&lt;pre&gt;</code> or <code>&lt;syntaxhighlight&gt;</code>.
{| class="wikitable" style="width:100%"
* <code>&lt;code&gt;</code> is raw HTML: the text inside is not escaped. Type <code>&amp;lt;</code> for a literal <code>&lt;</code>, <code>&amp;gt;</code> for <code>&gt;</code>, <code>&amp;amp;</code> for <code>&amp;</code>.
|-
* In <code>&lt;pre&gt;</code> and <code>&lt;syntaxhighlight&gt;</code> wiki markup is not interpreted — <code>&#123;&#123;</code>, <code>&#91;&#91;</code> and quotes appear literally, which is what you want for code.
! Markdown habit !! On this wiki
* Blocks larger than 100&nbsp;kB lose highlighting automatically (a safety guard).
|-
| Fenced code blocks (<code>```</code>)
| There are no fenced blocks — <code>```</code> does nothing. Use <code>&lt;pre&gt;</code> or <code>&lt;syntaxhighlight&gt;</code>.
|-
| Inline code (<code>`code`</code>)
| <code>&lt;code&gt;</code> is raw HTML: the text inside is not escaped. Type <code>&amp;lt;</code> for a literal <code>&lt;</code>, <code>&amp;gt;</code> for <code>&gt;</code>, <code>&amp;amp;</code> for <code>&amp;</code>.
|-
| Literal <code>&#91;&#91;</code> and <code>&#123;&#123;</code> inside a fenced block
| In <code>&lt;pre&gt;</code> and <code>&lt;syntaxhighlight&gt;</code> wiki markup is not interpreted — <code>&#123;&#123;</code>, <code>&#91;&#91;</code> and quotes appear literally, which is what you want for code.
|}
 
Blocks larger than 100&nbsp;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

Languages: English · français · Esperanto

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

inline and copy are 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 &lt; for a literal <, &gt; for >, &amp; 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