hscolour 1.6 → 1.9
raw patch · 20 files changed
+855/−140 lines, 20 filesnew-uploader
Files
- .hscolour +2/−0
- HsColour.hs +92/−26
- Language/Haskell/HsColour.hs +60/−10
- Language/Haskell/HsColour/Anchors.hs +6/−3
- Language/Haskell/HsColour/CSS.hs +26/−19
- Language/Haskell/HsColour/Classify.hs +31/−9
- Language/Haskell/HsColour/Colourise.hs +20/−7
- Language/Haskell/HsColour/General.hs +15/−0
- Language/Haskell/HsColour/HTML.hs +23/−7
- Language/Haskell/HsColour/LaTeX.hs +16/−18
- Language/Haskell/HsColour/MIRC.hs +68/−0
- Makefile +28/−8
- README +23/−8
- docs/hscolour/Language-Haskell-HsColour-LaTeX.html +247/−0
- docs/hscolour/Language/Haskell/HsColour/LaTeX.html +112/−0
- docs/hscolour/doc-index-H.html +21/−1
- docs/hscolour/index.html +10/−0
- hscolour.cabal +7/−4
- hscolour.css +1/−0
- index.html +47/−20
.hscolour view
@@ -10,6 +10,8 @@ , string = [Foreground Magenta] , char = [Foreground Magenta] , number = [Foreground Magenta]+ , cpp = [Dim,Foreground Magenta] , selection = [Bold, Foreground Magenta] , variantselection = [Dim, Foreground Red, Underscore]+ , definition = [Normal] }
HsColour.hs view
@@ -5,40 +5,106 @@ import System import IO (hPutStrLn,hFlush,stdout,stderr,hSetBuffering,BufferMode(..))+import Monad (when)+import List (intersperse) -version = "1.6"+version = "1.9" +-- | Command-line options+data Option =+ Help -- ^ print usage message+ | Version -- ^ report version+ | Information -- ^ report auxiliary information, e.g. CSS defaults+ | Format Output -- ^ what type of output to produce+ | LHS Bool -- ^ literate input (i.e. multiple embedded fragments)+ | Anchors Bool -- ^ whether to add anchors+ | Partial Bool -- ^ whether to produce a full document or partial+ | Input FilePath -- ^ input source file+ | Output FilePath -- ^ output source file+ deriving Eq++optionTable :: [(String,Option)]+optionTable = [ ("help", Help)+ , ("version", Version)+ , ("print-css", Information)+ , ("html", Format HTML)+ , ("css", Format CSS)+ , ("tty", Format TTY)+ , ("latex", Format LaTeX)+ , ("mirc", Format MIRC)+ , ("lit", LHS True)+ , ("nolit", LHS False)+ , ("anchor", Anchors True)+ , ("noanchor", Anchors False)+ , ("partial", Partial True)+ , ("nopartial", Partial False)+ ]++parseOption :: String -> Either String Option+parseOption ('-':'o':s) = Right (Output s)+parseOption s@('-':_) = maybe (Left s) Right+ (lookup (dropWhile (== '-') s) optionTable)+parseOption s = Right (Input s)+ main :: IO () main = do prog <- System.getProgName args <- System.getArgs pref <- readColourPrefs- (ioWrapper,output,anchors) <- case args of- [] -> errorOut (help prog)- ["-h"] -> errorOut (help prog)- ["-help"] -> errorOut (help prog)- ["--help"] -> errorOut (help prog)- ["-v"] -> errorOut (prog++" "++version)- ["-version"] -> errorOut (prog++" "++version)- ["--version"] -> errorOut (prog++" "++version)- ["-tty"] -> return (ttyInteract, TTY, False)- ["-html"] -> return (ttyInteract, HTML, False)- ["-css"] -> return (ttyInteract, CSS, False)- ["-latex"] -> return (ttyInteract, LaTeX,False)- ["-anchorHTML"] -> return (ttyInteract, HTML, True)- ["-anchorCSS"] -> return (ttyInteract, CSS, True)- [a] -> return (fileInteract a, TTY, False)- ["-tty",a] -> return (fileInteract a, TTY, False)- ["-html",a] -> return (fileInteract a, HTML, False)- ["-css",a] -> return (fileInteract a, CSS, False)- ["-latex",a] -> return (fileInteract a, LaTeX,False)- ["-anchorHTML",a] -> return (fileInteract a, HTML, True)- ["-anchorCSS",a] -> return (fileInteract a, CSS, True)- _ -> errorOut (help prog)- ioWrapper (hscolour output pref anchors)+ let options = map parseOption args+ bad = [ o | Left o <- options ]+ good = [ o | Right o <- options ]+ formats = [ f | Format f <- good ]+ outFile = [ f | Output f <- good ]+ fileInteract = fileInteractOut outFile+ output = useDefault TTY id formats+ ioWrapper = useDefault ttyInteract fileInteract [ f | Input f <- good ]+ anchors = useDefault False id [ b | Anchors b <- good ]+ partial = useDefault False id [ b | Partial b <- good ]+ lhs = useDefault False id [ b | LHS b <- good ] + when (not (null bad))+ (errorOut ("Unrecognised option(s): "++unwords bad++"\n"++usage prog))+ when (Help `elem` good) (do putStrLn (usage prog); exitSuccess)+ when (Version `elem` good) (do putStrLn (prog++" "++version); exitSuccess)+ when (Information `elem` good)+ (do writeResult outFile cssDefaults; exitSuccess)+ when (length formats > 1)+ (errorOut ("Can only choose one output format at a time: "+ ++unwords (map show formats)))+ when (length outFile > 1)+ (errorOut ("Can only have one output file at a time."))+ ioWrapper (hscolour output pref anchors partial lhs) hFlush stdout+ where- fileInteract f u = do readFile f >>= putStr . u+ writeResult outF = if null outF then putStr else writeFile (last outF)+ fileInteractOut outF inF u = do readFile inF >>= writeResult outF . u ttyInteract s = do hSetBuffering stdout NoBuffering >> Prelude.interact s+ exitSuccess = exitWith ExitSuccess errorOut s = hPutStrLn stderr s >> hFlush stderr >> exitFailure- help p = "Usage: "++p++" [-tty|-html|-css|-latex|-anchorHTML|-anchorCSS] [file.hs]"+ usage prog = "Usage: "++prog+ ++" options [file.hs]\n where\n options = [ "+ ++ (indent 15 . unwords . width 58 58 . intersperse "|"+ . ("-oOUTPUT":)+ . map (('-':) . fst)) optionTable ++ " ]"+ useDefault d f list | null list = d+ | otherwise = f (head list)++-- some simple text formatting for usage messages+width n left [] = []+width n left (s:ss) = if size > left then "\n":s : width n n ss+ else s : width n (left-size-1) ss+ where size = length s++indent n [] = []+indent n ('\n':s) = '\n':replicate n ' '++indent n s+indent n (c:s) = c: indent n s++-- Rather than have a separate .css file, define some reasonable defaults here.+cssDefaults = "\+\.keyglyph, .layout {color: red;}\n\+\.keyword {color: blue;}\n\+\.comment, .comment a {color: green;}\n\+\.str, .chr {color: teal;}\n\+\.keyword,.conid, .varid, .conop, .varop, .num, .cpp, .sel, .definition {}\n\+\"
Language/Haskell/HsColour.hs view
@@ -1,5 +1,5 @@ -- | This is a library with colourises Haskell code. --- It currently has three output formats: +-- It currently has four output formats: -- -- * ANSI terminal codes --@@ -9,28 +9,78 @@ -- -- * HTML with CSS. --+-- * mIRC chat client colour codes.+-- module Language.Haskell.HsColour (Output(..), ColourPrefs(..), hscolour) where import Language.Haskell.HsColour.Colourise (ColourPrefs(..))-import qualified Language.Haskell.HsColour.TTY as TTY-import qualified Language.Haskell.HsColour.HTML as HTML-import qualified Language.Haskell.HsColour.CSS as CSS-import qualified Language.Haskell.HsColour.LaTeX as LaTeX+import qualified Language.Haskell.HsColour.TTY as TTY+import qualified Language.Haskell.HsColour.HTML as HTML+import qualified Language.Haskell.HsColour.CSS as CSS+import qualified Language.Haskell.HsColour.LaTeX as LaTeX+import qualified Language.Haskell.HsColour.MIRC as MIRC -- | The supported output formats. data Output = TTY -- ^ ANSI terminal codes | LaTeX -- ^ TeX macros | HTML -- ^ HTML with font tags | CSS -- ^ HTML with CSS.+ | MIRC -- ^ mIRC chat clients+ deriving (Eq,Show) --- | Colourise Haskell source code with the give output format.+-- | Colourise Haskell source code with the given output format. hscolour :: Output -- ^ Output format. -> ColourPrefs -- ^ Colour preferences for formats that support it. -> Bool -- ^ Whether to include anchors.+ -> Bool -- ^ Whether output document is partial or complete.+ -> Bool -- ^ Whether input document is literate haskell or not -> String -- ^ Haskell source code. -> String -- ^ Coloured Haskell source code.-hscolour TTY pref _ = TTY.hscolour pref-hscolour LaTeX pref _ = LaTeX.hscolour pref-hscolour HTML pref anchor = HTML.hscolour pref anchor-hscolour CSS _ anchor = CSS.hscolour anchor+hscolour output pref anchor partial False = + hscolour' output pref anchor partial+hscolour output pref anchor partial True = + concatMap chunk . joinL . map lhsClassify . inlines+ where+ chunk (Literate c) = c+ chunk (Code c) = hscolour' output pref anchor True c++hscolour' :: Output -- ^ Output format.+ -> ColourPrefs -- ^ Colour preferences for formats that support it.+ -> Bool -- ^ Whether to include anchors.+ -> Bool -- ^ Whether output document is partial or complete.+ -> String -- ^ Haskell source code.+ -> String -- ^ Coloured Haskell source code.+hscolour' TTY pref _ _ = TTY.hscolour pref+hscolour' MIRC pref _ _ = MIRC.hscolour pref+hscolour' LaTeX pref _ partial = LaTeX.hscolour pref partial+hscolour' HTML pref anchor partial = HTML.hscolour pref anchor partial+hscolour' CSS _ anchor partial = CSS.hscolour anchor partial++-- | Separating literate files into code\/comment chunks.+data Literate = Code {unL :: String} | Literate {unL :: String}++-- Re-implementation of 'lines', for better efficiency (but decreased laziness).+-- Also, importantly, accepts non-standard DOS and Mac line ending characters.+-- And retains the trailing '\n' character in each resultant string.+inlines :: String -> [String]+inlines s = lines' s id+ where+ lines' [] acc = [acc []]+ lines' ('\^M':'\n':s) acc = acc ['\n'] : lines' s id -- DOS+--lines' ('\^M':s) acc = acc ['\n'] : lines' s id -- MacOS+ lines' ('\n':s) acc = acc ['\n'] : lines' s id -- Unix+ lines' (c:s) acc = lines' s (acc . (c:))++-- Note, I just pass the > symbol to the colouriser, which assumes that+-- it's token based and not parse-based!!!+lhsClassify :: String -> Literate+lhsClassify ('>':xs) = Code ('>':xs)+lhsClassify xs = Literate xs++joinL :: [Literate] -> [Literate]+joinL [] = []+joinL (Code c:Code c2:xs) = joinL (Code (c++c2):xs)+joinL (Literate c:Literate c2:xs) = joinL (Literate (c++c2):xs)+joinL (any:xs) = any: joinL xs+
Language/Haskell/HsColour/Anchors.hs view
@@ -3,6 +3,7 @@ ) where import Language.Haskell.HsColour.Classify+import Language.Haskell.HsColour.General import List -- This is an attempt to find the first defining occurrence of an@@ -40,7 +41,7 @@ -- Given that we are at the beginning of a line, determine whether there -- is an identifier defined here, and if so, return it. -- precondition: have just seen a newline token.-identifier st t@((Varid,v):stream) =+identifier st t@((kind,v):stream) | kind`elem`[Varid,Definition] = case skip stream of ((Varop,v):_) | not (v`inST`st) -> Just (fix v) notVarop -- | typesig stream -> Nothing -- not a defn@@ -57,6 +58,7 @@ _ -> Nothing identifier st t@((Keyword,"foreign"):stream) = Nothing -- not yet implemented identifier st t@((Keyword,"data"):stream) = getConid stream+identifier st t@((Keyword,"newtype"):stream) = getConid stream identifier st t@((Keyword,"type"):stream) = getConid stream identifier st t@((Keyword,"class"):stream) = getConid stream identifier st t@((Comment,_):(Space,"\n"):stream) = identifier st stream@@ -81,7 +83,7 @@ munch _ [] = [] -- source is ill-formed -- ensure anchor name is correct for a Varop-fix ('`':v) = init v+fix ('`':v) = dropLast '`' v fix v = v -- look past whitespace and comments to next "real" token@@ -107,7 +109,7 @@ v -> debug v ("(...) =>") v -> debug v ("(...) no =>") v -> debug v ("no Conid or (...)")- where debug (s:t) c = Nothing+ where debug _ _ = Nothing -- debug (s:t) c = error ("HsColour: getConid failed: "++show s -- ++"\n in the context of: "++c) @@ -115,6 +117,7 @@ context stream@((Keyglyph,"="):_) = stream context stream@((Keyglyph,"=>"):_) = stream context (_:stream) = context stream+context [] = [] -- simple implementation of a string lookup table. -- replace this with something more sophisticated if needed.
Language/Haskell/HsColour/CSS.hs view
@@ -1,27 +1,27 @@ -- | Formats Haskell source code as HTML with CSS.-module Language.Haskell.HsColour.CSS (hscolour, hscolourFragment) where+module Language.Haskell.HsColour.CSS (hscolour) where import Language.Haskell.HsColour.Anchors import Language.Haskell.HsColour.Classify as Classify-import Language.Haskell.HsColour.HTML (renderAnchors, renderComment, escape)+import Language.Haskell.HsColour.HTML (renderAnchors, renderComment,+ renderNewLinesAnchors, escape) -- | Formats Haskell source code as a complete HTML document with CSS.-hscolour :: Bool -- ^ Whether to include anchors+hscolour :: Bool -- ^ Whether to include anchors.+ -> Bool -- ^ Whether output should be partial+ -- (= no stylesheet link will be included.) -> String -- ^ Haskell source code. -> String -- ^ An HTML document containing the coloured -- Haskell source code.-hscolour anchor = top'n'tail . hscolourFragment anchor---- | Formats Haskell source code as an HTML fragment with CSS.--- No stylesheet link is included in the output.-hscolourFragment :: Bool -- ^ Whether to include anchors- -> String -- ^ Haskell source code.- -> String -- ^ An HTML fragment containing the coloured - -- Haskell source code.-hscolourFragment anchor = - pre . (if anchor - then concatMap (renderAnchors renderToken) . insertAnchors- else concatMap renderToken) . tokenise+hscolour anchor partial =+ (if partial then id else top'n'tail)+ . pre+ . (if anchor + then renderNewLinesAnchors+ . concatMap (renderAnchors renderToken)+ . insertAnchors+ else concatMap renderToken)+ . tokenise top'n'tail :: String -> String top'n'tail = (cssPrefix++) . (++cssSuffix)@@ -30,11 +30,14 @@ pre = ("<pre>"++) . (++"</pre>") renderToken :: (TokenType,String) -> String-renderToken (Space,text) = text-renderToken (cls,text) = "<span class='" ++ cssClass cls ++ "'>" ++- (if cls == Comment then renderComment text else escape text) ++- "</span>"+renderToken (cls,text) =+ before ++ (if cls == Comment then renderComment text else escape text) ++ after+ where+ before = if null cls2 then "" else "<span class='" ++ cls2 ++ "'>"+ after = if null cls2 then "" else "</span>"+ cls2 = cssClass cls + cssClass Keyword = "keyword" cssClass Keyglyph = "keyglyph" cssClass Layout = "layout"@@ -46,7 +49,11 @@ cssClass String = "str" cssClass Char = "chr" cssClass Number = "num"+cssClass Cpp = "cpp" cssClass Error = "sel"+cssClass Definition = "definition"+cssClass _ = ""+ cssPrefix = unlines ["<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
Language/Haskell/HsColour/Classify.hs view
@@ -9,7 +9,10 @@ -- Lex Haskell source code into an annotated token stream, without -- discarding any characters or layout. tokenise :: String -> [(TokenType,String)]-tokenise = map (\s-> (classify s,s)) . glue . chunk+tokenise str = + let chunks = glue . chunk $ str + newline = True : (map ("\n" `isPrefixOf`) chunks)+ in map (\(s,n)-> (classify s n,s)) (zip chunks newline) -- Basic Haskell lexing, except we keep whitespace. chunk :: String -> [String]@@ -27,7 +30,7 @@ where (com,s') = eolcomment rest ((tok,rest):_) -> tok: chunk rest -isLinearSpace c = c `elem` " \t" -- " \t\xa0"+isLinearSpace c = c `elem` " \t\f" -- " \t\xa0" -- Glue sequences of tokens into more useful blobs --glue (q:".":n:rest) | Char.isUpper (head q) -- qualified names@@ -35,12 +38,19 @@ glue ("`":rest) = -- `varid` -> varop case glue rest of (qn:"`":rest) -> ("`"++qn++"`"): glue rest- _ -> ("`": rest)+ _ -> "`": glue rest glue (s:ss) | all (=='-') s && length s >=2 -- eol comment = (s++concat c): glue rest where (c,rest) = break ('\n'`elem`) ss --glue ("{":"-":ss) = ("{-"++c): glue rest -- nested comment -- where (c,rest) = nestcomment 0 ss+glue ("(":ss) = case rest of+ ")":rest -> ("(" ++ concat tuple ++ ")") : glue rest+ _ -> "(" : glue ss+ where (tuple,rest) = span (==",") ss+glue ("[":"]":ss) = "[]" : glue ss+glue ("\n":"#":ss)= "\n" : ('#':concat line) : glue rest+ where (line,rest) = break ('\n'`elem`) ss glue (s:ss) = s: glue ss glue [] = [] @@ -57,17 +67,19 @@ eolcomment :: String -> (String,String) eolcomment s@('\n':_) = ([], s)+eolcomment ('\r':s) = eolcomment s eolcomment (c:s) = (c:cs, s') where (cs,s') = eolcomment s eolcomment [] = ([],[]) -- Classify tokens data TokenType = Space | Keyword | Keyglyph | Layout | Comment | Conid | Varid |- Conop | Varop | String | Char | Number | Error+ Conop | Varop | String | Char | Number | Cpp | Error |+ Definition deriving (Eq,Show) -classify :: String -> TokenType-classify s@(h:_)+classify :: String -> Bool -> TokenType+classify s@(h:t) newline | isSpace h = Space | all (=='-') s = Comment | "--" `isPrefixOf` s@@ -77,7 +89,10 @@ | s `elem` keyglyphs = Keyglyph | s `elem` layoutchars = Layout | isUpper h = Conid- | isLower h = Varid+ | s == "[]" = Conid+ | h == '(' && isTupleTail t = Conid+ | h == '#' = Cpp+ | isLower h = if newline then Definition else Varid | h `elem` symbols = Varop | h==':' = Conop | h=='`' = Varop@@ -85,12 +100,19 @@ | h=='\'' = Char | isDigit h = Number | otherwise = Error+classify _ _ = Space +isTupleTail [')'] = True+isTupleTail (',':xs) = isTupleTail xs+isTupleTail _ = False++ -- Haskell keywords keywords =- ["case","class","data","default","deriving","do","else"+ ["case","class","data","default","deriving","do","else","forall" ,"if","import","in","infix","infixl","infixr","instance","let","module"- ,"newtype","of","then","type","where","_","foreign","ccall","as"]+ ,"newtype","of","qualified","then","type","where","_"+ ,"foreign","ccall","as","safe","unsafe"] keyglyphs = ["..","::","=","\\","|","<-","->","@","~","=>","[","]"] layoutchars =
Language/Haskell/HsColour/Colourise.hs view
@@ -8,16 +8,16 @@ import Language.Haskell.HsColour.ColourHighlight import Language.Haskell.HsColour.Classify (TokenType(..)) +import IO (hPutStrLn,stderr) import System (getEnv)-import Char import List -- | Colour preferences. data ColourPrefs = ColourPrefs { keyword, keyglyph, layout, comment , conid, varid, conop, varop- , string, char, number- , selection, variantselection :: [Highlight]+ , string, char, number, cpp+ , selection, variantselection, definition :: [Highlight] } deriving (Eq,Show,Read) defaultColourPrefs = ColourPrefs@@ -32,19 +32,30 @@ , string = [Foreground Magenta] , char = [Foreground Magenta] , number = [Foreground Magenta]+ , cpp = [Foreground Magenta,Dim] , selection = [Bold, Foreground Magenta] , variantselection = [Dim, Foreground Red, Underscore]+ , definition = [Foreground Blue] } +-- NOTE, should we give a warning message on a failed reading?+parseColourPrefs :: String -> String -> IO ColourPrefs+parseColourPrefs file x =+ case reads x of+ (res,_):_ -> return res+ _ -> do hPutStrLn stderr ("Could not parse colour prefs from "++file+ ++": reverting to defaults")+ return defaultColourPrefs+ readColourPrefs :: IO ColourPrefs readColourPrefs = catch (do val <- readFile ".hscolour"- return (read val))- (\e-> catch+ parseColourPrefs ".hscolour" val)+ (\_-> catch (do home <- getEnv "HOME" val <- readFile (home++"/.hscolour")- return (read val))- (\e-> return defaultColourPrefs))+ parseColourPrefs (home++"/.hscolour") val)+ (\_-> return defaultColourPrefs)) -- Convert classification to colour highlights. colourise :: ColourPrefs -> TokenType -> [Highlight]@@ -60,5 +71,7 @@ colourise pref String = string pref colourise pref Char = char pref colourise pref Number = number pref+colourise pref Cpp = cpp pref colourise pref Error = selection pref+colourise pref Definition = definition pref
+ Language/Haskell/HsColour/General.hs view
@@ -0,0 +1,15 @@++module Language.Haskell.HsColour.General(+ dropLast, dropFirst+ ) where+++dropLast :: Eq a => a -> [a] -> [a]+dropLast x [y] | x == y = []+dropLast x (y:ys) = y : dropLast x ys+dropLast x [] = []+++dropFirst :: Eq a => a -> [a] -> [a]+dropFirst x (y:ys) | x == y = ys+dropFirst x ys = ys
Language/Haskell/HsColour/HTML.hs view
@@ -2,7 +2,7 @@ module Language.Haskell.HsColour.HTML (hscolour, -- * Internals- renderAnchors, renderComment, escape) where+ renderAnchors, renderComment, renderNewLinesAnchors, escape) where import Language.Haskell.HsColour.Anchors import Language.Haskell.HsColour.Classify as Classify@@ -13,17 +13,25 @@ -- | Formats Haskell source code using HTML with font tags. hscolour :: ColourPrefs -- ^ Colour preferences.- -> Bool -- ^ Whether to include anchors+ -> Bool -- ^ Whether to include anchors.+ -> Bool -- ^ Whether output should be partial. -> String -- ^ Haskell source code. -> String -- ^ Coloured Haskell source code.-hscolour pref anchor = - top'n'tail . (if anchor then concatMap (renderAnchors (renderToken pref)) - . insertAnchors- else concatMap (renderToken pref)) . tokenise+hscolour pref anchor partial = + (if partial then id else top'n'tail)+ . pre+ . (if anchor then renderNewLinesAnchors+ . concatMap (renderAnchors (renderToken pref))+ . insertAnchors+ else concatMap (renderToken pref))+ . tokenise top'n'tail :: String -> String-top'n'tail = ("<pre>"++) . (++"</pre>")+top'n'tail = (htmlHeader++) . (++htmlClose) +pre :: String -> String+pre = ("<pre>"++) . (++"</pre>")+ renderToken :: ColourPrefs -> (TokenType,String) -> String renderToken pref (t,s) = fontify (colourise pref t) (if t == Comment then renderComment s else escape s)@@ -47,6 +55,10 @@ renderComment (x:xs) = escape [x] ++ renderComment xs renderComment [] = [] +renderNewLinesAnchors :: String -> String+renderNewLinesAnchors = unlines . map render . zip [1..] . lines+ where render (line, s) = "<a name=\"line-" ++ show line ++ "\"></a>" ++ s+ -- Html stuff fontify [] s = s fontify (h:hs) s = font h (fontify hs s)@@ -66,3 +78,7 @@ escape ('&':cs) = "&"++escape cs escape (c:cs) = c: escape cs escape [] = []++htmlHeader = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">"+ ++"\n<html>\n"+htmlClose = "\n</html>"
Language/Haskell/HsColour/LaTeX.hs view
@@ -1,22 +1,20 @@ -- | Formats Haskell source code using LaTeX macros.-module Language.Haskell.HsColour.LaTeX (hscolour, hscolourFragment) where+module Language.Haskell.HsColour.LaTeX (hscolour) where import Language.Haskell.HsColour.Classify as Classify import Language.Haskell.HsColour.Colourise+import Language.Haskell.HsColour.General -- | Formats Haskell source code as a complete LaTeX document. hscolour :: ColourPrefs -- ^ Colour preferences.+ -> Bool -- ^ Whether output should be partial (= no prologue). -> String -- ^ Haskell source code.- -> String -- ^ An LaTeX document containing the coloured + -> String -- ^ A LaTeX document\/fragment containing the coloured -- Haskell source code.-hscolour pref = top'n'tail . hscolourFragment pref---- | Formats Haskell source code as an LaTeX fragment.-hscolourFragment :: ColourPrefs -- ^ Colour preferences.- -> String -- ^ Haskell source code.- -> String -- ^ An LaTeX fragment containing the coloured - -- Haskell source code.-hscolourFragment pref = concatMap (renderToken pref) . tokenise+hscolour pref partial =+ ( if partial then id else top'n'tail)+ . concatMap (renderToken pref)+ . tokenise top'n'tail :: String -> String top'n'tail = (latexPrefix++) . (++latexSuffix)@@ -25,13 +23,13 @@ -- TODO: filter dangerous characters like "{}_$" renderToken :: ColourPrefs -> (TokenType,String) -> String renderToken pref (Space,text) = filterSpace text-renderToken pref (cls,text) = let - symb = case cls of- String -> "``" ++ (reverse . tail . reverse . tail $ text) ++ "''"- _ -> text- style = colourise pref cls- (pre, post) = unzip $ map latexHighlight style in- (concat pre) ++ filterSpecial symb ++ (concat post) +renderToken pref (cls,text) =+ let symb = case cls of+ String -> "``" ++ (dropFirst '\"' $ dropLast '\"' $ text) ++ "''"+ _ -> text+ style = colourise pref cls+ (pre, post) = unzip $ map latexHighlight style+ in concat pre ++ filterSpecial symb ++ concat post -- | Filter white space characters. filterSpace :: String@@ -52,7 +50,7 @@ filterSpecial ('&':cs) = '\\':'&':(filterSpecial cs) filterSpecial ('~':cs) = "\\tilde{ }"++(filterSpecial cs) filterSpecial ('_':cs) = '\\':'_':(filterSpecial cs)-filterSpecial ('^':cs) = "\\hat{ }"++(filterSpecial cs)+filterSpecial ('^':cs) = "\\ensuremath{\\hat{ }}"++(filterSpecial cs) filterSpecial ('\\':cs) = "$\\backslash$"++(filterSpecial cs) filterSpecial ('{':cs) = '\\':'{':(filterSpecial cs) filterSpecial ('}':cs) = '\\':'}':(filterSpecial cs)
+ Language/Haskell/HsColour/MIRC.hs view
@@ -0,0 +1,68 @@+-- | Formats Haskell source code using mIRC codes.+-- (see http://irssi.org/documentation/formats)+module Language.Haskell.HsColour.MIRC (hscolour) where++import Language.Haskell.HsColour.Classify as Classify+import Language.Haskell.HsColour.Colourise++import Data.Char(isAlphaNum)+++-- | Formats Haskell source code using mIRC codes.+hscolour :: ColourPrefs -- ^ Colour preferences.+ -> String -- ^ Haskell source code.+ -> String -- ^ Coloured Haskell source code.+hscolour pref = concatMap (renderToken pref) . tokenise++renderToken :: ColourPrefs -> (TokenType,String) -> String+renderToken pref (t,s) = fontify (colourise pref t) s+++-- mIRC stuff+fontify hs =+ mircColours (joinColours hs)+ . highlight (filter (`elem`[Normal,Bold,Underscore,ReverseVideo]) hs)+ where+ highlight [] s = s+ highlight (h:hs) s = font h (highlight hs s)++ font Normal s = s+ font Bold s = '\^B':s++"\^B"+ font Underscore s = '\^_':s++"\^_"+ font ReverseVideo s = '\^V':s++"\^V"++-- mIRC combines colour codes in a non-modular way+data MircColour = Mirc { fg::Colour, dim::Bool, bg::Maybe Colour, blink::Bool}++joinColours :: [Highlight] -> MircColour+joinColours = foldr join (Mirc {fg=Black, dim=False, bg=Nothing, blink=False})+ where+ join Blink mirc = mirc {blink=True}+ join Dim mirc = mirc {dim=True}+ join (Foreground fg) mirc = mirc {fg=fg}+ join (Background bg) mirc = mirc {bg=Just bg}+ join Concealed mirc = mirc {fg=Black, bg=Just Black}+ join _ mirc = mirc++mircColours :: MircColour -> String -> String+mircColours (Mirc fg dim Nothing blink) s = '\^C': code fg dim++s++"\^O"+mircColours (Mirc fg dim (Just bg) blink) s = '\^C': code fg dim++','+ : code bg blink++s++"\^O"++code :: Colour -> Bool -> String+code Black False = "1"+code Red False = "5"+code Green False = "3"+code Yellow False = "7"+code Blue False = "2"+code Magenta False = "6"+code Cyan False = "10"+code White False = "0"+code Black True = "14"+code Red True = "4"+code Green True = "9"+code Yellow True = "8"+code Blue True = "12"+code Magenta True = "13"+code Cyan True = "11"+code White True = "15"
Makefile view
@@ -1,9 +1,10 @@ LIBRARY = hscolour-VERSION = 1.6+VERSION = 1.9 DIRS = Language/Haskell/HsColour SRCS = Language/Haskell/HsColour.hs \+ Language/Haskell/HsColour/General.hs \ Language/Haskell/HsColour/ANSI.hs \ Language/Haskell/HsColour/Anchors.hs \ Language/Haskell/HsColour/CSS.hs \@@ -12,16 +13,18 @@ Language/Haskell/HsColour/Colourise.hs \ Language/Haskell/HsColour/HTML.hs \ Language/Haskell/HsColour/LaTeX.hs \- Language/Haskell/HsColour/TTY.hs+ Language/Haskell/HsColour/TTY.hs \+ Language/Haskell/HsColour/MIRC.hs AUX = README LICENCE* $(LIBRARY).cabal Setup.hs Makefile \ HsColour.hs hscolour.css .hscolour \ index.html docs/* +GHC = ghc+ #all: $(LIBRARY) executable: $(SRCS) HsColour.hs- ghc --make HsColour- mv a.out $(LIBRARY)+ $(GHC) --make -o $(LIBRARY) HsColour package: tar cf tmp.tar $(SRCS) $(AUX) mkdir $(LIBRARY)-$(VERSION)@@ -33,7 +36,7 @@ mkdir -p docs/$(LIBRARY) for dir in $(DIRS); do mkdir -p docs/$(LIBRARY)/$$dir; done for file in $(SRCS); \- do HsColour -anchorHTML $$file \+ do ./$(LIBRARY) -html -anchor $$file \ >docs/$(LIBRARY)/`dirname $$file`/`basename $$file .hs`.html;\ done haddock --html --title=$(LIBRARY) --odir=docs/$(LIBRARY) \@@ -42,6 +45,23 @@ --source-entity="%{MODULE/.//}.html#%{NAME}" \ $(SRCS) -$(LIBRARY): $(SRCS)- $(HC) $(HFLAGS) $(HEAP) -o $@ $(SRCS)- $(STRIP) $@+#$(LIBRARY): $(SRCS)+# $(HC) $(HFLAGS) $(HEAP) -o $@ $(SRCS)+# $(STRIP) $@++docs: haddock++install:+ install -D $(LIBRARY) $(DESTDIR)/usr/bin/$(LIBRARY)+ install -D $(LIBRARY).css $(DESTDIR)/usr/share/doc/$(LIBRARY)/examples/$(LIBRARY).css++install-docs:+ install -D index.html $(DESTDIR)/usr/share/doc/$(LIBRARY)/index.html+ cp -a docs/$(LIBRARY)/Language/Haskell/* $(DESTDIR)/usr/share/doc/$(LIBRARY)/++clean:+ rm -f $(DIRS)/*.o $(DIRS)/*.hi+ rm -f Language/Haskell/HsColour.o Language/Haskell/HsColour.hi+ rm -f *.o *.hi+ rm -f $(LIBRARY)+ rm -rf docs
README view
@@ -1,6 +1,6 @@ HsColour: A Haskell source-code colouriser. --------------------------------------------Copyright: 2003-2006, Malcolm Wallace, University of York+Copyright: 2003-2007, Malcolm Wallace, University of York Licence: GPL Building:@@ -9,21 +9,36 @@ ghc --make HsColour Usage:- HsColour [ -tty | -html | -css | -anchorHTML | -anchorCSS ] [file.hs]+ HsColour [-Ofile]+ [ -tty | -html | -css | -latex | -mirc ]+ [ -lit | -anchor | -partial ]+ [ -nolit | -noanchor| -nopartial ]+ [file.hs] The program can colourise a Haskell source file for either terminal-output (option -tty) or HTML output with font tags (option -html), or-HTML output with CSS (option -css). The default is for terminal output,-which uses standard ANSI screen codes for the colours.+output (option -tty), or HTML 3.2 output with font tags (option -html),+or HTML 4.01 output with CSS (option -css), or LaTeX output (option+-latex), or IRC chat client (option -mirc). The default is for terminal+output, which uses standard ANSI screen codes for the colours. If no file argument is given, it reads standard input. Output is-always written to standard output.+written to the file specified by the -O option, or by default, stdout. -HsColour can add named anchors (with -anchorHTML or -anchorCSS) to+HsColour can add named anchors in HTML or CSS (option -anchor) to top-level definitions in the source file (functions, datatypes, classes). This enables you to make links to a specific location in the generated file with the standard "file.html#anchor" notation. +If you want to embed several individually colourised fragments into a+larger document, then use the <tt>-partial</tt> option , to omit the+HTML DOCTYPE header, CSS stylesheet link, or LaTeX prologue.++Alternatively, if you have a literate input source (e.g. .lhs file),+then the -lit option passes the literate parts untouched, and+colourises only the code fragments (indicated by Bird-tracks - a+> symbol in the left-most column), as if each was called individually+with -partial.+ You can configure the colours for different lexical entities by editing a configuration file called .hscolour in the current directory. (An example is included in the distribution.) The file format is as@@ -33,7 +48,7 @@ data ColourPrefs = ColourPrefs { keyword, keyglyph, layout, comment , conid, varid, conop, varop- , string, char, number+ , string, char, number, cpp , selection, variantselection :: [Highlight] }
+ docs/hscolour/Language-Haskell-HsColour-LaTeX.html view
@@ -0,0 +1,247 @@+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">+<!--Rendered using the Haskell Html Library v0.2-->+<HTML+><HEAD+><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"+><TITLE+>Language.Haskell.HsColour.LaTeX</TITLE+><LINK HREF="haddock.css" REL="stylesheet" TYPE="text/css"+><SCRIPT SRC="haddock.js" TYPE="text/javascript"+></SCRIPT+></HEAD+><BODY+><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"+><TR+><TD CLASS="topbar"+><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"+><TR+><TD+><IMG SRC="haskell_icon.gif" WIDTH="16" HEIGHT="16" ALT=" "+></TD+><TD CLASS="title"+>hscolour</TD+><TD CLASS="topbut"+><A HREF="Language/Haskell/HsColour/LaTeX.html"+>Source code</A+></TD+><TD CLASS="topbut"+><A HREF="index.html"+>Contents</A+></TD+><TD CLASS="topbut"+><A HREF="doc-index.html"+>Index</A+></TD+></TR+></TABLE+></TD+></TR+><TR+><TD CLASS="modulebar"+><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"+><TR+><TD+><FONT SIZE="6"+>Language.Haskell.HsColour.LaTeX</FONT+></TD+></TR+></TABLE+></TD+></TR+><TR+><TD CLASS="s15"+></TD+></TR+><TR+><TD+><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"+></TABLE+></TD+></TR+><TR+><TD CLASS="s15"+></TD+></TR+><TR+><TD CLASS="section1"+>Description</TD+></TR+><TR+><TD CLASS="doc"+>Formats Haskell source code using LaTeX macros.+</TD+></TR+><TR+><TD CLASS="s15"+></TD+></TR+><TR+><TD CLASS="section1"+>Synopsis</TD+></TR+><TR+><TD CLASS="s15"+></TD+></TR+><TR+><TD CLASS="body"+><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"+><TR+><TD CLASS="decl"+><A HREF="#v%3Ahscolour"+>hscolour</A+> :: <A HREF="Language-Haskell-HsColour-Colourise.html#t%3AColourPrefs"+>ColourPrefs</A+> -> String -> String</TD+></TR+><TR+><TD CLASS="s8"+></TD+></TR+><TR+><TD CLASS="decl"+><A HREF="#v%3AhscolourFragment"+>hscolourFragment</A+> :: <A HREF="Language-Haskell-HsColour-Colourise.html#t%3AColourPrefs"+>ColourPrefs</A+> -> String -> String</TD+></TR+></TABLE+></TD+></TR+><TR+><TD CLASS="s15"+></TD+></TR+><TR+><TD CLASS="section1"+>Documentation</TD+></TR+><TR+><TD CLASS="s15"+></TD+></TR+><TR+><TD CLASS="topdecl"+><TABLE CLASS="declbar"+><TR+><TD CLASS="declname"+><A NAME="v%3Ahscolour"+></A+><B+>hscolour</B+></TD+><TD CLASS="declbut"+><A HREF="Language/Haskell/HsColour/LaTeX.html#hscolour"+>Source</A+></TD+></TR+></TABLE+></TD+></TR+><TR+><TD CLASS="body"+><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"+><TR+><TD CLASS="arg"+>:: <A HREF="Language-Haskell-HsColour-Colourise.html#t%3AColourPrefs"+>ColourPrefs</A+></TD+><TD CLASS="rdoc"+>Colour preferences.+</TD+></TR+><TR+><TD CLASS="arg"+>-> String</TD+><TD CLASS="rdoc"+>Haskell source code.+</TD+></TR+><TR+><TD CLASS="arg"+>-> String</TD+><TD CLASS="rdoc"+>An LaTeX document containing the coloured + Haskell source code.+</TD+></TR+><TR+><TD CLASS="ndoc" COLSPAN="2"+>Formats Haskell source code as a complete LaTeX document.+</TD+></TR+></TABLE+></TD+></TR+><TR+><TD CLASS="s15"+></TD+></TR+><TR+><TD CLASS="topdecl"+><TABLE CLASS="declbar"+><TR+><TD CLASS="declname"+><A NAME="v%3AhscolourFragment"+></A+><B+>hscolourFragment</B+></TD+><TD CLASS="declbut"+><A HREF="Language/Haskell/HsColour/LaTeX.html#hscolourFragment"+>Source</A+></TD+></TR+></TABLE+></TD+></TR+><TR+><TD CLASS="body"+><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"+><TR+><TD CLASS="arg"+>:: <A HREF="Language-Haskell-HsColour-Colourise.html#t%3AColourPrefs"+>ColourPrefs</A+></TD+><TD CLASS="rdoc"+>Colour preferences.+</TD+></TR+><TR+><TD CLASS="arg"+>-> String</TD+><TD CLASS="rdoc"+>Haskell source code.+</TD+></TR+><TR+><TD CLASS="arg"+>-> String</TD+><TD CLASS="rdoc"+>An LaTeX fragment containing the coloured + Haskell source code.+</TD+></TR+><TR+><TD CLASS="ndoc" COLSPAN="2"+>Formats Haskell source code as an LaTeX fragment.+</TD+></TR+></TABLE+></TD+></TR+><TR+><TD CLASS="s15"+></TD+></TR+><TR+><TD CLASS="botbar"+>Produced by <A HREF="http://www.haskell.org/haddock/"+>Haddock</A+> version 0.8</TD+></TR+></TABLE+></BODY+></HTML+>
+ docs/hscolour/Language/Haskell/HsColour/LaTeX.html view
@@ -0,0 +1,112 @@+<pre><font color=Blue>-- | Formats Haskell source code using LaTeX macros.</font>+<u><font color=Green>module</font></u> Language<font color=Cyan>.</font>Haskell<font color=Cyan>.</font>HsColour<font color=Cyan>.</font>LaTeX <font color=Cyan>(</font>hscolour<font color=Cyan>,</font> hscolourFragment<font color=Cyan>)</font> <u><font color=Green>where</font></u>++<u><font color=Green>import</font></u> Language<font color=Cyan>.</font>Haskell<font color=Cyan>.</font>HsColour<font color=Cyan>.</font>Classify <u><font color=Green>as</font></u> Classify+<u><font color=Green>import</font></u> Language<font color=Cyan>.</font>Haskell<font color=Cyan>.</font>HsColour<font color=Cyan>.</font>Colourise++<font color=Blue>-- | Formats Haskell source code as a complete LaTeX document.</font>+hscolour <font color=Red>::</font> ColourPrefs <font color=Blue>-- ^ Colour preferences.</font>+ <font color=Red>-></font> String <font color=Blue>-- ^ Haskell source code.</font>+ <font color=Red>-></font> String <font color=Blue>-- ^ An LaTeX document containing the coloured </font>+ <font color=Blue>-- Haskell source code.</font>+<a name="hscolour"></a>hscolour pref <font color=Red>=</font> top'n'tail <font color=Cyan>.</font> hscolourFragment pref++<font color=Blue>-- | Formats Haskell source code as an LaTeX fragment.</font>+hscolourFragment <font color=Red>::</font> ColourPrefs <font color=Blue>-- ^ Colour preferences.</font>+ <font color=Red>-></font> String <font color=Blue>-- ^ Haskell source code.</font>+ <font color=Red>-></font> String <font color=Blue>-- ^ An LaTeX fragment containing the coloured </font>+ <font color=Blue>-- Haskell source code.</font>+<a name="hscolourFragment"></a>hscolourFragment pref <font color=Red>=</font> concatMap <font color=Cyan>(</font>renderToken pref<font color=Cyan>)</font> <font color=Cyan>.</font> tokenise++top'n'tail <font color=Red>::</font> String <font color=Red>-></font> String+<a name="top'n'tail"></a>top'n'tail <font color=Red>=</font> <font color=Cyan>(</font>latexPrefix<font color=Cyan>++</font><font color=Cyan>)</font> <font color=Cyan>.</font> <font color=Cyan>(</font><font color=Cyan>++</font>latexSuffix<font color=Cyan>)</font>++<font color=Blue>-- | Wrap each lexeme in the appropriate LaTeX macro.</font>+<font color=Blue>-- TODO: filter dangerous characters like "{}_$"</font>+renderToken <font color=Red>::</font> ColourPrefs <font color=Red>-></font> <font color=Cyan>(</font>TokenType<font color=Cyan>,</font>String<font color=Cyan>)</font> <font color=Red>-></font> String+<a name="renderToken"></a>renderToken pref <font color=Cyan>(</font>Space<font color=Cyan>,</font>text<font color=Cyan>)</font> <font color=Red>=</font> filterSpace text+renderToken pref <font color=Cyan>(</font>cls<font color=Cyan>,</font>text<font color=Cyan>)</font> <font color=Red>=</font> <u><font color=Green>let</font></u> + symb <font color=Red>=</font> <u><font color=Green>case</font></u> cls <u><font color=Green>of</font></u>+ String <font color=Red>-></font> <font color=Magenta>"``"</font> <font color=Cyan>++</font> <font color=Cyan>(</font>reverse <font color=Cyan>.</font> tail <font color=Cyan>.</font> reverse <font color=Cyan>.</font> tail <font color=Cyan>$</font> text<font color=Cyan>)</font> <font color=Cyan>++</font> <font color=Magenta>"''"</font>+ <u><font color=Green>_</font></u> <font color=Red>-></font> text+ style <font color=Red>=</font> colourise pref cls+ <font color=Cyan>(</font>pre<font color=Cyan>,</font> post<font color=Cyan>)</font> <font color=Red>=</font> unzip <font color=Cyan>$</font> map latexHighlight style <u><font color=Green>in</font></u>+ <font color=Cyan>(</font>concat pre<font color=Cyan>)</font> <font color=Cyan>++</font> filterSpecial symb <font color=Cyan>++</font> <font color=Cyan>(</font>concat post<font color=Cyan>)</font> ++<font color=Blue>-- | Filter white space characters.</font>+filterSpace <font color=Red>::</font> String+ <font color=Red>-></font> String+<a name="filterSpace"></a>filterSpace <font color=Cyan>(</font><font color=Magenta>'\n'</font><b><font color=Red>:</font></b>ss<font color=Cyan>)</font> <font color=Red>=</font> <font color=Magenta>'\\'</font><b><font color=Red>:</font></b><font color=Magenta>'\\'</font><b><font color=Red>:</font></b><font color=Cyan>(</font>filterSpace ss<font color=Cyan>)</font>+filterSpace <font color=Cyan>(</font><font color=Magenta>' '</font><b><font color=Red>:</font></b>ss<font color=Cyan>)</font> <font color=Red>=</font> <font color=Magenta>"\\hsspace "</font><font color=Cyan>++</font><font color=Cyan>(</font>filterSpace ss<font color=Cyan>)</font>+filterSpace <font color=Cyan>(</font><font color=Magenta>'\t'</font><b><font color=Red>:</font></b>ss<font color=Cyan>)</font> <font color=Red>=</font> <font color=Magenta>"\\hstab "</font><font color=Cyan>++</font><font color=Cyan>(</font>filterSpace ss<font color=Cyan>)</font>+filterSpace <font color=Cyan>(</font>c<b><font color=Red>:</font></b>ss<font color=Cyan>)</font> <font color=Red>=</font> c<b><font color=Red>:</font></b><font color=Cyan>(</font>filterSpace ss<font color=Cyan>)</font>+filterSpace <font color=Red>[</font><font color=Red>]</font> <font color=Red>=</font> <font color=Red>[</font><font color=Red>]</font>++<font color=Blue>-- | Filters the characters "#$%&~_^\{}</font><font color=Magenta>" which are special+-- in LaTeX.+filterSpecial :: String -- ^ The string to filter. + -> String -- ^ The LaTeX-safe string.+filterSpecial ('#':cs) = '\\':'#':(filterSpecial cs)+filterSpecial ('$':cs) = '\\':'$':(filterSpecial cs)+filterSpecial ('%':cs) = '\\':'%':(filterSpecial cs)+filterSpecial ('&':cs) = '\\':'&':(filterSpecial cs)+filterSpecial ('~':cs) = "</font><font color=Cyan>\\</font>tilde<font color=Cyan>{</font> <font color=Cyan>}</font><font color=Magenta>"++(filterSpecial cs)+filterSpecial ('_':cs) = '\\':'_':(filterSpecial cs)+filterSpecial ('^':cs) = "</font><font color=Cyan>\\</font>hat<font color=Cyan>{</font> <font color=Cyan>}</font><font color=Magenta>"++(filterSpecial cs)+filterSpecial ('\\':cs) = "</font><font color=Cyan>$\\</font>backslash<font color=Cyan>$</font><font color=Magenta>"++(filterSpecial cs)+filterSpecial ('{':cs) = '\\':'{':(filterSpecial cs)+filterSpecial ('}':cs) = '\\':'}':(filterSpecial cs)+filterSpecial ('|':cs) = "</font><font color=Cyan>\\</font>ensuremath<font color=Cyan>{</font><font color=Red>|</font><font color=Cyan>}</font><font color=Magenta>"++(filterSpecial cs)+filterSpecial ('<':'-':cs) = "</font><font color=Cyan>\\</font>ensuremath<font color=Cyan>{</font><font color=Cyan>\\</font>leftarrow<font color=Cyan>}</font><font color=Magenta>"++(filterSpecial cs)+filterSpecial ('<':cs) = "</font><font color=Cyan>\\</font>ensuremath<font color=Cyan>{</font><font color=Cyan>\\</font>langle<font color=Cyan>}</font><font color=Magenta>"++(filterSpecial cs)+filterSpecial ('-':'>':cs) = "</font><font color=Cyan>\\</font>ensuremath<font color=Cyan>{</font><font color=Cyan>\\</font>rightarrow<font color=Cyan>}</font><font color=Magenta>"++(filterSpecial cs)+filterSpecial ('>':cs) = "</font><font color=Cyan>\\</font>ensuremath<font color=Cyan>{</font><font color=Cyan>\\</font>rangle<font color=Cyan>}</font><font color=Magenta>"++(filterSpecial cs)+filterSpecial (c:cs) = c:(filterSpecial cs)+filterSpecial [] = []+++-- | Constructs the appropriate LaTeX macro for the given style.+latexHighlight :: Highlight -> (String, String)+latexHighlight Normal = ("</font><font color=Cyan>{</font><font color=Cyan>\\</font>rm<font color=Cyan>{</font><font color=Cyan>}</font><font color=Magenta>", "</font><font color=Cyan>}</font><font color=Magenta>")+latexHighlight Bold = ("</font><font color=Cyan>{</font><font color=Cyan>\\</font>bf<font color=Cyan>{</font><font color=Cyan>}</font><font color=Magenta>", "</font><font color=Cyan>}</font><font color=Magenta>")+latexHighlight Dim = ("</font><font color=Magenta>", "</font><font color=Magenta>")+latexHighlight Underscore = ("</font><font color=Cyan>\\</font>underline<font color=Cyan>{</font><font color=Magenta>", "</font><font color=Cyan>}</font><font color=Magenta>")+latexHighlight Blink = ("</font><font color=Magenta>", "</font><font color=Magenta>")+latexHighlight ReverseVideo = ("</font><font color=Magenta>", "</font><font color=Magenta>")+latexHighlight Concealed = ("</font><font color=Cyan>\\</font>conceal<font color=Cyan>{</font><font color=Magenta>", "</font><font color=Cyan>}</font><font color=Magenta>")+latexHighlight (Foreground c) = ("</font><font color=Cyan>\\</font>textcolor<font color=Cyan>{</font><font color=Magenta>"++ latexColour c ++"</font><font color=Cyan>}</font><font color=Cyan>{</font><font color=Magenta>", "</font><font color=Cyan>}</font><font color=Magenta>")+latexHighlight (Background c) = ("</font><font color=Cyan>\\</font>colorbox<font color=Cyan>{</font><font color=Magenta>"++ latexColour c ++"</font><font color=Cyan>}</font><font color=Cyan>{</font><font color=Magenta>", "</font><font color=Cyan>}</font><font color=Magenta>")++-- | Translate a 'Colour' into a LaTeX colour name.+latexColour :: Colour -> String+latexColour Black = "</font>black<font color=Magenta>"+latexColour Red = "</font>red<font color=Magenta>"+latexColour Green = "</font>green<font color=Magenta>"+latexColour Yellow = "</font>yellow<font color=Magenta>"+latexColour Blue = "</font>blue<font color=Magenta>"+latexColour Magenta = "</font>magenta<font color=Magenta>"+latexColour Cyan = "</font>cyan<font color=Magenta>"+latexColour White = "</font>white<font color=Magenta>"++-- | Generic LaTeX document preamble.+latexPrefix = unlines+ ["</font><font color=Cyan>\\</font>documentclass<font color=Red>[</font>a4paper<font color=Cyan>,</font> <font color=Magenta>12</font>pt<font color=Red>]</font><font color=Cyan>{</font>article<font color=Cyan>}</font><font color=Magenta>"+ ,"</font><font color=Cyan>\\</font>usepackage<font color=Red>[</font>usenames<font color=Red>]</font><font color=Cyan>{</font>color<font color=Cyan>}</font><font color=Magenta>"+ ,"</font><font color=Cyan>\\</font>usepackage<font color=Cyan>{</font>hyperref<font color=Cyan>}</font><font color=Magenta>"+ ,"</font><font color=Cyan>\\</font>newsavebox<font color=Cyan>{</font><font color=Cyan>\\</font>spaceb<font color=Cyan>}</font><font color=Magenta>"+ ,"</font><font color=Cyan>\\</font>newsavebox<font color=Cyan>{</font><font color=Cyan>\\</font>tabb<font color=Cyan>}</font><font color=Magenta>"+ ,"</font><font color=Cyan>\\</font>savebox<font color=Cyan>{</font><font color=Cyan>\\</font>spaceb<font color=Cyan>}</font><font color=Red>[</font><font color=Magenta>1</font>ex<font color=Red>]</font><font color=Cyan>{</font><font color=Red>~</font><font color=Cyan>}</font><font color=Magenta>"+ ,"</font><font color=Cyan>\\</font>savebox<font color=Cyan>{</font><font color=Cyan>\\</font>tabb<font color=Cyan>}</font><font color=Red>[</font><font color=Magenta>4</font>ex<font color=Red>]</font><font color=Cyan>{</font><font color=Red>~</font><font color=Cyan>}</font><font color=Magenta>"+ ,"</font><font color=Cyan>\\</font>newcommand<font color=Cyan>{</font><font color=Cyan>\\</font>hsspace<font color=Cyan>}</font><font color=Cyan>{</font><font color=Cyan>\\</font>usebox<font color=Cyan>{</font><font color=Cyan>\\</font>spaceb<font color=Cyan>}</font><font color=Cyan>}</font><font color=Magenta>"+ ,"</font><font color=Cyan>\\</font>newcommand<font color=Cyan>{</font><font color=Cyan>\\</font>hstab<font color=Cyan>}</font><font color=Cyan>{</font><font color=Cyan>\\</font>usebox<font color=Cyan>{</font><font color=Cyan>\\</font>tabb<font color=Cyan>}</font><font color=Cyan>}</font><font color=Magenta>"+ ,"</font><font color=Cyan>\\</font>newcommand<font color=Cyan>{</font><font color=Cyan>\\</font>conceal<font color=Cyan>}</font><font color=Red>[</font><font color=Magenta>1</font><font color=Red>]</font><font color=Cyan>{</font><font color=Cyan>}</font><font color=Magenta>"+ ,"</font><font color=Cyan>\\</font>begin<font color=Cyan>{</font>document<font color=Cyan>}</font><font color=Magenta>"+ ,"</font><font color=Cyan>\\</font>noindent<font color=Magenta>"+ ]++-- | Generic LaTeX document postamble.+latexSuffix = unlines+ ["</font><font color=Magenta>"+ ,"</font><font color=Cyan>\\</font>end<font color=Cyan>{</font>document<font color=Cyan>}</font><font color=Magenta>"</font>+ <font color=Red>]</font>+</pre>
docs/hscolour/doc-index-H.html view
@@ -199,16 +199,36 @@ ><TD CLASS="indexannot" >4 (Function)</TD ><TD CLASS="indexlinks"+><A HREF="Language-Haskell-HsColour-LaTeX.html#v%3Ahscolour"+>Language.Haskell.HsColour.LaTeX</A+></TD+></TR+><TR+><TD CLASS="indexannot"+>5 (Function)</TD+><TD CLASS="indexlinks" ><A HREF="Language-Haskell-HsColour-TTY.html#v%3Ahscolour" >Language.Haskell.HsColour.TTY</A ></TD ></TR ><TR-><TD CLASS="indexentry"+><TD CLASS="indexentry" COLSPAN="2" >hscolourFragment</TD+></TR+><TR+><TD CLASS="indexannot"+>1 (Function)</TD ><TD CLASS="indexlinks" ><A HREF="Language-Haskell-HsColour-CSS.html#v%3AhscolourFragment" >Language.Haskell.HsColour.CSS</A+></TD+></TR+><TR+><TD CLASS="indexannot"+>2 (Function)</TD+><TD CLASS="indexlinks"+><A HREF="Language-Haskell-HsColour-LaTeX.html#v%3AhscolourFragment"+>Language.Haskell.HsColour.LaTeX</A ></TD ></TR ></TABLE
docs/hscolour/index.html view
@@ -149,6 +149,16 @@ ></TR ><TR ><TD STYLE="padding-left: 1.25em;width: 44em"+><A HREF="Language-Haskell-HsColour-LaTeX.html"+>Language.Haskell.HsColour.LaTeX</A+></TD+><TD+></TD+><TD+></TD+></TR+><TR+><TD STYLE="padding-left: 1.25em;width: 44em" ><A HREF="Language-Haskell-HsColour-TTY.html" >Language.Haskell.HsColour.TTY</A ></TD
hscolour.cabal view
@@ -1,6 +1,6 @@ Name: hscolour-Version: 1.6-Copyright: Malcolm Wallace, University of York, 2003-2005, Bjorn Bringert 2006+Version: 1.9+Copyright: Malcolm Wallace, University of York, 2003-2007, Bjorn Bringert 2006 Maintainer: Malcolm Wallace Author: Malcolm Wallace Homepage: http://www.cs.york.ac.uk/fp/darcs/hscolour/@@ -12,7 +12,7 @@ Description: hscolour is a small Haskell script to colourise Haskell code. It currently has four output formats: - ANSI terminal codes, HTML with <font> tags, HTML with CSS, and LaTeX.+ ANSI terminal codes, HTML 3.2 with <font> tags, HTML 4.01 with CSS, and LaTeX. Exposed-Modules: Language.Haskell.HsColour, Language.Haskell.HsColour.ANSI,@@ -23,8 +23,11 @@ Language.Haskell.HsColour.TTY, Language.Haskell.HsColour.HTML, Language.Haskell.HsColour.LaTeX,+ Language.Haskell.HsColour.General,+ Language.Haskell.HsColour.MIRC, Language.Haskell.HsColour.CSS-ghc-options: -O2 -W+data-files: hscolour.css+ghc-options: -O -W Executable: HsColour Main-is: HsColour.hs
hscolour.css view
@@ -3,3 +3,4 @@ .keyword {color: blue;} .comment, .comment a {color: green;} .str, .chr {color: teal;}+.keyword,.conid, .varid, .conop, .varop, .num, .cpp, .sel, .definition {}
index.html view
@@ -10,8 +10,8 @@ <p> <b>hscolour</b> is a small Haskell script to colourise Haskell code.-It currently has four output formats: ANSI terminal codes, HTML with-<font> tags, HTML with CSS, and LaTeX.+It currently has five output formats: ANSI terminal codes, HTML 3.2 with+<font> tags, HTML 4.01 with CSS, LaTeX, and mIRC chat client codes. <h2>Example</h2> <p>@@ -31,6 +31,7 @@ <h2>Download</h2> <p> <ul>+<li> Find it on <a href="http://hackage.haskell.org">Hackage</a>. <li> Download a tarfile package from: <a href="ftp://ftp.cs.york.ac.uk/pub/haskell/contrib/"> ftp://ftp.cs.york.ac.uk/pub/haskell/contrib/</a>@@ -50,38 +51,58 @@ <li><tt>hmake HsColour</tt> <li><tt>ghc --make HsColour</tt> <li><tt>runhugs HsColour</tt>+<li><tt>runhaskell Setup.hs configure/build/install</tt> </ul> <h2>Use</h2> <p> <ul>-<li><tt>HsColour [ -tty | -html | -css | -latex | -anchorHTML | -anchorCSS ] [file.hs]</tt>-</ul>+<li><pre>+HsColour [ -help | -version | -print-css ]+ [ -oOUTPUT ]+ [ -tty | -html | -css | -latex | -mirc ]+ [ -lit | -nolit ]+ [ -anchor | -noanchor | -partial | -nopartial ]+ [file.hs]</pre>+</li></ul> <p> You can colourise a Haskell source file for either ANSI terminal codes-(option -tty), or HTML with font tags (option -html), or HTML output-with CSS (option -css), or for LaTeX. The default is for terminal output.+(option -tty), or HTML 3.2 with font tags (option -html), or HTML 4.01+output with CSS (option -css), or for LaTeX (option -latex), or for IRC+(option -mirc). The default is for terminal output. <p> If no file argument is given, it reads standard input. Output is-always written to standard output.+written to file OUTPUT (option -o), defaulting to stdout. <p>-HsColour can add named anchors (options -anchorHTML or -anchorCSS) to+HsColour can add named anchors in HTML (option -anchor) to top-level definitions in the source file (functions, datatypes, classes). This enables you to make links to a specific location in the generated file with the standard <tt>file.html#anchor</tt> notation (e.g. from <a href="http://haskell.org/haddock">haddock</a>-generated library documentation). <a href="#haddock">See below for details.</a>+<p>+If you want to embed several individual colourised fragments into a+larger document, then use the <tt>-partial</tt> option with each+fragment, to omit the HTML DOCTYPE header, CSS stylesheet link, or LaTeX+prologue.+<p>+Alternatively, if you have a literate input source (e.g. .lhs file),+then the <tt>-lit</tt> option will pass the literate parts untouched,+and colourise only the code fragments (indicated by Bird-tracks - a >+in the left-most column), as if each was called individually with+<tt>-partial</tt>. <h2>Configuration of colours</h2> <p>-If you use either -html, -tty, or -latex, you can configure the colours for-different lexical entities by editing a configuration file called-<tt>.hscolour</tt> in the current directory.+If you use either -html, -tty, -mirc, or -latex, you can configure the+colours for different lexical entities by editing a configuration file+called <tt>.hscolour</tt> in the current directory. (An <a href=".hscolour">example</a> is included in the distribution.) For CSS output, it is sufficient to edit the <a href="hscolour.css"><tt>hscolour.css</tt></a> file, also in-the distribution.+the distribution. The <tt>-print-css</tt> option prints out the default+CSS definitions, in case you lose the .css file. <p> The <tt>.hscolour</tt> file format is a simple Haskell value of type@@ -90,8 +111,9 @@ <pre> <u><font color=Green>data</font></u> ColourPrefs <font color=Red>=</font> ColourPrefs <font color=Cyan>{</font> keyword<font color=Cyan>,</font> keyglyph<font color=Cyan>,</font> layout<font color=Cyan>,</font> comment <font color=Cyan>,</font> conid<font color=Cyan>,</font> varid<font color=Cyan>,</font> conop<font color=Cyan>,</font> varop- <font color=Cyan>,</font> string<font color=Cyan>,</font> char<font color=Cyan>,</font> number- <font color=Cyan>,</font> selection<font color=Cyan>,</font> variantselection <font color=Red>::</font> <font color=Red>[</font>Highlight<font color=Red>]</font>+ <font color=Cyan>,</font> string<font color=Cyan>,</font> char<font color=Cyan>,</font> number <font color=Cyan>,</font> cpp+ <font color=Cyan>,</font> selection<font color=Cyan>,</font> variantselection+ <font color=Cyan>,</font> definition <font color=Red>::</font> <font color=Red>[</font>Highlight<font color=Red>]</font> <font color=Cyan>}</font> <u><font color=Green>data</font></u> Colour <font color=Red>=</font> Black <font color=Red>|</font> Red <font color=Red>|</font> Green <font color=Red>|</font> Yellow <font color=Red>|</font> Blue <font color=Red>|</font> Magenta <font color=Red>|</font> Cyan <font color=Red>|</font> White@@ -128,12 +150,11 @@ Let's say you want to generate some pretty-coloured HTML versions of your source files, at the same time as you are generating library documentation using <a href="http://haskell.org/haddock">Haddock</a>.-Haddock (1.8 onwards, currently only available in darcs) has options-to link the API docs to the source code itself. Here is a quick summary-of the shell commands to use:+Haddock (0.8 onwards) has options to link the API docs to the source+code itself. Here is a quick summary of the shell commands to use: <pre> for file in $(SRCS)-do HsColour -anchorHTML $file >docs/`dirname $file'/`basename $file .hs`.html+do HsColour -html -anchor $file >docs/`dirname $file`/`basename $file .hs`.html done haddock --html --title="My Library" --odir=docs $(SRCS) \ --source-module="src/%{MODULE/.//}.html" \@@ -142,7 +163,7 @@ <p> <h2>Copyright and licence</h2>-<b>hscolour</b> is © Malcolm Wallace 2003-2006. It is distributed+<b>hscolour</b> is © Malcolm Wallace 2003-2007. It is distributed under the Gnu GPL, which can be found in the file <a href="LICENCE-GPL">LICENCE-GPL</a>. @@ -166,6 +187,12 @@ <h2>History</h2> <p> <dl>+<dt>1.9</dt><dd> added the -mirc and -lit options, and -print-css</dd>+<dt>1.8</dt><dd> added highlights for cpp lines+ <br>tuple constructors now treated as ConIds+ <br>checked for absence of pattern-match failure by Catch</dd>+<dt>1.7</dt><dd> renamed -anchorHTML -anchorCSS options to -anchor,+ added -partial option</dd> <dt>1.6</dt><dd> added -latex output mode</dd> <dt>1.5</dt><dd> move generated HTML anchors to before comments/typesigs</dd> <dt>1.4</dt><dd> made available as a Cabal-ised library</dd>@@ -176,7 +203,7 @@ </dl> <p>-This page last modified: 21st December 2006<br>+This page last modified: 8th Jan 2008<br> Malcolm Wallace<br> </td></tr></table>