summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web/Pandoc.hs
blob: 8b0e0dea1396d50295fbac66370ddea0b4cd73d2 (plain)
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
-- | Module exporting pandoc bindings
--
module Hakyll.Web.Pandoc
    ( -- * The basic building blocks
      readPandoc
    , readPandocWith
    , writePandoc
    , writePandocWith

      -- * Functions working on pages/compilers
    , pageReadPandoc
    , pageReadPandocWith
    , pageReadPandocWithA
    , pageRenderPandoc
    , pageRenderPandocWith

      -- * Default options
    , defaultHakyllParserState
    , defaultHakyllWriterOptions
    ) where

import Prelude hiding (id)
import Control.Applicative ((<$>))
import Control.Arrow ((>>>), (>>^), (&&&), (***))
import Control.Category (id)
import Data.Maybe (fromMaybe)

import Text.Pandoc

import Hakyll.Core.Compiler
import Hakyll.Core.Identifier
import Hakyll.Core.Util.Arrow
import Hakyll.Web.Pandoc.FileType
import Hakyll.Web.Page.Internal

-- | Read a string using pandoc, with the default options
--
readPandoc :: FileType              -- ^ Determines how parsing happens
           -> Maybe (Identifier a)  -- ^ Optional, for better error messages
           -> String                -- ^ String to read
           -> Pandoc                -- ^ Resulting document
readPandoc = readPandocWith defaultHakyllParserState

-- | Read a string using pandoc, with the supplied options
--
readPandocWith :: ParserState           -- ^ Parser options
               -> FileType              -- ^ Determines parsing method
               -> Maybe (Identifier a)  -- ^ Optional, for better error messages
               -> String                -- ^ String to read
               -> Pandoc                -- ^ Resulting document
readPandocWith state fileType' id' = case fileType' of
    Html              -> readHtml state
    LaTeX             -> readLaTeX state
    LiterateHaskell t ->
        readPandocWith state {stateLiterateHaskell = True} t id'
    Markdown          -> readMarkdown state
    Rst               -> readRST state
    Textile           -> readTextile state
    t                 -> error $
        "Hakyll.Web.readPandocWith: I don't know how to read a file of the " ++
        "type " ++ show t ++ fromMaybe "" (fmap ((" for: " ++) . show) id')

-- | Write a document (as HTML) using pandoc, with the default options
--
writePandoc :: Pandoc  -- ^ Document to write
            -> String  -- ^ Resulting HTML
writePandoc = writePandocWith defaultHakyllWriterOptions

-- | Write a document (as HTML) using pandoc, with the supplied options
--
writePandocWith :: WriterOptions  -- ^ Writer options for pandoc
                -> Pandoc         -- ^ Document to write
                -> String         -- ^ Resulting HTML
writePandocWith = writeHtmlString

-- | Read the resource using pandoc
--
pageReadPandoc :: Compiler (Page String) (Page Pandoc)
pageReadPandoc = pageReadPandocWith defaultHakyllParserState

-- | Read the resource using pandoc
--
pageReadPandocWith :: ParserState -> Compiler (Page String) (Page Pandoc)
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'
  where
    pageReadPandocWithA' (s, (p, (i, t))) = readPandocWith s t (Just i) <$> p

-- | Render the resource using pandoc
--
pageRenderPandoc :: Compiler (Page String) (Page String)
pageRenderPandoc =
    pageRenderPandocWith defaultHakyllParserState defaultHakyllWriterOptions

-- | Render the resource using pandoc
--
pageRenderPandocWith :: ParserState
                     -> WriterOptions
                     -> Compiler (Page String) (Page String)
pageRenderPandocWith state options =
    pageReadPandocWith state >>^ fmap (writePandocWith options)

-- | The default reader options for pandoc parsing in hakyll
--
defaultHakyllParserState :: ParserState
defaultHakyllParserState = defaultParserState
    { -- The following option causes pandoc to read smart typography, a nice
      -- and free bonus.
      stateSmart = True
    }

-- | The default writer options for pandoc rendering in hakyll
--
defaultHakyllWriterOptions :: WriterOptions
defaultHakyllWriterOptions = defaultWriterOptions
    { -- This option causes literate haskell to be written using '>' marks in
      -- html, which I think is a good default.
      writerLiterateHaskell = True
    }