@@ -0,0 +1,98 @@
1+-- | Module exporting pandoc bindings
2+--
3+module Hakyll.Web.Pandoc
4+ ( readPandoc
5+ , readPandocWith
6+ , writePandoc
7+ , writePandocWith
8+ , targetReadPandoc
9+ , targetReadPandocWith
10+ , targetRenderPandoc
11+ , targetRenderPandocWith
12+ , defaultParserState
13+ , defaultWriterOptions
14+ ) where
15+
16+import Control.Applicative ((<$>), (<*>))
17+
18+import Text.Pandoc (Pandoc)
19+import qualified Text.Pandoc as P
20+
21+import Hakyll.Web.FileType
22+import Hakyll.Core.Target
23+
24+-- | Read a string using pandoc, with the default options
25+--
26+readPandoc :: FileType -- ^ File type, determines how parsing happens
27+ -> String -- ^ String to read
28+ -> Pandoc -- ^ Resulting document
29+readPandoc = readPandocWith defaultParserState
30+
31+-- | Read a string using pandoc, with the supplied options
32+--
33+readPandocWith :: P.ParserState -- ^ Parser options
34+ -> FileType -- ^ File type, determines how parsing happens
35+ -> String -- ^ String to read
36+ -> Pandoc -- ^ Resulting document
37+readPandocWith state fileType' = case fileType' of
38+ Html -> P.readHtml state
39+ LaTeX -> P.readLaTeX state
40+ LiterateHaskell t -> readPandocWith state {P.stateLiterateHaskell = True} t
41+ Markdown -> P.readMarkdown state
42+ Rst -> P.readRST state
43+ t -> error $
44+ "readPandoc: I don't know how to read " ++ show t
45+
46+-- | Write a document (as HTML) using pandoc, with the default options
47+--
48+writePandoc :: Pandoc -- ^ Document to write
49+ -> String -- ^ Resulting HTML
50+writePandoc = writePandocWith defaultWriterOptions
51+
52+-- | Write a document (as HTML) using pandoc, with the supplied options
53+--
54+writePandocWith :: P.WriterOptions -- ^ Writer options for pandoc
55+ -> Pandoc -- ^ Document to write
56+ -> String -- ^ Resulting HTML
57+writePandocWith = P.writeHtmlString
58+
59+-- | Read the resource using pandoc
60+--
61+targetReadPandoc :: TargetM a Pandoc
62+targetReadPandoc = targetReadPandocWith defaultParserState
63+
64+-- | Read the resource using pandoc
65+--
66+targetReadPandocWith :: P.ParserState -> TargetM a Pandoc
67+targetReadPandocWith state =
68+ readPandocWith state <$> getFileType <*> getResourceString
69+
70+-- | Render the resource using pandoc
71+--
72+targetRenderPandoc :: TargetM a String
73+targetRenderPandoc =
74+ targetRenderPandocWith defaultParserState defaultWriterOptions
75+
76+-- | Render the resource using pandoc
77+--
78+targetRenderPandocWith :: P.ParserState -> P.WriterOptions -> TargetM a String
79+targetRenderPandocWith state options =
80+ writePandocWith options <$> targetReadPandocWith state
81+
82+-- | The default reader options for pandoc parsing in hakyll
83+--
84+defaultParserState :: P.ParserState
85+defaultParserState = P.defaultParserState
86+ { -- The following option causes pandoc to read smart typography, a nice
87+ -- and free bonus.
88+ P.stateSmart = True
89+ }
90+
91+-- | The default writer options for pandoc rendering in hakyll
92+--
93+defaultWriterOptions :: P.WriterOptions
94+defaultWriterOptions = P.defaultWriterOptions
95+ { -- This option causes literate haskell to be written using '>' marks in
96+ -- html, which I think is a good default.
97+ P.writerLiterateHaskell = True
98+ }