git.iko.soy/typeable/pandoc
on master help sign in
HTML Reader: parse figure and figcaption (#3813)
commit7d9b782f73edfc49fbe6f0c3d6ce61328811cbc7 authorMauro Bieg <mb21@users.noreply.github.com> dateSat 22 Jul 2017, 19:22 (9 years ago) parentf9309bc
2 files changed+65
src/Text/Pandoc/Readers/HTML.hs+20 test/command/html-read-figure.md+45
src/Text/Pandoc/Readers/HTML.hs+20
@@ -188,6 +188,7 @@ block = do
188188 , pBody
189189 , pDiv
190190 , pPlain
191+ , pFigure
191192 , pRawHtmlBlock
192193 ]
193194 trace (take 60 $ show $ B.toList res)
@@ -553,6 +554,25 @@ pPara = do
553554 contents <- trimInlines <$> pInTags "p" inline
554555 return $ B.para contents
555556
557+pFigure :: PandocMonad m => TagParser m Blocks
558+pFigure = do
559+ TagOpen _ _ <- pSatisfy (matchTagOpen "figure" [])
560+ skipMany pBlank
561+ let pImg = pOptInTag "p" pImage <* skipMany pBlank
562+ pCapt = option mempty $ pInTags "figcaption" inline <* skipMany pBlank
563+ pImgCapt = do
564+ img <- pImg
565+ cap <- pCapt
566+ return (img, cap)
567+ pCaptImg = do
568+ cap <- pCapt
569+ img <- pImg
570+ return (img, cap)
571+ (imgMany, caption) <- pImgCapt <|> pCaptImg
572+ TagClose _ <- pSatisfy (matchTagClose "figure")
573+ let (Image attr _ (url, tit)):_ = B.toList imgMany
574+ return $ B.para $ B.imageWith attr url ("fig:" ++ tit) caption
575+
556576pCodeBlock :: PandocMonad m => TagParser m Blocks
557577pCodeBlock = try $ do
558578 TagOpen _ attr' <- pSatisfy (matchTagOpen "pre" [])
test/command/html-read-figure.mdnew file+45
@@ -0,0 +1,45 @@
1+```
2+% pandoc -f html -t native
3+<figure>
4+ <img src="foo.png" title="voyage">
5+ <figcaption>bar</figcaption>
6+</figure>
7+^D
8+[Para [Image ("",[],[]) [Str "bar"] ("foo.png","fig:voyage")]]
9+```
10+
11+```
12+% pandoc -f html -t native
13+<figure>
14+ <figcaption>bar</figcaption>
15+ <img src="foo.png" title="voyage">
16+</figure>
17+^D
18+[Para [Image ("",[],[]) [Str "bar"] ("foo.png","fig:voyage")]]
19+```
20+
21+```
22+% pandoc -f html -t native
23+<figure>
24+ <img src="foo.png" title="voyage">
25+</figure>
26+^D
27+[Para [Image ("",[],[]) [] ("foo.png","fig:voyage")]]
28+```
29+
30+```
31+% pandoc -f html -t native
32+<figure>
33+ <p><img src="foo.png" title="voyage"></p>
34+ <figcaption>bar</figcaption>
35+</figure>
36+^D
37+[Para [Image ("",[],[]) [Str "bar"] ("foo.png","fig:voyage")]]
38+```
39+
40+```
41+% pandoc -f html -t native
42+<figure><img src="foo.png" title="voyage" alt="this is ignored"><figcaption>bar <strong>baz</strong></figcaption></figure>
43+^D
44+[Para [Image ("",[],[]) [Str "bar",Space,Strong [Str "baz"]] ("foo.png","fig:voyage")]]
45+```