git.iko.soy/typeable/pandoc
on master help sign in
Org reader: Ensure images in paragraphs are not parsed as figures

This fixes a regression introduced in 7e5220b57c5a48fabe6e43ba270db812593d3463.

commit1a8af5fc442287ef2f7775c4de179e4520487a4f authorAlbert Krewinkel <albert@zeitkraut.de> dateSat 19 Nov 2016, 01:17 (10 years ago) parentf9df62c
4 files changed+54−27
src/Text/Pandoc/Readers/Org/BlockStarts.hs+25−1 src/Text/Pandoc/Readers/Org/Blocks.hs+5−1 src/Text/Pandoc/Readers/Org/Inlines.hs+2−13 tests/Tests/Readers/Org.hs+22−12
src/Text/Pandoc/Readers/Org/BlockStarts.hs+25−1
@@ -37,9 +37,11 @@ module Text.Pandoc.Readers.Org.BlockStarts
3737 , commentLineStart
3838 , bulletListStart
3939 , orderedListStart
40+ , endOfBlock
4041 ) where
4142
42−import Text.Pandoc.Readers.Org.Parsing
43+import Control.Monad ( void )
44+import Text.Pandoc.Readers.Org.Parsing
4345
4446-- | Horizontal Line (five -- dashes or more)
4547hline :: OrgParser ()
@@ -110,3 +112,25 @@ noteMarker = try $ do
110112 , (++) <$> string "fn:"
111113 <*> many1Till (noneOf "\n\r\t ") (char ']')
112114 ]
115+
116+ -- | Succeeds if the parser is at the end of a block.
117+endOfBlock :: OrgParser ()
118+endOfBlock = lookAhead . try $ do
119+ void blankline <|> anyBlockStart <|> void noteMarker
120+ where
121+ -- | Succeeds if there is a new block starting at this position.
122+ anyBlockStart :: OrgParser ()
123+ anyBlockStart = try . choice $
124+ [ exampleLineStart
125+ , hline
126+ , metaLineStart
127+ , commentLineStart
128+ , void noteMarker
129+ , void tableStart
130+ , void drawerStart
131+ , void headerStart
132+ , void latexEnvStart
133+ , void bulletListStart
134+ , void orderedListStart
135+ ]
136+
src/Text/Pandoc/Readers/Org/Blocks.hs+5−1
@@ -623,7 +623,7 @@ propertiesDrawer = try $ do
623623figure :: OrgParser (F Blocks)
624624figure = try $ do
625625 figAttrs <- blockAttributes
626− src <- skipSpaces *> selfTarget <* skipSpaces <* newline
626+ src <- skipSpaces *> selfTarget <* skipSpaces <* endOfParagraph
627627 case cleanLinkString src of
628628 Nothing -> mzero
629629 Just imgSrc -> do
@@ -652,6 +652,10 @@ figure = try $ do
652652 then cs
653653 else "fig:" ++ cs
654654
655+-- | Succeeds if looking at the end of the current paragraph
656+endOfParagraph :: OrgParser ()
657+endOfParagraph = try $ skipSpaces *> newline *> endOfBlock
658+
655659
656660--
657661-- Examples
src/Text/Pandoc/Readers/Org/Inlines.hs+2−13
@@ -33,7 +33,7 @@ module Text.Pandoc.Readers.Org.Inlines
3333 , linkTarget
3434 ) where
3535
36−import Text.Pandoc.Readers.Org.BlockStarts
36+import Text.Pandoc.Readers.Org.BlockStarts ( endOfBlock, noteMarker )
3737import Text.Pandoc.Readers.Org.ParserState
3838import Text.Pandoc.Readers.Org.Parsing
3939import Text.Pandoc.Readers.Org.Shared
@@ -152,18 +152,7 @@ str = return . B.str <$> many1 (noneOf $ specialChars ++ "\n\r ")
152152endline :: OrgParser (F Inlines)
153153endline = try $ do
154154 newline
155− notFollowedBy blankline
156− notFollowedBy' exampleLineStart
157− notFollowedBy' hline
158− notFollowedBy' noteMarker
159− notFollowedBy' tableStart
160− notFollowedBy' drawerStart
161− notFollowedBy' headerStart
162− notFollowedBy' metaLineStart
163− notFollowedBy' latexEnvStart
164− notFollowedBy' commentLineStart
165− notFollowedBy' bulletListStart
166− notFollowedBy' orderedListStart
155+ notFollowedBy' endOfBlock
167156 decEmphasisNewlinesCount
168157 guard =<< newlinesCountWithinLimits
169158 updateLastPreCharPos
tests/Tests/Readers/Org.hs+22−12
@@ -188,19 +188,29 @@ tests =
188188 "=is_subst = True=" =?>
189189 para (code "is_subst = True")
190190
191− , "Image" =:
192− "[[./sunset.jpg]]" =?>
193− (para $ image "./sunset.jpg" "" "")
194−
195− , "Image with explicit file: prefix" =:
196− "[[file:sunrise.jpg]]" =?>
197− (para $ image "sunrise.jpg" "" "")
191+ , testGroup "Images"
192+ [ "Image" =:
193+ "[[./sunset.jpg]]" =?>
194+ (para $ image "./sunset.jpg" "" "")
195+
196+ , "Image with explicit file: prefix" =:
197+ "[[file:sunrise.jpg]]" =?>
198+ (para $ image "sunrise.jpg" "" "")
199+
200+ , "Multiple images within a paragraph" =:
201+ unlines [ "[[file:sunrise.jpg]]"
202+ , "[[file:sunset.jpg]]"
203+ ] =?>
204+ (para $ (image "sunrise.jpg" "" "")
205+ <> softbreak
206+ <> (image "sunset.jpg" "" ""))
198207
199− , "Image with html attributes" =:
200− unlines [ "#+ATTR_HTML: :width 50%"
201− , "[[file:guinea-pig.gif]]"
202− ] =?>
203− (para $ imageWith ("", [], [("width", "50%")]) "guinea-pig.gif" "" "")
208+ , "Image with html attributes" =:
209+ unlines [ "#+ATTR_HTML: :width 50%"
210+ , "[[file:guinea-pig.gif]]"
211+ ] =?>
212+ (para $ imageWith ("", [], [("width", "50%")]) "guinea-pig.gif" "" "")
213+ ]
204214
205215 , "Explicit link" =:
206216 "[[http://zeitlens.com/][pseudo-random /nonsense/]]" =?>