git.iko.soy/typeable/pandoc
on master help sign in
MediaWiki reader: Correctly handle indented preformatted text
48b23d4John MacFarlane
src/Text/Pandoc/Readers/MediaWiki.hs+17−5
@@ -50,7 +50,8 @@ import Control.Monad
50import Data.List (intersperse, intercalate, isPrefixOf )
50import Data.List (intersperse, intercalate, isPrefixOf )
51import Text.HTML.TagSoup
51import Text.HTML.TagSoup
52import Data.Sequence (viewl, ViewL(..), (<|))
52import Data.Sequence (viewl, ViewL(..), (<|))
53+import qualified Data.Foldable as F
53−import Data.Char (isDigit)
54+import Data.Char (isDigit, isSpace)
54
55
55-- | Read mediawiki from an input string and return a Pandoc document.
56-- | Read mediawiki from an input string and return a Pandoc document.
56readMediaWiki :: ReaderOptions -- ^ Reader options
57readMediaWiki :: ReaderOptions -- ^ Reader options
@@ -175,7 +176,11 @@ block = mempty <$ skipMany1 blankline
175 <|> para
176 <|> para
176
177
177para :: MWParser Blocks
178para :: MWParser Blocks
179+para = do
178−para = B.para . trimInlines . mconcat <$> many1 inline
180+ contents <- trimInlines . mconcat <$> many1 inline
181+ if F.all (==Space) contents
182+ then return mempty
183+ else return $ B.para contents
179
184
180table :: MWParser Blocks
185table :: MWParser Blocks
181table = do
186table = do
@@ -330,10 +335,16 @@ preformatted = try $ do
330 lines . fromEntities . map spToNbsp <$> try
335 lines . fromEntities . map spToNbsp <$> try
331 (htmlTag (~== TagOpen "nowiki" []) *>
336 (htmlTag (~== TagOpen "nowiki" []) *>
332 manyTill anyChar (htmlTag (~== TagClose "nowiki")))
337 manyTill anyChar (htmlTag (~== TagClose "nowiki")))
333− let inline' = whitespace' <|> endline' <|> nowiki' <|> inline
338+ let inline' = whitespace' <|> endline' <|> nowiki'
339+ <|> (notFollowedBy newline *> inline)
334 let strToCode (Str s) = Code ("",[],[]) s
340 let strToCode (Str s) = Code ("",[],[]) s
335 strToCode x = x
341 strToCode x = x
336− B.para . bottomUp strToCode . mconcat <$> many1 inline'
342+ contents <- mconcat <$> many1 inline'
343+ let spacesStr (Str xs) = all isSpace xs
344+ spacesStr _ = False
345+ if F.all spacesStr contents
346+ then return mempty
347+ else return $ B.para $ bottomUp strToCode contents
337
348
338header :: MWParser Blocks
349header :: MWParser Blocks
339header = try $ do
350header = try $ do
@@ -504,7 +515,8 @@ whitespace = B.space <$ (skipMany1 spaceChar <|> endline <|> htmlComment)
504
515
505endline :: MWParser ()
516endline :: MWParser ()
506endline = () <$ try (newline <*
517endline = () <$ try (newline <*
507− notFollowedBy blankline <*
518+ notFollowedBy spaceChar <*
519+ notFollowedBy newline <*
508 notFollowedBy' hrule <*
520 notFollowedBy' hrule <*
509 notFollowedBy tableStart <*
521 notFollowedBy tableStart <*
510 notFollowedBy' header <*
522 notFollowedBy' header <*
tests/mediawiki-reader.native+3
@@ -170,6 +170,9 @@ Pandoc (Meta {docTitle = [], docAuthors = [], docDate = []})
170,Para [Str "Not"]
170,Para [Str "Not"]
171,RawBlock "html" "<hr/>"
171,RawBlock "html" "<hr/>"
172,Para [Str "preformatted"]
172,Para [Str "preformatted"]
173+,Para [Str "Don't",Space,Str "need"]
174+,Para [Code ("",[],[]) "a\160blank\160line"]
175+,Para [Str "around",Space,Str "a",Space,Str "preformatted",Space,Str "block."]
173,Header 2 ("",[],[]) [Str "templates"]
176,Header 2 ("",[],[]) [Str "templates"]
174,RawBlock "mediawiki" "{{Welcome}}"
177,RawBlock "mediawiki" "{{Welcome}}"
175,RawBlock "mediawiki" "{{Foo:Bar}}"
178,RawBlock "mediawiki" "{{Foo:Bar}}"
tests/mediawiki-reader.wiki+4
@@ -274,6 +274,10 @@ def function():
274
274
275Not<hr/> preformatted
275Not<hr/> preformatted
276
276
277+Don't need
278+ a blank line
279+around a preformatted block.
280+
277== templates ==
281== templates ==
278
282
279{{Welcome}}
283{{Welcome}}