Help:Contributing/code: Difference between revisions

From Wikibase
Jump to navigation Jump to search
No edit summary
Line 52: Line 52:


* Prefer <code>&lt;syntaxhighlight lang="…"&gt;</code> for anything syntax-highlightable; use <code>&lt;pre&gt;</code> for config dumps and logs where colours add noise.
* Prefer <code>&lt;syntaxhighlight lang="…"&gt;</code> for anything syntax-highlightable; use <code>&lt;pre&gt;</code> for config dumps and logs where colours add noise.
* Use the canonical Pygments language name — the same vocabulary as the code-snippet items.
* For the knowledge base itself, create code-snippet items via [[Special:AddCodeSnippet]] instead of dumping code into wiki pages: the item carries the language, description and provenance statements. See the [https://github.com/Ron-RONZZ-org/ronzz-wikibase/blob/main/docs/contribution-guide.md contribution guide].
* Cite your sources — code items should state where the snippet comes from (reference URL, stated in, retrieved).


== See also ==
== See also ==

Revision as of 06:39, 19 August 2026

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