diff options
author | John MacFarlane <jgm@berkeley.edu> | 2019-09-28 14:47:41 -0700 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2019-09-29 11:43:17 -0700 |
commit | 746c92a41a4f1df5ac97246fe69555cef5419d00 (patch) | |
tree | 4a300c58e87d1cf65cc5a55a5ef440b0bd71f1af /src/Text/Pandoc/Writers.hs | |
parent | 03d4e6b9ef87d4e6ed018c93b358f8557f8f7388 (diff) | |
download | pandoc-746c92a41a4f1df5ac97246fe69555cef5419d00.tar.gz |
Raise error on unsupported extensions. Closes #4338.
+ An error is now raised if you try to specify (enable or
disable) an extension that does not affect the given
format, e.g. `docx+pipe_tables`.
+ The `--list-extensions[=FORMAT]` option now lists only
extensions that affect the given FORMAT.
+ Text.Pandoc.Error: Add constructors `PandocUnknownReaderError`,
`PandocUnknownWriterError`, `PandocUnsupportedExtensionError`.
[API change]
+ Text.Pandoc.Extensions now exports `getAllExtensions`,
which returns the extensions that affect a given format
(whether enabled by default or not). [API change]
+ Text.Pandoc.Extensions: change type of `parseFormatSpec`
from `Either ParseError (String, Extensions -> Extensions)`
to `Either ParseError (String, [Extension], [Extension])`
[API change].
+ Text.Pandoc.Readers: change type of `getReader` so it returns
a value in the PandocMonad instance rather than an Either
[API change]. Exceptions for unknown formats and unsupported
extensions are now raised by this function and need not be handled by
the calling function.
+ Text.Pandoc.Writers: change type of `getWriter` so it returns
a value in the PandocMonad instance rather than an Either
[API change]. Exceptions for unknown formats and unsupported
extensions are now raised by this function and need not be handled by
the calling function.
Diffstat (limited to 'src/Text/Pandoc/Writers.hs')
-rw-r--r-- | src/Text/Pandoc/Writers.hs | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/src/Text/Pandoc/Writers.hs b/src/Text/Pandoc/Writers.hs index ecf45839e..c88f860bb 100644 --- a/src/Text/Pandoc/Writers.hs +++ b/src/Text/Pandoc/Writers.hs @@ -70,6 +70,8 @@ module Text.Pandoc.Writers ) where import Prelude +import Control.Monad.Except (throwError) +import Control.Monad (unless) import Data.Aeson import qualified Data.ByteString.Lazy as BL import Data.List (intercalate) @@ -78,6 +80,7 @@ import Text.Pandoc.Class import Text.Pandoc.Definition import Text.Pandoc.Options import qualified Text.Pandoc.UTF8 as UTF8 +import Text.Pandoc.Error import Text.Pandoc.Writers.AsciiDoc import Text.Pandoc.Writers.CommonMark import Text.Pandoc.Writers.ConTeXt @@ -176,15 +179,29 @@ writers = [ ] -- | Retrieve writer, extensions based on formatSpec (format+extensions). -getWriter :: PandocMonad m => String -> Either String (Writer m, Extensions) -getWriter s - = case parseFormatSpec s of - Left e -> Left $ intercalate "\n" [m | Message m <- errorMessages e] - Right (writerName, setExts) -> - case lookup writerName writers of - Nothing -> Left $ "Unknown writer: " ++ writerName - Just r -> Right (r, setExts $ - getDefaultExtensions writerName) +getWriter :: PandocMonad m => String -> m (Writer m, Extensions) +getWriter s = + case parseFormatSpec s of + Left e -> throwError $ PandocAppError + $ intercalate "\n" [m | Message m <- errorMessages e] + Right (writerName, extsToEnable, extsToDisable) -> + case lookup writerName writers of + Nothing -> throwError $ + PandocUnknownWriterError writerName + Just w -> do + let allExts = getAllExtensions writerName + let exts = foldr disableExtension + (foldr enableExtension + (getDefaultExtensions writerName) + extsToEnable) extsToDisable + mapM_ (\ext -> + unless (extensionEnabled ext allExts) $ + throwError $ + PandocUnsupportedExtensionError + (drop 4 $ show ext) writerName) + (extsToEnable ++ extsToDisable) + return (w, exts) + writeJSON :: PandocMonad m => WriterOptions -> Pandoc -> m Text writeJSON _ = return . UTF8.toText . BL.toStrict . encode |