Ansi2Html 0.1 → 0.9
raw patch · 3 files changed
+44/−19 lines, 3 files
Files
- Ansi2Html.cabal +1/−1
- Ansi2Html.hs +19/−17
- ansi2html.css +24/−1
Ansi2Html.cabal view
@@ -1,5 +1,5 @@ Name: Ansi2Html-Version: 0.1+Version: 0.9 Synopsis: Convert ANSI Terminal Sequences to nice HTML markup Description: Particularly with xterm in mind, this software enables integration of terminal screen state in html pages. See project homepage for hints on how to setup xterm for it. Homepage: http://janzzstimmpfle.de/~jens/software/Ansi2Html/
Ansi2Html.hs view
@@ -9,7 +9,7 @@ import Control.Monad import Control.Monad.State as S --- the (Integer,Integer) state holds an (couner,columns) state+-- the (Integer,Integer) state holds a (counter,columns) state type MyParser = GenParser Char (Integer,Integer) data ANSIEnv = AnsiEnv { ansiBrightOrBold :: Bool@@ -66,6 +66,7 @@ 35 -> \e -> e { ansiFGColor = AnsiMagenta } 36 -> \e -> e { ansiFGColor = AnsiCyan } 37 -> \e -> e { ansiFGColor = AnsiWhite }+ 39 -> \e -> e { ansiFGColor = AnsiWhite } 40 -> \e -> e { ansiBGColor = AnsiBlack } 41 -> \e -> e { ansiBGColor = AnsiRed } 42 -> \e -> e { ansiBGColor = AnsiGreen }@@ -74,6 +75,7 @@ 45 -> \e -> e { ansiBGColor = AnsiMagenta } 46 -> \e -> e { ansiBGColor = AnsiCyan } 47 -> \e -> e { ansiBGColor = AnsiWhite }+ 49 -> \e -> e { ansiBGColor = AnsiBlack } 51 -> \e -> e { ansiFramed = True } 52 -> \e -> e { ansiEncircled = True } 53 -> \e -> e { ansiOverlined = True }@@ -95,23 +97,20 @@ deriving Eq envAttrs :: ANSIEnv -> [HtmlAttr]-envAttrs e =- let r = ansiReverse env- env = reverseEnv r e+envAttrs env =+ let bob = when' (ansiBrightOrBold env) ("bold") italic = when' (ansiItalic env) ("italic") underline = when' (ansiUnderline env) ("underline") fontnum = when' (ansiFontNumber env /= 0) ("fontNum" ++ (show $ ansiFontNumber env)) framed = when' (ansiFramed env) ("framed")- fgcolor = when' (ansiFGColor env /= AnsiBlack) ("fg" ++ (show $ ansiFGColor env))- bgcolor = when' (ansiBGColor env /= AnsiBlack) ("bg" ++ (show $ ansiBGColor env))+ fgcolor = when' (True) (reversed "bg" "fg" ++ (show $ ansiFGColor env))+ bgcolor = when' (True) (reversed "fg" "bg" ++ (show $ ansiBGColor env)) classes = map ("ansi2html-"++) $ catMaybes [bob, italic, underline, fontnum, framed, fgcolor, bgcolor]- joined = foldl (\a b -> if null a then b else a ++ " " ++ b) "" classes- in [theclass joined]+ in [theclass $ unwords classes] where+ reversed a b = if ansiReverse env then a else b when' x y = if x then Just y else Nothing- -- TODO- reverseEnv = flip const instance Show ANSIColor where show color = case color of@@ -162,14 +161,12 @@ updateState (mapFst (const 0)) return "\n" else return ""- rr <- if (r == '\n')+ if (r == '\n') then do updateState (mapFst (const 0))- return $ replicate (fromInteger (columns - counter)) ' ' ++ "\n" else do updateState (mapFst (+1))- return [r]- return $ implicitbreak ++ rr+ return $ implicitbreak ++ [r] where mapFst f (a,b) = (f a,b) ctlSeq :: MyParser ANSIToken@@ -212,10 +209,11 @@ ansi2Html :: [ANSIToken] -> X.Html ansi2Html toks = foldl (+++) noHtml htmls- where htmls = evalState (mapM ansiToken2Html toks) defaultEnv+ where+ htmls = evalState (mapM ansiToken2Html toks) defaultEnv makeHtml :: X.Html -> X.Html-makeHtml content = X.thespan ! [theclass "ansi2html"] << content+makeHtml content = X.thediv ! [theclass "ansi2html"] << content printUsage :: IO () printUsage = do@@ -234,4 +232,8 @@ Left err -> do error $ "parse error at " ++ show err Right x -> return x- print . makeHtml $ ansi2Html tokens+ print . makeHtml $ ansi2Html tokens +++ dummyLine columns+ where+ -- force width of terminal. Is there a better solution?+ dummyLine columns = X.thespan ! [X.theclass "ansi2html-dummyline"] <<+ (X.lineToHtml $ replicate (fromInteger columns) ' ')
ansi2html.css view
@@ -1,6 +1,29 @@-span.ansi2html {+div.ansi2html { font-family: monospace;+ color: white; background-color: black;+ display: inline-block;+}++/* Without line-height: 0, the dummy line would be still be invisible, but it+ * would nevertheless blow up the surrounding div vertically (we want the+ * effect only horizontally) */+span.ansi2html-dummyline {+ position: relative;+ left: -99999px;+ line-height: 0;+}++.ansi2html-underline {+ text-decoration: underline;+}++.ansi2html-bold {+ font-weight: bold;+}++.ansi2html-italic {+ font-style: italic; } /* foreground colors */