-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
MermaidParserFunction.php
107 lines (92 loc) · 2.04 KB
/
MermaidParserFunction.php
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace Mermaid;
use Parser;
use Html;
use MediaWiki\MediaWikiServices;
/**
* @license GNU GPL v2+
* @since 1.0
*
* @author mwjames
*/
class MermaidParserFunction
{
/**
* @var Parser
*/
private $parser;
/**
* @var Config
*/
private $config;
/**
* @var MermaidConfigExtractor
*/
private $paramExtractor;
/**
* @param Parser $parser
* @param Config $config
* @param MermaidConfigExtractor $mermaidConfigExtractor
* @since 1.0
*/
public function __construct(Parser $parser, Config $config, MermaidConfigExtractor $mermaidConfigExtractor)
{
$this->parser = $parser;
$this->config = $config;
$this->paramExtractor = $mermaidConfigExtractor;
}
/**
* @param Parser $parser
* @return callable
* @since 1.1
*
*/
public static function onParserFunction(Parser $parser)
{
$config = MediaWikiServices::getInstance()->getService('Mermaid.Config');
$paramExtractor = MediaWikiServices::getInstance()->getService('Mermaid.MermaidConfigExtractor');
$function = new self($parser, $config, $paramExtractor);
return $function->parse(func_get_args());
}
/**
* @param array $params
* @return string
* @since 1.0
*
*/
public function parse(array $params)
{
$class = 'ext-mermaid';
$parserOutput = $this->parser->getOutput();
if (isset($params[0]) && $params[0] instanceof \Parser) {
array_shift($params);
}
// Signal the OutputPageParserOutput hook
$parserOutput->setExtensionData('ext-mermaid', true);
$parserOutput->addModules('ext.mermaid');
$graphConfig = [
'theme' => $this->config->getDefaultTheme()
];
list($mermaidConfig, $mwParams) = $this->paramExtractor->extract($params);
$content = implode("|", $mwParams);
$graphConfig = array_merge($graphConfig, $mermaidConfig);
return Html::rawElement(
'div',
[
'class' => $class,
'data-mermaid' => json_encode(
[
'content' => htmlspecialchars($content),
'config' => $graphConfig
]
)
],
Html::rawElement(
'div',
[
'class' => 'mermaid-dots',
]
)
);
}
}