module Hakyllbars.Pandoc where

import Hakyll hiding (pandocCompilerWith)
import System.FilePath
import Text.Pandoc

-- | Compiles an item using the pandoc renderer with the default options if it isn't already HTML.
pandocCompiler :: Item String -> Compiler (Item String)
pandocCompiler :: Item String -> Compiler (Item String)
pandocCompiler = ReaderOptions
-> WriterOptions -> Item String -> Compiler (Item String)
pandocCompilerWith ReaderOptions
defaultHakyllReaderOptions WriterOptions
defaultHakyllWriterOptions

-- | Compiles an item using the pandoc renderer with the given options if it isn't already HTML.
pandocCompilerWith :: ReaderOptions -> WriterOptions -> Item String -> Compiler (Item String)
pandocCompilerWith :: ReaderOptions
-> WriterOptions -> Item String -> Compiler (Item String)
pandocCompilerWith ReaderOptions
readerOpts WriterOptions
writerOpts item :: Item String
item@(Item Identifier
id' String
_) = do
  let ext :: String
ext = String -> String
takeExtension forall a b. (a -> b) -> a -> b
$ Identifier -> String
toFilePath Identifier
id'
  String -> Compiler (Item String)
go String
ext
  where
    go :: String -> Compiler (Item String)
go String
".html" = forall (m :: * -> *) a. Monad m => a -> m a
return Item String
item
    go String
_ = ReaderOptions
-> WriterOptions -> Item String -> Compiler (Item String)
renderPandocWith ReaderOptions
readerOpts WriterOptions
writerOpts Item String
item