git.iko.soy/typeable/pandoc
on 1.9.0.2 help sign in
260 lines 9.8 kB history raw
1{-# LANGUAGE ScopedTypeVariables, FlexibleInstances #-}
2{-
3Copyright (C) 2006-2010 John MacFarlane <jgm@berkeley.edu>
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2 of the License, or
8(at your option) any later version.
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18-}
20{- |
21 Module : Text.Pandoc
22 Copyright : Copyright (C) 2006-2010 John MacFarlane
23 License : GNU GPL, version 2 or above
25 Maintainer : John MacFarlane <jgm@berkeley.edu>
26 Stability : alpha
27 Portability : portable
29This helper module exports the main writers, readers, and data
30structure definitions from the Pandoc libraries.
32A typical application will chain together a reader and a writer
33to convert strings from one format to another. For example, the
34following simple program will act as a filter converting markdown
35fragments to reStructuredText, using reference-style links instead of
36inline links:
38> module Main where
39> import Text.Pandoc
40> -- include the following two lines only if you're using ghc < 6.12:
41> import Prelude hiding (getContents, putStrLn)
42> import System.IO.UTF8
44> markdownToRST :: String -> String
45> markdownToRST =
46> (writeRST defaultWriterOptions {writerReferenceLinks = True}) .
47> readMarkdown defaultParserState
48>
49> main = getContents >>= putStrLn . markdownToRST
51Note: all of the readers assume that the input text has @'\n'@
52line endings. So if you get your input text from a web form,
53you should remove @'\r'@ characters using @filter (/='\r')@.
55-}
57module Text.Pandoc
58 (
59 -- * Definitions
60 module Text.Pandoc.Definition
61 -- * Generics
62 , module Text.Pandoc.Generic
63 -- * Lists of readers and writers
64 , readers
65 , writers
66 -- * Readers: converting /to/ Pandoc format
67 , readMarkdown
68 , readRST
69 , readLaTeX
70 , readHtml
71 , readTextile
72 , readNative
73 -- * Parser state used in readers
74 , ParserState (..)
75 , defaultParserState
76 , ParserContext (..)
77 , QuoteContext (..)
78 , KeyTable
79 , NoteTable
80 , HeaderType (..)
81 -- * Writers: converting /from/ Pandoc format
82 , writeNative
83 , writeMarkdown
84 , writePlain
85 , writeRST
86 , writeLaTeX
87 , writeConTeXt
88 , writeTexinfo
89 , writeHtml
90 , writeHtmlString
91 , writeDocbook
92 , writeOpenDocument
93 , writeMan
94 , writeMediaWiki
95 , writeTextile
96 , writeRTF
97 , writeODT
98 , writeDocx
99 , writeEPUB
100 , writeOrg
101 , writeAsciiDoc
102 -- * Writer options used in writers
103 , WriterOptions (..)
104 , HTMLSlideVariant (..)
105 , HTMLMathMethod (..)
106 , CiteMethod (..)
107 , defaultWriterOptions
108 -- * Rendering templates and default templates
109 , module Text.Pandoc.Templates
110 -- * Version
111 , pandocVersion
112 -- * Miscellaneous
113 , rtfEmbedImage
114 , jsonFilter
115 , ToJsonFilter(..)
116 ) where
118import Text.Pandoc.Definition
119import Text.Pandoc.Generic
120import Text.Pandoc.Readers.Markdown
121import Text.Pandoc.Readers.RST
122import Text.Pandoc.Readers.LaTeX
123import Text.Pandoc.Readers.HTML
124import Text.Pandoc.Readers.Textile
125import Text.Pandoc.Readers.Native
126import Text.Pandoc.Writers.Native
127import Text.Pandoc.Writers.Markdown
128import Text.Pandoc.Writers.RST
129import Text.Pandoc.Writers.LaTeX
130import Text.Pandoc.Writers.ConTeXt
131import Text.Pandoc.Writers.Texinfo
132import Text.Pandoc.Writers.HTML
133import Text.Pandoc.Writers.ODT
134import Text.Pandoc.Writers.Docx
135import Text.Pandoc.Writers.EPUB
136import Text.Pandoc.Writers.Docbook
137import Text.Pandoc.Writers.OpenDocument
138import Text.Pandoc.Writers.Man
139import Text.Pandoc.Writers.RTF
140import Text.Pandoc.Writers.MediaWiki
141import Text.Pandoc.Writers.Textile
142import Text.Pandoc.Writers.Org
143import Text.Pandoc.Writers.AsciiDoc
144import Text.Pandoc.Templates
145import Text.Pandoc.Parsing
146import Text.Pandoc.Shared
147import Data.Version (showVersion)
148import Text.JSON.Generic
149import Paths_pandoc (version)
151-- | Version number of pandoc library.
152pandocVersion :: String
153pandocVersion = showVersion version
155-- | Association list of formats and readers.
156readers :: [(String, ParserState -> String -> Pandoc)]
157readers = [("native" , \_ -> readNative)
158 ,("json" , \_ -> decodeJSON)
159 ,("markdown" , readMarkdown)
160 ,("markdown+lhs" , \st ->
161 readMarkdown st{ stateLiterateHaskell = True})
162 ,("rst" , readRST)
163 ,("rst+lhs" , \st ->
164 readRST st{ stateLiterateHaskell = True})
165 ,("textile" , readTextile) -- TODO : textile+lhs
166 ,("html" , readHtml)
167 ,("latex" , readLaTeX)
168 ,("latex+lhs" , \st ->
169 readLaTeX st{ stateLiterateHaskell = True})
170 ]
172-- | Association list of formats and writers (omitting the
173-- binary writers, odt, docx, and epub).
174writers :: [ ( String, WriterOptions -> Pandoc -> String ) ]
175writers = [("native" , writeNative)
176 ,("json" , \_ -> encodeJSON)
177 ,("html" , writeHtmlString)
178 ,("html5" , \o ->
179 writeHtmlString o{ writerHtml5 = True })
180 ,("html+lhs" , \o ->
181 writeHtmlString o{ writerLiterateHaskell = True })
182 ,("html5+lhs" , \o ->
183 writeHtmlString o{ writerLiterateHaskell = True,
184 writerHtml5 = True })
185 ,("s5" , writeHtmlString)
186 ,("slidy" , writeHtmlString)
187 ,("dzslides" , writeHtmlString)
188 ,("docbook" , writeDocbook)
189 ,("opendocument" , writeOpenDocument)
190 ,("latex" , writeLaTeX)
191 ,("latex+lhs" , \o ->
192 writeLaTeX o{ writerLiterateHaskell = True })
193 ,("beamer" , \o ->
194 writeLaTeX o{ writerBeamer = True })
195 ,("context" , writeConTeXt)
196 ,("texinfo" , writeTexinfo)
197 ,("man" , writeMan)
198 ,("markdown" , writeMarkdown)
199 ,("markdown+lhs" , \o ->
200 writeMarkdown o{ writerLiterateHaskell = True })
201 ,("plain" , writePlain)
202 ,("rst" , writeRST)
203 ,("rst+lhs" , \o ->
204 writeRST o{ writerLiterateHaskell = True })
205 ,("mediawiki" , writeMediaWiki)
206 ,("textile" , writeTextile)
207 ,("rtf" , writeRTF)
208 ,("org" , writeOrg)
209 ,("asciidoc" , writeAsciiDoc)
210 ]
212{-# DEPRECATED jsonFilter "Use toJsonFilter instead" #-}
213-- | Converts a transformation on the Pandoc AST into a function
214-- that reads and writes a JSON-encoded string. This is useful
215-- for writing small scripts.
216jsonFilter :: (Pandoc -> Pandoc) -> String -> String
217jsonFilter f = encodeJSON . f . decodeJSON
219-- | 'toJsonFilter' convert a function into a filter that reads pandoc's json output
220-- from stdin, transforms it by walking the AST and applying the specified
221-- function, and writes the result as json to stdout. Usage example:
222--
223-- > -- capitalize.hs
224-- > -- compile with: ghc --make capitalize
225-- > -- run with: pandoc -t json | ./capitalize | pandoc -f json
226-- >
227-- > import Text.Pandoc
228-- > import Data.Char (toUpper)
229-- >
230-- > main :: IO ()
231-- > main = toJsonFilter capitalizeStrings
232-- >
233-- > capitalizeStrings :: Inline -> Inline
234-- > capitalizeStrings (Str s) = Str $ map toUpper s
235-- > capitalizeStrings x = x
236--
237-- The function can be any type @(a -> a)@, @(a -> IO a)@, @(a -> [a])@,
238-- or @(a -> IO [a])@, where @a@ is an instance of 'Data'.
239-- So, for example, @a@ can be 'Pandoc', 'Inline', 'Block', ['Inline'],
240-- ['Block'], 'Meta', 'ListNumberStyle', 'Alignment', 'ListNumberDelim',
241-- 'QuoteType', etc. See 'Text.Pandoc.Definition'.
242class ToJsonFilter a where
243 toJsonFilter :: a -> IO ()
245instance (Data a) => ToJsonFilter (a -> a) where
246 toJsonFilter f = getContents
247 >>= putStr . encodeJSON . (bottomUp f :: Pandoc -> Pandoc) . decodeJSON
249instance (Data a) => ToJsonFilter (a -> IO a) where
250 toJsonFilter f = getContents >>= (bottomUpM f :: Pandoc -> IO Pandoc) . decodeJSON
251 >>= putStr . encodeJSON
253instance (Data a) => ToJsonFilter (a -> [a]) where
254 toJsonFilter f = getContents
255 >>= putStr . encodeJSON . (bottomUp (concatMap f) :: Pandoc -> Pandoc) . decodeJSON
257instance (Data a) => ToJsonFilter (a -> IO [a]) where
258 toJsonFilter f = getContents
259 >>= (bottomUpM (fmap concat . mapM f) :: Pandoc -> IO Pandoc) . decodeJSON
260 >>= putStr . encodeJSON