git.iko.soy/typeable/pandoc
on master help sign in
Class: Removed unnecessary withMedia, improved haddocks.
commit7f9e950d8da52dd2333843d7fd85d000c4a1cbe3 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 29 Jul 2017, 20:54 (9 years ago) parent200b5fb
3 files changed+28−10
src/Text/Pandoc/App.hs+3−2 src/Text/Pandoc/Class.hs+22−7 test/Tests/Readers/EPUB.hs+3−1
src/Text/Pandoc/App.hs+3−2
@@ -76,7 +76,7 @@ import System.IO.Error (isDoesNotExistError)
7676import Text.Pandoc
7777import Text.Pandoc.Builder (setMeta)
7878import Text.Pandoc.Class (PandocIO, extractMedia, fillMediaBag, getLog,
79− setResourcePath, withMediaBag, setTrace)
79+ setResourcePath, getMediaBag, setTrace)
8080import Text.Pandoc.Highlighting (highlightingStyles)
8181import Text.Pandoc.Lua (runLuaFilter, LuaException(..))
8282import Text.Pandoc.Writers.Math (defaultMathJaxURL, defaultKaTeXURL)
@@ -445,7 +445,7 @@ convertWithOpts opts = do
445445
446446 runIO' $ do
447447 setResourcePath (optResourcePath opts)
448− (doc, media) <- withMediaBag $ sourceToDoc sources >>=
448+ doc <- sourceToDoc sources >>=
449449 ( (if isJust (optExtractMedia opts)
450450 then fillMediaBag (writerSourceURL writerOptions)
451451 else return)
@@ -455,6 +455,7 @@ convertWithOpts opts = do
455455 >=> applyLuaFilters datadir (optLuaFilters opts) [format]
456456 >=> applyFilters datadir filters' [format]
457457 )
458+ media <- getMediaBag
458459
459460 case writer of
460461 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
3232 Stability : alpha
3333 Portability : portable
3434
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).
3640-}
3741
3842module Text.Pandoc.Class ( PandocMonad(..)
@@ -65,7 +69,6 @@ module Text.Pandoc.Class ( PandocMonad(..)
6569 , runIO
6670 , runIOorExplode
6771 , runPure
68− , withMediaBag
6972 , fillMediaBag
7073 , extractMedia
7174 ) where
@@ -173,8 +176,9 @@ class (Functor m, Applicative m, Monad m, MonadError PandocError m)
173176 modifyCommonState f = getCommonState >>= putCommonState . f
174177 -- Output a log message.
175178 logOutput :: LogMessage -> m ()
176− -- Output a debug message to sterr, using 'Debug.Trace.trace'.
177− -- Note: this writes to stderr even in pure instances.
179+ -- Output a debug message to sterr, using 'Debug.Trace.trace',
180+ -- if tracing is enabled. Note: this writes to stderr even in
181+ -- pure instances.
178182 trace :: String -> m ()
179183 trace msg = do
180184 tracing <- getsCommonState stTrace
@@ -241,13 +245,26 @@ readFileFromDirs (d:ds) f = catchError
241245
242246--
243247
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.
244252data CommonState = CommonState { stLog :: [LogMessage]
253+ -- ^ A list of log messages in reverse order
245254 , stMediaBag :: MediaBag
255+ -- ^ Media parsed from binary containers
246256 , stInputFiles :: Maybe [FilePath]
257+ -- ^ List of input files from command line
247258 , stOutputFile :: Maybe FilePath
259+ -- ^ Output file from command line
248260 , stResourcePath :: [FilePath]
261+ -- ^ Path to search for resources like
262+ -- included images
249263 , stVerbosity :: Verbosity
264+ -- ^ Verbosity level
250265 , stTrace :: Bool
266+ -- ^ Controls whether tracing messages are
267+ -- issued.
251268 }
252269
253270instance Default CommonState where
@@ -260,12 +277,10 @@ instance Default CommonState where
260277 , stTrace = False
261278 }
262279
280+-- | Evaluate a 'PandocIO' operation.
263281runIO :: PandocIO a -> IO (Either PandocError a)
264282runIO ma = flip evalStateT def $ runExceptT $ unPandocIO ma
265283
266−withMediaBag :: PandocMonad m => m a -> m (a, MediaBag)
267−withMediaBag ma = (,) <$> ma <*> getMediaBag
268−
269284runIOorExplode :: PandocIO a -> IO a
270285runIOorExplode ma = runIO ma >>= handleError
271286
test/Tests/Readers/EPUB.hs+3−1
@@ -11,7 +11,9 @@ import Text.Pandoc.Readers.EPUB
1111getMediaBag :: FilePath -> IO MediaBag
1212getMediaBag fp = do
1313 bs <- BL.readFile fp
14− snd <$> (P.runIOorExplode $ P.withMediaBag $ readEPUB def bs)
14+ P.runIOorExplode $ do
15+ readEPUB def bs
16+ P.getMediaBag
1517
1618testMediaBag :: FilePath -> [(String, String, Int)] -> IO ()
1719testMediaBag fp bag = do