git.iko.soy/typeable/pandoc
on master help sign in
API change: move reader functions to Text.Pandoc.Readers

Reader helper functions were defined in the top-level Text.Pandoc module. These functions are moved to the Readers submodule as to enable reuse in other submodules.

commit0e107a305f048cbe4cc45c6d2d0c8df14f00375a authorAlbert Krewinkel <albert@zeitkraut.de> dateMon 17 Apr 2017, 10:51 (10 years ago) parent3ac23ab
3 files changed+151−92
pandoc.cabal+1 src/Text/Pandoc.hs+4−92 src/Text/Pandoc/Readers.hs+146
pandoc.cabal+1
@@ -364,6 +364,7 @@ Library
364364 Text.Pandoc.Shared,
365365 Text.Pandoc.MediaBag,
366366 Text.Pandoc.Error,
367+ Text.Pandoc.Readers,
367368 Text.Pandoc.Readers.HTML,
368369 Text.Pandoc.Readers.LaTeX,
369370 Text.Pandoc.Readers.Markdown,
src/Text/Pandoc.hs+4−92
@@ -70,30 +70,10 @@ module Text.Pandoc
7070 , setVerbosity
7171 -- * Error handling
7272 , module Text.Pandoc.Error
73− -- * Lists of readers and writers
74− , readers
75− -- , writers
73+ -- * Lists of writers
7674 , writers
7775 -- * Readers: converting /to/ Pandoc format
78− , Reader (..)
79− , readDocx
80− , readOdt
81− , readMarkdown
82− , readCommonMark
83− , readMediaWiki
84− , readRST
85− , readOrg
86− , readLaTeX
87− , readHtml
88− , readTextile
89− , readDocBook
90− , readOPML
91− , readHaddock
92− , readNative
93− , readJSON
94− , readTWiki
95− , readTxt2Tags
96− , readEPUB
76+ , module Text.Pandoc.Readers
9777 -- * Writers: converting /from/ Pandoc format
9878 , Writer(..)
9979 , writeNative
@@ -142,12 +122,10 @@ module Text.Pandoc
142122 -- * Rendering templates and default templates
143123 , module Text.Pandoc.Templates
144124 -- * Miscellaneous
145− , getReader
146125 , getWriter
147126 , pandocVersion
148127 ) where
149128
150−import Control.Monad.Except (throwError)
151129import Data.Aeson
152130import qualified Data.ByteString.Lazy as BL
153131import Data.List (intercalate)
@@ -157,24 +135,8 @@ import Text.Pandoc.Error
157135import Text.Pandoc.Generic
158136import Text.Pandoc.Logging
159137import Text.Pandoc.Options
160−import Text.Pandoc.Readers.CommonMark
161−import Text.Pandoc.Readers.DocBook
162−import Text.Pandoc.Readers.Docx
163−import Text.Pandoc.Readers.EPUB
164−import Text.Pandoc.Readers.Haddock
165−import Text.Pandoc.Readers.HTML
166−import Text.Pandoc.Readers.LaTeX
167−import Text.Pandoc.Readers.Markdown
168−import Text.Pandoc.Readers.MediaWiki
169−import Text.Pandoc.Readers.Native
170−import Text.Pandoc.Readers.Odt
171−import Text.Pandoc.Readers.OPML
172−import Text.Pandoc.Readers.Org
173−import Text.Pandoc.Readers.RST
174−import Text.Pandoc.Readers.Textile
175−import Text.Pandoc.Readers.TWiki
176−import Text.Pandoc.Readers.Txt2Tags
177−import Text.Pandoc.Shared (mapLeft, pandocVersion)
138+import Text.Pandoc.Readers
139+import Text.Pandoc.Shared (pandocVersion)
178140import Text.Pandoc.Templates
179141import qualified Text.Pandoc.UTF8 as UTF8
180142import Text.Pandoc.Writers.AsciiDoc
@@ -209,38 +171,6 @@ import Text.Pandoc.Writers.Textile
209171import Text.Pandoc.Writers.ZimWiki
210172import Text.Parsec.Error
211173
212−data Reader m = StringReader (ReaderOptions -> String -> m Pandoc)
213− | ByteStringReader (ReaderOptions -> BL.ByteString -> m Pandoc)
214−
215−-- | Association list of formats and readers.
216−readers :: PandocMonad m => [(String, Reader m)]
217−readers = [ ("native" , StringReader readNative)
218− ,("json" , StringReader $ \o s ->
219− case readJSON o s of
220− Right doc -> return doc
221− Left _ -> throwError $ PandocParseError "JSON parse error")
222− ,("markdown" , StringReader readMarkdown)
223− ,("markdown_strict" , StringReader readMarkdown)
224− ,("markdown_phpextra" , StringReader readMarkdown)
225− ,("markdown_github" , StringReader readMarkdown)
226− ,("markdown_mmd", StringReader readMarkdown)
227− ,("commonmark" , StringReader readCommonMark)
228− ,("rst" , StringReader readRST)
229− ,("mediawiki" , StringReader readMediaWiki)
230− ,("docbook" , StringReader readDocBook)
231− ,("opml" , StringReader readOPML)
232− ,("org" , StringReader readOrg)
233− ,("textile" , StringReader readTextile) -- TODO : textile+lhs
234− ,("html" , StringReader readHtml)
235− ,("latex" , StringReader readLaTeX)
236− ,("haddock" , StringReader readHaddock)
237− ,("twiki" , StringReader readTWiki)
238− ,("docx" , ByteStringReader readDocx)
239− ,("odt" , ByteStringReader readOdt)
240− ,("t2t" , StringReader readTxt2Tags)
241− ,("epub" , ByteStringReader readEPUB)
242− ]
243−
244174data Writer m = StringWriter (WriterOptions -> Pandoc -> m String)
245175 | ByteStringWriter (WriterOptions -> Pandoc -> m BL.ByteString)
246176
@@ -296,21 +226,6 @@ writers = [
296226 ,("muse" , StringWriter writeMuse)
297227 ]
298228
299−-- | Retrieve reader based on formatSpec (format+extensions).
300−getReader :: PandocMonad m => String -> Either String (Reader m)
301−getReader s =
302− case parseFormatSpec s of
303− Left e -> Left $ intercalate "\n" [m | Message m <- errorMessages e]
304− Right (readerName, setExts) ->
305− case lookup readerName readers of
306− Nothing -> Left $ "Unknown reader: " ++ readerName
307− Just (StringReader r) -> Right $ StringReader $ \o ->
308− r o{ readerExtensions = setExts $
309− getDefaultExtensions readerName }
310− Just (ByteStringReader r) -> Right $ ByteStringReader $ \o ->
311− r o{ readerExtensions = setExts $
312− getDefaultExtensions readerName }
313−
314229getWriter :: PandocMonad m => String -> Either String (Writer m)
315230getWriter s
316231 = case parseFormatSpec s of
@@ -325,8 +240,5 @@ getWriter s
325240 \o -> r o{ writerExtensions = setExts $
326241 getDefaultExtensions writerName }
327242
328−readJSON :: ReaderOptions -> String -> Either PandocError Pandoc
329−readJSON _ = mapLeft PandocParseError . eitherDecode' . UTF8.fromStringLazy
330−
331243writeJSON :: WriterOptions -> Pandoc -> String
332244writeJSON _ = UTF8.toStringLazy . encode
src/Text/Pandoc/Readers.hsnew file+146
@@ -0,0 +1,146 @@
1+{-# LANGUAGE FlexibleInstances #-}
2+{-# LANGUAGE GADTs #-}
3+{-# LANGUAGE ScopedTypeVariables #-}
4+{-
5+Copyright (C) 2006-2017 John MacFarlane <jgm@berkeley.edu>
6+
7+This program is free software; you can redistribute it and/or modify
8+it under the terms of the GNU General Public License as published by
9+the Free Software Foundation; either version 2 of the License, or
10+(at your option) any later version.
11+
12+This program is distributed in the hope that it will be useful,
13+but WITHOUT ANY WARRANTY; without even the implied warranty of
14+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+GNU General Public License for more details.
16+
17+You should have received a copy of the GNU General Public License
18+along with this program; if not, write to the Free Software
19+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20+-}
21+
22+{- |
23+ Module : Text.Pandoc.Readers
24+ Copyright : Copyright (C) 2006-2017 John MacFarlane
25+ License : GNU GPL, version 2 or above
26+
27+ Maintainer : John MacFarlane <jgm@berkeley.edu>
28+ Stability : alpha
29+ Portability : portable
30+
31+This helper module exports the readers.
32+
33+Note: all of the readers assume that the input text has @'\n'@
34+line endings. So if you get your input text from a web form,
35+you should remove @'\r'@ characters using @filter (/='\r')@.
36+
37+-}
38+
39+module Text.Pandoc.Readers
40+ (
41+ -- * Readers: converting /to/ Pandoc format
42+ Reader (..)
43+ , readers
44+ , readDocx
45+ , readOdt
46+ , readMarkdown
47+ , readCommonMark
48+ , readMediaWiki
49+ , readRST
50+ , readOrg
51+ , readLaTeX
52+ , readHtml
53+ , readTextile
54+ , readDocBook
55+ , readOPML
56+ , readHaddock
57+ , readNative
58+ , readJSON
59+ , readTWiki
60+ , readTxt2Tags
61+ , readEPUB
62+ -- * Miscellaneous
63+ , getReader
64+ , getDefaultExtensions
65+ ) where
66+
67+import Control.Monad.Except (throwError)
68+import Data.Aeson
69+import Data.List (intercalate)
70+import Text.Pandoc.Class
71+import Text.Pandoc.Definition
72+import Text.Pandoc.Error
73+import Text.Pandoc.Extensions
74+import Text.Pandoc.Options
75+import Text.Pandoc.Readers.CommonMark
76+import Text.Pandoc.Readers.DocBook
77+import Text.Pandoc.Readers.Docx
78+import Text.Pandoc.Readers.EPUB
79+import Text.Pandoc.Readers.Haddock
80+import Text.Pandoc.Readers.HTML
81+import Text.Pandoc.Readers.LaTeX
82+import Text.Pandoc.Readers.Markdown
83+import Text.Pandoc.Readers.MediaWiki
84+import Text.Pandoc.Readers.Native
85+import Text.Pandoc.Readers.Odt
86+import Text.Pandoc.Readers.OPML
87+import Text.Pandoc.Readers.Org
88+import Text.Pandoc.Readers.RST
89+import Text.Pandoc.Readers.Textile
90+import Text.Pandoc.Readers.TWiki
91+import Text.Pandoc.Readers.Txt2Tags
92+import Text.Pandoc.Shared (mapLeft)
93+import Text.Parsec.Error
94+import qualified Text.Pandoc.UTF8 as UTF8
95+import qualified Data.ByteString.Lazy as BL
96+
97+data Reader m = StringReader (ReaderOptions -> String -> m Pandoc)
98+ | ByteStringReader (ReaderOptions -> BL.ByteString -> m Pandoc)
99+
100+-- | Association list of formats and readers.
101+readers :: PandocMonad m => [(String, Reader m)]
102+readers = [ ("native" , StringReader readNative)
103+ ,("json" , StringReader $ \o s ->
104+ case readJSON o s of
105+ Right doc -> return doc
106+ Left _ -> throwError $ PandocParseError "JSON parse error")
107+ ,("markdown" , StringReader readMarkdown)
108+ ,("markdown_strict" , StringReader readMarkdown)
109+ ,("markdown_phpextra" , StringReader readMarkdown)
110+ ,("markdown_github" , StringReader readMarkdown)
111+ ,("markdown_mmd", StringReader readMarkdown)
112+ ,("commonmark" , StringReader readCommonMark)
113+ ,("rst" , StringReader readRST)
114+ ,("mediawiki" , StringReader readMediaWiki)
115+ ,("docbook" , StringReader readDocBook)
116+ ,("opml" , StringReader readOPML)
117+ ,("org" , StringReader readOrg)
118+ ,("textile" , StringReader readTextile) -- TODO : textile+lhs
119+ ,("html" , StringReader readHtml)
120+ ,("latex" , StringReader readLaTeX)
121+ ,("haddock" , StringReader readHaddock)
122+ ,("twiki" , StringReader readTWiki)
123+ ,("docx" , ByteStringReader readDocx)
124+ ,("odt" , ByteStringReader readOdt)
125+ ,("t2t" , StringReader readTxt2Tags)
126+ ,("epub" , ByteStringReader readEPUB)
127+ ]
128+
129+-- | Retrieve reader based on formatSpec (format+extensions).
130+getReader :: PandocMonad m => String -> Either String (Reader m)
131+getReader s =
132+ case parseFormatSpec s of
133+ Left e -> Left $ intercalate "\n" [m | Message m <- errorMessages e]
134+ Right (readerName, setExts) ->
135+ case lookup readerName readers of
136+ Nothing -> Left $ "Unknown reader: " ++ readerName
137+ Just (StringReader r) -> Right $ StringReader $ \o ->
138+ r o{ readerExtensions = setExts $
139+ getDefaultExtensions readerName }
140+ Just (ByteStringReader r) -> Right $ ByteStringReader $ \o ->
141+ r o{ readerExtensions = setExts $
142+ getDefaultExtensions readerName }
143+
144+-- | Read pandoc document from JSON format.
145+readJSON :: ReaderOptions -> String -> Either PandocError Pandoc
146+readJSON _ = mapLeft PandocParseError . eitherDecode' . UTF8.fromStringLazy