module Language.Haskell.HsColour.LaTeX (hscolour, top'n'tail) where
import Language.Haskell.HsColour.Classify as Classify
import Language.Haskell.HsColour.Colourise
import Language.Haskell.HsColour.General
hscolour :: ColourPrefs
-> String
-> String
hscolour pref = concatMap (renderToken pref) . tokenise
top'n'tail :: String -> String -> String
top'n'tail title = (latexPrefix title++) . (++latexSuffix)
renderToken :: ColourPrefs -> (TokenType,String) -> String
renderToken pref (Space,text) = filterSpace text
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
filterSpace :: String
-> String
filterSpace ('\n':ss) = '\\':'\\':(filterSpace ss)
filterSpace (' ':ss) = "\\hsspace "++(filterSpace ss)
filterSpace ('\t':ss) = "\\hstab "++(filterSpace ss)
filterSpace (c:ss) = c:(filterSpace ss)
filterSpace [] = []
filterSpecial :: String
-> String
filterSpecial ('#':cs) = '\\':'#':(filterSpecial cs)
filterSpecial ('$':cs) = '\\':'$':(filterSpecial cs)
filterSpecial ('%':cs) = '\\':'%':(filterSpecial cs)
filterSpecial ('&':cs) = '\\':'&':(filterSpecial cs)
filterSpecial ('~':cs) = "\\tilde{ }"++(filterSpecial cs)
filterSpecial ('_':cs) = '\\':'_':(filterSpecial cs)
filterSpecial ('^':cs) = "\\ensuremath{\\hat{ }}"++(filterSpecial cs)
filterSpecial ('\\':cs) = "$\\backslash$"++(filterSpecial cs)
filterSpecial ('{':cs) = '\\':'{':(filterSpecial cs)
filterSpecial ('}':cs) = '\\':'}':(filterSpecial cs)
filterSpecial ('|':cs) = "\\ensuremath{|}"++(filterSpecial cs)
filterSpecial ('<':'-':cs) = "\\ensuremath{\\leftarrow}"++(filterSpecial cs)
filterSpecial ('<':cs) = "\\ensuremath{\\langle}"++(filterSpecial cs)
filterSpecial ('-':'>':cs) = "\\ensuremath{\\rightarrow}"++(filterSpecial cs)
filterSpecial ('>':cs) = "\\ensuremath{\\rangle}"++(filterSpecial cs)
filterSpecial (c:cs) = c:(filterSpecial cs)
filterSpecial [] = []
latexHighlight :: Highlight -> (String, String)
latexHighlight Normal = ("{\\rm{}", "}")
latexHighlight Bold = ("{\\bf{}", "}")
latexHighlight Dim = ("", "")
latexHighlight Underscore = ("\\underline{", "}")
latexHighlight Blink = ("", "")
latexHighlight ReverseVideo = ("", "")
latexHighlight Concealed = ("\\conceal{", "}")
latexHighlight (Foreground c) = ("\\textcolor{"++ latexColour c ++"}{", "}")
latexHighlight (Background c) = ("\\colorbox{"++ latexColour c ++"}{", "}")
latexHighlight Italic = ("{\\it{}", "}")
latexColour :: Colour -> String
latexColour Black = "black"
latexColour Red = "red"
latexColour Green = "green"
latexColour Yellow = "yellow"
latexColour Blue = "blue"
latexColour Magenta = "magenta"
latexColour Cyan = "cyan"
latexColour White = "white"
latexColour c@(Rgb _ _ _) = latexColour (projectToBasicColour8 c)
latexPrefix title = unlines
["\\documentclass[a4paper, 12pt]{article}"
,"\\usepackage[usenames]{color}"
,"\\usepackage{hyperref}"
,"\\newsavebox{\\spaceb}"
,"\\newsavebox{\\tabb}"
,"\\savebox{\\spaceb}[1ex]{~}"
,"\\savebox{\\tabb}[4ex]{~}"
,"\\newcommand{\\hsspace}{\\usebox{\\spaceb}}"
,"\\newcommand{\\hstab}{\\usebox{\\tabb}}"
,"\\newcommand{\\conceal}[1]{}"
,"\\title{"++title++"}"
,"%% Generated by HsColour"
,"\\begin{document}"
,"\\maketitle"
,"\\noindent"
]
latexSuffix = unlines
[""
,"\\end{document}"
]