1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# Extension:Code
A MediaWiki extension that builds on [SyntaxHighlight]
to provide the following (all in a configurable manner):
* shorter tags
e.g. `<query>` instead of `<syntaxhighlight lang=sparql>`
* code actions
e.g. automatically link the [WDQS] for SPARQL code blocks[^1]
* 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
$wgCode_namespacesWithCodePages[NS_MAIN] = true;
$wgCode_languages[] = [
'tag' => 'query',
'pygmentsLexer' => 'sparql',
'actions' => [
'run' => 'https://query.wikidata.org/#$code',
'embed' => 'https://query.wikidata.org/embed.html#$code',
],
'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
`Special:CodeAction` special page, e.g. `Special:CodeAction/run/Example.rq`
attempts to execute the `run` action for the `Example.rq` code page
and redirect the user accordingly.
## Installation
1. Place the extension in your extensions directory.
2. Add `wfLoadExtension('Code');` to your `LocalSettings.php`.
3. Visit `Special:CodeAction` it will tell you what other configuration you need.
(If you want syntax highlighting via [SyntaxHighlight], additionally add
`wfLoadExtension('SyntaxHighlight_GeSHi');` to your `LocalSettings.php`).
[^1]: While this can also be achieved just via MediaWiki templates,
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/
|