{-# LANGUAGE ExtendedDefaultRules #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
module Knit.Report.Output.Html
(
htmlWriterOptions
, toBlazeDocument
, pandocWriterToBlazeDocument
, mindocOptionsF
, markDownTextToBlazeFragment
)
where
import qualified Data.ByteString.Char8 as BS
import qualified Data.Text as T
import qualified Data.Map as M
import qualified Text.Blaze.Html as BH
import qualified Text.Pandoc as PA
import qualified Polysemy as P
import qualified Knit.Effect.Pandoc as PE
import qualified Knit.Effect.PandocMonad as PM
import Knit.Report.Input.MarkDown.PandocMarkDown
( markDownReaderOptions )
import Knit.Report.Output ( PandocWriterConfig(..) )
htmlWriterOptions :: PA.WriterOptions
htmlWriterOptions = PA.def
{ PA.writerExtensions = PA.extensionsFromList [PA.Ext_raw_html]
, PA.writerHTMLMathMethod = PA.MathJax ""
}
htmlFullDocWriterOptions
:: PA.PandocMonad m
=> Maybe FilePath
-> M.Map String String
-> m PA.WriterOptions
htmlFullDocWriterOptions pathM tVars = do
template <- case pathM of
Nothing -> PA.getDefaultTemplate "Html5"
Just fp -> do
exists <- PA.fileExists fp
if exists
then fmap BS.unpack (PA.readFileStrict fp)
else PA.logOutput (PA.IgnoredIOError ("Couldn't find " ++ show fp))
>> PA.getDefaultTemplate "Html5"
return $ htmlWriterOptions { PA.writerTemplate = Just template
, PA.writerVariables = M.toList tVars
, PA.writerSetextHeaders = True
}
markDownTextToBlazeFragment
:: PM.PandocEffects effs
=> T.Text
-> P.Sem effs BH.Html
markDownTextToBlazeFragment =
PE.fromPandocE PE.WriteHtml5 htmlWriterOptions
. PE.addFrom PE.ReadMarkDown markDownReaderOptions
toBlazeDocument
:: PM.PandocEffects effs
=> PandocWriterConfig
-> PE.PandocWithRequirements
-> P.Sem effs BH.Html
toBlazeDocument writeConfig pdocWR = do
writerOptions <- htmlFullDocWriterOptions (templateFP writeConfig)
(templateVars writeConfig)
PE.fromPandoc PE.WriteHtml5 (optionsF writeConfig writerOptions) pdocWR
pandocWriterToBlazeDocument
:: PM.PandocEffects effs
=> PandocWriterConfig
-> P.Sem (PE.ToPandoc ': effs) ()
-> P.Sem effs BH.Html
pandocWriterToBlazeDocument writeConfig pw =
PE.runPandocWriter pw >>= toBlazeDocument writeConfig
mindocOptionsF :: PA.WriterOptions -> PA.WriterOptions
mindocOptionsF op = op { PA.writerSectionDivs = True }