module AnsiiToHtml where
import FilePath (replaceExtension)
import List (isPrefixOf)
import HTML.Parser
import CPC.Files (inCpcSubdir)
defaultStart:: String
defaultStart = "
"
defaultEnd::String
defaultEnd = " "
convertToHTML:: (String,String) -> IO ()
convertToHTML (ansi,name) =
writeFile (inCpcSubdir $ replaceExtension name "html")
(convertToHTML' ansi defaultStart)
convertToHTML':: String -> String -> String
convertToHTML' ansi@(x:xs) html
| isPrefixOf "\ESC" ansi = handleColor xs html
| isPrefixOf "\n" ansi = convertToHTML' xs (html ++ "
")
| isPrefixOf "\t" ansi = convertToHTML' xs (html ++ "  ")
| isPrefixOf " " ansi = convertToHTML' xs (html ++ " ")
| otherwise = convertToHTML' xs (html ++ [x])
convertToHTML' [] html = html ++ defaultEnd
handleColor:: String -> String -> String
handleColor ansi@(a:b:c:d:rest) html
| isPrefixOf "[41m" ansi = convertToHTML' rest
(html ++ "")
| isPrefixOf "[42m" ansi = convertToHTML' rest
(html ++ "")
| isPrefixOf "[43m" ansi = convertToHTML' rest
(html ++ "")
| isPrefixOf "[49m" ansi = convertToHTML' rest
(html ++ "")
| otherwise = error (a:b:c:d:rest)
handleColor [] _ = error "ansii code error"
handleColor [_] _ = error "ansii code error"
handleColor [_, _] _ = error "ansii code error"
handleColor [_, _, _] _ = error "ansii code error"