11package main
22
33import (
4+ "encoding/csv"
45 "fmt"
6+ "html"
7+ "io"
58 "regexp"
69 "strconv"
710 "strings"
@@ -288,13 +291,42 @@ var color265 = map[string]string{
288291}
289292
290293var col256 = regexp .MustCompile (`^\[([34]8);5;(\d+)m` )
291- var colrgb = regexp .MustCompile (`^\[([34]8);2;(\d+);(\d+);(\d+)m` )
292- var escape = regexp .MustCompile (`^\[(\d+;)?( \d+;)?(\d+;)?(\d+;)?(\d+) ([A-Za-z])` )
294+ var colrgb = regexp .MustCompile (`^\[([34]8|1 );2;(\d+);(\d+);(\d+)m` )
295+ var escape = regexp .MustCompile (`^\[(;? \d+)+ ([A-Za-z])` )
293296
294- func toHtml (raw []byte ) [] byte {
297+ func fromRawToString (raw []byte ) string {
295298 var output = strings .TrimSuffix (string (raw ), "\x00 " )
296299 output = strings .ReplaceAll (output , "\r " , "" )
297- output = strings .Trim (output , "\n " )
300+ return strings .Trim (output , "\n " )
301+ }
302+
303+ func toCsv (output string ) ([]byte , bool ) {
304+ reader := csv .NewReader (strings .NewReader (output ))
305+ reader .Comma = ','
306+
307+ var htmlStr = "<table>\n "
308+ for i := 0 ; ; i ++ {
309+ record , err := reader .Read ()
310+ if err == io .EOF {
311+ break
312+ } else if err != nil || (i == 0 && len (record ) < 2 ) {
313+ return nil , false
314+ }
315+
316+ htmlStr += "<tr>"
317+ for _ , elem := range record {
318+ htmlStr += "\n <td>"
319+ htmlStr += html .EscapeString (elem )
320+ htmlStr += "</td>"
321+ }
322+ htmlStr += "\n </tr>\n "
323+ }
324+
325+ htmlStr += "</table>"
326+ return []byte (htmlStr ), true
327+ }
328+
329+ func toHtml (output string ) []byte {
298330 if output == "" {
299331 return []byte {}
300332 }
@@ -303,20 +335,21 @@ func toHtml(raw []byte) []byte {
303335 output = strings .ReplaceAll (output , ">" , ">" )
304336 output = strings .ReplaceAll (output , "\" " , """ )
305337 output = strings .ReplaceAll (output , "'" , "'" )
306- var html = ""
338+ var htmlStr = ""
307339 tokens := strings .Split (output , "\x1b " )
308340 for _ , token := range tokens {
309341 if token == "" {
310342 continue
311343 }
344+
312345 if strings .HasPrefix (token , "[H" ) ||
313346 strings .HasPrefix (token , "[s" ) ||
314347 strings .HasPrefix (token , "[u" ) ||
315348 strings .HasPrefix (token , "[J" ) ||
316349 strings .HasPrefix (token , "[K" ) {
317- html += token [2 :]
350+ htmlStr += token [2 :]
318351 } else if strings .HasPrefix (token , "[#;#R" ) {
319- html += token [5 :]
352+ htmlStr += token [5 :]
320353 } else if strings .HasPrefix (token , "[#" ) ||
321354 strings .HasPrefix (token , "[0m" ) ||
322355 strings .HasPrefix (token , "[0J" ) ||
@@ -326,41 +359,41 @@ func toHtml(raw []byte) []byte {
326359 strings .HasPrefix (token , "[1K" ) ||
327360 strings .HasPrefix (token , "[2K" ) ||
328361 strings .HasPrefix (token , "[6n" ) {
329- html += token [3 :]
362+ htmlStr += token [3 :]
330363 } else if strings .HasPrefix (token , "[1m" ) ||
331364 strings .HasPrefix (token , "[2m" ) {
332- html += "<b>"
333- html += token [3 :]
334- html += "</b>"
365+ htmlStr += "<b>"
366+ htmlStr += token [3 :]
367+ htmlStr += "</b>"
335368 } else if strings .HasPrefix (token , "[3m" ) {
336- html += "<i>"
337- html += token [3 :]
338- html += "</i>"
369+ htmlStr += "<i>"
370+ htmlStr += token [3 :]
371+ htmlStr += "</i>"
339372 } else if strings .HasPrefix (token , "[4m" ) {
340- html += "<ins>"
341- html += token [3 :]
342- html += "</ins>"
373+ htmlStr += "<ins>"
374+ htmlStr += token [3 :]
375+ htmlStr += "</ins>"
343376 } else if strings .HasPrefix (token , "[5m" ) {
344- html += "<blink>"
345- html += token [3 :]
346- html += "</blink>"
377+ htmlStr += "<blink>"
378+ htmlStr += token [3 :]
379+ htmlStr += "</blink>"
347380 } else if strings .HasPrefix (token , "[7m" ) ||
348381 strings .HasPrefix (token , "[8m" ) {
349- html += token [3 :]
382+ htmlStr += token [3 :]
350383 } else if strings .HasPrefix (token , "[9m" ) {
351- html += "<strike>"
352- html += token [3 :]
353- html += "</strike>"
384+ htmlStr += "<strike>"
385+ htmlStr += token [3 :]
386+ htmlStr += "</strike>"
354387 } else if strings .HasPrefix (token , "[22m" ) ||
355388 strings .HasPrefix (token , "[23m" ) ||
356389 strings .HasPrefix (token , "[24m" ) ||
357390 strings .HasPrefix (token , "[25m" ) ||
358391 strings .HasPrefix (token , "[28m" ) ||
359392 strings .HasPrefix (token , "[27m" ) ||
360393 strings .HasPrefix (token , "[29m" ) {
361- html += token [4 :]
394+ htmlStr += token [4 :]
362395 } else if token == "7" || token == "8" {
363- html += token [1 :]
396+ htmlStr += token [1 :]
364397 } else {
365398 var btok = []byte (token )
366399 var found = col256 .FindSubmatch (btok )
@@ -371,14 +404,14 @@ func toHtml(raw []byte) []byte {
371404 color , ok := color265 [string (found [2 ])]
372405 if ok {
373406 if string (found [1 ]) == "48" {
374- html += fmt .Sprintf ("<span style=\" background-color: %s\" >" , color )
407+ htmlStr += fmt .Sprintf ("<span style=\" background-color: %s\" >" , color )
375408 } else {
376- html += fmt .Sprintf ("<span style=\" color: %s\" >" , color )
409+ htmlStr += fmt .Sprintf ("<span style=\" color: %s\" >" , color )
377410 }
378411 }
379- html += token [len (found [0 ]):]
412+ htmlStr += token [len (found [0 ]):]
380413 if ok {
381- html += "</span>"
414+ htmlStr += "</span>"
382415 }
383416 continue
384417 }
@@ -395,21 +428,21 @@ func toHtml(raw []byte) []byte {
395428 g &= 0xFF
396429 b &= 0xFF
397430 if string (found [1 ]) == "48" {
398- html += fmt .Sprintf ("<span style=\" background-color: #%02x%02x%02x\" >" , r , g , b )
431+ htmlStr += fmt .Sprintf ("<span style=\" background-color: #%02x%02x%02x\" >" , r , g , b )
399432 } else {
400- html += fmt .Sprintf ("<span style=\" color: #%02x%02x%02x\" >" , r , g , b )
433+ htmlStr += fmt .Sprintf ("<span style=\" color: #%02x%02x%02x\" >" , r , g , b )
401434 }
402- html += token [len (found [0 ]):]
403- html += "</span>"
435+ htmlStr += token [len (found [0 ]):]
436+ htmlStr += "</span>"
404437 continue
405438 }
406439
407440 found = escape .FindSubmatch (btok )
408441 if len (found ) < 1 {
409- html += token
442+ htmlStr += token
410443 continue
411444 } else if string (found [len (found )- 1 ]) != "m" {
412- html += token [len (found [0 ]):]
445+ htmlStr += token [len (found [0 ]):]
413446 continue
414447 } else if len (token ) == len (found [0 ]) {
415448 continue
@@ -424,30 +457,30 @@ func toHtml(raw []byte) []byte {
424457 e = e [:len (e )- 1 ]
425458 }
426459 if e == "0" {
427- html += tags
460+ htmlStr += tags
428461 tags = ""
429462 continue
430463 } else if tag , ok := tagMap [e ]; ok {
431- html += fmt .Sprintf ("<%s>" , tag )
464+ htmlStr += fmt .Sprintf ("<%s>" , tag )
432465 tags += fmt .Sprintf ("</%s>" , tag )
433466 } else if len (e ) == 2 && (e [0 ] == '3' || e [0 ] == '4' || e [0 ] == '9' ) && e [1 ] != '8' {
434467 color , _ := colMap [string (e [1 ])]
435468 if e [0 ] == '4' {
436- html += fmt .Sprintf ("<span style=\" background-color: %s\" >" , color )
469+ htmlStr += fmt .Sprintf ("<span style=\" background-color: %s\" >" , color )
437470 } else {
438- html += fmt .Sprintf ("<span style=\" color: %s\" >" , color )
471+ htmlStr += fmt .Sprintf ("<span style=\" color: %s\" >" , color )
439472 }
440473 tags += "</span>"
441474 } else if len (e ) == 3 && e [0 ] == '1' && e [1 ] == '0' && e [2 ] != '8' {
442475 color , _ := colMap [string (e [1 ])]
443- html += fmt .Sprintf ("<span style=\" background-color: %s\" >" , color )
476+ htmlStr += fmt .Sprintf ("<span style=\" background-color: %s\" >" , color )
444477 tags += "</span>"
445478 }
446479 }
447- html += token [len (found [0 ]):]
448- html += tags
480+ htmlStr += token [len (found [0 ]):]
481+ htmlStr += tags
449482 }
450483 }
451- html = strings .ReplaceAll (html , "\n " , "<br>\n " )
452- return []byte (html )
484+ htmlStr = strings .ReplaceAll (htmlStr , "\n " , "<br>\n " )
485+ return []byte ("<pre>" + htmlStr + "</pre>" )
453486}
0 commit comments