Skip to content

Commit

Permalink
MediaWiki reader: ensure that list starts begin at left margin.
Browse files Browse the repository at this point in the history
Including when they're in tables or other list items.

Closes jgm#2606.
  • Loading branch information
jgm committed Feb 21, 2017
1 parent f90b82d commit 5d71e37
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/Text/Pandoc/Readers/MediaWiki.hs
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,11 @@ tableCell = try $ do
attrs <- option [] $ try $ parseAttrs <* skipSpaces <* char '|' <*
notFollowedBy (char '|')
skipMany spaceChar
pos' <- getPosition
ls <- concat <$> many (notFollowedBy (cellsep <|> rowsep <|> tableEnd) *>
((snd <$> withRaw table) <|> count 1 anyChar))
bs <- parseFromString (mconcat <$> many block) ls
bs <- parseFromString (do setPosition pos'
mconcat <$> many block) ls
let align = case lookup "align" attrs of
Just "left" -> AlignLeft
Just "right" -> AlignRight
Expand Down Expand Up @@ -428,8 +430,13 @@ defListItem = try $ do
return (terms, defs)

defListTerm :: PandocMonad m => MWParser m Inlines
defListTerm = char ';' >> skipMany spaceChar >> anyLine >>=
parseFromString (trimInlines . mconcat <$> many inline)
defListTerm = do
guardColumnOne
char ';'
skipMany spaceChar
pos' <- getPosition
anyLine >>= parseFromString (do setPosition pos'
trimInlines . mconcat <$> many inline)

listStart :: PandocMonad m => Char -> MWParser m ()
listStart c = char c *> notFollowedBy listStartChar
Expand All @@ -438,27 +445,27 @@ listStartChar :: PandocMonad m => MWParser m Char
listStartChar = oneOf "*#;:"

anyListStart :: PandocMonad m => MWParser m Char
anyListStart = char '*'
<|> char '#'
<|> char ':'
<|> char ';'
anyListStart = guardColumnOne >> oneOf "*#:;"

li :: PandocMonad m => MWParser m Blocks
li = lookAhead (htmlTag (~== TagOpen "li" [])) *>
(firstParaToPlain <$> blocksInTags "li") <* spaces

listItem :: PandocMonad m => Char -> MWParser m Blocks
listItem c = try $ do
guardColumnOne
extras <- many (try $ char c <* lookAhead listStartChar)
if null extras
then listItem' c
else do
skipMany spaceChar
pos' <- getPosition
first <- concat <$> manyTill listChunk newline
rest <- many
(try $ string extras *> lookAhead listStartChar *>
(concat <$> manyTill listChunk newline))
contents <- parseFromString (many1 $ listItem' c)
contents <- parseFromString (do setPosition pos'
many1 $ listItem' c)
(unlines (first : rest))
case c of
'*' -> return $ B.bulletList contents
Expand All @@ -480,10 +487,12 @@ listItem' :: PandocMonad m => Char -> MWParser m Blocks
listItem' c = try $ do
listStart c
skipMany spaceChar
pos' <- getPosition
first <- concat <$> manyTill listChunk newline
rest <- many (try $ char c *> lookAhead listStartChar *>
(concat <$> manyTill listChunk newline))
parseFromString (firstParaToPlain . mconcat <$> many1 block)
parseFromString (do setPosition pos'
firstParaToPlain . mconcat <$> many1 block)
$ unlines $ first : rest

firstParaToPlain :: Blocks -> Blocks
Expand Down
58 changes: 58 additions & 0 deletions test/command/2606.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
```
% pandoc -f mediawiki -t html5
{|
| * hello
|}
^D
<table>
<tbody>
<tr class="odd">
<td><p>* hello</p></td>
</tr>
</tbody>
</table>
```

```
% pandoc -f mediawiki -t html5
{|
|
* hello
|}
^D
<table>
<tbody>
<tr class="odd">
<td><ul>
<li>hello</li>
</ul></td>
</tr>
</tbody>
</table>
```

```
% pandoc -f mediawiki -t html5
{|
|
* hello
|}
^D
<table>
<tbody>
<tr class="odd">
<td><p><code>* hello</code></p></td>
</tr>
</tbody>
</table>
```

```
% pandoc -f mediawiki -t html5
* * hi
^D
<ul>
<li>* hi</li>
</ul>
```

0 comments on commit 5d71e37

Please sign in to comment.