1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
--------------------------------------------------------------------------------
-- | Module exporting convenient pandoc bindings
module Hakyll.Web.Pandoc
( -- * The basic building blocks
readPandoc
, readPandocWith
, writePandoc
, writePandocWith
, renderPandoc
, renderPandocWith
, renderPandocWithTransform
, renderPandocWithTransformM
-- * Derived compilers
, pandocCompiler
, pandocCompilerWith
, pandocCompilerWithTransform
, pandocCompilerWithTransformM
-- * Default options
, defaultHakyllReaderOptions
, defaultHakyllWriterOptions
) where
--------------------------------------------------------------------------------
import qualified Data.Text as T
import Text.Pandoc
import Text.Pandoc.Highlighting (pygments)
--------------------------------------------------------------------------------
import Hakyll.Core.Compiler
import Hakyll.Core.Item
import Hakyll.Web.Pandoc.FileType
--------------------------------------------------------------------------------
-- | Read a string using pandoc, with the default options
readPandoc
:: Item String -- ^ String to read
-> Compiler (Item Pandoc) -- ^ Resulting document
readPandoc = readPandocWith defaultHakyllReaderOptions
--------------------------------------------------------------------------------
-- | Read a string using pandoc, with the supplied options
readPandocWith
:: ReaderOptions -- ^ Parser options
-> Item String -- ^ String to read
-> Compiler (Item Pandoc) -- ^ Resulting document
readPandocWith ropt item =
case runPure $ traverse (reader ropt (itemFileType item)) (fmap T.pack item) of
Left err -> fail $
"Hakyll.Web.Pandoc.readPandocWith: parse failed: " ++ show err
Right item' -> return item'
where
reader ro t = case t of
DocBook -> readDocBook ro
Html -> readHtml ro
Jupyter -> readIpynb ro
LaTeX -> readLaTeX ro
LiterateHaskell t' -> reader (addExt ro Ext_literate_haskell) t'
Markdown -> readMarkdown ro
MediaWiki -> readMediaWiki ro
OrgMode -> readOrg ro
Rst -> readRST ro
Textile -> readTextile ro
_ -> error $
"Hakyll.Web.readPandocWith: I don't know how to read a file of " ++
"the type " ++ show t ++ " for: " ++ show (itemIdentifier item)
addExt ro e = ro {readerExtensions = enableExtension e $ readerExtensions ro}
--------------------------------------------------------------------------------
-- | Write a document (as HTML) using pandoc, with the default options
writePandoc :: Item Pandoc -- ^ Document to write
-> Item String -- ^ Resulting HTML
writePandoc = writePandocWith defaultHakyllWriterOptions
--------------------------------------------------------------------------------
-- | Write a document (as HTML) using pandoc, with the supplied options
writePandocWith :: WriterOptions -- ^ Writer options for pandoc
-> Item Pandoc -- ^ Document to write
-> Item String -- ^ Resulting HTML
writePandocWith wopt (Item itemi doc) =
case runPure $ writeHtml5String wopt doc of
Left err -> error $ "Hakyll.Web.Pandoc.writePandocWith: " ++ show err
Right item' -> Item itemi $ T.unpack item'
--------------------------------------------------------------------------------
-- | Render the resource using pandoc
renderPandoc :: Item String -> Compiler (Item String)
renderPandoc =
renderPandocWith defaultHakyllReaderOptions defaultHakyllWriterOptions
--------------------------------------------------------------------------------
-- | Render the resource using pandoc
renderPandocWith
:: ReaderOptions -> WriterOptions -> Item String -> Compiler (Item String)
renderPandocWith ropt wopt item =
writePandocWith wopt <$> readPandocWith ropt item
--------------------------------------------------------------------------------
-- | An extension of `renderPandocWith`, which allows you to specify a custom
-- Pandoc transformation on the input `Item`.
-- Useful if you want to do your own transformations before running
-- custom Pandoc transformations, e.g. using a `funcField` to transform raw content.
renderPandocWithTransform :: ReaderOptions -> WriterOptions
-> (Pandoc -> Pandoc)
-> Item String
-> Compiler (Item String)
renderPandocWithTransform ropt wopt f =
renderPandocWithTransformM ropt wopt (return . f)
--------------------------------------------------------------------------------
-- | Similar to `renderPandocWithTransform`, but the Pandoc transformation is
-- monadic. This is useful when you want the pandoc
-- transformation to use the `Compiler` information such as routes,
-- metadata, etc. along with your own transformations beforehand.
renderPandocWithTransformM :: ReaderOptions -> WriterOptions
-> (Pandoc -> Compiler Pandoc)
-> Item String
-> Compiler (Item String)
renderPandocWithTransformM ropt wopt f i =
writePandocWith wopt <$> (traverse f =<< readPandocWith ropt i)
--------------------------------------------------------------------------------
-- | Read a page render using pandoc
pandocCompiler :: Compiler (Item String)
pandocCompiler =
pandocCompilerWith defaultHakyllReaderOptions defaultHakyllWriterOptions
--------------------------------------------------------------------------------
-- | A version of 'pandocCompiler' which allows you to specify your own pandoc
-- options
pandocCompilerWith :: ReaderOptions -> WriterOptions -> Compiler (Item String)
pandocCompilerWith ropt wopt =
cached "Hakyll.Web.Pandoc.pandocCompilerWith" $
pandocCompilerWithTransform ropt wopt id
--------------------------------------------------------------------------------
-- | An extension of 'pandocCompilerWith' which allows you to specify a custom
-- pandoc transformation for the content
pandocCompilerWithTransform :: ReaderOptions -> WriterOptions
-> (Pandoc -> Pandoc)
-> Compiler (Item String)
pandocCompilerWithTransform ropt wopt f =
pandocCompilerWithTransformM ropt wopt (return . f)
--------------------------------------------------------------------------------
-- | Similar to 'pandocCompilerWithTransform', but the transformation
-- function is monadic. This is useful when you want the pandoc
-- transformation to use the 'Compiler' information such as routes,
-- metadata, etc
pandocCompilerWithTransformM :: ReaderOptions -> WriterOptions
-> (Pandoc -> Compiler Pandoc)
-> Compiler (Item String)
pandocCompilerWithTransformM ropt wopt f =
getResourceBody >>= renderPandocWithTransformM ropt wopt f
--------------------------------------------------------------------------------
-- | The default reader options for pandoc parsing in hakyll
defaultHakyllReaderOptions :: ReaderOptions
defaultHakyllReaderOptions = def
{ -- The following option causes pandoc to read smart typography, a nice
-- and free bonus.
readerExtensions = enableExtension Ext_smart pandocExtensions
}
--------------------------------------------------------------------------------
-- | The default writer options for pandoc rendering in hakyll
defaultHakyllWriterOptions :: WriterOptions
defaultHakyllWriterOptions = def
{ -- This option causes literate haskell to be written using '>' marks in
-- html, which I think is a good default.
writerExtensions = enableExtension Ext_smart pandocExtensions
, -- We want to have hightlighting by default, to be compatible with earlier
-- Hakyll releases
writerHighlightStyle = Just pygments
, -- Do not word-wrap produced HTML, and do not undo any word-wrapping
-- that's already present in the markup. This is how Pandoc operated
-- prior to 2.17, but the behaviour was changed for consistency with
-- other Pandoc writers. We retain the old behaviour because it spares us
-- the trouble of updating our golden tests.
writerWrapText = WrapPreserve
}
|