11<?php
22
3- /*
3+ /**
44 * This file is part of ansi-to-html.
55 *
66 * (c) 2013 Fabien Potencier
1818 */
1919class AnsiToHtmlConverter
2020{
21+ /** @var Theme */
2122 protected $ theme ;
23+
24+ /** @var string */
2225 protected $ charset ;
26+
27+ /** @var bool */
2328 protected $ inlineStyles ;
29+
30+ /** @var string[] */
2431 protected $ inlineColors ;
32+
33+ /** @var string[] */
2534 protected $ colorNames ;
2635
2736 public function __construct (Theme $ theme = null , $ inlineStyles = true , $ charset = 'UTF-8 ' )
2837 {
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 = [
3443 'black ' , 'red ' , 'green ' , 'yellow ' , 'blue ' , 'magenta ' , 'cyan ' , 'white ' ,
3544 '' , '' ,
3645 'brblack ' , 'brred ' , 'brgreen ' , 'bryellow ' , 'brblue ' , 'brmagenta ' , 'brcyan ' , 'brwhite ' ,
37- ) ;
46+ ] ;
3847 }
3948
4049 public function convert ($ text )
4150 {
4251 // 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 );
4453 // remove character set sequences
45- $ text = preg_replace ('#\e(\(|\))(A|B|[0-2])# ' , '' , $ text );
54+ $ text = \ preg_replace ('#\e(\(|\))(A|B|[0-2])# ' , '' , $ text );
4655
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 );
4857
4958 // carriage return
50- $ text = preg_replace ('#^.*\r(?!\n)#m ' , '' , $ text );
59+ $ text = \ preg_replace ('#^.*\r(?!\n)#m ' , '' , $ text );
5160
5261 $ tokens = $ this ->tokenize ($ text );
5362
@@ -56,8 +65,8 @@ public function convert($text)
5665 if ('backspace ' == $ token [0 ]) {
5766 $ j = $ i ;
5867 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 );
6170
6271 break ;
6372 }
@@ -75,20 +84,35 @@ public function convert($text)
7584 }
7685
7786 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 );
7988 } 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 );
8190 }
8291
8392 // remove empty span
84- $ html = preg_replace ('#<span[^>]*></span># ' , '' , $ html );
93+ $ html = \ preg_replace ('#<span[^>]*></span># ' , '' , $ html );
8594
8695 return $ html ;
8796 }
8897
89- public function getTheme ( )
98+ protected function tokenize ( $ text )
9099 {
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 ;
92116 }
93117
94118 protected function convertAnsiToColor ($ ansi )
@@ -97,7 +121,7 @@ protected function convertAnsiToColor($ansi)
97121 $ fg = 7 ;
98122 $ as = '' ;
99123 if ('0 ' != $ ansi && '' != $ ansi ) {
100- $ options = explode ('; ' , $ ansi );
124+ $ options = \ explode ('; ' , $ ansi );
101125
102126 foreach ($ options as $ option ) {
103127 if ($ option >= 30 && $ option < 38 ) {
@@ -112,46 +136,75 @@ protected function convertAnsiToColor($ansi)
112136 }
113137
114138 // options: bold => 1, underscore => 4, blink => 5, reverse => 7, conceal => 8
115- if (in_array (1 , $ options )) {
139+ if (\ in_array (1 , $ options )) {
116140 $ fg += 10 ;
117141 $ bg += 10 ;
118142 }
119143
120- if (in_array (4 , $ options )) {
144+ if (\ in_array (4 , $ options )) {
121145 $ as = '; text-decoration: underline ' ;
122146 }
123147
124- if (in_array (7 , $ options )) {
148+ if (\ in_array (7 , $ options )) {
125149 $ tmp = $ fg ;
126150 $ fg = $ bg ;
127151 $ bg = $ tmp ;
128152 }
129153 }
130154
131155 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 );
133157 } 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 ]);
135159 }
136160 }
137161
138- protected function tokenize ($ text )
162+ /**
163+ * @return Theme
164+ */
165+ public function getTheme ()
139166 {
140- $ tokens = array () ;
141- preg_match_all ( " /(?: \e \[(.*?)m|( \x08 ))/ " , $ text , $ matches , PREG_OFFSET_CAPTURE );
167+ return $ this -> theme ;
168+ }
142169
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+ }
154178
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 ;
156209 }
157210}
0 commit comments