git.iko.soy/typeable/pandoc
on master help sign in
Class: Removed unnecessary withMedia, improved haddocks.
7f9e950John MacFarlane
src/Text/Pandoc/App.hs+3−2
@@ -76,7 +76,7 @@ import System.IO.Error (isDoesNotExistError)
76import Text.Pandoc
76import Text.Pandoc
77import Text.Pandoc.Builder (setMeta)
77import Text.Pandoc.Builder (setMeta)
78import Text.Pandoc.Class (PandocIO, extractMedia, fillMediaBag, getLog,
78import Text.Pandoc.Class (PandocIO, extractMedia, fillMediaBag, getLog,
79− setResourcePath, withMediaBag, setTrace)
79+ setResourcePath, getMediaBag, setTrace)
80import Text.Pandoc.Highlighting (highlightingStyles)
80import Text.Pandoc.Highlighting (highlightingStyles)
81import Text.Pandoc.Lua (runLuaFilter, LuaException(..))
81import Text.Pandoc.Lua (runLuaFilter, LuaException(..))
82import Text.Pandoc.Writers.Math (defaultMathJaxURL, defaultKaTeXURL)
82import Text.Pandoc.Writers.Math (defaultMathJaxURL, defaultKaTeXURL)
@@ -445,7 +445,7 @@ convertWithOpts opts = do
445
445
446 runIO' $ do
446 runIO' $ do
447 setResourcePath (optResourcePath opts)
447 setResourcePath (optResourcePath opts)
448− (doc, media) <- withMediaBag $ sourceToDoc sources >>=
448+ doc <- sourceToDoc sources >>=
449 ( (if isJust (optExtractMedia opts)
449 ( (if isJust (optExtractMedia opts)
450 then fillMediaBag (writerSourceURL writerOptions)
450 then fillMediaBag (writerSourceURL writerOptions)
451 else return)
451 else return)
@@ -455,6 +455,7 @@ convertWithOpts opts = do
455 >=> applyLuaFilters datadir (optLuaFilters opts) [format]
455 >=> applyLuaFilters datadir (optLuaFilters opts) [format]
456 >=> applyFilters datadir filters' [format]
456 >=> applyFilters datadir filters' [format]
457 )
457 )
458+ media <- getMediaBag
458
459
459 case writer of
460 case writer of
460 ByteStringWriter f -> f writerOptions doc >>= writeFnBinary outputFile
461 ByteStringWriter f -> f writerOptions doc >>= writeFnBinary outputFile
src/Text/Pandoc/Class.hs+22−7
@@ -32,7 +32,11 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 Stability : alpha
32 Stability : alpha
33 Portability : portable
33 Portability : portable
34
34
35−Typeclass for pandoc readers and writers, allowing both IO and pure instances.
35+This module defines a type class, 'PandocMonad', for pandoc readers
36+and writers. A pure instance 'PandocPure' and an impure instance
37+'PandocIO' are provided. This allows users of the library to choose
38+whether they want conversions to perform IO operations (such as
39+reading include files or images).
36-}
40-}
37
41
38module Text.Pandoc.Class ( PandocMonad(..)
42module Text.Pandoc.Class ( PandocMonad(..)
@@ -65,7 +69,6 @@ module Text.Pandoc.Class ( PandocMonad(..)
65 , runIO
69 , runIO
66 , runIOorExplode
70 , runIOorExplode
67 , runPure
71 , runPure
68− , withMediaBag
69 , fillMediaBag
72 , fillMediaBag
70 , extractMedia
73 , extractMedia
71 ) where
74 ) where
@@ -173,8 +176,9 @@ class (Functor m, Applicative m, Monad m, MonadError PandocError m)
173 modifyCommonState f = getCommonState >>= putCommonState . f
176 modifyCommonState f = getCommonState >>= putCommonState . f
174 -- Output a log message.
177 -- Output a log message.
175 logOutput :: LogMessage -> m ()
178 logOutput :: LogMessage -> m ()
176− -- Output a debug message to sterr, using 'Debug.Trace.trace'.
179+ -- Output a debug message to sterr, using 'Debug.Trace.trace',
177− -- Note: this writes to stderr even in pure instances.
180+ -- if tracing is enabled. Note: this writes to stderr even in
181+ -- pure instances.
178 trace :: String -> m ()
182 trace :: String -> m ()
179 trace msg = do
183 trace msg = do
180 tracing <- getsCommonState stTrace
184 tracing <- getsCommonState stTrace
@@ -241,13 +245,26 @@ readFileFromDirs (d:ds) f = catchError
241
245
242--
246--
243
247
248+-- | 'CommonState' represents state that is used by all
249+-- instances of 'PandocMonad'. Normally users should not
250+-- need to interact with it directly; instead, auxiliary
251+-- functions like 'setVerbosity' and 'withMediaBag' should be used.
244data CommonState = CommonState { stLog :: [LogMessage]
252data CommonState = CommonState { stLog :: [LogMessage]
253+ -- ^ A list of log messages in reverse order
245 , stMediaBag :: MediaBag
254 , stMediaBag :: MediaBag
255+ -- ^ Media parsed from binary containers
246 , stInputFiles :: Maybe [FilePath]
256 , stInputFiles :: Maybe [FilePath]
257+ -- ^ List of input files from command line
247 , stOutputFile :: Maybe FilePath
258 , stOutputFile :: Maybe FilePath
259+ -- ^ Output file from command line
248 , stResourcePath :: [FilePath]
260 , stResourcePath :: [FilePath]
261+ -- ^ Path to search for resources like
262+ -- included images
249 , stVerbosity :: Verbosity
263 , stVerbosity :: Verbosity
264+ -- ^ Verbosity level
250 , stTrace :: Bool
265 , stTrace :: Bool
266+ -- ^ Controls whether tracing messages are
267+ -- issued.
251 }
268 }
252
269
253instance Default CommonState where
270instance Default CommonState where
@@ -260,12 +277,10 @@ instance Default CommonState where
260 , stTrace = False
277 , stTrace = False
261 }
278 }
262
279
280+-- | Evaluate a 'PandocIO' operation.
263runIO :: PandocIO a -> IO (Either PandocError a)
281runIO :: PandocIO a -> IO (Either PandocError a)
264runIO ma = flip evalStateT def $ runExceptT $ unPandocIO ma
282runIO ma = flip evalStateT def $ runExceptT $ unPandocIO ma
265
283
266−withMediaBag :: PandocMonad m => m a -> m (a, MediaBag)
267−withMediaBag ma = (,) <$> ma <*> getMediaBag
268−
269runIOorExplode :: PandocIO a -> IO a
284runIOorExplode :: PandocIO a -> IO a
270runIOorExplode ma = runIO ma >>= handleError
285runIOorExplode ma = runIO ma >>= handleError
271
286
test/Tests/Readers/EPUB.hs+3−1
@@ -11,7 +11,9 @@ import Text.Pandoc.Readers.EPUB
11getMediaBag :: FilePath -> IO MediaBag
11getMediaBag :: FilePath -> IO MediaBag
12getMediaBag fp = do
12getMediaBag fp = do
13 bs <- BL.readFile fp
13 bs <- BL.readFile fp
14− snd <$> (P.runIOorExplode $ P.withMediaBag $ readEPUB def bs)
14+ P.runIOorExplode $ do
15+ readEPUB def bs
16+ P.getMediaBag
15
17
16testMediaBag :: FilePath -> [(String, String, Int)] -> IO ()
18testMediaBag :: FilePath -> [(String, String, Int)] -> IO ()
17testMediaBag fp bag = do
19testMediaBag fp bag = do