Skip to content

Commit

Permalink
HTML Reader: be more forgiving about figcaption
Browse files Browse the repository at this point in the history
fixes jgm#4183
  • Loading branch information
mb21 committed Dec 21, 2017
1 parent 5d3573e commit 893fef2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Text/Pandoc/Readers/HTML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -588,17 +588,23 @@ pFigure = try $ do
skipMany pBlank
let pImg = (\x -> (Just x, Nothing)) <$>
(pOptInTag "p" pImage <* skipMany pBlank)
pCapt = (\x -> (Nothing, Just x)) <$>
(pInTags "figcaption" inline <* skipMany pBlank)
pCapt = (\x -> (Nothing, Just x)) <$> do
skipMany pBlank
bs <- pInTags "figcaption" block
skipMany pBlank
let getInlines (Plain xs) = xs
getInlines (Para xs) = xs
getInlines _ = mempty
return $ query getInlines bs
pSkip = (Nothing, Nothing) <$ pSatisfy (not . matchTagClose "figure")
res <- many (pImg <|> pCapt <|> pSkip)
let mbimg = msum $ map fst res
let mbcap = msum $ map snd res
TagClose _ <- pSatisfy (matchTagClose "figure")
let caption = fromMaybe mempty mbcap
case B.toList <$> mbimg of
Just [Image attr _ (url, tit)] ->
return $ B.para $ B.imageWith attr url ("fig:" ++ tit) caption
Just [Image attr alt (url, tit)] ->
let caption = fromMaybe alt mbcap
in return $ B.para $ B.imageWith attr url ("fig:" ++ tit) $ B.fromList caption
_ -> mzero

pCodeBlock :: PandocMonad m => TagParser m Blocks
Expand Down
32 changes: 32 additions & 0 deletions test/command/4183.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
```
% pandoc -f html -t native
<figure>
<img src="foo" alt="bar">
</figure>
^D
[Para [Image ("",[],[]) [Str "bar"] ("foo","fig:")]]
```

```
% pandoc -f html -t native
<figure>
<img src="foo" alt="bar">
<figcaption>
<div>
baz
</div>
</figcaption>
</figure>
^D
[Para [Image ("",[],[]) [Str "baz"] ("foo","fig:")]]
```

```
% pandoc -f html -t native
<figure>
<img src="foo">
<figcaption><p><em>baz</em></p></figcaption>
</figure>
^D
[Para [Image ("",[],[]) [Emph [Str "baz"]] ("foo","fig:")]]
```

0 comments on commit 893fef2

Please sign in to comment.