aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2017-04-17 10:04:20 +0200
committerAlbert Krewinkel <albert+github@zeitkraut.de>2017-04-26 23:28:40 +0200
commit3ac23ab615e67c5d08941fcebeb1d19d37ff1a46 (patch)
tree544ae88298e455cfb1f2c40a880c4097ef8e00d3 /src
parent9cd20c9b8b1fa3bd4581399327d61551558cf899 (diff)
downloadpandoc-3ac23ab615e67c5d08941fcebeb1d19d37ff1a46.tar.gz
API change: move extension handling to Text.Pandoc.Extensions
Extension parsing and processing functions were defined in the top-level Text.Pandoc module. These functions are moved to the Extensions submodule as to enable reuse in other submodules.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc.hs61
-rw-r--r--src/Text/Pandoc/Extensions.hs78
2 files changed, 75 insertions, 64 deletions
diff --git a/src/Text/Pandoc.hs b/src/Text/Pandoc.hs
index 977ad1ab4..5d495299f 100644
--- a/src/Text/Pandoc.hs
+++ b/src/Text/Pandoc.hs
@@ -144,7 +144,6 @@ module Text.Pandoc
-- * Miscellaneous
, getReader
, getWriter
- , getDefaultExtensions
, pandocVersion
) where
@@ -175,7 +174,7 @@ import Text.Pandoc.Readers.RST
import Text.Pandoc.Readers.Textile
import Text.Pandoc.Readers.TWiki
import Text.Pandoc.Readers.Txt2Tags
-import Text.Pandoc.Shared (mapLeft, pandocVersion, safeRead)
+import Text.Pandoc.Shared (mapLeft, pandocVersion)
import Text.Pandoc.Templates
import qualified Text.Pandoc.UTF8 as UTF8
import Text.Pandoc.Writers.AsciiDoc
@@ -208,29 +207,8 @@ import Text.Pandoc.Writers.TEI
import Text.Pandoc.Writers.Texinfo
import Text.Pandoc.Writers.Textile
import Text.Pandoc.Writers.ZimWiki
-import Text.Parsec
import Text.Parsec.Error
-parseFormatSpec :: String
- -> Either ParseError (String, Extensions -> Extensions)
-parseFormatSpec = parse formatSpec ""
- where formatSpec = do
- name <- formatName
- extMods <- many extMod
- return (name, \x -> foldl (flip ($)) x extMods)
- formatName = many1 $ noneOf "-+"
- extMod = do
- polarity <- oneOf "-+"
- name <- many $ noneOf "-+"
- ext <- case safeRead ("Ext_" ++ name) of
- Just n -> return n
- Nothing
- | name == "lhs" -> return Ext_literate_haskell
- | otherwise -> fail $ "Unknown extension: " ++ name
- return $ case polarity of
- '-' -> disableExtension ext
- _ -> enableExtension ext
-
data Reader m = StringReader (ReaderOptions -> String -> m Pandoc)
| ByteStringReader (ReaderOptions -> BL.ByteString -> m Pandoc)
@@ -318,43 +296,6 @@ writers = [
,("muse" , StringWriter writeMuse)
]
-getDefaultExtensions :: String -> Extensions
-getDefaultExtensions "markdown_strict" = strictExtensions
-getDefaultExtensions "markdown_phpextra" = phpMarkdownExtraExtensions
-getDefaultExtensions "markdown_mmd" = multimarkdownExtensions
-getDefaultExtensions "markdown_github" = githubMarkdownExtensions
-getDefaultExtensions "markdown" = pandocExtensions
-getDefaultExtensions "plain" = plainExtensions
-getDefaultExtensions "org" = extensionsFromList
- [Ext_citations,
- Ext_auto_identifiers]
-getDefaultExtensions "html" = extensionsFromList
- [Ext_auto_identifiers,
- Ext_native_divs,
- Ext_native_spans]
-getDefaultExtensions "html4" = getDefaultExtensions "html"
-getDefaultExtensions "html5" = getDefaultExtensions "html"
-getDefaultExtensions "epub" = extensionsFromList
- [Ext_raw_html,
- Ext_native_divs,
- Ext_native_spans,
- Ext_epub_html_exts]
-getDefaultExtensions "epub2" = getDefaultExtensions "epub"
-getDefaultExtensions "epub3" = getDefaultExtensions "epub"
-getDefaultExtensions "latex" = extensionsFromList
- [Ext_smart,
- Ext_auto_identifiers]
-getDefaultExtensions "context" = extensionsFromList
- [Ext_smart,
- Ext_auto_identifiers]
-getDefaultExtensions "textile" = extensionsFromList
- [Ext_old_dashes,
- Ext_smart,
- Ext_raw_html,
- Ext_auto_identifiers]
-getDefaultExtensions _ = extensionsFromList
- [Ext_auto_identifiers]
-
-- | Retrieve reader based on formatSpec (format+extensions).
getReader :: PandocMonad m => String -> Either String (Reader m)
getReader s =
diff --git a/src/Text/Pandoc/Extensions.hs b/src/Text/Pandoc/Extensions.hs
index 54f38f4a0..24f7d56ec 100644
--- a/src/Text/Pandoc/Extensions.hs
+++ b/src/Text/Pandoc/Extensions.hs
@@ -1,7 +1,5 @@
-{-# LANGUAGE DeriveDataTypeable #-}
-{-# LANGUAGE DeriveGeneric #-}
{-
-Copyright (C) 2012-2016 John MacFarlane <jgm@berkeley.edu>
+Copyright (C) 2012-2017 John MacFarlane <jgm@berkeley.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -17,10 +15,12 @@ You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}
+{-# LANGUAGE DeriveDataTypeable #-}
+{-# LANGUAGE DeriveGeneric #-}
{- |
Module : Text.Pandoc.Extensions
- Copyright : Copyright (C) 2012-2016 John MacFarlane
+ Copyright : Copyright (C) 2012-2017 John MacFarlane
License : GNU GPL, version 2 or above
Maintainer : John MacFarlane <jgm@berkeley.edu>
@@ -33,9 +33,11 @@ module Text.Pandoc.Extensions ( Extension(..)
, Extensions
, emptyExtensions
, extensionsFromList
+ , parseFormatSpec
, extensionEnabled
, enableExtension
, disableExtension
+ , getDefaultExtensions
, pandocExtensions
, plainExtensions
, strictExtensions
@@ -47,6 +49,8 @@ import Data.Bits (clearBit, setBit, testBit)
import Data.Data (Data)
import Data.Typeable (Typeable)
import GHC.Generics (Generic)
+import Text.Pandoc.Shared (safeRead)
+import Text.Parsec
newtype Extensions = Extensions Integer
deriving (Show, Read, Eq, Ord, Data, Typeable, Generic)
@@ -135,6 +139,7 @@ data Extension =
| Ext_old_dashes -- ^ -- = em, - before number = en
deriving (Show, Read, Enum, Eq, Ord, Bounded, Data, Typeable, Generic)
+-- | Extensions to be used with pandoc-flavored markdown.
pandocExtensions :: Extensions
pandocExtensions = extensionsFromList
[ Ext_footnotes
@@ -182,6 +187,7 @@ pandocExtensions = extensionsFromList
, Ext_smart
]
+-- | Extensions to be used with github-flavored markdown.
plainExtensions :: Extensions
plainExtensions = extensionsFromList
[ Ext_table_captions
@@ -200,6 +206,7 @@ plainExtensions = extensionsFromList
, Ext_strikeout
]
+-- | Extensions to be used with github-flavored markdown.
phpMarkdownExtraExtensions :: Extensions
phpMarkdownExtraExtensions = extensionsFromList
[ Ext_footnotes
@@ -215,6 +222,7 @@ phpMarkdownExtraExtensions = extensionsFromList
, Ext_shortcut_reference_links
]
+-- | Extensions to be used with github-flavored markdown.
githubMarkdownExtensions :: Extensions
githubMarkdownExtensions = extensionsFromList
[ Ext_angle_brackets_escapable
@@ -234,6 +242,7 @@ githubMarkdownExtensions = extensionsFromList
, Ext_shortcut_reference_links
]
+-- | Extensions to be used with multimarkdown.
multimarkdownExtensions :: Extensions
multimarkdownExtensions = extensionsFromList
[ Ext_pipe_tables
@@ -264,9 +273,70 @@ multimarkdownExtensions = extensionsFromList
, Ext_subscript
]
+-- | Language extensions to be used with strict markdown.
strictExtensions :: Extensions
strictExtensions = extensionsFromList
[ Ext_raw_html
, Ext_shortcut_reference_links
]
+-- | Default extensions from format-describing string.
+getDefaultExtensions :: String -> Extensions
+getDefaultExtensions "markdown_strict" = strictExtensions
+getDefaultExtensions "markdown_phpextra" = phpMarkdownExtraExtensions
+getDefaultExtensions "markdown_mmd" = multimarkdownExtensions
+getDefaultExtensions "markdown_github" = githubMarkdownExtensions
+getDefaultExtensions "markdown" = pandocExtensions
+getDefaultExtensions "plain" = plainExtensions
+getDefaultExtensions "org" = extensionsFromList
+ [Ext_citations,
+ Ext_auto_identifiers]
+getDefaultExtensions "html" = extensionsFromList
+ [Ext_auto_identifiers,
+ Ext_native_divs,
+ Ext_native_spans]
+getDefaultExtensions "html4" = getDefaultExtensions "html"
+getDefaultExtensions "html5" = getDefaultExtensions "html"
+getDefaultExtensions "epub" = extensionsFromList
+ [Ext_raw_html,
+ Ext_native_divs,
+ Ext_native_spans,
+ Ext_epub_html_exts]
+getDefaultExtensions "epub2" = getDefaultExtensions "epub"
+getDefaultExtensions "epub3" = getDefaultExtensions "epub"
+getDefaultExtensions "latex" = extensionsFromList
+ [Ext_smart,
+ Ext_auto_identifiers]
+getDefaultExtensions "context" = extensionsFromList
+ [Ext_smart,
+ Ext_auto_identifiers]
+getDefaultExtensions "textile" = extensionsFromList
+ [Ext_old_dashes,
+ Ext_smart,
+ Ext_raw_html,
+ Ext_auto_identifiers]
+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.
+parseFormatSpec :: String
+ -> Either ParseError (String, Extensions -> Extensions)
+parseFormatSpec = parse formatSpec ""
+ where formatSpec = do
+ name <- formatName
+ extMods <- many extMod
+ return (name, \x -> foldl (flip ($)) x extMods)
+ formatName = many1 $ noneOf "-+"
+ extMod = do
+ polarity <- oneOf "-+"
+ name <- many $ noneOf "-+"
+ ext <- case safeRead ("Ext_" ++ name) of
+ Just n -> return n
+ Nothing
+ | name == "lhs" -> return Ext_literate_haskell
+ | otherwise -> fail $ "Unknown extension: " ++ name
+ return $ case polarity of
+ '-' -> disableExtension ext
+ _ -> enableExtension ext