git.iko.soy/typeable/pandoc
on master help sign in
Introduce `HasSyntaxExtensions` typeclass (#4074)
26e59b3Alexander
src/Text/Pandoc/Options.hs+11−2
@@ -56,6 +56,9 @@ import Skylighting (SyntaxMap, defaultSyntaxMap)
56import Text.Pandoc.Extensions
56import Text.Pandoc.Extensions
57import Text.Pandoc.Highlighting (Style, pygments)
57import Text.Pandoc.Highlighting (Style, pygments)
58
58
59+class HasSyntaxExtensions a where
60+ getExtensions :: a -> Extensions
61+
59data ReaderOptions = ReaderOptions{
62data ReaderOptions = ReaderOptions{
60 readerExtensions :: Extensions -- ^ Syntax extensions
63 readerExtensions :: Extensions -- ^ Syntax extensions
61 , readerStandalone :: Bool -- ^ Standalone document with header
64 , readerStandalone :: Bool -- ^ Standalone document with header
@@ -69,6 +72,9 @@ data ReaderOptions = ReaderOptions{
69 , readerStripComments :: Bool -- ^ Strip HTML comments instead of parsing as raw HTML
72 , readerStripComments :: Bool -- ^ Strip HTML comments instead of parsing as raw HTML
70} deriving (Show, Read, Data, Typeable, Generic)
73} deriving (Show, Read, Data, Typeable, Generic)
71
74
75+instance HasSyntaxExtensions ReaderOptions where
76+ getExtensions opts = readerExtensions opts
77+
72instance ToJSON ReaderOptions where
78instance ToJSON ReaderOptions where
73 toEncoding = genericToEncoding defaultOptions
79 toEncoding = genericToEncoding defaultOptions
74instance FromJSON ReaderOptions
80instance FromJSON ReaderOptions
@@ -259,6 +265,9 @@ instance Default WriterOptions where
259 , writerSyntaxMap = defaultSyntaxMap
265 , writerSyntaxMap = defaultSyntaxMap
260 }
266 }
261
267
268+instance HasSyntaxExtensions WriterOptions where
269+ getExtensions opts = writerExtensions opts
270+
262-- | Returns True if the given extension is enabled.
271-- | Returns True if the given extension is enabled.
263−isEnabled :: Extension -> WriterOptions -> Bool
272+isEnabled :: HasSyntaxExtensions a => Extension -> a -> Bool
264−isEnabled ext opts = ext `extensionEnabled` writerExtensions opts
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)
48-- | Parse a CommonMark formatted string into a 'Pandoc' structure.
48-- | Parse a CommonMark formatted string into a 'Pandoc' structure.
49readCommonMark :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
49readCommonMark :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
50readCommonMark opts s = return $
50readCommonMark opts s = return $
51− (if enabled Ext_gfm_auto_identifiers opts
51+ (if isEnabled Ext_gfm_auto_identifiers opts
52 then addHeaderIdentifiers
52 then addHeaderIdentifiers
53 else id) $
53 else id) $
54 nodeToPandoc opts $ commonmarkToNode opts' exts s
54 nodeToPandoc opts $ commonmarkToNode opts' exts s
55− where opts' = [ optSmart | enabled Ext_smart opts ]
55+ where opts' = [ optSmart | isEnabled Ext_smart opts ]
56− exts = [ extStrikethrough | enabled Ext_strikeout opts ] ++
56+ exts = [ extStrikethrough | isEnabled Ext_strikeout opts ] ++
57− [ extTable | enabled Ext_pipe_tables opts ] ++
57+ [ extTable | isEnabled Ext_pipe_tables opts ] ++
58− [ extAutolink | enabled Ext_autolink_bare_uris opts ]
58+ [ extAutolink | isEnabled 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
63
59
64convertEmojis :: String -> String
60convertEmojis :: String -> String
65convertEmojis (':':xs) =
61convertEmojis (':':xs) =
@@ -112,7 +108,7 @@ addBlock _ (Node _ THEMATIC_BREAK _) =
112addBlock opts (Node _ BLOCK_QUOTE nodes) =
108addBlock opts (Node _ BLOCK_QUOTE nodes) =
113 (BlockQuote (addBlocks opts nodes) :)
109 (BlockQuote (addBlocks opts nodes) :)
114addBlock opts (Node _ (HTML_BLOCK t) _)
110addBlock 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) :)
116 | otherwise = id
112 | otherwise = id
117-- Note: the cmark parser will never generate CUSTOM_BLOCK,
113-- Note: the cmark parser will never generate CUSTOM_BLOCK,
118-- so we don't need to handle it:
114-- so we don't need to handle it:
@@ -210,15 +206,15 @@ addInline opts (Node _ (TEXT t) _) = (map toinl clumps ++)
210 samekind _ ' ' = False
206 samekind _ ' ' = False
211 samekind _ _ = True
207 samekind _ _ = True
212 toinl (' ':_) = Space
208 toinl (' ':_) = Space
213− toinl xs = Str $ if enabled Ext_emoji opts
209+ toinl xs = Str $ if isEnabled Ext_emoji opts
214 then convertEmojis xs
210 then convertEmojis xs
215 else xs
211 else xs
216addInline _ (Node _ LINEBREAK _) = (LineBreak :)
212addInline _ (Node _ LINEBREAK _) = (LineBreak :)
217addInline opts (Node _ SOFTBREAK _)
213addInline opts (Node _ SOFTBREAK _)
218− | enabled Ext_hard_line_breaks opts = (LineBreak :)
214+ | isEnabled Ext_hard_line_breaks opts = (LineBreak :)
219 | otherwise = (SoftBreak :)
215 | otherwise = (SoftBreak :)
220addInline opts (Node _ (HTML_INLINE t) _)
216addInline 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) :)
222 | otherwise = id
218 | otherwise = id
223-- Note: the cmark parser will never generate CUSTOM_BLOCK,
219-- Note: the cmark parser will never generate CUSTOM_BLOCK,
224-- so we don't need to handle it:
220-- so we don't need to handle it: