-
Notifications
You must be signed in to change notification settings - Fork 8
/
syntax.php
157 lines (128 loc) · 4.42 KB
/
syntax.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* DokuWiki Plugin flowcharts (Syntax Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Jakob Schwichtenberg <[email protected]>
*/
use dokuwiki\Parsing\Parser;
// must be run within Dokuwiki
if (!defined('DOKU_INC')) {
die();
}
/**
* if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
* if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
* require_once(DOKU_PLUGIN.'syntax.php');
*/
class syntax_plugin_flowcharts extends DokuWiki_Syntax_Plugin
{
function getType(){ return 'protected'; }
// must return a number lower than returned by native 'code' mode (200)
function getSort(){ return 158; }
/**
* Connect lookup pattern to lexer.
*
* @param string $mode Parser mode
*/
function connectTo($mode) {
$this->Lexer->addEntryPattern('<flow>(?=.*?</flow>)',$mode,'plugin_flowcharts');
}
function postConnect() {
$this->Lexer->addExitPattern('</flow>','plugin_flowcharts');
}
/**
* Handle matches of the flowcharts syntax
*/
function handle($match, $state, $pos, Doku_Handler $handler){
switch ($state) {
case DOKU_LEXER_ENTER:
return array($state, '');
case DOKU_LEXER_UNMATCHED :
return array($state, $match);
case DOKU_LEXER_EXIT :
return array($state, '');
}
return false;
}
/**
* Render xhtml output or metadata
*/
function render($mode, Doku_Renderer $renderer, $indata) {
if($mode == 'xhtml'){
list($state, $match) = $indata;
switch ($state) {
case DOKU_LEXER_ENTER :
// securityLevel loose allows more advanced functionality such as subgraphs to run.
// @todo: this should be an option in the interface.
$renderer->doc .= '<div class="mermaid">';
break;
case DOKU_LEXER_UNMATCHED :
$instructions = $this->p_get_instructions($match);
$xhtml = $this->p_render($instructions);
$renderer->doc .= $xhtml;
break;
case DOKU_LEXER_EXIT :
$renderer->doc .= "</div>";
break;
}
return true;
}
return false;
}
/*
* Get the parser instructions siutable for the mermaid
*
*/
function p_get_instructions($text) {
//import parser classes and mode definitions
require_once DOKU_INC . 'inc/parser/parser.php';
$modes = array();
// add default modes
$std_modes = array('internallink', 'media','externallink');
foreach($std_modes as $m){
$class = 'dokuwiki\\Parsing\\ParserMode\\'.ucfirst($m);
$obj = new $class();
$modes[] = array(
'sort' => $obj->getSort(),
'mode' => $m,
'obj' => $obj
);
}
// add formatting modes
$fmt_modes = array('strong','emphasis','underline','monospace',
'subscript','superscript','deleted');
foreach($fmt_modes as $m){
$obj = new \dokuwiki\Parsing\ParserMode\Formatting($m);
$modes[] = array(
'sort' => $obj->getSort(),
'mode' => $m,
'obj' => $obj
);
}
// Create the parser and handler
$Parser = new Parser(new Doku_Handler());
//add modes to parser
foreach($modes as $mode){
$Parser->addMode($mode['mode'],$mode['obj']);
}
// Do the parsing
$p = $Parser->parse($text);
return $p;
}
public function p_render($instructions) {
$Renderer = p_get_renderer('flowcharts');
$Renderer->smileys = getSmileys();
$Renderer->entities = getEntities();
$Renderer->acronyms = getAcronyms();
$Renderer->interwiki = getInterwiki();
// Loop through the instructions
foreach ($instructions as $instruction) {
// Execute the callback against the Renderer
if(method_exists($Renderer, $instruction[0])){
call_user_func_array(array(&$Renderer, $instruction[0]), $instruction[1] ? $instruction[1] : array());
}
}
return $Renderer->doc;
}
}