module CMark.Highlight
(
highlight,
highlightWith,
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
highlight :: Node -> Node
highlight = highlightWith (\_ _ x -> x)
highlightWith
:: (Text -> Text -> FormatOptions -> FormatOptions) -> Node -> Node
highlightWith f (Node pos (CODE_BLOCK info code) ns) =
Node pos (HTML_BLOCK formatted) (map (highlightWith 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
highlightWith f (Node pos type_ ns) =
Node pos type_ (map (highlightWith f) ns)