git.iko.soy/typeable/pandoc
on master help sign in
Org reader: fix treatment of links to images

Links with descriptions which are pointing to images are no longer read as inline images, but as proper links. Fixes: #5191

commitc0caaaeabb3646d6156a0857e250969b4244cdc5 authorAlbert Krewinkel <albert@zeitkraut.de> dateTue 1 Jan 2019, 20:53 (8 years ago) parentaf887e9
3 files changed+33−26
src/Text/Pandoc/Readers/Org/Inlines.hs+15−11 src/Text/Pandoc/Readers/Org/Shared.hs+1−2 test/Tests/Readers/Org/Inline.hs+17−13
src/Text/Pandoc/Readers/Org/Inlines.hs+15−11
@@ -432,21 +432,27 @@ explicitOrImageLink :: PandocMonad m => OrgParser m (F Inlines)
432432explicitOrImageLink = try $ do
433433 char '['
434434 srcF <- applyCustomLinkFormat =<< possiblyEmptyLinkTarget
435− title <- enclosedRaw (char '[') (char ']')
436− title' <- parseFromString (mconcat <$> many inline) title
435+ descr <- enclosedRaw (char '[') (char ']')
436+ titleF <- parseFromString (mconcat <$> many inline) descr
437437 char ']'
438438 return $ do
439439 src <- srcF
440− case cleanLinkString title of
440+ title <- titleF
441+ case cleanLinkString descr of
441442 Just imgSrc | isImageFilename imgSrc ->
442− pure . B.link src "" $ B.image imgSrc mempty mempty
443+ return . B.link src "" $ B.image imgSrc mempty mempty
443444 _ ->
444− linkToInlinesF src =<< title'
445+ linkToInlinesF src title
445446
446447selflinkOrImage :: PandocMonad m => OrgParser m (F Inlines)
447448selflinkOrImage = try $ do
448− src <- char '[' *> linkTarget <* char ']'
449− return $ linkToInlinesF src (B.str src)
449+ target <- char '[' *> linkTarget <* char ']'
450+ case cleanLinkString target of
451+ Nothing -> return $ internalLink target (B.str target)
452+ Just nonDocTgt -> returnF $
453+ if isImageFilename nonDocTgt
454+ then B.image nonDocTgt "" ""
455+ else B.link nonDocTgt "" (B.str target)
450456
451457plainLink :: PandocMonad m => OrgParser m (F Inlines)
452458plainLink = try $ do
@@ -481,10 +487,8 @@ linkToInlinesF linkStr =
481487 "" -> pure . B.link mempty "" -- wiki link (empty by convention)
482488 ('#':_) -> pure . B.link linkStr "" -- document-local fraction
483489 _ -> case cleanLinkString linkStr of
484− (Just cleanedLink) -> if isImageFilename cleanedLink
485− then const . pure $ B.image cleanedLink "" ""
486− else pure . B.link cleanedLink ""
487− Nothing -> internalLink linkStr -- other internal link
490+ Just extTgt -> return . B.link extTgt ""
491+ Nothing -> internalLink linkStr -- other internal link
488492
489493internalLink :: String -> Inlines -> F Inlines
490494internalLink link title = do
src/Text/Pandoc/Readers/Org/Shared.hs+1−2
@@ -61,8 +61,7 @@ cleanLinkString s =
6161 '.':'.':'/':_ -> Just s -- relative path
6262 -- Relative path or URL (file schema)
6363 'f':'i':'l':'e':':':s' -> Just $ if "//" `isPrefixOf` s' then s else s'
64− _ | isUrl s -> Just s -- URL
65− _ -> Nothing
64+ _ -> if isUrl s then Just s else Nothing
6665 where
6766 isUrl :: String -> Bool
6867 isUrl cs =
test/Tests/Readers/Org/Inline.hs+17−13
@@ -184,26 +184,26 @@ tests =
184184
185185 , testGroup "Images"
186186 [ "Image" =:
187− "[[./sunset.jpg]]" =?>
188− para (image "./sunset.jpg" "" "")
187+ "[[./sunset.jpg]]" =?>
188+ para (image "./sunset.jpg" "" "")
189189
190190 , "Image with explicit file: prefix" =:
191− "[[file:sunrise.jpg]]" =?>
192− para (image "sunrise.jpg" "" "")
191+ "[[file:sunrise.jpg]]" =?>
192+ para (image "sunrise.jpg" "" "")
193193
194194 , "Multiple images within a paragraph" =:
195− T.unlines [ "[[file:sunrise.jpg]]"
196− , "[[file:sunset.jpg]]"
197− ] =?>
198− para ((image "sunrise.jpg" "" "")
195+ T.unlines [ "[[file:sunrise.jpg]]"
196+ , "[[file:sunset.jpg]]"
197+ ] =?>
198+ para (image "sunrise.jpg" "" ""
199199 <> softbreak
200− <> (image "sunset.jpg" "" ""))
200+ <> image "sunset.jpg" "" "")
201201
202202 , "Image with html attributes" =:
203− T.unlines [ "#+ATTR_HTML: :width 50%"
204− , "[[file:guinea-pig.gif]]"
205− ] =?>
206− para (imageWith ("", [], [("width", "50%")]) "guinea-pig.gif" "" "")
203+ T.unlines [ "#+ATTR_HTML: :width 50%"
204+ , "[[file:guinea-pig.gif]]"
205+ ] =?>
206+ para (imageWith ("", [], [("width", "50%")]) "guinea-pig.gif" "" "")
207207 ]
208208
209209 , "Explicit link" =:
@@ -235,6 +235,10 @@ tests =
235235 "[[http://example.com][./logo.png]]" =?>
236236 para (link "http://example.com" "" (image "./logo.png" "" ""))
237237
238+ , "Link to image" =:
239+ "[[https://example.com/image.jpg][Look!]]" =?>
240+ para (link "https://example.com/image.jpg" "" (str "Look!"))
241+
238242 , "Plain link" =:
239243 "Posts on http://zeitlens.com/ can be funny at times." =?>
240244 para (spcSep [ "Posts", "on"