1
1
<?php
2
2
/*----------------------------------------------------------------------------*/
3
-
3
+
4
4
class BitterFormatSymphony extends BitterFormat {
5
5
protected $ tabsize = 4 ;
6
6
protected $ line = 1 ;
7
7
protected $ output = '' ;
8
-
8
+
9
9
public function process ($ source ) {
10
10
$ this ->output = $ source ;
11
-
11
+
12
12
$ this ->processTabs ();
13
13
$ this ->processLines ();
14
-
14
+
15
15
return sprintf (
16
16
'<pre>%s</pre> ' ,
17
17
$ this ->output
18
18
);
19
19
}
20
-
20
+
21
21
protected function processTabs () {
22
22
// first split the output into manageable chunks
23
23
$ lines = explode (PHP_EOL , $ this ->output );
24
24
$ linesCount = count ($ lines );
25
25
// fix lines one by one
26
26
for ($ x = 0 ; $ x < $ linesCount ; $ x ++) {
27
27
// while there are still tabs
28
- while (strpos ($ lines [$ x ], "\t" ) !== FALSE ) {
28
+ while (strpos ($ lines [$ x ], "\t" ) !== false ) {
29
29
// replace tabs for spaces
30
30
$ lines [$ x ] = preg_replace_callback ('%^([^\t]*)([\t]+)% ' , array ($ this , 'processTabsLine ' ), $ lines [$ x ]);
31
31
}
32
32
}
33
33
// concat the final output
34
34
$ this ->output = implode (PHP_EOL , $ lines );
35
35
}
36
-
36
+
37
37
protected function processTabsLine ($ matches ) {
38
38
return $ matches [1 ] . str_repeat (
39
39
' ' , strlen ($ matches [2 ]) * $ this ->tabsize
40
40
);
41
41
}
42
-
42
+
43
43
protected function processLines () {
44
44
$ tokens = preg_split ('%(<span class=".*?">|</span>)% ' , $ this ->output , 0 , PREG_SPLIT_DELIM_CAPTURE );
45
45
$ stack = array (); $ this ->output = '' ;
46
-
46
+
47
47
$ this ->startLine ();
48
-
48
+
49
49
foreach ($ tokens as $ token ) {
50
50
// Close:
51
51
if (preg_match ('%^</% ' , $ token )) {
52
52
array_pop ($ stack );
53
53
$ this ->output .= $ token ;
54
54
}
55
-
55
+
56
56
// Open:
57
57
else if (preg_match ('%^<% ' , $ token )) {
58
58
array_push ($ stack , $ token );
59
59
$ this ->output .= $ token ;
60
60
}
61
-
61
+
62
62
else {
63
63
$ characters = preg_split ('// ' , $ token );
64
-
64
+
65
65
foreach ($ characters as $ character ) {
66
66
if ($ character == "\n" ) {
67
67
$ this ->endLine ();
68
-
68
+
69
69
foreach ($ stack as $ alt_token ) $ this ->output .= '</span> ' ;
70
70
}
71
-
71
+
72
72
$ this ->output .= $ character ;
73
-
73
+
74
74
if ($ character == "\n" ) {
75
75
$ this ->startLine ();
76
-
76
+
77
77
foreach ($ stack as $ alt_token ) $ this ->output .= $ alt_token ;
78
78
}
79
79
}
80
80
}
81
81
}
82
-
82
+
83
83
$ this ->endLine ();
84
84
}
85
-
85
+
86
86
protected function startLine () {
87
87
$ this ->output .= "<line id= \"{$ this ->line }\"> " ;
88
88
$ this ->output .= "<marker></marker> " ;
89
89
$ this ->output .= "<content> " ;
90
90
}
91
-
91
+
92
92
protected function endLine () {
93
93
$ this ->line ++;
94
94
$ this ->output .= '</content> ' ;
95
95
$ this ->output .= '</line> ' ;
96
96
}
97
97
}
98
-
98
+
99
99
/*----------------------------------------------------------------------------*/
100
-
100
+
101
101
return new BitterFormatSymphony ();
102
-
102
+
103
103
/*----------------------------------------------------------------------------*/
104
- ?>
104
+ ?>
0 commit comments