{-# LANGUAGE CPP #-}
{-# LANGUAGE NoOverloadedStrings #-}
module Skylighting.Format.HTML (
formatHtmlInline
, formatHtmlBlock
, styleToCss
) where
import Data.List (intersperse, sort)
import qualified Data.Map as Map
import qualified Data.Text as Text
import Skylighting.Types
import Text.Blaze.Html
import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributes as A
import Data.String (fromString)
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup
#endif
formatHtmlInline :: FormatOptions -> [SourceLine] -> Html
formatHtmlInline :: FormatOptions -> [SourceLine] -> Html
formatHtmlInline opts :: FormatOptions
opts = FormatOptions -> Html -> Html
wrapCode FormatOptions
opts
(Html -> Html) -> ([SourceLine] -> Html) -> [SourceLine] -> Html
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Html] -> Html
forall a. Monoid a => [a] -> a
mconcat ([Html] -> Html)
-> ([SourceLine] -> [Html]) -> [SourceLine] -> Html
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Html -> [Html] -> [Html]
forall a. a -> [a] -> [a]
intersperse ([Char] -> Html
forall a. ToMarkup a => a -> Html
toHtml "\n")
([Html] -> [Html])
-> ([SourceLine] -> [Html]) -> [SourceLine] -> [Html]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SourceLine -> Html) -> [SourceLine] -> [Html]
forall a b. (a -> b) -> [a] -> [b]
map ((Token -> Html) -> SourceLine -> Html
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (FormatOptions -> Token -> Html
tokenToHtml FormatOptions
opts))
formatHtmlBlock :: FormatOptions -> [SourceLine] -> Html
formatHtmlBlock :: FormatOptions -> [SourceLine] -> Html
formatHtmlBlock opts :: FormatOptions
opts ls :: [SourceLine]
ls =
Html -> Html
H.div (Html -> Html) -> Attribute -> Html -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ ([Char] -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue "sourceCode") (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$
Html -> Html
H.pre (Html -> Html) -> Attribute -> Html -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
Text.unwords [Text]
classes)
(Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ FormatOptions -> Html -> Html
wrapCode FormatOptions
opts
(Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ [Html] -> Html
forall a. Monoid a => [a] -> a
mconcat ([Html] -> Html) -> ([Html] -> [Html]) -> [Html] -> Html
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Html -> [Html] -> [Html]
forall a. a -> [a] -> [a]
intersperse ([Char] -> Html
forall a. ToMarkup a => a -> Html
toHtml "\n")
([Html] -> Html) -> [Html] -> Html
forall a b. (a -> b) -> a -> b
$ (LineNo -> SourceLine -> Html)
-> [LineNo] -> [SourceLine] -> [Html]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (FormatOptions -> LineNo -> SourceLine -> Html
sourceLineToHtml FormatOptions
opts) [LineNo
startNum..] [SourceLine]
ls
where classes :: [Text]
classes = [Char] -> Text
Text.pack "sourceCode" Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
:
[[Char] -> Text
Text.pack "numberSource" | FormatOptions -> Bool
numberLines FormatOptions
opts] [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++
[Text
x | Text
x <- FormatOptions -> [Text]
containerClasses FormatOptions
opts
, Text
x Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= [Char] -> Text
Text.pack "sourceCode"]
startNum :: LineNo
startNum = Int -> LineNo
LineNo (Int -> LineNo) -> Int -> LineNo
forall a b. (a -> b) -> a -> b
$ FormatOptions -> Int
startNumber FormatOptions
opts
wrapCode :: FormatOptions -> Html -> Html
wrapCode :: FormatOptions -> Html -> Html
wrapCode opts :: FormatOptions
opts h :: Html
h = Html -> Html
H.code (Html -> Html) -> Attribute -> Html -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
Text.unwords
([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ [Char] -> Text
Text.pack "sourceCode"
Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: FormatOptions -> [Text]
codeClasses FormatOptions
opts)
(Html -> Html) -> (Bool, Attribute) -> Html -> Html
forall h. Attributable h => h -> (Bool, Attribute) -> h
!? (Int
startZero Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= 0, AttributeValue -> Attribute
A.style ([Char] -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue [Char]
counterOverride))
(Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ Html
h
where counterOverride :: [Char]
counterOverride = "counter-reset: source-line " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> Int -> [Char]
forall a. Show a => a -> [Char]
show Int
startZero [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> ";"
startZero :: Int
startZero = FormatOptions -> Int
startNumber FormatOptions
opts Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1
sourceLineToHtml :: FormatOptions -> LineNo -> SourceLine -> Html
sourceLineToHtml :: FormatOptions -> LineNo -> SourceLine -> Html
sourceLineToHtml opts :: FormatOptions
opts lno :: LineNo
lno cont :: SourceLine
cont =
Html -> Html
H.span (Html -> Html) -> Attribute -> Html -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.id AttributeValue
lineNum
(Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ do
Html -> Html
H.a (Html -> Html) -> Attribute -> Html -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.href AttributeValue
lineRef
(Html -> Html) -> Attribute -> Html -> Html
forall h. Attributable h => h -> Attribute -> h
! (if FormatOptions -> Bool
numberLines FormatOptions
opts
then Attribute
forall a. Monoid a => a
mempty
else Tag -> AttributeValue -> Attribute
customAttribute ([Char] -> Tag
forall a. IsString a => [Char] -> a
fromString "aria-hidden")
([Char] -> AttributeValue
forall a. IsString a => [Char] -> a
fromString "true"))
(Html -> Html) -> Attribute -> Html -> Html
forall h. Attributable h => h -> Attribute -> h
! (if FormatOptions -> Bool
numberLines FormatOptions
opts
then Attribute
forall a. Monoid a => a
mempty
else Tag -> AttributeValue -> Attribute
customAttribute ([Char] -> Tag
forall a. IsString a => [Char] -> a
fromString "tabindex")
([Char] -> AttributeValue
forall a. IsString a => [Char] -> a
fromString "-1"))
(Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ Html
forall a. Monoid a => a
mempty
(Token -> Html) -> SourceLine -> Html
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (FormatOptions -> Token -> Html
tokenToHtml FormatOptions
opts) SourceLine
cont
where lineNum :: AttributeValue
lineNum = [Char] -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue [Char]
prefixedLineNo
lineRef :: AttributeValue
lineRef = [Char] -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue ('#'Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
:[Char]
prefixedLineNo)
prefixedLineNo :: [Char]
prefixedLineNo = Text -> [Char]
Text.unpack (FormatOptions -> Text
lineIdPrefix FormatOptions
opts) [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> Int -> [Char]
forall a. Show a => a -> [Char]
show (LineNo -> Int
lineNo LineNo
lno)
tokenToHtml :: FormatOptions -> Token -> Html
tokenToHtml :: FormatOptions -> Token -> Html
tokenToHtml _ (NormalTok, txt :: Text
txt) = Text -> Html
forall a. ToMarkup a => a -> Html
toHtml Text
txt
tokenToHtml opts :: FormatOptions
opts (toktype :: TokenType
toktype, txt :: Text
txt) =
if FormatOptions -> Bool
titleAttributes FormatOptions
opts
then Html
sp Html -> Attribute -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.title ([Char] -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue ([Char] -> AttributeValue) -> [Char] -> AttributeValue
forall a b. (a -> b) -> a -> b
$ TokenType -> [Char]
forall a. Show a => a -> [Char]
show TokenType
toktype)
else Html
sp
where sp :: Html
sp = Html -> Html
H.span (Html -> Html) -> Attribute -> Html -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ ([Char] -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue ([Char] -> AttributeValue) -> [Char] -> AttributeValue
forall a b. (a -> b) -> a -> b
$ TokenType -> [Char]
short TokenType
toktype) (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ Text -> Html
forall a. ToMarkup a => a -> Html
toHtml Text
txt
short :: TokenType -> String
short :: TokenType -> [Char]
short KeywordTok = "kw"
short DataTypeTok = "dt"
short DecValTok = "dv"
short BaseNTok = "bn"
short FloatTok = "fl"
short CharTok = "ch"
short StringTok = "st"
short CommentTok = "co"
short OtherTok = "ot"
short AlertTok = "al"
short FunctionTok = "fu"
short RegionMarkerTok = "re"
short ErrorTok = "er"
short ConstantTok = "cn"
short SpecialCharTok = "sc"
short VerbatimStringTok = "vs"
short SpecialStringTok = "ss"
short ImportTok = "im"
short DocumentationTok = "do"
short AnnotationTok = "an"
short CommentVarTok = "cv"
short VariableTok = "va"
short ControlFlowTok = "cf"
short OperatorTok = "op"
short BuiltInTok = "bu"
short ExtensionTok = "ex"
short PreprocessorTok = "pp"
short AttributeTok = "at"
short InformationTok = "in"
short WarningTok = "wa"
short NormalTok = ""
styleToCss :: Style -> String
styleToCss :: Style -> [Char]
styleToCss f :: Style
f = [[Char]] -> [Char]
unlines ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$
[[Char]]
divspec [[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ [[Char]]
numberspec [[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ [[Char]]
colorspec [[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ [[Char]]
linkspec [[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++
[[Char]] -> [[Char]]
forall a. Ord a => [a] -> [a]
sort (((TokenType, TokenStyle) -> [Char])
-> [(TokenType, TokenStyle)] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map (TokenType, TokenStyle) -> [Char]
toCss (Map TokenType TokenStyle -> [(TokenType, TokenStyle)]
forall k a. Map k a -> [(k, a)]
Map.toList (Style -> Map TokenType TokenStyle
tokenStyles Style
f)))
where colorspec :: [[Char]]
colorspec = [Char] -> [[Char]]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Char] -> [[Char]])
-> ([[Char]] -> [Char]) -> [[Char]] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> [Char]
unwords ([[Char]] -> [[Char]]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> a -> b
$ [
"div.sourceCode\n {"
, [Char] -> (Color -> [Char]) -> Maybe Color -> [Char]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe "" (\c :: Color
c -> "color: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Color -> [Char]
forall a. FromColor a => Color -> a
fromColor Color
c [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ ";") (Style -> Maybe Color
defaultColor Style
f)
, [Char] -> (Color -> [Char]) -> Maybe Color -> [Char]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe "" (\c :: Color
c -> "background-color: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Color -> [Char]
forall a. FromColor a => Color -> a
fromColor Color
c [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ ";") (Style -> Maybe Color
backgroundColor Style
f)
, "}"
]
numberspec :: [[Char]]
numberspec = [
"pre.numberSource code"
, " { counter-reset: source-line 0; }"
, "pre.numberSource code > span"
, " { position: relative; left: -4em; counter-increment: source-line; }"
, "pre.numberSource code > span > a:first-child::before"
, " { content: counter(source-line);"
, " position: relative; left: -1em; text-align: right; vertical-align: baseline;"
, " border: none; display: inline-block;"
, " -webkit-touch-callout: none; -webkit-user-select: none;"
, " -khtml-user-select: none; -moz-user-select: none;"
, " -ms-user-select: none; user-select: none;"
, " padding: 0 4px; width: 4em;"
, [Char] -> (Color -> [Char]) -> Maybe Color -> [Char]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe "" (\c :: Color
c -> " background-color: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Color -> [Char]
forall a. FromColor a => Color -> a
fromColor Color
c [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ ";\n")
(Style -> Maybe Color
lineNumberBackgroundColor Style
f) [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
[Char] -> (Color -> [Char]) -> Maybe Color -> [Char]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe "" (\c :: Color
c -> " color: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Color -> [Char]
forall a. FromColor a => Color -> a
fromColor Color
c [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ ";\n")
(Style -> Maybe Color
lineNumberColor Style
f) [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
" }"
, "pre.numberSource { margin-left: 3em; " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
[Char] -> (Color -> [Char]) -> Maybe Color -> [Char]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe "" (\c :: Color
c -> "border-left: 1px solid " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Color -> [Char]
forall a. FromColor a => Color -> a
fromColor Color
c [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ "; ") (Style -> Maybe Color
lineNumberColor Style
f) [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
" padding-left: 4px; }"
]
divspec :: [[Char]]
divspec = [
"pre > code.sourceCode { white-space: pre; position: relative; }"
, "pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }"
, "pre > code.sourceCode > span:empty { height: 1.2em; }"
, ".sourceCode { overflow: visible; }"
, "code.sourceCode > span { color: inherit; text-decoration: inherit; }"
, "div.sourceCode { margin: 1em 0; }"
, "pre.sourceCode { margin: 0; }"
, "@media screen {"
, "div.sourceCode { overflow: auto; }"
, "}"
, "@media print {"
, "pre > code.sourceCode { white-space: pre-wrap; }"
, "pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }"
, "}"
]
linkspec :: [[Char]]
linkspec = [ "@media screen {"
, "pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }"
, "}"
]
toCss :: (TokenType, TokenStyle) -> String
toCss :: (TokenType, TokenStyle) -> [Char]
toCss (t :: TokenType
t,tf :: TokenStyle
tf) = "code span" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ (if [Char] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (TokenType -> [Char]
short TokenType
t) then "" else ('.' Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: TokenType -> [Char]
short TokenType
t)) [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ " { "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
colorspec [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
backgroundspec [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
weightspec [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
stylespec
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
decorationspec [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ "} /* " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ TokenType -> [Char]
forall a. Show a => a -> [Char]
showTokenType TokenType
t [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ " */"
where colorspec :: [Char]
colorspec = [Char] -> (Color -> [Char]) -> Maybe Color -> [Char]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe "" (\col :: Color
col -> "color: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Color -> [Char]
forall a. FromColor a => Color -> a
fromColor Color
col [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ "; ") (Maybe Color -> [Char]) -> Maybe Color -> [Char]
forall a b. (a -> b) -> a -> b
$ TokenStyle -> Maybe Color
tokenColor TokenStyle
tf
backgroundspec :: [Char]
backgroundspec = [Char] -> (Color -> [Char]) -> Maybe Color -> [Char]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe "" (\col :: Color
col -> "background-color: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Color -> [Char]
forall a. FromColor a => Color -> a
fromColor Color
col [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ "; ") (Maybe Color -> [Char]) -> Maybe Color -> [Char]
forall a b. (a -> b) -> a -> b
$ TokenStyle -> Maybe Color
tokenBackground TokenStyle
tf
weightspec :: [Char]
weightspec = if TokenStyle -> Bool
tokenBold TokenStyle
tf then "font-weight: bold; " else ""
stylespec :: [Char]
stylespec = if TokenStyle -> Bool
tokenItalic TokenStyle
tf then "font-style: italic; " else ""
decorationspec :: [Char]
decorationspec = if TokenStyle -> Bool
tokenUnderline TokenStyle
tf then "text-decoration: underline; " else ""
showTokenType :: a -> [Char]
showTokenType t' :: a
t' = case [Char] -> [Char]
forall a. [a] -> [a]
reverse (a -> [Char]
forall a. Show a => a -> [Char]
show a
t') of
'k':'o':'T':xs :: [Char]
xs -> [Char] -> [Char]
forall a. [a] -> [a]
reverse [Char]
xs
_ -> ""