diff options
author | Martin Fischer <martin@push-f.com> | 2022-12-03 07:07:55 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2022-12-03 08:50:45 +0100 |
commit | 46a390d6759065271b2158537691a53679c58989 (patch) | |
tree | 68afc83f78580794ca5f2d1875355ac7509ecef0 /README.md | |
parent | de6badb52bd58154baad30a73ec16e129d738fc2 (diff) |
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 45 |
1 files changed, 39 insertions, 6 deletions
@@ -9,13 +9,14 @@ to provide the following (all in a configurable manner): * code actions e.g. automatically link the [WDQS] for SPARQL code blocks[^1] -* code linkification - e.g. automatically link Wikidata identifiers in code blocks - * code pages e.g. automatically higlight pages with names ending in `.rq` as SPARQL (and also display the code actions for them) +* customizable code display via Lua/[Scribunto] + e.g. automatically link Wikidata identifiers in code blocks + + ## Example configuration ```php @@ -28,13 +29,44 @@ $wgCode_languages[] = [ 'run' => 'https://query.wikidata.org/#$code', 'embed' => 'https://query.wikidata.org/embed.html#$code', ], - 'linkifiers' => [ - '/\\b[QP][0-9]+\\b/' => 'https://www.wikidata.org/entity/$1', - ], 'suffix' => '.rq', ]; ``` +## Customizable code display + +The display of code blocks can be customized via Lua/[Scribunto]. For example +if you specify e.g. `"scribuntoModule" => "QueryCode"` for a language then this +extension will additionally invoke the `Module:QueryCode` Scribunto module for +every code tag and code page. Such a module could look as follows: + +```lua +local p = {} + +p.formatCode = function(frame) + local code, tag, is_code_page = frame.args[1], frame.args[2], frame.args[3] + local formattedCode = frame.args[4] + formattedCode = formattedCode:gsub('[QP][0-9]+', function(m) + local a = mw.html.create('a') + a:attr('href', 'https://www.wikidata.org/entity/' .. m) + a:wikitext(m) + return tostring(a) + end) + return formattedCode; +end + +p.additionalOutput = function(frame) + local code, tag, is_code_page = frame.args[1], frame.args[2], frame.args[3] + -- HTML returned here is displayed after the code action links +end + +return p +``` + +Note that the returned strings are not parsed as Wikitext they must already be +HTML; any dangerous tags and attributes are removed via MediaWiki's builtin +`Sanitizer` class. + ## Linking code actions Code actions can be linked from other pages via the @@ -57,4 +89,5 @@ this bears the problem that `|` has to be escaped as `{{!}}`, which can be quite annoying for languages like SPARQL that use `|` as an operator. [SyntaxHighlight]: https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:SyntaxHighlight +[Scribunto]: https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:Scribunto [WDQS]: https://query.wikidata.org/ |