git.iko.soy/typeable/pandoc
on master help sign in
MediaWiki reader: Use MWState instead of ParserState.
commit8cc5625bd6ef2bda13b156b381d238f21beb789e authorJohn MacFarlane <jgm@berkeley.edu> dateThu 13 Sep 2012, 15:10 (14 years ago) parent5b29f0f
2 files changed+25−6
src/Text/Pandoc/Readers/HTML.hs+1−1 src/Text/Pandoc/Readers/MediaWiki.hs+24−5
src/Text/Pandoc/Readers/HTML.hs+1−1
@@ -597,7 +597,7 @@ htmlInBalanced f = try $ do
597597 return $ tag ++ concat contents ++ endtag
598598
599599-- | Matches a tag meeting a certain condition.
600−htmlTag :: (Tag String -> Bool) -> Parser [Char] ParserState (Tag String, String)
600+htmlTag :: (Tag String -> Bool) -> Parser [Char] st (Tag String, String)
601601htmlTag f = try $ do
602602 lookAhead (char '<')
603603 (next : _) <- getInput >>= return . canonicalizeTags . parseTags
src/Text/Pandoc/Readers/MediaWiki.hs+24−5
@@ -34,6 +34,7 @@ _ support internal links http://www.mediawiki.org/wiki/Help:Links
3434_ support external links (partially implemented)
3535_ support images http://www.mediawiki.org/wiki/Help:Images
3636_ support tables http://www.mediawiki.org/wiki/Help:Tables
37+- footnotes?
3738-}
3839module Text.Pandoc.Readers.MediaWiki ( readMediaWiki ) where
3940
@@ -44,7 +45,7 @@ import Text.Pandoc.Options
4445import Text.Pandoc.Readers.HTML ( htmlTag, isInlineTag,
4546 isBlockTag, isCommentTag )
4647import Text.Pandoc.XML ( fromEntities )
47−import Text.Pandoc.Parsing
48+import Text.Pandoc.Parsing hiding ( nested )
4849import Text.Pandoc.Generic ( bottomUp )
4950import Text.Pandoc.Shared ( stripTrailingNewlines, safeRead )
5051import Data.Monoid (mconcat, mempty)
@@ -56,17 +57,35 @@ import Data.Sequence (viewl, ViewL(..), (<|))
5657
5758-- | Read mediawiki from an input string and return a Pandoc document.
5859readMediaWiki :: ReaderOptions -- ^ Reader options
59− -> String -- ^ String to parse (assuming @'\n'@ line endings)
60− -> Pandoc
60+ -> String -- ^ String to parse (assuming @'\n'@ line endings)
61+ -> Pandoc
6162readMediaWiki opts s =
62− (readWith parseMediaWiki) def{ stateOptions = opts } (s ++ "\n\n")
63+ case runParser parseMediaWiki MWState{ mwOptions = opts, mwMaxNestingLevel = 4 }
64+ "source" (s ++ "\n") of
65+ Left err' -> error $ "\nError:\n" ++ show err'
66+ Right result -> result
6367
64−type MWParser = Parser [Char] ParserState
68+data MWState = MWState { mwOptions :: ReaderOptions
69+ , mwMaxNestingLevel :: Int
70+ }
71+
72+type MWParser = Parser [Char] MWState
6573
6674--
6775-- auxiliary functions
6876--
6977
78+-- This is used to prevent exponential blowups for things like:
79+-- ''a'''a''a'''a''a'''a''a'''a
80+nested :: MWParser a -> MWParser a
81+nested p = do
82+ nestlevel <- mwMaxNestingLevel `fmap` getState
83+ guard $ nestlevel > 0
84+ updateState $ \st -> st{ mwMaxNestingLevel = mwMaxNestingLevel st - 1 }
85+ res <- p
86+ updateState $ \st -> st{ mwMaxNestingLevel = nestlevel }
87+ return res
88+
7089specialChars :: [Char]
7190specialChars = "'[]<=&*{}"
7291