{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Text.MMark.Extension.GhcSyntaxHighlighter
( ghcSyntaxHighlighter,
)
where
import Data.Text (Text)
import qualified Data.Text as T
import GHC.SyntaxHighlighter
import Lucid
import Text.MMark.Extension (Block (..), Extension)
import qualified Text.MMark.Extension as Ext
ghcSyntaxHighlighter :: Extension
ghcSyntaxHighlighter :: Extension
ghcSyntaxHighlighter = ((Block (Ois, Html ()) -> Html ())
-> Block (Ois, Html ()) -> Html ())
-> Extension
Ext.blockRender (((Block (Ois, Html ()) -> Html ())
-> Block (Ois, Html ()) -> Html ())
-> Extension)
-> ((Block (Ois, Html ()) -> Html ())
-> Block (Ois, Html ()) -> Html ())
-> Extension
forall a b. (a -> b) -> a -> b
$ \Block (Ois, Html ()) -> Html ()
old Block (Ois, Html ())
block ->
case Block (Ois, Html ())
block of
cb :: Block (Ois, Html ())
cb@(CodeBlock (Just Text
"haskell") Text
txt) ->
case Text -> Maybe [(Token, Text)]
tokenizeHaskell Text
txt of
Maybe [(Token, Text)]
Nothing -> Block (Ois, Html ()) -> Html ()
old Block (Ois, Html ())
cb
Just [(Token, Text)]
toks -> do
[Attribute] -> Html () -> Html ()
forall arg result. Term arg result => arg -> result
div_ [Text -> Attribute
class_ Text
"source-code"]
(Html () -> Html ()) -> (Html () -> Html ()) -> Html () -> Html ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Html () -> Html ()
forall arg result. Term arg result => arg -> result
pre_
(Html () -> Html ()) -> (Html () -> Html ()) -> Html () -> Html ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Attribute] -> Html () -> Html ()
forall arg result. Term arg result => arg -> result
code_ [Text -> Attribute
class_ Text
"language-haskell"]
(Html () -> Html ()) -> Html () -> Html ()
forall a b. (a -> b) -> a -> b
$ ((Token, Text) -> Html ()) -> [(Token, Text)] -> Html ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Token, Text) -> Html ()
tokenToHtml [(Token, Text)]
toks
Html ()
newline
Block (Ois, Html ())
other -> Block (Ois, Html ()) -> Html ()
old Block (Ois, Html ())
other
where
newline :: Html ()
newline :: Html ()
newline = Html ()
"\n"
tokenToHtml :: (Token, Text) -> Html ()
tokenToHtml :: (Token, Text) -> Html ()
tokenToHtml (Token
tokenType, Text
txt) =
[Attribute] -> Html () -> Html ()
forall arg result. Term arg result => arg -> result
span_ [Text -> Attribute
class_ Text
rawClass | Bool -> Bool
not (Text -> Bool
T.null Text
rawClass)] (Text -> Html ()
forall a (m :: * -> *). (ToHtml a, Monad m) => a -> HtmlT m ()
toHtml Text
txt)
where
rawClass :: Text
rawClass = Token -> Text
tokenClass Token
tokenType
tokenClass :: Token -> Text
tokenClass :: Token -> Text
tokenClass = \case
Token
KeywordTok -> Text
"kw"
Token
PragmaTok -> Text
"pr"
Token
SymbolTok -> Text
"sy"
Token
VariableTok -> Text
"va"
Token
ConstructorTok -> Text
"cr"
Token
OperatorTok -> Text
"op"
Token
CharTok -> Text
"ch"
Token
StringTok -> Text
"st"
Token
IntegerTok -> Text
"it"
Token
RationalTok -> Text
"ra"
Token
CommentTok -> Text
"co"
Token
SpaceTok -> Text
""
Token
OtherTok -> Text
"ot"