forked from lupo49/dokuwiki-plugin-fontcolor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.php
207 lines (184 loc) · 6.16 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* fontcolor Plugin: Allows user-defined font colors
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author modified by ThorstenStratmann <[email protected]>
* @link https://www.dokuwiki.org/plugin:fontcolor
* @version 3.1
*/
if(!defined('DOKU_INC')) die();
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_fontcolor extends DokuWiki_Syntax_Plugin {
protected $odt_styles;
/**
* What kind of syntax are we?
*
* @return string
*/
public function getType() {
return 'formatting';
}
/**
* What kind of syntax do we allow (optional)
*
* @return array
*/
public function getAllowedTypes() {
return array('formatting', 'substition', 'disabled');
}
/**
* What about paragraphs? (optional)
*
* @return string
*/
public function getPType() {
return 'normal';
}
/**
* Where to sort in?
*
* @return int
*/
public function getSort() {
return 90;
}
/**
* Connect pattern to lexer
*
* @param string $mode
*/
public function connectTo($mode) {
$this->Lexer->addEntryPattern('<fc.*?>(?=.*?</fc>)', $mode, 'plugin_fontcolor');
/* $this->Lexer->addEntryPattern('<FC.*?>(?=.*?</FC>)', $mode, 'plugin_fontcolor'); */
}
public function postConnect() {
$this->Lexer->addExitPattern('</fc>', 'plugin_fontcolor');
//$this->Lexer->addExitPattern('</FC>', 'plugin_fontcolor');
}
/**
* override default accepts() method to allow nesting - ie, to get the plugin accepts its own entry syntax
*
* @param string $mode
* @return bool
*/
public function accepts($mode) {
if($mode == 'plugin_fontcolor') return true;
return parent::accepts($mode);
}
/**
* Handle the match
*
* @param string $match The text matched by the patterns
* @param int $state The lexer state for the match
* @param int $pos The character position of the matched text
* @param Doku_Handler $handler The Doku_Handler object
* @return array Return an array with all data you want to use in render
*/
public function handle($match, $state, $pos, Doku_Handler $handler) {
switch($state) {
case DOKU_LEXER_ENTER :
$color = trim(substr($match, 4, -1)); // get the color
$color = $this->_color2hexdec($color);
if($color) {
return array($state, $color);
}
break;
case DOKU_LEXER_UNMATCHED :
$handler->_addCall('cdata', array($match), $pos);
return false;
case DOKU_LEXER_EXIT :
break;
}
return array($state, "#ffff00");
}
/**
* Create output
*
* @param $mode string output format being rendered
* @param $renderer Doku_Renderer the current renderer object
* @param $data array data created by handler()
* @return boolean rendered correctly?
*/
public function render($mode, Doku_Renderer $renderer, $data) {
if($mode == 'xhtml') {
/** @var $renderer Doku_Renderer_xhtml */
list($state, $color) = $data;
switch($state) {
case DOKU_LEXER_ENTER :
$renderer->doc .= "<span style=\"color: $color\">";
break;
case DOKU_LEXER_EXIT :
$renderer->doc .= "</span>";
break;
}
return true;
}
if($mode == 'odt') {
/** @var $renderer renderer_plugin_odt */
list($state, $color) = $data;
switch($state) {
case DOKU_LEXER_ENTER :
$style_index = $color;
//store style
if(empty($this->odt_styles[$style_index])) {
$stylename = "ColorizedText" . count($this->odt_styles);
$this->odt_styles[$style_index] = $stylename;
//Attention: ODT only accepts hexidecimal colors of format #ffffff, not #fff.
$color = $color ? 'fo:color="' . $color . '" ' : '';
$renderer->autostyles[$stylename] = '
<style:style style:name="' . $stylename . '" style:family="text">
<style:text-properties ' . $color . '/>
</style:style>';
}
$renderer->doc .= '<text:span text:style-name="' . $this->odt_styles[$style_index] . '">';
break;
case DOKU_LEXER_EXIT :
$renderer->doc .= "</text:span>";
break;
}
return true;
}
return false;
}
/**
* Returns #hexdec color code, or false
*
* @param string $color
* @return bool|string
*/
protected function _color2hexdec($color) {
$less = new lessc();
$less->importDir[] = DOKU_INC;
$css = '.test { color: spin('.$color.', 0); }'; //less try to spin all colors, and output them as hexdec
try {
$parsedcss = $less->compile($css);
} catch(Exception $e) {
return false;
}
$hexdec = substr($parsedcss, 17, -4);
return $hexdec;
}
/**
* 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[%])"
*
* @param string $c
* @return int
*/
protected function _isValid($c) {
$c = trim($c);
$pattern = "/
(^[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
/x";
return (preg_match($pattern, $c));
}
}
//Setup VIM: ex: et ts=4 sw=4 enc=utf-8 :