git.iko.soy/typeable/pandoc
on master help sign in
Introduce `HasSyntaxExtensions` typeclass (#4074)

+ Added new `HasSyntaxExtensions` typeclass for `ReaderOptions` and `WriterOptions`. + Reimplemented `isEnabled` function from `Options.hs` to accept both `ReaderOptions` and `WriterOptions`. + Replaced `enabled` from `CommonMark.hs` with new `isEnabled`.

commit26e59b331fc6ce2509a3b53f4454c0fadfbc58ee authorAlexander <ilabdsf@gmail.com> dateThu 16 Nov 2017, 20:24 (9 years ago) parent4e02ed5
2 files changed+20−15
src/Text/Pandoc/Options.hs+11−2 src/Text/Pandoc/Readers/CommonMark.hs+9−13
src/Text/Pandoc/Options.hs+11−2
@@ -56,6 +56,9 @@ import Skylighting (SyntaxMap, defaultSyntaxMap)
5656import Text.Pandoc.Extensions
5757import Text.Pandoc.Highlighting (Style, pygments)
5858
59+class HasSyntaxExtensions a where
60+ getExtensions :: a -> Extensions
61+
5962data ReaderOptions = ReaderOptions{
6063 readerExtensions :: Extensions -- ^ Syntax extensions
6164 , readerStandalone :: Bool -- ^ Standalone document with header
@@ -69,6 +72,9 @@ data ReaderOptions = ReaderOptions{
6972 , readerStripComments :: Bool -- ^ Strip HTML comments instead of parsing as raw HTML
7073} deriving (Show, Read, Data, Typeable, Generic)
7174
75+instance HasSyntaxExtensions ReaderOptions where
76+ getExtensions opts = readerExtensions opts
77+
7278instance ToJSON ReaderOptions where
7379 toEncoding = genericToEncoding defaultOptions
7480instance FromJSON ReaderOptions
@@ -259,6 +265,9 @@ instance Default WriterOptions where
259265 , writerSyntaxMap = defaultSyntaxMap
260266 }
261267
268+instance HasSyntaxExtensions WriterOptions where
269+ getExtensions opts = writerExtensions opts
270+
262271-- | Returns True if the given extension is enabled.
263−isEnabled :: Extension -> WriterOptions -> Bool
264−isEnabled ext opts = ext `extensionEnabled` writerExtensions opts
272+isEnabled :: HasSyntaxExtensions a => Extension -> a -> Bool
273+isEnabled ext opts = ext `extensionEnabled` getExtensions opts
src/Text/Pandoc/Readers/CommonMark.hs+9−13
@@ -48,18 +48,14 @@ import Text.Pandoc.Walk (walkM)
4848-- | Parse a CommonMark formatted string into a 'Pandoc' structure.
4949readCommonMark :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
5050readCommonMark opts s = return $
51− (if enabled Ext_gfm_auto_identifiers opts
51+ (if isEnabled Ext_gfm_auto_identifiers opts
5252 then addHeaderIdentifiers
5353 else id) $
5454 nodeToPandoc opts $ commonmarkToNode opts' exts s
55− where opts' = [ optSmart | enabled Ext_smart opts ]
56− exts = [ extStrikethrough | enabled Ext_strikeout opts ] ++
57− [ extTable | enabled Ext_pipe_tables opts ] ++
58− [ extAutolink | enabled Ext_autolink_bare_uris opts ]
59−
60−-- | Returns True if the given extension is enabled.
61−enabled :: Extension -> ReaderOptions -> Bool
62−enabled ext opts = ext `extensionEnabled` readerExtensions opts
55+ where opts' = [ optSmart | isEnabled Ext_smart opts ]
56+ exts = [ extStrikethrough | isEnabled Ext_strikeout opts ] ++
57+ [ extTable | isEnabled Ext_pipe_tables opts ] ++
58+ [ extAutolink | isEnabled Ext_autolink_bare_uris opts ]
6359
6460convertEmojis :: String -> String
6561convertEmojis (':':xs) =
@@ -112,7 +108,7 @@ addBlock _ (Node _ THEMATIC_BREAK _) =
112108addBlock opts (Node _ BLOCK_QUOTE nodes) =
113109 (BlockQuote (addBlocks opts nodes) :)
114110addBlock opts (Node _ (HTML_BLOCK t) _)
115− | enabled Ext_raw_html opts = (RawBlock (Format "html") (unpack t) :)
111+ | isEnabled Ext_raw_html opts = (RawBlock (Format "html") (unpack t) :)
116112 | otherwise = id
117113-- Note: the cmark parser will never generate CUSTOM_BLOCK,
118114-- so we don't need to handle it:
@@ -210,15 +206,15 @@ addInline opts (Node _ (TEXT t) _) = (map toinl clumps ++)
210206 samekind _ ' ' = False
211207 samekind _ _ = True
212208 toinl (' ':_) = Space
213− toinl xs = Str $ if enabled Ext_emoji opts
209+ toinl xs = Str $ if isEnabled Ext_emoji opts
214210 then convertEmojis xs
215211 else xs
216212addInline _ (Node _ LINEBREAK _) = (LineBreak :)
217213addInline opts (Node _ SOFTBREAK _)
218− | enabled Ext_hard_line_breaks opts = (LineBreak :)
214+ | isEnabled Ext_hard_line_breaks opts = (LineBreak :)
219215 | otherwise = (SoftBreak :)
220216addInline opts (Node _ (HTML_INLINE t) _)
221− | enabled Ext_raw_html opts = (RawInline (Format "html") (unpack t) :)
217+ | isEnabled Ext_raw_html opts = (RawInline (Format "html") (unpack t) :)
222218 | otherwise = id
223219-- Note: the cmark parser will never generate CUSTOM_BLOCK,
224220-- so we don't need to handle it: