aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Extensions.hs
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2019-09-28 14:47:41 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2019-09-29 11:43:17 -0700
commit746c92a41a4f1df5ac97246fe69555cef5419d00 (patch)
tree4a300c58e87d1cf65cc5a55a5ef440b0bd71f1af /src/Text/Pandoc/Extensions.hs
parent03d4e6b9ef87d4e6ed018c93b358f8557f8f7388 (diff)
downloadpandoc-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/Extensions.hs')
-rw-r--r--src/Text/Pandoc/Extensions.hs166
1 files changed, 151 insertions, 15 deletions
diff --git a/src/Text/Pandoc/Extensions.hs b/src/Text/Pandoc/Extensions.hs
index 1c787b7d3..d85b26200 100644
--- a/src/Text/Pandoc/Extensions.hs
+++ b/src/Text/Pandoc/Extensions.hs
@@ -26,6 +26,7 @@ module Text.Pandoc.Extensions ( Extension(..)
, enableExtension
, disableExtension
, getDefaultExtensions
+ , getAllExtensions
, pandocExtensions
, plainExtensions
, strictExtensions
@@ -312,12 +313,12 @@ strictExtensions = extensionsFromList
-- | Default extensions from format-describing string.
getDefaultExtensions :: String -> Extensions
-getDefaultExtensions "markdown_strict" = strictExtensions
+getDefaultExtensions "markdown_strict" = strictExtensions
getDefaultExtensions "markdown_phpextra" = phpMarkdownExtraExtensions
-getDefaultExtensions "markdown_mmd" = multimarkdownExtensions
-getDefaultExtensions "markdown_github" = githubMarkdownExtensions
-getDefaultExtensions "markdown" = pandocExtensions
-getDefaultExtensions "ipynb" =
+getDefaultExtensions "markdown_mmd" = multimarkdownExtensions
+getDefaultExtensions "markdown_github" = githubMarkdownExtensions
+getDefaultExtensions "markdown" = pandocExtensions
+getDefaultExtensions "ipynb" =
extensionsFromList
[ Ext_all_symbols_escapable
, Ext_pipe_tables
@@ -379,16 +380,149 @@ getDefaultExtensions "opml" = pandocExtensions -- affects notes
getDefaultExtensions _ = extensionsFromList
[Ext_auto_identifiers]
--- | Parse a format-specifying string into a markup format and a function that
--- takes Extensions and enables and disables extensions as defined in the format
--- spec.
+allMarkdownExtensions :: Extensions
+allMarkdownExtensions =
+ pandocExtensions <>
+ extensionsFromList
+ [ Ext_old_dashes
+ , Ext_angle_brackets_escapable
+ , Ext_lists_without_preceding_blankline
+ , Ext_four_space_rule
+ , Ext_spaced_reference_links
+ , Ext_hard_line_breaks
+ , Ext_ignore_line_breaks
+ , Ext_east_asian_line_breaks
+ , Ext_emoji
+ , Ext_tex_math_single_backslash
+ , Ext_tex_math_double_backslash
+ , Ext_markdown_attribute
+ , Ext_mmd_title_block
+ , Ext_abbreviations
+ , Ext_autolink_bare_uris
+ , Ext_mmd_link_attributes
+ , Ext_mmd_header_identifiers
+ , Ext_compact_definition_lists
+ , Ext_gutenberg
+ , Ext_smart
+ , Ext_literate_haskell
+ ]
+
+
+-- | Get all valid extensions for a format. This is used
+-- mainly in checking format specifications for validity.
+getAllExtensions :: String -> Extensions
+getAllExtensions f = universalExtensions <> getAll f
+ where
+ autoIdExtensions = extensionsFromList
+ [ Ext_auto_identifiers
+ , Ext_gfm_auto_identifiers
+ , Ext_ascii_identifiers
+ ]
+ universalExtensions = extensionsFromList
+ [ Ext_east_asian_line_breaks ]
+ getAll "markdown_strict" = allMarkdownExtensions
+ getAll "markdown_phpextra" = allMarkdownExtensions
+ getAll "markdown_mmd" = allMarkdownExtensions
+ getAll "markdown_github" = allMarkdownExtensions
+ getAll "markdown" = allMarkdownExtensions
+ getAll "ipynb" = allMarkdownExtensions
+ getAll "docx" = extensionsFromList
+ [ Ext_empty_paragraphs
+ , Ext_styles
+ ]
+ getAll "opendocument" = extensionsFromList
+ [ Ext_empty_paragraphs
+ , Ext_native_numbering
+ ]
+ getAll "odt" = getAll "opendocument" <> autoIdExtensions
+ getAll "muse" = autoIdExtensions <>
+ extensionsFromList
+ [ Ext_amuse ]
+ getAll "asciidoc" = autoIdExtensions
+ getAll "plain" = allMarkdownExtensions
+ getAll "gfm" = githubMarkdownExtensions <>
+ autoIdExtensions <>
+ extensionsFromList
+ [ Ext_raw_html
+ , Ext_raw_tex -- only supported in writer (for math)
+ , Ext_hard_line_breaks
+ , Ext_smart
+ ]
+ getAll "commonmark" = getAll "gfm"
+ getAll "org" = autoIdExtensions <>
+ extensionsFromList
+ [ Ext_citations
+ , Ext_smart
+ ]
+ getAll "html" = autoIdExtensions <>
+ extensionsFromList
+ [ Ext_native_divs
+ , Ext_line_blocks
+ , Ext_native_spans
+ , Ext_empty_paragraphs
+ , Ext_raw_html
+ , Ext_raw_tex
+ , Ext_task_lists
+ , Ext_tex_math_dollars
+ , Ext_tex_math_single_backslash
+ , Ext_tex_math_double_backslash
+ , Ext_literate_haskell
+ , Ext_epub_html_exts
+ ]
+ getAll "html4" = getAll "html"
+ getAll "html5" = getAll "html"
+ getAll "epub" = getAll "html"
+ getAll "epub2" = getAll "epub"
+ getAll "epub3" = getAll "epub"
+ getAll "latex" = autoIdExtensions <>
+ extensionsFromList
+ [ Ext_smart
+ , Ext_latex_macros
+ , Ext_raw_tex
+ , Ext_task_lists
+ , Ext_literate_haskell
+ ]
+ getAll "beamer" = getAll "latex"
+ getAll "context" = autoIdExtensions <>
+ extensionsFromList
+ [ Ext_smart
+ , Ext_raw_tex
+ , Ext_ntb
+ ]
+ getAll "textile" = autoIdExtensions <>
+ extensionsFromList
+ [ Ext_old_dashes
+ , Ext_smart
+ , Ext_raw_tex
+ ]
+ getAll "opml" = allMarkdownExtensions -- affects notes
+ getAll "twiki" = autoIdExtensions <>
+ extensionsFromList
+ [ Ext_smart ]
+ getAll "vimwiki" = autoIdExtensions
+ getAll "dokuwiki" = autoIdExtensions
+ getAll "tikiwiki" = autoIdExtensions
+ getAll "rst" = autoIdExtensions <>
+ extensionsFromList
+ [ Ext_smart
+ , Ext_literate_haskell
+ ]
+ getAll "mediawiki" = autoIdExtensions <>
+ extensionsFromList
+ [ Ext_smart ]
+ getAll _ = mempty
+
+
+-- | Parse a format-specifying string into a markup format,
+-- a set of extensions to enable, and a set of extensions to disable.
parseFormatSpec :: String
- -> Either ParseError (String, Extensions -> Extensions)
+ -> Either ParseError (String, [Extension], [Extension])
parseFormatSpec = parse formatSpec ""
where formatSpec = do
name <- formatName
- extMods <- many extMod
- return (name, \x -> foldl (flip ($)) x extMods)
+ (extsToEnable, extsToDisable) <- foldl (flip ($)) ([],[]) <$>
+ many extMod
+ return (name, reverse extsToEnable, reverse extsToDisable)
formatName = many1 $ noneOf "-+"
extMod = do
polarity <- oneOf "-+"
@@ -397,10 +531,12 @@ parseFormatSpec = parse formatSpec ""
Just n -> return n
Nothing
| name == "lhs" -> return Ext_literate_haskell
- | otherwise -> Prelude.fail $ "Unknown extension: " ++ name
- return $ case polarity of
- '-' -> disableExtension ext
- _ -> enableExtension ext
+ | otherwise -> Prelude.fail $
+ "Unknown extension: " ++ name
+ return $ \(extsToEnable, extsToDisable) ->
+ case polarity of
+ '+' -> (ext : extsToEnable, extsToDisable)
+ _ -> (extsToEnable, ext : extsToDisable)
#ifdef DERIVE_JSON_VIA_TH
$(deriveJSON defaultOptions ''Extension)