-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax.php
More file actions
190 lines (164 loc) · 5.25 KB
/
syntax.php
File metadata and controls
190 lines (164 loc) · 5.25 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
* Plugin Color: Sets new colors for text and background.
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Mykola Ostrovskyy <spambox03@mail.ru>
* Based on Color plugin by Christopher Smith <chris@jalakai.co.uk>
* Based on Hilited plugin by Esther Brunner <esther@kaffeehaus.ch>
*/
/* Must be run within Dokuwiki */
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
require_once(DOKU_PLUGIN . 'syntax.php');
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_color extends DokuWiki_Syntax_Plugin {
var $mode;
/**
* Constructor
*/
function syntax_plugin_color() {
$this->mode = substr(get_class($this), 7);
}
/**
* Return some info
*/
function getInfo(){
return array(
'author' => 'Mykola Ostrovskyy',
'email' => 'spambox03@mail.ru',
'date' => '2009-03-07',
'name' => 'Color Plugin',
'desc' => 'Changes text colour and background',
'url' => 'http://code.google.com/p/dwp-forge/'
);
}
function getType() {
return 'formatting';
}
function getAllowedTypes() {
return array('formatting', 'substition', 'disabled');
}
function getSort() {
return 158;
}
function connectTo($mode) {
$this->Lexer->addEntryPattern('<color.*?>(?=.*?</color>)', $mode, $this->mode);
$this->Lexer->addEntryPattern('<m\d?>(?=.*?</m>)', $mode, $this->mode);
$this->Lexer->addEntryPattern('!!(?=.*!!)', $mode, $this->mode);
}
function postConnect() {
$this->Lexer->addExitPattern('</color>', $this->mode);
$this->Lexer->addExitPattern('</m>', $this->mode);
$this->Lexer->addExitPattern('!!', $this->mode);
}
/**
* Handle the match
*/
function handle($match, $state, $pos, Doku_Handler $handler) {
switch ($state) {
case DOKU_LEXER_ENTER:
switch ($match{1}) {
case 'c':
$data = $this->_handleColorEnter($match);
break;
case 'm':
$data = $this->_handleMarkerEnter($match);
break;
case '!':
$data = $this->_handleMarkerEnter('<m>');
break;
}
return array($state, $data);
case DOKU_LEXER_UNMATCHED:
return array($state, $match);
case DOKU_LEXER_EXIT:
return array($state, '');
}
return array();
}
/**
* Create output
*/
function render($mode, Doku_Renderer $renderer, $data) {
if($mode == 'xhtml'){
list($state, $match) = $data;
switch ($state) {
case DOKU_LEXER_ENTER:
list($color, $background) = $match;
$renderer->doc .= $this->_renderOpenTag($color, $background);
break;
case DOKU_LEXER_UNMATCHED:
$renderer->doc .= $renderer->_xmlEntities($match);
break;
case DOKU_LEXER_EXIT :
$renderer->doc .= "</span>";
break;
}
return true;
}
return false;
}
/**
*
*/
function _handleColorEnter($syntax) {
list($color, $background) = preg_split('/\//', substr($syntax, 6, -1), 2);
$color = $this->_validateColor($color);
$background = $this->_validateColor($background);
return array($color, $background);
}
/**
*
*/
function _handleMarkerEnter($syntax) {
preg_match('/<m(\d?)>/', $syntax, $match);
if ($match[1] != '') {
$marker = intval($match[1]);
}
else {
$marker = 0;
}
//TODO: load marker colors from configuration.
$color = 'gray';
$background = 'yellow';
return array($color, $background);
}
/**
*
*/
function _renderOpenTag($color, $background) {
$style = '';
if ($color != '') {
$style .= 'color: ' . $color . ';';
}
if ($background != '') {
$style .= 'background-color: ' . $background . ';';
}
$html .= '<span';
if ($style != '') {
$html .= ' style="' . $style . '"';
}
return $html . '>';
}
/**
* Validate color value $c
* this is cut price validation - only to ensure the basic format is correct and there is nothing harmful
* three basic formats "colorname", "#fff[fff]", "rgb(255[%],255[%],255[%])"
*/
function _validateColor($c) {
$c = trim($c);
$pattern = "/^\s*(
([a-zA-z]+)| #colorname - not verified
(\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}))| #colorvalue
(rgb\(([0-9]{1,3}%?,){2}[0-9]{1,3}%?\)) #rgb triplet
)\s*$/x";
if (preg_match($pattern, $c)) {
return trim($c);
}
return '';
}
}