git.iko.soy/typeable/pandoc
on master help sign in
MediaWiki reader: Correctly handle indented preformatted text

without preceding or following blank line.

commit48b23d491d0b4d98792fc9e5b90ea0059ef43076 authorJohn MacFarlane <fiddlosopher@gmail.com> dateThu 28 Mar 2013, 10:47 (14 years ago) parent099b4b7
3 files changed+24−5
src/Text/Pandoc/Readers/MediaWiki.hs+17−5 tests/mediawiki-reader.native+3 tests/mediawiki-reader.wiki+4
src/Text/Pandoc/Readers/MediaWiki.hs+17−5
@@ -50,7 +50,8 @@ import Control.Monad
5050import Data.List (intersperse, intercalate, isPrefixOf )
5151import Text.HTML.TagSoup
5252import Data.Sequence (viewl, ViewL(..), (<|))
53−import Data.Char (isDigit)
53+import qualified Data.Foldable as F
54+import Data.Char (isDigit, isSpace)
5455
5556-- | Read mediawiki from an input string and return a Pandoc document.
5657readMediaWiki :: ReaderOptions -- ^ Reader options
@@ -175,7 +176,11 @@ block = mempty <$ skipMany1 blankline
175176 <|> para
176177
177178para :: MWParser Blocks
178−para = B.para . trimInlines . mconcat <$> many1 inline
179+para = do
180+ contents <- trimInlines . mconcat <$> many1 inline
181+ if F.all (==Space) contents
182+ then return mempty
183+ else return $ B.para contents
179184
180185table :: MWParser Blocks
181186table = do
@@ -330,10 +335,16 @@ preformatted = try $ do
330335 lines . fromEntities . map spToNbsp <$> try
331336 (htmlTag (~== TagOpen "nowiki" []) *>
332337 manyTill anyChar (htmlTag (~== TagClose "nowiki")))
333− let inline' = whitespace' <|> endline' <|> nowiki' <|> inline
338+ let inline' = whitespace' <|> endline' <|> nowiki'
339+ <|> (notFollowedBy newline *> inline)
334340 let strToCode (Str s) = Code ("",[],[]) s
335341 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
337348
338349header :: MWParser Blocks
339350header = try $ do
@@ -504,7 +515,8 @@ whitespace = B.space <$ (skipMany1 spaceChar <|> endline <|> htmlComment)
504515
505516endline :: MWParser ()
506517endline = () <$ try (newline <*
507− notFollowedBy blankline <*
518+ notFollowedBy spaceChar <*
519+ notFollowedBy newline <*
508520 notFollowedBy' hrule <*
509521 notFollowedBy tableStart <*
510522 notFollowedBy' header <*
tests/mediawiki-reader.native+3
@@ -170,6 +170,9 @@ Pandoc (Meta {docTitle = [], docAuthors = [], docDate = []})
170170,Para [Str "Not"]
171171,RawBlock "html" "<hr/>"
172172,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."]
173176,Header 2 ("",[],[]) [Str "templates"]
174177,RawBlock "mediawiki" "{{Welcome}}"
175178,RawBlock "mediawiki" "{{Foo:Bar}}"
tests/mediawiki-reader.wiki+4
@@ -274,6 +274,10 @@ def function():
274274
275275Not<hr/> preformatted
276276
277+Don't need
278+ a blank line
279+around a preformatted block.
280+
277281== templates ==
278282
279283{{Welcome}}