@@ -30,7 +30,6 @@ Conversion of mediawiki text to 'Pandoc' document.
3030-}
3131{-
3232TODO:
33−_ support images http://www.mediawiki.org/wiki/Help:Images
3433_ support tables http://www.mediawiki.org/wiki/Help:Tables
3534- footnotes?
3635-}
@@ -147,6 +146,7 @@ parseMediaWiki = do
147146
148147block :: MWParser Blocks
149148block = mempty <$ skipMany1 blankline
149+ <|> table
150150 <|> header
151151 <|> hrule
152152 <|> orderedList
@@ -161,6 +161,48 @@ block = mempty <$ skipMany1 blankline
161161para :: MWParser Blocks
162162para = B.para . trimInlines . mconcat <$> many1 inline
163163
164+table :: MWParser Blocks
165+table = do
166+ tableStart
167+ caption <- option mempty tableCaption
168+ optional rowsep
169+ hasheader <- option False $ True <$ (lookAhead (char '!'))
170+ hdr <- tableRow
171+ rows' <- many $ try $ rowsep *> tableRow
172+ tableEnd
173+ -- TODO handle cellspecs from styles and aligns...
174+ let cols = length $ head rows'
175+ let (headers,rows) = if hasheader
176+ then (hdr, rows')
177+ else (replicate cols mempty, hdr:rows')
178+ let cellspecs = replicate cols (AlignDefault, 0.0)
179+ return $ B.table caption cellspecs headers rows
180+
181+tableStart :: MWParser ()
182+tableStart = try $ guardColumnOne *> sym "{|" <* blanklines
183+
184+tableEnd :: MWParser ()
185+tableEnd = try $ guardColumnOne *> sym "|}" <* blanklines
186+
187+rowsep :: MWParser ()
188+rowsep = try $ guardColumnOne *> sym "|-" <* blanklines
189+
190+-- TODO add something like 'guard inTable' since this is used in endline
191+cellsep :: MWParser ()
192+cellsep = try $ guardColumnOne <*
193+ (char '!' <|> (char '|' <* notFollowedBy (oneOf "-}+")))
194+
195+tableCaption :: MWParser Inlines
196+tableCaption = try $ guardColumnOne *> sym "|+" *> skipMany spaceChar *>
197+ (trimInlines . mconcat <$> (many inline)) <* skipMany blankline
198+
199+tableRow :: MWParser [Blocks]
200+tableRow = try $ many tableCell <* skipMany blankline
201+
202+tableCell :: MWParser Blocks
203+tableCell =
204+ try $ cellsep *> skipMany spaceChar *> (mconcat <$> (many block))
205+
164206template :: MWParser Blocks
165207template = B.rawBlock "mediawiki" <$> doublebrackets
166208 where doublebrackets = try $ do
@@ -200,9 +242,12 @@ syntaxhighlight attrs = try $ do
200242hrule :: MWParser Blocks
201243hrule = B.horizontalRule <$ try (string "----" *> many (char '-') *> newline)
202244
245+guardColumnOne :: MWParser ()
246+guardColumnOne = getPosition >>= \pos -> guard (sourceColumn pos == 1)
247+
203248preformatted :: MWParser Blocks
204249preformatted = try $ do
205− getPosition >>= \pos -> guard (sourceColumn pos == 1)
250+ guardColumnOne
206251 char ' '
207252 let endline' = B.linebreak <$ (try $ newline <* char ' ')
208253 let whitespace' = B.str <$> many1 ('\160' <$ spaceChar)
@@ -219,8 +264,7 @@ preformatted = try $ do
219264
220265header :: MWParser Blocks
221266header = try $ do
222− col <- sourceColumn <$> getPosition
223− guard $ col == 1 -- header must be at beginning of line
267+ guardColumnOne
224268 eqs <- many1 (char '=')
225269 let lev = length eqs
226270 guard $ lev <= 6
@@ -361,7 +405,7 @@ inlineTag = do
361405
362406special :: MWParser Inlines
363407special = B.str <$> count 1 (notFollowedBy' (htmlTag isBlockTag') *>
364− oneOf specialChars)
408+ notFollowedBy (char '|') *> oneOf specialChars)
365409
366410inlineHtml :: MWParser Inlines
367411inlineHtml = B.rawInline "html" . snd <$> htmlTag isInlineTag
@@ -373,6 +417,9 @@ endline :: MWParser ()
373417endline = () <$ try (newline <*
374418 notFollowedBy blankline <*
375419 notFollowedBy' hrule <*
420+ notFollowedBy tableStart <*
421+ notFollowedBy' template <*
422+ notFollowedBy cellsep <*
376423 notFollowedBy anyListStart)
377424
378425image :: MWParser Inlines