Skip to content

Commit d92bc5c

Browse files
committed
Update bitter library
1 parent bea2283 commit d92bc5c

8 files changed

Lines changed: 277 additions & 68 deletions

File tree

lib/bitter/bitter.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,35 @@
1818
/*----------------------------------------------------------------------------*/
1919

2020
class Bitter {
21-
public function encode($subject) {
21+
static public function encode($subject) {
2222
return str_replace(
2323
array('&', '<', '>'),
2424
array('&amp;', '&lt;', '&gt;'),
2525
$subject
2626
);
2727
}
2828

29-
public function capture($expression, $flags = '') {
29+
static public function capture($expression, $flags = '') {
3030
return new BitterMatch($expression, $flags, BitterMatch::CAPTURE);
3131
}
3232

33-
public function start($expression, $flags = '') {
33+
static public function start($expression, $flags = '') {
3434
return new BitterMatch($expression, $flags, BitterMatch::START);
3535
}
3636

37-
public function stop($expression, $flags = '') {
37+
static public function stop($expression, $flags = '') {
3838
return new BitterMatch($expression, $flags, BitterMatch::STOP);
3939
}
4040

41-
public function id($name) {
41+
static public function id($name) {
4242
return new BitterId($name);
4343
}
4444

45-
public function tag($value) {
45+
static public function tag($value) {
4646
return new BitterTag($value);
4747
}
4848

49-
public function rule(BitterId $id) {
49+
static public function rule(BitterId $id) {
5050
$values = func_get_args();
5151
$rule = new BitterRule();
5252

@@ -141,13 +141,14 @@ public function process($source) {
141141
// Sanitise newline formats:
142142
$source = trim(preg_replace('/(\r\n|\r|\n)/i', "\n", $source));
143143

144+
$this->cache_time = 0;
144145
$this->cache_file = BITTER_CACHE_PATH . '/' . implode('-', array(
145146
md5($this->language_name),
146147
md5($this->format_name),
147148
md5($source)
148149
));
149150

150-
if (is_readable($this->cache_file)) {
151+
if (is_readable($this->cache_file) and is_file($this->cache_file)) {
151152
$this->cache_time = (integer)filemtime($this->cache_file);
152153
$this->refresh_time = max($this->language_time, $this->format_time);
153154
}
@@ -352,9 +353,13 @@ public function process($context, $root = false) {
352353
$output = ''; $count = 0;
353354
$this->disabled = array();
354355

355-
while ($count++ < 1000000 and strlen($context)) {
356+
while (strlen($context)) {
356357
$rule = $this->getRule($context);
357358

359+
if ($count++ > 1000000) {
360+
throw new Exception('Processing failed, recursion limit reached.');
361+
}
362+
358363
if ($rule instanceof BitterRule) {
359364
$state = $rule->getState($context);
360365

@@ -504,4 +509,4 @@ public function setStop(BitterMatch $match) {
504509
}
505510

506511
/*----------------------------------------------------------------------------*/
507-
?>
512+
?>

lib/bitter/formats/default.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/*----------------------------------------------------------------------------*/
3+
4+
class BitterFormatDefault extends BitterFormat {
5+
protected $tabsize = 4;
6+
protected $line = 1;
7+
protected $output = '';
8+
9+
public function process($source) {
10+
$this->output = $source;
11+
12+
$this->processTabs();
13+
$this->processLines();
14+
15+
return sprintf(
16+
'<pre>%s</pre>',
17+
$this->output
18+
);
19+
}
20+
21+
protected function processTabs() {
22+
if (!function_exists('__expander')) eval("
23+
function __expander(\$matches) {
24+
return \$matches[1] . str_repeat(
25+
' ', strlen(\$matches[2]) * {$this->tabsize} - (strlen(\$matches[1]) % {$this->tabsize})
26+
);
27+
}
28+
");
29+
30+
while (strstr($this->output, "\t")) {
31+
$this->output = preg_replace_callback('%^([^\t\n]*)(\t+)%m', '__expander', $this->output);
32+
}
33+
}
34+
35+
protected function processLines() {
36+
$tokens = preg_split('%(<span class=".*?">|</span>)%', $this->output, 0, PREG_SPLIT_DELIM_CAPTURE);
37+
$stack = array(); $this->output = '';
38+
39+
$this->startLine();
40+
41+
foreach ($tokens as $token) {
42+
// Close:
43+
if (preg_match('%^</%', $token)) {
44+
array_pop($stack);
45+
$this->output .= $token;
46+
}
47+
48+
// Open:
49+
else if (preg_match('%^<%', $token)) {
50+
array_push($stack, $token);
51+
$this->output .= $token;
52+
}
53+
54+
else {
55+
$characters = preg_split('//', $token);
56+
57+
foreach ($characters as $character) {
58+
if ($character == "\n") {
59+
$this->endLine();
60+
61+
foreach ($stack as $alt_token) $this->output .= '</span>';
62+
}
63+
64+
$this->output .= $character;
65+
66+
if ($character == "\n") {
67+
$this->startLine();
68+
69+
foreach ($stack as $alt_token) $this->output .= $alt_token;
70+
}
71+
}
72+
}
73+
}
74+
75+
$this->endLine();
76+
}
77+
78+
protected function startLine() {
79+
$this->output .= "<code class=\"line line-{$this->line}\">";
80+
}
81+
82+
protected function endLine() {
83+
$this->line++;
84+
$this->output .= '</code>';
85+
}
86+
}
87+
88+
/*----------------------------------------------------------------------------*/
89+
90+
return new BitterFormatDefault();
91+
92+
/*----------------------------------------------------------------------------*/
93+
?>

lib/bitter/formats/symphony.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*----------------------------------------------------------------------------*/
33

44
class BitterFormatSymphony extends BitterFormat {
5-
protected $tabsize = 2;
5+
protected $tabsize = 4;
66
protected $line = 1;
77
protected $output = '';
88

@@ -54,9 +54,9 @@ protected function processLines() {
5454

5555
foreach ($characters as $character) {
5656
if ($character == "\n") {
57-
foreach ($stack as $alt_token) $this->output .= '</span>';
58-
5957
$this->endLine();
58+
59+
foreach ($stack as $alt_token) $this->output .= '</span>';
6060
}
6161

6262
$this->output .= $character;

lib/bitter/formats/tabsize-4.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ public function process($source) {
2323
return new BitterFormatTabsizeFour();
2424

2525
/*----------------------------------------------------------------------------*/
26-
?>
26+
?>

lib/bitter/languages/css.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
Bitter::start('\{'),
3030
Bitter::stop('\}'),
3131

32+
Bitter::rule(
33+
Bitter::id('css-property-proprietry'),
34+
Bitter::tag('property proprietry'),
35+
Bitter::capture('-(moz|ms|webkit|khtml|o)-[a-z\-]+(?=:)', 's')
36+
),
3237
Bitter::rule(
3338
Bitter::id('css-property'),
3439
Bitter::tag('property'),
@@ -43,7 +48,8 @@
4348
Bitter::id('css-string-double'),
4449
Bitter::id('css-number'),
4550
Bitter::id('css-color'),
46-
Bitter::id('css-keyword')
51+
Bitter::id('css-keyword'),
52+
Bitter::id('css-keyword-proprietry')
4753
),
4854

4955
Bitter::id('css-comment')
@@ -59,6 +65,12 @@
5965
Bitter::capture('(!important|\b(xx-small|xx-large|xor|x-small|x-large|wider|wait|w-resize|visual|visible|url|uppercase|upper-roman|upper-latin|upper-alpha|underline|ultra-expanded|ultra-condensed|transparent|top|thin|thick|textfield|textarea|text-top|text-bottom|text|table-row-group|table-row|table-header-group|table-footer-group|table-column-group|table-column|table-cell|table-caption|table|sw-resize|super|sub|status-bar|static|square-button|square|space|source-over|source-out|source-in|source-atop|solid|smaller|small-caption|small-caps|small|sliderthumb-vertical|sliderthumb-horizontal|slider-vertical|slider-horizontal|show|serif|separate|semi-expanded|semi-condensed|searchfield-results-decoration|searchfield-results-button|searchfield-decoration|searchfield-cancel-button|searchfield|se-resize|scrollbartrack-vertical|scrollbartrack-horizontal|scrollbarthumb-vertical|scrollbarthumb-horizontal|scrollbargripper-vertical|scrollbargripper-horizontal|scrollbarbutton-up|scrollbarbutton-right|scrollbarbutton-left|scrollbarbutton-down|scroll|sans-serif|s-resize|run-in|rtl|right|ridge|rgba|rgb|repeat-y|repeat-x|repeat|relative|rect|read-write-plaintext-only|read-write|read-only|radio|push-button|progress|pre|pointer|plus-lighter|plus-darker|overline|outside|outset|open-quote|oblique|nw-resize|nowrap|normal|none|no-repeat|no-open-quote|no-close-quote|ne-resize|narrower|n-resize|move|middle|message-box|menulist-textfield|menulist-text|menulist-button|menulist|menu|medium|ltr|lowercase|lower-roman|lower-latin|lower-greek|lower-alpha|logical|listitem|listbox|list-item|lines|line-through|lighter|left|larger|large|justify|italic|invert|inside|inset|inline-table|inline-block|inline|inherit|ignore|icon|hsla|hsl|highlight|hide|hidden|help|groove|georgian|fixed|extra-expanded|extra-condensed|expanded|embed|element|e-resize|double|dotted|discard|disc|destination-over|destination-out|destination-in|destination-atop|default|decimal-leading-zero|decimal|dashed|crosshair|counters|counter|copy|condensed|collapse|close-quote|clear|circle|checkbox|center|caret|caption|capitalize|button-bevel|button|bottom|both|bolder|bold|block|blink|bidi-override|baseline|auto|attr|armenian|after-white-space|absolute))\b')
6066
);
6167

68+
Bitter::rule(
69+
Bitter::id('css-keyword-proprietry'),
70+
Bitter::tag('keyword proprietry'),
71+
Bitter::capture('(-(moz|ms|webkit|khtml|o)-[a-z\-]+)\b')
72+
);
73+
6274
/*------------------------------------------------------------------------------
6375
Strings
6476
------------------------------------------------------------------------------*/

lib/bitter/languages/php.php

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,29 +104,51 @@
104104
Bitter::rule(
105105
Bitter::id('php-define-class-name'),
106106
Bitter::capture('[a-z_][a-z0-9_]+$', 'i'),
107-
Bitter::tag('keyword class')
107+
Bitter::tag('class defined')
108108
),
109109

110110
Bitter::id('php-keyword')
111111
);
112112
Bitter::rule(
113-
Bitter::id('php-keyword-class-alt'),
114-
Bitter::capture('[a-z_][a-z0-9_]+(?=::)', 'i'),
115-
Bitter::tag('keyword class')
113+
Bitter::id('php-keyword-function'),
114+
Bitter::capture('\b(function)\s+[a-z_][a-z0-9_]+', 'i'),
115+
116+
Bitter::rule(
117+
Bitter::id('php-keyword-function-name'),
118+
Bitter::capture('[a-z_][a-z0-9_]+$', 'i'),
119+
Bitter::tag('function defined')
120+
),
121+
122+
Bitter::id('php-keyword')
116123
);
117124
Bitter::rule(
118-
Bitter::id('php-keyword-function'),
119-
Bitter::capture('[a-z_][a-z0-9_]*(?=[a-z0-9_]*\s*\()', 'i'),
125+
Bitter::id('php-keyword-class-access'),
126+
Bitter::capture('[a-z_][a-z0-9_]+::\$*[a-z_][a-z0-9_]*\(?', 'i'),
120127

121128
Bitter::rule(
122-
Bitter::id('php-keyword-function-ignore'),
123-
Bitter::capture('^array|catch|if|elseif$', 'i'),
124-
Bitter::tag('keyword')
129+
Bitter::id('php-keyword-class-access-name'),
130+
Bitter::capture('^.+?(?=::)', 'i'),
131+
Bitter::tag('keyword class')
132+
),
133+
Bitter::rule(
134+
Bitter::id('php-keyword-class-access-variable'),
135+
Bitter::capture('::\$+[a-z_][a-z0-9_]*', 'i'),
136+
Bitter::tag('variable')
137+
),
138+
Bitter::rule(
139+
Bitter::id('php-keyword-class-access-function'),
140+
Bitter::capture('::[a-z_][a-z0-9_]*\(', 'i'),
141+
142+
Bitter::rule(
143+
Bitter::id('php-keyword-class-access-function-call'),
144+
Bitter::capture('[^\(]+', 'i'),
145+
Bitter::tag('function')
146+
)
125147
),
126148
Bitter::rule(
127-
Bitter::id('php-keyword-function-other'),
128-
Bitter::capture('.+'),
129-
Bitter::tag('keyword function')
149+
Bitter::id('php-keyword-class-access-constant'),
150+
Bitter::capture('::[a-z_][a-z0-9_]*\b', 'i'),
151+
Bitter::tag('constant')
130152
)
131153
);
132154

@@ -136,21 +158,46 @@
136158

137159
Bitter::rule(
138160
Bitter::id('php-variable-normal'),
139-
Bitter::capture('(->\$*|\$+)[a-z_][a-z0-9_]*(->\$*[a-z_][a-z0-9_]*)*', 'i'),
161+
Bitter::capture('(->\$*|(::)?\$+)[a-z_][a-z0-9_]*(->\$*[a-z_][a-z0-9_]*)*', 'i'),
140162
Bitter::tag('variable')
141163
);
142164
Bitter::rule(
143165
Bitter::id('php-variable-function'),
144-
Bitter::capture('(->\$*|\$+)[a-z_][a-z0-9_]*(->\$*[a-z_][a-z0-9_]*)*(?=[a-z0-9_]*\s*\()', 'i'),
166+
Bitter::capture('(->\$*|(::)?\$+)[a-z_][a-z0-9_]*(->\$*[a-z_][a-z0-9_]*)*(?=[a-z0-9_]*\s*\()', 'i'),
145167
Bitter::tag('variable'),
146168

147169
Bitter::rule(
148170
Bitter::id('php-variable-function-call'),
149171
Bitter::capture('(\$+|->\$*)[a-z_][a-z0-9_]*$', 'i'),
150-
Bitter::tag('function')
172+
Bitter::tag('function called')
151173
)
152174
);
153175

176+
/*------------------------------------------------------------------------------
177+
Functions
178+
------------------------------------------------------------------------------*/
179+
180+
Bitter::rule(
181+
Bitter::id('php-function'),
182+
Bitter::capture('\b[a-z_][a-z0-9_]*\(', 'i'),
183+
184+
Bitter::rule(
185+
Bitter::id('php-function-call'),
186+
Bitter::capture('[^\(]+', 'i'),
187+
Bitter::tag('function called')
188+
)
189+
);
190+
191+
/*------------------------------------------------------------------------------
192+
Constants
193+
------------------------------------------------------------------------------*/
194+
195+
Bitter::rule(
196+
Bitter::id('php-constant'),
197+
Bitter::capture('\b[a-z_][a-z0-9_]*\b', 'i'),
198+
Bitter::tag('constant')
199+
);
200+
154201
/*------------------------------------------------------------------------------
155202
Main
156203
------------------------------------------------------------------------------*/
@@ -172,9 +219,11 @@
172219
Bitter::id('php-variable-function'),
173220
Bitter::id('php-variable-normal'),
174221
Bitter::id('php-keyword-class'),
175-
Bitter::id('php-keyword-class-alt'),
222+
Bitter::id('php-keyword-class-access'),
176223
Bitter::id('php-keyword-function'),
177-
Bitter::id('php-keyword')
224+
Bitter::id('php-keyword'),
225+
Bitter::id('php-function'),
226+
Bitter::id('php-constant')
178227
);
179228

180229
Bitter::rule(

0 commit comments

Comments
 (0)