1
1
<?php
2
2
3
- /*
3
+ /**
4
4
* This file is part of ansi-to-html.
5
5
*
6
6
* (c) 2013 Fabien Potencier
18
18
*/
19
19
class AnsiToHtmlConverter
20
20
{
21
+ /** @var Theme */
21
22
protected $ theme ;
23
+
24
+ /** @var string */
22
25
protected $ charset ;
26
+
27
+ /** @var bool */
23
28
protected $ inlineStyles ;
29
+
30
+ /** @var string[] */
24
31
protected $ inlineColors ;
32
+
33
+ /** @var string[] */
25
34
protected $ colorNames ;
26
35
27
36
public function __construct (Theme $ theme = null , $ inlineStyles = true , $ charset = 'UTF-8 ' )
28
37
{
29
- $ this ->theme = null === $ theme ? new Theme () : $ theme ;
30
- $ this ->inlineStyles = $ inlineStyles ;
31
- $ this ->charset = $ charset ;
32
- $ this -> inlineColors = $ this -> theme -> asArray ();
33
- $ this ->colorNames = array (
38
+ $ this ->setTheme ( $ theme) ;
39
+ $ this ->setInlineStyles ( $ inlineStyles) ;
40
+ $ this ->setCharset ( $ charset) ;
41
+
42
+ $ this ->colorNames = [
34
43
'black ' , 'red ' , 'green ' , 'yellow ' , 'blue ' , 'magenta ' , 'cyan ' , 'white ' ,
35
44
'' , '' ,
36
45
'brblack ' , 'brred ' , 'brgreen ' , 'bryellow ' , 'brblue ' , 'brmagenta ' , 'brcyan ' , 'brwhite ' ,
37
- ) ;
46
+ ] ;
38
47
}
39
48
40
49
public function convert ($ text )
41
50
{
42
51
// remove cursor movement sequences
43
- $ text = preg_replace ('#\e\[(K|s|u|2J|2K|\d+(A|B|C|D|E|F|G|J|K|S|T)|\d+;\d+(H|f))# ' , '' , $ text );
52
+ $ text = \ preg_replace ('#\e\[(K|s|u|2J|2K|\d+(A|B|C|D|E|F|G|J|K|S|T)|\d+;\d+(H|f))# ' , '' , $ text );
44
53
// remove character set sequences
45
- $ text = preg_replace ('#\e(\(|\))(A|B|[0-2])# ' , '' , $ text );
54
+ $ text = \ preg_replace ('#\e(\(|\))(A|B|[0-2])# ' , '' , $ text );
46
55
47
- $ text = htmlspecialchars ($ text , PHP_VERSION_ID >= 50400 ? ENT_QUOTES | ENT_SUBSTITUTE : ENT_QUOTES , $ this ->charset );
56
+ $ text = \ htmlspecialchars ($ text , \ PHP_VERSION_ID >= 50400 ? ENT_QUOTES | ENT_SUBSTITUTE : ENT_QUOTES , $ this ->charset );
48
57
49
58
// carriage return
50
- $ text = preg_replace ('#^.*\r(?!\n)#m ' , '' , $ text );
59
+ $ text = \ preg_replace ('#^.*\r(?!\n)#m ' , '' , $ text );
51
60
52
61
$ tokens = $ this ->tokenize ($ text );
53
62
@@ -56,8 +65,8 @@ public function convert($text)
56
65
if ('backspace ' == $ token [0 ]) {
57
66
$ j = $ i ;
58
67
while (--$ j >= 0 ) {
59
- if ('text ' == $ tokens [$ j ][0 ] && strlen ($ tokens [$ j ][1 ]) > 0 ) {
60
- $ tokens [$ j ][1 ] = substr ($ tokens [$ j ][1 ], 0 , -1 );
68
+ if ('text ' == $ tokens [$ j ][0 ] && \ strlen ($ tokens [$ j ][1 ]) > 0 ) {
69
+ $ tokens [$ j ][1 ] = \ substr ($ tokens [$ j ][1 ], 0 , -1 );
61
70
62
71
break ;
63
72
}
@@ -75,20 +84,35 @@ public function convert($text)
75
84
}
76
85
77
86
if ($ this ->inlineStyles ) {
78
- $ html = sprintf ('<span style="background-color: %s; color: %s">%s</span> ' , $ this ->inlineColors ['black ' ], $ this ->inlineColors ['white ' ], $ html );
87
+ $ html = \ sprintf ('<span style="background-color: %s; color: %s">%s</span> ' , $ this ->inlineColors ['black ' ], $ this ->inlineColors ['white ' ], $ html );
79
88
} else {
80
- $ html = sprintf ('<span class="ansi_color_bg_black ansi_color_fg_white">%s</span> ' , $ html );
89
+ $ html = \ sprintf ('<span class="ansi_color_bg_black ansi_color_fg_white">%s</span> ' , $ html );
81
90
}
82
91
83
92
// remove empty span
84
- $ html = preg_replace ('#<span[^>]*></span># ' , '' , $ html );
93
+ $ html = \ preg_replace ('#<span[^>]*></span># ' , '' , $ html );
85
94
86
95
return $ html ;
87
96
}
88
97
89
- public function getTheme ( )
98
+ protected function tokenize ( $ text )
90
99
{
91
- return $ this ->theme ;
100
+ $ tokens = [];
101
+ \preg_match_all ('/(?:\e\[(.*?)m|(\x08))/ ' , $ text , $ matches , PREG_OFFSET_CAPTURE );
102
+
103
+ $ offset = 0 ;
104
+ foreach ($ matches [0 ] as $ i => $ match ) {
105
+ if ($ match [1 ] - $ offset > 0 ) {
106
+ $ tokens [] = ['text ' , \substr ($ text , $ offset , $ match [1 ] - $ offset )];
107
+ }
108
+ $ tokens [] = ["\x08" == $ match [0 ] ? 'backspace ' : 'color ' , $ matches [1 ][$ i ][0 ]];
109
+ $ offset = $ match [1 ] + \strlen ($ match [0 ]);
110
+ }
111
+ if ($ offset < \strlen ($ text )) {
112
+ $ tokens [] = ['text ' , \substr ($ text , $ offset )];
113
+ }
114
+
115
+ return $ tokens ;
92
116
}
93
117
94
118
protected function convertAnsiToColor ($ ansi )
@@ -97,7 +121,7 @@ protected function convertAnsiToColor($ansi)
97
121
$ fg = 7 ;
98
122
$ as = '' ;
99
123
if ('0 ' != $ ansi && '' != $ ansi ) {
100
- $ options = explode ('; ' , $ ansi );
124
+ $ options = \ explode ('; ' , $ ansi );
101
125
102
126
foreach ($ options as $ option ) {
103
127
if ($ option >= 30 && $ option < 38 ) {
@@ -112,46 +136,75 @@ protected function convertAnsiToColor($ansi)
112
136
}
113
137
114
138
// options: bold => 1, underscore => 4, blink => 5, reverse => 7, conceal => 8
115
- if (in_array (1 , $ options )) {
139
+ if (\ in_array (1 , $ options )) {
116
140
$ fg += 10 ;
117
141
$ bg += 10 ;
118
142
}
119
143
120
- if (in_array (4 , $ options )) {
144
+ if (\ in_array (4 , $ options )) {
121
145
$ as = '; text-decoration: underline ' ;
122
146
}
123
147
124
- if (in_array (7 , $ options )) {
148
+ if (\ in_array (7 , $ options )) {
125
149
$ tmp = $ fg ;
126
150
$ fg = $ bg ;
127
151
$ bg = $ tmp ;
128
152
}
129
153
}
130
154
131
155
if ($ this ->inlineStyles ) {
132
- return sprintf ('</span><span style="background-color: %s; color: %s%s"> ' , $ this ->inlineColors [$ this ->colorNames [$ bg ]], $ this ->inlineColors [$ this ->colorNames [$ fg ]], $ as );
156
+ return \ sprintf ('</span><span style="background-color: %s; color: %s%s"> ' , $ this ->inlineColors [$ this ->colorNames [$ bg ]], $ this ->inlineColors [$ this ->colorNames [$ fg ]], $ as );
133
157
} else {
134
- return sprintf ('</span><span class="ansi_color_bg_%s ansi_color_fg_%s"> ' , $ this ->colorNames [$ bg ], $ this ->colorNames [$ fg ]);
158
+ return \ sprintf ('</span><span class="ansi_color_bg_%s ansi_color_fg_%s"> ' , $ this ->colorNames [$ bg ], $ this ->colorNames [$ fg ]);
135
159
}
136
160
}
137
161
138
- protected function tokenize ($ text )
162
+ /**
163
+ * @return Theme
164
+ */
165
+ public function getTheme ()
139
166
{
140
- $ tokens = array () ;
141
- preg_match_all ( " /(?: \e \[(.*?)m|( \x08 ))/ " , $ text , $ matches , PREG_OFFSET_CAPTURE );
167
+ return $ this -> theme ;
168
+ }
142
169
143
- $ offset = 0 ;
144
- foreach ($ matches [0 ] as $ i => $ match ) {
145
- if ($ match [1 ] - $ offset > 0 ) {
146
- $ tokens [] = array ('text ' , substr ($ text , $ offset , $ match [1 ] - $ offset ));
147
- }
148
- $ tokens [] = array ("\x08" == $ match [0 ] ? 'backspace ' : 'color ' , $ matches [1 ][$ i ][0 ]);
149
- $ offset = $ match [1 ] + strlen ($ match [0 ]);
150
- }
151
- if ($ offset < strlen ($ text )) {
152
- $ tokens [] = array ('text ' , substr ($ text , $ offset ));
153
- }
170
+ /**
171
+ * @param Theme|null $theme
172
+ */
173
+ public function setTheme (Theme $ theme = null )
174
+ {
175
+ $ this ->theme = null === $ theme ? new Theme () : $ theme ;
176
+ $ this ->inlineColors = $ this ->theme ->asArray ();
177
+ }
154
178
155
- return $ tokens ;
179
+ /**
180
+ * @return bool
181
+ */
182
+ public function isInlineStyles ()
183
+ {
184
+ return $ this ->inlineStyles ;
185
+ }
186
+
187
+ /**
188
+ * @param bool $inlineStyles
189
+ */
190
+ public function setInlineStyles ($ inlineStyles )
191
+ {
192
+ $ this ->inlineStyles = $ inlineStyles ;
193
+ }
194
+
195
+ /**
196
+ * @return string
197
+ */
198
+ public function getCharset ()
199
+ {
200
+ return $ this ->charset ;
201
+ }
202
+
203
+ /**
204
+ * @param string $charset
205
+ */
206
+ public function setCharset ($ charset )
207
+ {
208
+ $ this ->charset = $ charset ;
156
209
}
157
210
}
0 commit comments