hakyll/src/Hakyll/Web/Pandoc.hs

125 lines
4.3 KiB
Haskell
Raw Normal View History

2010-12-26 12:22:25 +00:00
-- | Module exporting pandoc bindings
--
module Hakyll.Web.Pandoc
( -- * The basic building blocks
readPandoc
2010-12-26 12:22:25 +00:00
, readPandocWith
, writePandoc
, writePandocWith
2010-12-30 09:02:25 +00:00
-- * Functions working on pages/compilers
, pageReadPandoc
, pageReadPandocWith
2011-11-22 07:39:44 +00:00
, pageReadPandocWithA
, pageRenderPandoc
, pageRenderPandocWith
-- * Default options
2011-02-12 14:21:26 +00:00
, defaultHakyllParserState
, defaultHakyllWriterOptions
2010-12-26 12:22:25 +00:00
) where
2010-12-30 09:02:25 +00:00
import Prelude hiding (id)
import Control.Applicative ((<$>))
2011-11-22 07:39:44 +00:00
import Control.Arrow ((>>>), (>>^), (&&&), (***))
2010-12-30 09:02:25 +00:00
import Control.Category (id)
2011-05-17 08:46:48 +00:00
import Data.Maybe (fromMaybe)
2010-12-26 12:22:25 +00:00
2011-02-12 14:21:26 +00:00
import Text.Pandoc
2010-12-26 12:22:25 +00:00
2010-12-29 21:59:38 +00:00
import Hakyll.Core.Compiler
2011-05-17 08:46:48 +00:00
import Hakyll.Core.Identifier
2011-11-22 07:39:44 +00:00
import Hakyll.Core.Util.Arrow
2011-03-30 15:37:56 +00:00
import Hakyll.Web.Pandoc.FileType
import Hakyll.Web.Page.Internal
2010-12-26 12:22:25 +00:00
-- | Read a string using pandoc, with the default options
--
2011-05-24 09:58:13 +00:00
readPandoc :: FileType -- ^ Determines how parsing happens
-> Maybe (Identifier a) -- ^ Optional, for better error messages
-> String -- ^ String to read
-> Pandoc -- ^ Resulting document
2011-02-12 14:21:26 +00:00
readPandoc = readPandocWith defaultHakyllParserState
2010-12-26 12:22:25 +00:00
-- | Read a string using pandoc, with the supplied options
--
2011-05-24 09:58:13 +00:00
readPandocWith :: ParserState -- ^ Parser options
-> FileType -- ^ Determines parsing method
-> Maybe (Identifier a) -- ^ Optional, for better error messages
-> String -- ^ String to read
-> Pandoc -- ^ Resulting document
2011-05-17 08:46:48 +00:00
readPandocWith state fileType' id' = case fileType' of
2011-02-12 14:21:26 +00:00
Html -> readHtml state
LaTeX -> readLaTeX state
2011-05-17 08:46:48 +00:00
LiterateHaskell t ->
readPandocWith state {stateLiterateHaskell = True} t id'
2011-02-12 14:21:26 +00:00
Markdown -> readMarkdown state
Rst -> readRST state
2010-12-26 12:22:25 +00:00
t -> error $
2011-05-17 08:46:48 +00:00
"Hakyll.Web.readPandocWith: I don't know how to read a file of the " ++
"type " ++ show t ++ fromMaybe "" (fmap ((" for: " ++) . show) id')
2010-12-26 12:22:25 +00:00
-- | Write a document (as HTML) using pandoc, with the default options
--
2010-12-28 10:12:45 +00:00
writePandoc :: Pandoc -- ^ Document to write
-> String -- ^ Resulting HTML
2011-02-12 14:21:26 +00:00
writePandoc = writePandocWith defaultHakyllWriterOptions
2010-12-26 12:22:25 +00:00
-- | Write a document (as HTML) using pandoc, with the supplied options
--
2011-02-12 14:21:26 +00:00
writePandocWith :: WriterOptions -- ^ Writer options for pandoc
-> Pandoc -- ^ Document to write
-> String -- ^ Resulting HTML
writePandocWith = writeHtmlString
2010-12-26 12:22:25 +00:00
-- | Read the resource using pandoc
--
2010-12-30 09:02:25 +00:00
pageReadPandoc :: Compiler (Page String) (Page Pandoc)
2011-02-12 14:21:26 +00:00
pageReadPandoc = pageReadPandocWith defaultHakyllParserState
2010-12-26 12:22:25 +00:00
-- | Read the resource using pandoc
--
2011-02-12 14:21:26 +00:00
pageReadPandocWith :: ParserState -> Compiler (Page String) (Page Pandoc)
2011-11-22 07:39:44 +00:00
pageReadPandocWith state = constA state &&& id >>> pageReadPandocWithA
-- | Read the resource using pandoc. This is a (rarely needed) variant, which
-- comes in very useful when the parser state is the result of some arrow.
--
pageReadPandocWithA :: Compiler (ParserState, Page String) (Page Pandoc)
pageReadPandocWithA =
id *** id &&& getIdentifier &&& getFileType >>^ pageReadPandocWithA'
2010-12-30 09:02:25 +00:00
where
2011-11-22 07:39:44 +00:00
pageReadPandocWithA' (s, (p, (i, t))) = readPandocWith s t (Just i) <$> p
2010-12-26 12:22:25 +00:00
-- | Render the resource using pandoc
--
2010-12-30 09:02:25 +00:00
pageRenderPandoc :: Compiler (Page String) (Page String)
2011-02-12 14:21:26 +00:00
pageRenderPandoc =
pageRenderPandocWith defaultHakyllParserState defaultHakyllWriterOptions
2010-12-26 12:22:25 +00:00
-- | Render the resource using pandoc
--
2011-02-12 14:21:26 +00:00
pageRenderPandocWith :: ParserState
-> WriterOptions
2010-12-30 09:02:25 +00:00
-> Compiler (Page String) (Page String)
pageRenderPandocWith state options =
2011-01-14 07:50:34 +00:00
pageReadPandocWith state >>^ fmap (writePandocWith options)
2010-12-26 12:22:25 +00:00
-- | The default reader options for pandoc parsing in hakyll
--
2011-02-12 14:21:26 +00:00
defaultHakyllParserState :: ParserState
defaultHakyllParserState = defaultParserState
2010-12-26 12:22:25 +00:00
{ -- The following option causes pandoc to read smart typography, a nice
-- and free bonus.
2011-02-12 14:21:26 +00:00
stateSmart = True
2010-12-26 12:22:25 +00:00
}
-- | The default writer options for pandoc rendering in hakyll
--
2011-02-12 14:21:26 +00:00
defaultHakyllWriterOptions :: WriterOptions
defaultHakyllWriterOptions = defaultWriterOptions
2010-12-26 12:22:25 +00:00
{ -- This option causes literate haskell to be written using '>' marks in
-- html, which I think is a good default.
2011-02-12 14:21:26 +00:00
writerLiterateHaskell = True
2010-12-26 12:22:25 +00:00
}