-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDUBB.php
234 lines (208 loc) · 6.9 KB
/
DUBB.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* Load the DUBB tag class(es) when registering them.
*
* @author Stijn Leenknegt <[email protected]>
* @version 1.0
* @package DUBB
*/
function __autoload($class)
{
include_once '.' . DIRECTORY_SEPARATOR . str_replace('_' , DIRECTORY_SEPARATOR , $class) . '.php';
}
/**
* Setup the include path to find the tag classes.
*/
set_include_path(get_include_path() . PATH_SEPARATOR . '.' . DIRECTORY_SEPARATOR . 'DUBB' . DIRECTORY_SEPARATOR);
/**
* <h1>Dotted UBB</h1>
*
* Dotted UBB, short DUBB, is a new and fast way to style your text.
* The usability is unique and has never been used before.
* An introduction:
*
* [The quick.{i} brown.{color('brown')} fox jumps over the lazy dog].{b, size('3')}
*
* The render will give the follow output on your screen.
*
* <h3><strong>The <em>quick</em> <span style="color: brown;">brown</span> fox jumps over the lazy dog</strong></h3>
*
* <h2>Features</h2>
*
* You can style a (group) word(s) like the follow example:
*
* word.{b}
*
* If you want to style a single word, you put a dot after the word and then write braces.
* Between the braces you put your style or styles.
* You can divide multiple styles with a comma.
*
* word.{b , i}
*
* In the example above the word will be printed bold and italic.
* If you want add a special style, example creating a link of the word.
* You have to have some parameters like an URL address or more.
* This is called a styletag function. You write the name of the styletag function and between
* the parentheses you give the parameter(s).
* Next is an example of a style function.
*
* word.{url(http://www.google.com)}
*
* If you have to pass more then one parameter, you can divide them by a comma.
* You can use normal styletags (b, i, ...) and styletag functions on the same word without any order.
*
* If you want to style more then one word, you can group the words between square brackets.
* After the ] bracket you put the dot and the style(s) between the braces.
*
* [This is my first wordgroup].{b , url(http://www.google.com , Google websearch!) , i}
*
* The words "This is my first wordgroup" will be an URL to google.com with a title attribute.
* The group of words will also be printed bold and italic.
*
* Since version 1.1 you can group multiple lines together.
*
* [foo
* bar
* baz].{b}
*
* If you have large text to style, maybe to make your introduction bold, you can group it and style it.
* You can also nest styletags, like follow example:
*
* [foo bar.{b} baz]{i}
*
* "foo bar baz" is italic and "bar" bold.
* Je can also nest group in group, see follow example:
*
* [Blandit crisare, facilisi autem feugiat suscipit illum feugiat eum, ut illum.
* Esse magna [te facilisi erat delenit et. Commodo in vel eros te in.].{b}
* Nulla eu nonummy velit vel in, amet vulputate et eros tation nostrud vero, in aliquip amet, in facilisi feugait eu et.
* Qui vero eu blandit delenit facilisi magna consequat illum, ullamcorper tation.].{i}
*
* The whole section is printed italic and the second sentence is also printed bold.
*
*
* <h2>How to use in you PHP script(s)?</h2>
*
* Before you can render text, you have to tell the DUBB class which styletags you want to use.
* You can register the styletags with the function DUBB::registerTag($tag).
* If you don't register any styletags, you're text will be plain and boring.
*
* DUBB::registerTag('b')
*
* If you want to register a styletag function, you need a second parameter for the registerTag($tag) function.
* The second parameter tells DUBB how many parameters the function needs.
*
* DUBB::registerTag('url' , 2);
*
* If you want to register more then one styletags, you can use the function registerTags(array $tags).
* See follow example:
*
* DUBB::registerTags( array('b' , 'url' => 2) );
*
* The function DUBB#render() will render the string you passed to the constructor.
* It gives the styled string back. You can print it or save it or eat it.
* I don't care as long it renders I'm happy.
*
*
* Here you can see a small example, enjoy!
*
* <code>
* DUBB::registerTags(array('b' , 'i' , 'color' => 1));
* $string = "blaat.{b} - [foo bar].{i, color(red)} :)";
* $dubb = new DUBB($string);
* echo $dubb->render();
* </code>
*
*
* @author Stijn Leenknegt <[email protected]>
* @version 1.4.1
* @package DUBB
*/
class DUBB
{
const DUBB_TAG_CLASS_PREFIX = 'DUBB_Tag_';
protected static $_tags = array();
protected $_string;
/**
* Create a new DUBB instance.
*
* @param string $string
*/
public function __construct($string)
{
$this->_string = $string;
}
/**
* Register a DUBB styletag.
*
* @static
* @param string $tag
* @param int $params optional
* @throws DUBB_Exception
*/
public static function registerTag($tag , $params = 0)
{
if(! class_exists(self::DUBB_TAG_CLASS_PREFIX . ucfirst($tag)))
throw new DUBB_Exception("The class " . self::DUBB_TAG_CLASS_PREFIX . ucfirst($tag) . " could not be found!");
self::$_tags[$tag] = $params;
}
/**
* Register multiple DUBB styletags.
*
* @static
* @param array $tags
*/
public static function registerTags(array $tags)
{
foreach($tags as $tag => $params) {
if(is_int($tag))
self::registerTag($params);
else
self::registerTag($tag , $params);
}
}
/**
* Render the string, DUBB style!
*
* @return string
*/
public function render()
{
//replace all words.{b} in [words].{b}
$this->_string = preg_replace("~(\w+)\.\{(.*?)\}~" , "[\\1].{\\2}" , $this->_string);
/**
* It happens here. Every [...].{...} will be rendert.
*
* $results[0] contains the matched part.
* $results[1] contains the string that has to be styled without the square brackets.
* $results[2] contains the tags without the braces.
*/
while(preg_match("~\[([^\[]*?)\]\.\{(.*?)\}~" , $this->_string , $results)) {
$str = $results[1];
//build the style tag list
$tagList = new DUBB_Render_TagList($results[2]);
//now render every tag in $tagList on the string
$iterator = $tagList->getTagListIterator();
while($iterator->valid()) {
$str = $iterator->current()->render($str);
$iterator->next();
}
//replace the styled $str in $this->_string
$this->_string = str_replace($results[0] , $str , $this->_string);
}
return $this->_string;
}
/**
* Check if the tag is a registered styletag.
*
* @static
* @param string $tag
* @param int $amountParams
* @return boolean
*/
public static function isRegisteredTag($tag , $amountParams)
{
return array_key_exists($tag , self::$_tags) &&
((is_array(self::$_tags[$tag]) && in_array($amountParams , self::$_tags[$tag])) || (self::$_tags[$tag] == $amountParams));
}
}