Mediawiki reader: improve table parsing.

This fixes detection of table attributes and also
handles `!` characters in cells.

Closes #4508.
This commit is contained in:
John MacFarlane 2018-03-28 08:59:34 -07:00
parent d0a7dbd948
commit 5a79948e0c
2 changed files with 29 additions and 14 deletions

View file

@ -232,7 +232,8 @@ para = do
table :: PandocMonad m => MWParser m Blocks
table = do
tableStart
styles <- option [] parseAttrs
styles <- option [] $
parseAttrs <* skipMany spaceChar <* optional (char '|')
skipMany spaceChar
optional blanklines
let tableWidth = case lookup "width" styles of
@ -283,17 +284,29 @@ rowsep = try $ guardColumnOne *> skipSpaces *> sym "|-" <*
cellsep :: PandocMonad m => MWParser m ()
cellsep = try $ do
col <- sourceColumn <$> getPosition
skipSpaces
(char '|' *> notFollowedBy (oneOf "-}+") *> optional (char '|'))
<|> (char '!' *> optional (char '!'))
let pipeSep = do
char '|'
notFollowedBy (oneOf "-}+")
if col == 1
then optional (char '|')
else void (char '|')
let exclSep = do
char '!'
if col == 1
then optional (char '!')
else void (char '!')
pipeSep <|> exclSep
tableCaption :: PandocMonad m => MWParser m Inlines
tableCaption = try $ do
guardColumnOne
skipSpaces
sym "|+"
optional (try $ parseAttr *> skipSpaces *> char '|' *> skipSpaces)
(trimInlines . mconcat) <$> many (notFollowedBy (cellsep <|> rowsep) *> inline)
optional (try $ parseAttr *> skipSpaces *> char '|' *> blanklines)
(trimInlines . mconcat) <$>
many (notFollowedBy (cellsep <|> rowsep) *> inline)
tableRow :: PandocMonad m => MWParser m [((Alignment, Double), Blocks)]
tableRow = try $ skipMany htmlComment *> many tableCell

View file

@ -67,7 +67,8 @@
! Aantal
|-
| 1
|align=left| {{FR-VLAG}} [[Sébastien Loeb]] | 78
|align=left| {{FR-VLAG}} [[Sébastien Loeb]]
| 78
|-
| 2
|align=left| {{FR-VLAG}} '''[[Sébastien Ogier]]'''
@ -79,24 +80,25 @@
|}
^D
<table>
<thead>
<tr class="header">
<th><p>Plaats</p></th>
<th><p>Rijder</p></th>
<th><p>Aantal</p></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td></td>
<td><p>Plaats</p></td>
<td><p>Rijder</p></td>
<td><p>Aantal</p></td>
</tr>
<tr class="even">
<td><p>1</p></td>
<td><p><a href="Sébastien_Loeb" title="wikilink">Sébastien Loeb</a></p></td>
<td><p>78</p></td>
</tr>
<tr class="odd">
<tr class="even">
<td><p>2</p></td>
<td><p><strong><a href="Sébastien_Ogier" title="wikilink">Sébastien Ogier</a></strong></p></td>
<td><p>38</p></td>
</tr>
<tr class="even">
<tr class="odd">
<td><p>10</p></td>
<td><p><a href="Hannu_Mikkola" title="wikilink">Hannu Mikkola</a></p></td>
<td><p>18</p></td>