module CMark.Highlight
(
highlightNode,
highlightNodeWith,
FormatOptions(..),
defaultFormatOpts,
module Text.Highlighting.Kate.Styles,
styleToCss,
)
where
import qualified Data.Text as T
import Data.Text (Text)
import qualified Data.Text.Lazy as TL
import CMark
import Text.Highlighting.Kate
import Text.Highlighting.Kate.Styles
import Text.Blaze.Html.Renderer.Text
highlightNode :: Node -> Node
highlightNode = highlightNodeWith (\_ _ x -> x)
highlightNodeWith
:: (Text -> Text -> FormatOptions -> FormatOptions) -> Node -> Node
highlightNodeWith f (Node pos (CODE_BLOCK info code) ns) =
Node pos (HTML_BLOCK formatted) (map (highlightNodeWith f) ns)
where
(codeLang, T.drop 1 -> codeInfo) = T.break (== ' ') (T.strip info)
highlighted = highlightAs (T.unpack codeLang) (T.unpack code)
opts = defaultFormatOpts {
containerClasses = words (T.unpack codeInfo) }
formatted = TL.toStrict . renderHtml $
formatHtmlBlock (f codeLang codeInfo opts) highlighted
highlightNodeWith f (Node pos type_ ns) =
Node pos type_ (map (highlightNodeWith f) ns)