{-# LANGUAGE CPP #-}
{- |
   Module      : Text.Highlighting.Kate.Format.HTML
   Copyright   : Copyright (C) 2008-2011 John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley.edu>
   Stability   : alpha
   Portability : portable

Formatters that convert a list of annotated source lines to HTML.
-}

module Text.Highlighting.Kate.Format.HTML (
      formatHtmlInline, formatHtmlBlock, styleToCss
   ) where
import Text.Highlighting.Kate.Types
#if MIN_VERSION_blaze_html(0,5,0)
import Text.Blaze.Html
#else
import Text.Blaze
#endif
import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributes as A
import Data.Monoid
import Data.List (intersperse)

-- | Format tokens using HTML spans inside @code@ tags. For example,
-- A @KeywordTok@ is rendered as a span with class @kw@.
-- Short class names correspond to 'TokenType's as follows:
-- 'KeywordTok' = @kw@, 'DataTypeTok' = @dt@,
-- 'DecValTok' = @dv@, 'BaseNTok' = @bn@, 'FloatTok' = @fl@,
-- 'CharTok' = @ch@, 'StringTok' = @st@, 'CommontTok' = @co@,
-- 'OtherTok' = @ot@, 'AlertTok' = @al@, 'FunctionTok' = @fu@,
-- 'RegionMarkerTok' = @re@, 'ErrorTok' = @er@,
-- 'ConstantTok' = @cn@, 'SpecialCharTok' = @sc@,
-- 'VerbatimStringTok' = @vs@, 'SpecialStringTok' = @ss@,
-- 'ImportTok' = @im@, 'DocumentationTok' = @do@,
-- 'AnnotationTok' = @an@, 'CommentVarTok' = @cv@,
-- 'VariableTok' = @va@, 'ControlFlowTok' = @cf@,
-- 'OperatorTok' = @op@, 'BuiltInTok' = @bu@,
-- 'ExtensionTok' = @ex@, 'PreprocessorTok' = @pp@,
-- 'AttributeTok' = @at@, 'InformationTok' = @in@,
-- 'WarningTok' = @wa@.
-- A 'NormalTok' is not marked up at all.
formatHtmlInline :: FormatOptions -> [SourceLine] -> Html
formatHtmlInline :: FormatOptions -> [SourceLine] -> Html
formatHtmlInline FormatOptions
opts = (Html -> Html
H.code forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (forall a. ToValue a => a -> AttributeValue
toValue forall a b. (a -> b) -> a -> b
$ [String] -> String
unwords
                                                    forall a b. (a -> b) -> a -> b
$ String
"sourceCode" forall a. a -> [a] -> [a]
: FormatOptions -> [String]
codeClasses FormatOptions
opts))
                                forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> [a] -> [a]
intersperse (forall a. ToMarkup a => a -> Html
toHtml String
"\n")
                                forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (FormatOptions -> SourceLine -> Html
sourceLineToHtml FormatOptions
opts)

tokenToHtml :: FormatOptions -> Token -> Html
tokenToHtml :: FormatOptions -> Token -> Html
tokenToHtml FormatOptions
_ (TokenType
NormalTok, String
txt)  = forall a. ToMarkup a => a -> Html
toHtml String
txt
tokenToHtml FormatOptions
opts (TokenType
toktype, String
txt) =
  if FormatOptions -> Bool
titleAttributes FormatOptions
opts
     then Html
sp forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.title (forall a. ToValue a => a -> AttributeValue
toValue forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show TokenType
toktype)
     else Html
sp
   where sp :: Html
sp = Html -> Html
H.span forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (forall a. ToValue a => a -> AttributeValue
toValue forall a b. (a -> b) -> a -> b
$ TokenType -> String
short TokenType
toktype) forall a b. (a -> b) -> a -> b
$ forall a. ToMarkup a => a -> Html
toHtml String
txt

short :: TokenType -> String
short :: TokenType -> String
short TokenType
KeywordTok        = String
"kw"
short TokenType
DataTypeTok       = String
"dt"
short TokenType
DecValTok         = String
"dv"
short TokenType
BaseNTok          = String
"bn"
short TokenType
FloatTok          = String
"fl"
short TokenType
CharTok           = String
"ch"
short TokenType
StringTok         = String
"st"
short TokenType
CommentTok        = String
"co"
short TokenType
OtherTok          = String
"ot"
short TokenType
AlertTok          = String
"al"
short TokenType
FunctionTok       = String
"fu"
short TokenType
RegionMarkerTok   = String
"re"
short TokenType
ErrorTok          = String
"er"
short TokenType
ConstantTok       = String
"cn"
short TokenType
SpecialCharTok    = String
"sc"
short TokenType
VerbatimStringTok = String
"vs"
short TokenType
SpecialStringTok  = String
"ss"
short TokenType
ImportTok         = String
"im"
short TokenType
DocumentationTok  = String
"do"
short TokenType
AnnotationTok     = String
"an"
short TokenType
CommentVarTok     = String
"cv"
short TokenType
VariableTok       = String
"va"
short TokenType
ControlFlowTok    = String
"cf"
short TokenType
OperatorTok       = String
"op"
short TokenType
BuiltInTok        = String
"bu"
short TokenType
ExtensionTok      = String
"ex"
short TokenType
PreprocessorTok   = String
"pp"
short TokenType
AttributeTok      = String
"at"
short TokenType
InformationTok    = String
"in"
short TokenType
WarningTok        = String
"wa"
short TokenType
NormalTok         = String
""

sourceLineToHtml :: FormatOptions -> SourceLine -> Html
sourceLineToHtml :: FormatOptions -> SourceLine -> Html
sourceLineToHtml FormatOptions
opts SourceLine
contents = forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (FormatOptions -> Token -> Html
tokenToHtml FormatOptions
opts) SourceLine
contents

formatHtmlBlockPre :: FormatOptions -> [SourceLine] -> Html
formatHtmlBlockPre :: FormatOptions -> [SourceLine] -> Html
formatHtmlBlockPre FormatOptions
opts = Html -> Html
H.pre forall b c a. (b -> c) -> (a -> b) -> a -> c
. FormatOptions -> [SourceLine] -> Html
formatHtmlInline FormatOptions
opts

-- | Format tokens as an HTML @pre@ block. If line numbering is
-- selected, this is put into a table row with line numbers in the
-- left cell.  The whole code block is wrapped in a @div@ element
-- to aid styling (e.g. the overflow-x property).
formatHtmlBlock :: FormatOptions -> [SourceLine] -> Html
formatHtmlBlock :: FormatOptions -> [SourceLine] -> Html
formatHtmlBlock FormatOptions
opts [SourceLine]
ls = Html -> Html
H.div forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
sourceCode forall a b. (a -> b) -> a -> b
$
                            Html
container forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (forall a. ToValue a => a -> AttributeValue
toValue forall a b. (a -> b) -> a -> b
$ [String] -> String
unwords [String]
classes)
  where  container :: Html
container = if FormatOptions -> Bool
numberLines FormatOptions
opts
                        then Html -> Html
H.table forall a b. (a -> b) -> a -> b
$ Html -> Html
H.tr forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
sourceCode forall a b. (a -> b) -> a -> b
$
                                 Html
nums forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Html
source
                        else Html
pre
         sourceCode :: AttributeValue
sourceCode = forall a. ToValue a => a -> AttributeValue
toValue String
"sourceCode"
         classes :: [String]
classes = String
"sourceCode" forall a. a -> [a] -> [a]
:
                   [String
x | String
x <- FormatOptions -> [String]
containerClasses FormatOptions
opts, String
x forall a. Eq a => a -> a -> Bool
/= String
"sourceCode"]
         pre :: Html
pre = FormatOptions -> [SourceLine] -> Html
formatHtmlBlockPre FormatOptions
opts [SourceLine]
ls
         source :: Html
source = Html -> Html
H.td forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
sourceCode forall a b. (a -> b) -> a -> b
$ Html
pre
         startNum :: Int
startNum = FormatOptions -> Int
startNumber FormatOptions
opts
         nums :: Html
nums = Html -> Html
H.td forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (forall a. ToValue a => a -> AttributeValue
toValue String
"lineNumbers")
                     forall a b. (a -> b) -> a -> b
$ Html -> Html
H.pre
                     forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall {a}. Show a => a -> Html
lineNum [Int
startNum..(Int
startNum forall a. Num a => a -> a -> a
+ forall (t :: * -> *) a. Foldable t => t a -> Int
length [SourceLine]
ls forall a. Num a => a -> a -> a
- Int
1)]
         lineNum :: a -> Html
lineNum a
n = if FormatOptions -> Bool
lineAnchors FormatOptions
opts
                        then (Html -> Html
H.a forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.id (forall a. ToValue a => a -> AttributeValue
toValue String
nStr) forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.href (forall a. ToValue a => a -> AttributeValue
toValue forall a b. (a -> b) -> a -> b
$ String
"#" forall a. [a] -> [a] -> [a]
++ String
nStr) forall a b. (a -> b) -> a -> b
$ forall a. ToMarkup a => a -> Html
toHtml forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show a
n)
                              forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall a. ToMarkup a => a -> Html
toHtml String
"\n"
                        else forall a. ToMarkup a => a -> Html
toHtml forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show a
n forall a. [a] -> [a] -> [a]
++ String
"\n"
           where nStr :: String
nStr = forall a. Show a => a -> String
show a
n

-- | Returns CSS for styling highlighted code according to the given style.
styleToCss :: Style -> String
styleToCss :: Style -> String
styleToCss Style
f = [String] -> String
unlines forall a b. (a -> b) -> a -> b
$ [String]
divspec forall a. [a] -> [a] -> [a]
++ [String]
tablespec forall a. [a] -> [a] -> [a]
++ [String]
colorspec forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (TokenType, TokenStyle) -> String
toCss (Style -> [(TokenType, TokenStyle)]
tokenStyles Style
f)
   where colorspec :: [String]
colorspec = case (Style -> Maybe Color
defaultColor Style
f, Style -> Maybe Color
backgroundColor Style
f) of
                          (Maybe Color
Nothing, Maybe Color
Nothing) -> []
                          (Just Color
c, Maybe Color
Nothing)  -> [String
"pre, code { color: " forall a. [a] -> [a] -> [a]
++ forall a. FromColor a => Color -> a
fromColor Color
c forall a. [a] -> [a] -> [a]
++ String
"; }"]
                          (Maybe Color
Nothing, Just Color
c)  -> [String
"pre, code { background-color: " forall a. [a] -> [a] -> [a]
++ forall a. FromColor a => Color -> a
fromColor Color
c forall a. [a] -> [a] -> [a]
++ String
"; }"]
                          (Just Color
c1, Just Color
c2) -> [String
"pre, code { color: " forall a. [a] -> [a] -> [a]
++ forall a. FromColor a => Color -> a
fromColor Color
c1 forall a. [a] -> [a] -> [a]
++ String
"; background-color: " forall a. [a] -> [a] -> [a]
++
                                                  forall a. FromColor a => Color -> a
fromColor Color
c2 forall a. [a] -> [a] -> [a]
++ String
"; }"]
         tablespec :: [String]
tablespec = [
           String
"table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {"
          ,String
"  margin: 0; padding: 0; vertical-align: baseline; border: none; }"
          ,String
"table.sourceCode { width: 100%; line-height: 100%; " forall a. [a] -> [a] -> [a]
++
             forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" (\Color
c -> String
"background-color: " forall a. [a] -> [a] -> [a]
++ forall a. FromColor a => Color -> a
fromColor Color
c forall a. [a] -> [a] -> [a]
++ String
"; ") (Style -> Maybe Color
backgroundColor Style
f) forall a. [a] -> [a] -> [a]
++
             forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" (\Color
c -> String
"color: " forall a. [a] -> [a] -> [a]
++ forall a. FromColor a => Color -> a
fromColor Color
c forall a. [a] -> [a] -> [a]
++ String
"; ") (Style -> Maybe Color
defaultColor Style
f) forall a. [a] -> [a] -> [a]
++
             String
"}"
          ,String
"td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; " forall a. [a] -> [a] -> [a]
++
             forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" (\Color
c -> String
"background-color: " forall a. [a] -> [a] -> [a]
++ forall a. FromColor a => Color -> a
fromColor Color
c forall a. [a] -> [a] -> [a]
++ String
"; ") (Style -> Maybe Color
lineNumberBackgroundColor Style
f) forall a. [a] -> [a] -> [a]
++
             forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" (\Color
c -> String
"color: " forall a. [a] -> [a] -> [a]
++ forall a. FromColor a => Color -> a
fromColor Color
c forall a. [a] -> [a] -> [a]
++ String
"; ") (Style -> Maybe Color
lineNumberColor Style
f) forall a. [a] -> [a] -> [a]
++
             forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" (\Color
c -> String
"border-right: 1px solid " forall a. [a] -> [a] -> [a]
++ forall a. FromColor a => Color -> a
fromColor Color
c forall a. [a] -> [a] -> [a]
++ String
"; ") (Style -> Maybe Color
lineNumberColor Style
f) forall a. [a] -> [a] -> [a]
++
             String
"}"
          ,String
"td.sourceCode { padding-left: 5px; }"
          ]
         divspec :: [String]
divspec = [ String
"div.sourceCode { overflow-x: auto; }" ]

toCss :: (TokenType, TokenStyle) -> String
toCss :: (TokenType, TokenStyle) -> String
toCss (TokenType
t,TokenStyle
tf) = String
"code > span." forall a. [a] -> [a] -> [a]
++ TokenType -> String
short TokenType
t forall a. [a] -> [a] -> [a]
++ String
" { "
                forall a. [a] -> [a] -> [a]
++ String
colorspec forall a. [a] -> [a] -> [a]
++ String
backgroundspec forall a. [a] -> [a] -> [a]
++ String
weightspec forall a. [a] -> [a] -> [a]
++ String
stylespec
                forall a. [a] -> [a] -> [a]
++ String
decorationspec forall a. [a] -> [a] -> [a]
++ String
"} /* " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
showTokenType TokenType
t forall a. [a] -> [a] -> [a]
++ String
" */"
  where colorspec :: String
colorspec = forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" (\Color
col -> String
"color: " forall a. [a] -> [a] -> [a]
++ forall a. FromColor a => Color -> a
fromColor Color
col forall a. [a] -> [a] -> [a]
++ String
"; ") forall a b. (a -> b) -> a -> b
$ TokenStyle -> Maybe Color
tokenColor TokenStyle
tf
        backgroundspec :: String
backgroundspec = forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" (\Color
col -> String
"background-color: " forall a. [a] -> [a] -> [a]
++ forall a. FromColor a => Color -> a
fromColor Color
col forall a. [a] -> [a] -> [a]
++ String
"; ") forall a b. (a -> b) -> a -> b
$ TokenStyle -> Maybe Color
tokenBackground TokenStyle
tf
        weightspec :: String
weightspec = if TokenStyle -> Bool
tokenBold TokenStyle
tf then String
"font-weight: bold; " else String
""
        stylespec :: String
stylespec  = if TokenStyle -> Bool
tokenItalic TokenStyle
tf then String
"font-style: italic; " else String
""
        decorationspec :: String
decorationspec = if TokenStyle -> Bool
tokenUnderline TokenStyle
tf then String
"text-decoration: underline; " else String
""
        showTokenType :: a -> String
showTokenType a
t = case forall a. [a] -> [a]
reverse (forall a. Show a => a -> String
show a
t) of
                             Char
'k':Char
'o':Char
'T':String
xs -> forall a. [a] -> [a]
reverse String
xs
                             String
_              -> String
""