File: Highlight.hs

package info (click to toggle)
haskell-tagstream-conduit 0.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 96 kB
  • sloc: haskell: 436; makefile: 3
file content (29 lines) | stat: -rw-r--r-- 949 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import Data.Maybe
import System.IO
import System.Environment
import Text.HTML.TagStream
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as S
import System.Console.ANSI
import qualified Data.Conduit as C
import qualified Data.Conduit.Binary as CB
import qualified Data.Conduit.List as CL
import Blaze.ByteString.Builder (Builder)
import Data.Conduit.Blaze (builderToByteString)

color :: Color -> ByteString -> ByteString
color c s = S.concat [ S.pack $ setSGRCode [SetColor Foreground Dull c]
                     , s
                     , S.pack $ setSGRCode [SetColor Foreground Dull White]
                     ]

main :: IO ()
main = do
    args <- getArgs
    filename <- maybe (fail "Highlight file") return (listToMaybe args)
    C.runResourceT $
        CB.sourceFile filename
        C.$= tokenStream
        C.$= CL.map (showToken (color Red))
        C.$= builderToByteString
        C.$$ CB.sinkHandle stdout