33/*
44 * This file is part of ansi-to-html.
55 *
6- * (c) 2013 Fabien Potencier
6+ * (c) Fabien Potencier
77 *
88 * For the full copyright and license information, please view the LICENSE
99 * file that was distributed with this source code.
1818 */
1919class AnsiToHtmlConverter
2020{
21- protected $ theme ;
22- protected $ charset ;
23- protected $ inlineStyles ;
24- protected $ inlineColors ;
25- protected $ colorNames ;
26-
27- public function __construct (?Theme $ theme = null , $ inlineStyles = true , $ charset = 'UTF-8 ' )
28- {
29- $ this ->theme = null === $ theme ? new Theme () : $ theme ;
30- $ this ->inlineStyles = $ inlineStyles ;
31- $ this ->charset = $ charset ;
21+ protected array $ inlineColors ;
22+ protected array $ colorNames ;
23+
24+ public function __construct (
25+ protected Theme $ theme = new Theme (),
26+ protected bool $ inlineStyles = true ,
27+ protected string $ charset = 'UTF-8 ' ,
28+ ) {
3229 $ this ->inlineColors = $ this ->theme ->asArray ();
33- $ this ->colorNames = array (
30+ $ this ->colorNames = [
3431 'black ' , 'red ' , 'green ' , 'yellow ' , 'blue ' , 'magenta ' , 'cyan ' , 'white ' ,
3532 '' , '' ,
3633 'brblack ' , 'brred ' , 'brgreen ' , 'bryellow ' , 'brblue ' , 'brmagenta ' , 'brcyan ' , 'brwhite ' ,
37- ) ;
34+ ] ;
3835 }
3936
40- public function convert ($ text )
37+ public function convert (string $ text ): string
4138 {
4239 // remove cursor movement sequences
4340 $ 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 );
4441 // remove character set sequences
4542 $ text = preg_replace ('#\e(\(|\))(A|B|[0-2])# ' , '' , $ text );
4643
47- $ text = htmlspecialchars ($ text , PHP_VERSION_ID >= 50400 ? ENT_QUOTES | ENT_SUBSTITUTE : ENT_QUOTES , $ this ->charset );
44+ $ text = htmlspecialchars ($ text , \ PHP_VERSION_ID >= 50400 ? \ ENT_QUOTES | \ ENT_SUBSTITUTE : \ ENT_QUOTES , $ this ->charset );
4845
4946 // carriage return
5047 $ text = preg_replace ('#^.*\r(?!\n)#m ' , '' , $ text );
@@ -56,7 +53,7 @@ public function convert($text)
5653 if ('backspace ' == $ token [0 ]) {
5754 $ j = $ i ;
5855 while (--$ j >= 0 ) {
59- if ('text ' == $ tokens [$ j ][0 ] && strlen ( $ tokens [$ j ][1 ]) > 0 ) {
56+ if ('text ' == $ tokens [$ j ][0 ] && '' !== $ tokens [$ j ][1 ]) {
6057 $ tokens [$ j ][1 ] = substr ($ tokens [$ j ][1 ], 0 , -1 );
6158
6259 break ;
@@ -75,9 +72,9 @@ public function convert($text)
7572 }
7673
7774 if ($ this ->inlineStyles ) {
78- $ html = sprintf ('<span style="background-color: %s; color: %s">%s</span> ' , $ this ->inlineColors ['black ' ], $ this ->inlineColors ['white ' ], $ html );
75+ $ html = \ sprintf ('<span style="background-color: %s; color: %s">%s</span> ' , $ this ->inlineColors ['black ' ], $ this ->inlineColors ['white ' ], $ html );
7976 } else {
80- $ html = sprintf ('<span class="ansi_color_bg_black ansi_color_fg_white">%s</span> ' , $ html );
77+ $ html = \ sprintf ('<span class="ansi_color_bg_black ansi_color_fg_white">%s</span> ' , $ html );
8178 }
8279
8380 // remove empty span
@@ -86,12 +83,12 @@ public function convert($text)
8683 return $ html ;
8784 }
8885
89- public function getTheme ()
86+ public function getTheme (): Theme
9087 {
9188 return $ this ->theme ;
9289 }
9390
94- protected function convertAnsiToColor ($ ansi )
91+ protected function convertAnsiToColor (string $ ansi ): string
9592 {
9693 $ bg = 0 ;
9794 $ fg = 7 ;
@@ -102,6 +99,7 @@ protected function convertAnsiToColor($ansi)
10299 $ options = explode ('; ' , $ ansi );
103100
104101 foreach ($ options as $ key => $ option ) {
102+ $ option = (int ) $ option ;
105103 if ($ option >= 30 && $ option < 38 ) {
106104 $ fg = $ option - 30 ;
107105 } elseif ($ option >= 40 && $ option < 48 ) {
@@ -135,75 +133,75 @@ protected function convertAnsiToColor($ansi)
135133 }
136134
137135 // options: bold => 1, dim => 2, italic => 3, underscore => 4, blink => 5, rapid blink => 6, reverse => 7, conceal => 8, strikethrough => 9
138- if (in_array (1 , $ options ) || $ hi ) { // high intensity equals regular bold
136+ if (\ in_array (1 , $ options ) || $ hi ) { // high intensity equals regular bold
139137 $ fg += 10 ;
140138 // bold does not affect background color
141139 }
142140
143- if (in_array (2 , $ options )) { // dim
141+ if (\ in_array (2 , $ options )) { // dim
144142 $ fg = ($ fg >= 10 ) ? $ fg - 10 : $ fg ;
145143 }
146144
147- if (in_array (3 , $ options )) {
145+ if (\ in_array (3 , $ options )) {
148146 $ as .= '; font-style: italic ' ;
149147 $ cs .= ' ansi_color_italic ' ;
150148 }
151149
152- if (in_array (4 , $ options )) {
150+ if (\ in_array (4 , $ options )) {
153151 $ as .= '; text-decoration: underline ' ;
154152 $ cs .= ' ansi_color_underline ' ;
155153 }
156154
157- if (in_array (9 , $ options )) {
155+ if (\ in_array (9 , $ options )) {
158156 $ as .= '; text-decoration: line-through ' ;
159157 $ cs .= ' ansi_color_strikethrough ' ;
160158 }
161159
162- if (in_array (7 , $ options )) {
160+ if (\ in_array (7 , $ options )) {
163161 $ tmp = $ fg ;
164162 $ fg = $ bg ;
165163 $ bg = $ tmp ;
166164 }
167165 }
168166
169167 if ($ this ->inlineStyles ) {
170- return sprintf ('</span><span style="background-color: %s; color: %s%s"> ' , $ this ->inlineColors [$ this ->colorNames [$ bg ]], $ this ->inlineColors [$ this ->colorNames [$ fg ]], $ as );
168+ return \ sprintf ('</span><span style="background-color: %s; color: %s%s"> ' , $ this ->inlineColors [$ this ->colorNames [$ bg ]], $ this ->inlineColors [$ this ->colorNames [$ fg ]], $ as );
171169 } else {
172- return sprintf ('</span><span class="ansi_color_bg_%s ansi_color_fg_%s%s"> ' , $ this ->colorNames [$ bg ], $ this ->colorNames [$ fg ], $ cs );
170+ return \ sprintf ('</span><span class="ansi_color_bg_%s ansi_color_fg_%s%s"> ' , $ this ->colorNames [$ bg ], $ this ->colorNames [$ fg ], $ cs );
173171 }
174172 }
175173
176- protected function tokenize ($ text )
174+ protected function tokenize (string $ text ): array
177175 {
178- $ tokens = array () ;
179- preg_match_all ("/(?: \e\[(.*?)m|( \x08))/ " , $ text , $ matches , PREG_OFFSET_CAPTURE );
176+ $ tokens = [] ;
177+ preg_match_all ("/(?: \e\[(.*?)m|( \x08))/ " , $ text , $ matches , \ PREG_OFFSET_CAPTURE );
180178
181- $ codes = array () ;
179+ $ codes = [] ;
182180 $ offset = 0 ;
183181 foreach ($ matches [0 ] as $ i => $ match ) {
184182 if ($ match [1 ] - $ offset > 0 ) {
185- $ tokens [] = array ( 'text ' , substr ($ text , $ offset , $ match [1 ] - $ offset )) ;
183+ $ tokens [] = [ 'text ' , substr ($ text , $ offset , $ match [1 ] - $ offset )] ;
186184 }
187185
188186 foreach (explode ('; ' , $ matches [1 ][$ i ][0 ]) as $ code ) {
189187 if ('0 ' == $ code || '' == $ code ) {
190- $ codes = array () ;
188+ $ codes = [] ;
191189 } else {
192190 // remove existing occurrence to avoid processing duplicate styles
193- if (in_array ($ code , $ codes ) && ($ key = array_search ($ code , $ codes )) !== false ) {
191+ if (\ in_array ($ code , $ codes ) && ($ key = array_search ($ code , $ codes )) !== false ) {
194192 unset($ codes [$ key ]);
195193 }
196194 }
197195
198196 $ codes [] = $ code ;
199197 }
200198
201- $ tokens [] = array ( "\x08" == $ match [0 ] ? 'backspace ' : 'color ' , implode ('; ' , $ codes )) ;
202- $ offset = $ match [1 ] + strlen ($ match [0 ]);
199+ $ tokens [] = [ "\x08" == $ match [0 ] ? 'backspace ' : 'color ' , implode ('; ' , $ codes )] ;
200+ $ offset = $ match [1 ] + \ strlen ($ match [0 ]);
203201 }
204202
205- if ($ offset < strlen ($ text )) {
206- $ tokens [] = array ( 'text ' , substr ($ text , $ offset )) ;
203+ if ($ offset < \ strlen ($ text )) {
204+ $ tokens [] = [ 'text ' , substr ($ text , $ offset )] ;
207205 }
208206
209207 return $ tokens ;
0 commit comments