2010-12-26 12:22:25 +00:00
|
|
|
-- | Module exporting pandoc bindings
|
|
|
|
--
|
|
|
|
module Hakyll.Web.Pandoc
|
2010-12-27 10:14:04 +00:00
|
|
|
( -- * The basic building blocks
|
|
|
|
readPandoc
|
2010-12-26 12:22:25 +00:00
|
|
|
, readPandocWith
|
|
|
|
, writePandoc
|
|
|
|
, writePandocWith
|
2010-12-27 10:14:04 +00:00
|
|
|
|
2010-12-30 09:02:25 +00:00
|
|
|
-- * Functions working on pages/compilers
|
2010-12-27 10:14:04 +00:00
|
|
|
, pageReadPandoc
|
|
|
|
, pageReadPandocWith
|
|
|
|
, pageRenderPandoc
|
|
|
|
, pageRenderPandocWith
|
|
|
|
|
|
|
|
-- * Default options
|
2010-12-26 12:22:25 +00:00
|
|
|
, defaultParserState
|
|
|
|
, defaultWriterOptions
|
|
|
|
) where
|
|
|
|
|
2010-12-30 09:02:25 +00:00
|
|
|
import Prelude hiding (id)
|
2010-12-27 10:14:04 +00:00
|
|
|
import Control.Applicative ((<$>))
|
2010-12-30 09:11:37 +00:00
|
|
|
import Control.Arrow ((>>^), (&&&))
|
2010-12-30 09:02:25 +00:00
|
|
|
import Control.Category (id)
|
2010-12-26 12:22:25 +00:00
|
|
|
|
|
|
|
import Text.Pandoc (Pandoc)
|
|
|
|
import qualified Text.Pandoc as P
|
|
|
|
|
2010-12-29 21:59:38 +00:00
|
|
|
import Hakyll.Core.Compiler
|
2010-12-27 10:14:04 +00:00
|
|
|
import Hakyll.Web.FileType
|
|
|
|
import Hakyll.Web.Page
|
2010-12-26 12:22:25 +00:00
|
|
|
|
|
|
|
-- | Read a string using pandoc, with the default options
|
|
|
|
--
|
2010-12-28 10:12:45 +00:00
|
|
|
readPandoc :: FileType -- ^ File type, determines how parsing happens
|
|
|
|
-> String -- ^ String to read
|
|
|
|
-> Pandoc -- ^ Resulting document
|
2010-12-26 12:22:25 +00:00
|
|
|
readPandoc = readPandocWith defaultParserState
|
|
|
|
|
|
|
|
-- | Read a string using pandoc, with the supplied options
|
|
|
|
--
|
|
|
|
readPandocWith :: P.ParserState -- ^ Parser options
|
|
|
|
-> FileType -- ^ File type, determines how parsing happens
|
|
|
|
-> String -- ^ String to read
|
|
|
|
-> Pandoc -- ^ Resulting document
|
|
|
|
readPandocWith state fileType' = case fileType' of
|
|
|
|
Html -> P.readHtml state
|
|
|
|
LaTeX -> P.readLaTeX state
|
|
|
|
LiterateHaskell t -> readPandocWith state {P.stateLiterateHaskell = True} t
|
|
|
|
Markdown -> P.readMarkdown state
|
|
|
|
Rst -> P.readRST state
|
|
|
|
t -> error $
|
2011-01-08 08:09:11 +00:00
|
|
|
"Hakyll.Web.readPandocWith: I don't know how to read " ++ show t
|
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
|
2010-12-26 12:22:25 +00:00
|
|
|
writePandoc = writePandocWith defaultWriterOptions
|
|
|
|
|
|
|
|
-- | Write a document (as HTML) using pandoc, with the supplied options
|
|
|
|
--
|
|
|
|
writePandocWith :: P.WriterOptions -- ^ Writer options for pandoc
|
|
|
|
-> Pandoc -- ^ Document to write
|
|
|
|
-> String -- ^ Resulting HTML
|
|
|
|
writePandocWith = P.writeHtmlString
|
|
|
|
|
|
|
|
-- | Read the resource using pandoc
|
|
|
|
--
|
2010-12-30 09:02:25 +00:00
|
|
|
pageReadPandoc :: Compiler (Page String) (Page Pandoc)
|
2010-12-27 10:14:04 +00:00
|
|
|
pageReadPandoc = pageReadPandocWith defaultParserState
|
2010-12-26 12:22:25 +00:00
|
|
|
|
|
|
|
-- | Read the resource using pandoc
|
|
|
|
--
|
2010-12-30 09:02:25 +00:00
|
|
|
pageReadPandocWith :: P.ParserState -> Compiler (Page String) (Page Pandoc)
|
|
|
|
pageReadPandocWith state =
|
2010-12-30 09:11:37 +00:00
|
|
|
id &&& getFileType >>^ pageReadPandocWith'
|
2010-12-30 09:02:25 +00:00
|
|
|
where
|
|
|
|
pageReadPandocWith' (p, t) = readPandocWith state t <$> 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)
|
2010-12-27 10:14:04 +00:00
|
|
|
pageRenderPandoc = pageRenderPandocWith defaultParserState defaultWriterOptions
|
2010-12-26 12:22:25 +00:00
|
|
|
|
|
|
|
-- | Render the resource using pandoc
|
|
|
|
--
|
2010-12-27 10:14:04 +00:00
|
|
|
pageRenderPandocWith :: P.ParserState
|
|
|
|
-> P.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
|
|
|
|
--
|
|
|
|
defaultParserState :: P.ParserState
|
|
|
|
defaultParserState = P.defaultParserState
|
|
|
|
{ -- The following option causes pandoc to read smart typography, a nice
|
|
|
|
-- and free bonus.
|
|
|
|
P.stateSmart = True
|
|
|
|
}
|
|
|
|
|
|
|
|
-- | The default writer options for pandoc rendering in hakyll
|
|
|
|
--
|
|
|
|
defaultWriterOptions :: P.WriterOptions
|
|
|
|
defaultWriterOptions = P.defaultWriterOptions
|
|
|
|
{ -- This option causes literate haskell to be written using '>' marks in
|
|
|
|
-- html, which I think is a good default.
|
|
|
|
P.writerLiterateHaskell = True
|
|
|
|
}
|