Help:Contributing/code

From Wikibase
Jump to navigation Jump to search

Languages: English · français · Esperanto

Code appears on this wiki 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. The official MediaWiki formatting help covers the basics; this page covers what is special about code.

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="python">print("hello")</syntaxhighlight>
print("hello")
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)

The same language vocabulary is used by code-snippet items: a code item’s language is a Pygments language item, so the lang attribute on wiki pages and the language item in the data layer stay consistent.

Options for highlighted blocks

  • line — show line numbers
  • start="{int}" — first line number (with line)
  • highlight — highlight specific lines (comma-separated)
  • inline — render as inline code instead of a block
  • The copy attribute adds a button link that copies the content to the clipboard when clicked.

inline and copy are mutually exclusive.

Full parameter reference: SyntaxHighlight extension parameters (official).

Differences from Markdown

  • There are no fenced code blocks — ``` does nothing. Use <pre> or <syntaxhighlight>.
  • <code> is raw HTML: the text inside is not escaped. Type &lt; for a literal <, &gt; for >, &amp; for &.
  • 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