diff options
author | Albert Krewinkel <albert@zeitkraut.de> | 2019-01-01 20:53:52 +0100 |
---|---|---|
committer | Albert Krewinkel <albert@zeitkraut.de> | 2019-01-01 21:03:38 +0100 |
commit | c0caaaeabb3646d6156a0857e250969b4244cdc5 (patch) | |
tree | 0f2c65327954685d5a8b09ce0c15ce33b9d060fd | |
parent | af887e9198b2192daac4827923ea319892ad5bdd (diff) | |
download | pandoc-c0caaaeabb3646d6156a0857e250969b4244cdc5.tar.gz |
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
-rw-r--r-- | src/Text/Pandoc/Readers/Org/Inlines.hs | 26 | ||||
-rw-r--r-- | src/Text/Pandoc/Readers/Org/Shared.hs | 3 | ||||
-rw-r--r-- | test/Tests/Readers/Org/Inline.hs | 30 |
3 files changed, 33 insertions, 26 deletions
diff --git a/src/Text/Pandoc/Readers/Org/Inlines.hs b/src/Text/Pandoc/Readers/Org/Inlines.hs index b5b2b5c47..b8aee0322 100644 --- a/src/Text/Pandoc/Readers/Org/Inlines.hs +++ b/src/Text/Pandoc/Readers/Org/Inlines.hs @@ -432,21 +432,27 @@ explicitOrImageLink :: PandocMonad m => OrgParser m (F Inlines) explicitOrImageLink = try $ do char '[' srcF <- applyCustomLinkFormat =<< possiblyEmptyLinkTarget - title <- enclosedRaw (char '[') (char ']') - title' <- parseFromString (mconcat <$> many inline) title + descr <- enclosedRaw (char '[') (char ']') + titleF <- parseFromString (mconcat <$> many inline) descr char ']' return $ do src <- srcF - case cleanLinkString title of + title <- titleF + case cleanLinkString descr of Just imgSrc | isImageFilename imgSrc -> - pure . B.link src "" $ B.image imgSrc mempty mempty + return . B.link src "" $ B.image imgSrc mempty mempty _ -> - linkToInlinesF src =<< title' + linkToInlinesF src title selflinkOrImage :: PandocMonad m => OrgParser m (F Inlines) selflinkOrImage = try $ do - src <- char '[' *> linkTarget <* char ']' - return $ linkToInlinesF src (B.str src) + target <- char '[' *> linkTarget <* char ']' + case cleanLinkString target of + Nothing -> return $ internalLink target (B.str target) + Just nonDocTgt -> returnF $ + if isImageFilename nonDocTgt + then B.image nonDocTgt "" "" + else B.link nonDocTgt "" (B.str target) plainLink :: PandocMonad m => OrgParser m (F Inlines) plainLink = try $ do @@ -481,10 +487,8 @@ linkToInlinesF linkStr = "" -> pure . B.link mempty "" -- wiki link (empty by convention) ('#':_) -> pure . B.link linkStr "" -- document-local fraction _ -> case cleanLinkString linkStr of - (Just cleanedLink) -> if isImageFilename cleanedLink - then const . pure $ B.image cleanedLink "" "" - else pure . B.link cleanedLink "" - Nothing -> internalLink linkStr -- other internal link + Just extTgt -> return . B.link extTgt "" + Nothing -> internalLink linkStr -- other internal link internalLink :: String -> Inlines -> F Inlines internalLink link title = do diff --git a/src/Text/Pandoc/Readers/Org/Shared.hs b/src/Text/Pandoc/Readers/Org/Shared.hs index 71d1dd517..9e7ef9930 100644 --- a/src/Text/Pandoc/Readers/Org/Shared.hs +++ b/src/Text/Pandoc/Readers/Org/Shared.hs @@ -61,8 +61,7 @@ cleanLinkString s = '.':'.':'/':_ -> Just s -- relative path -- Relative path or URL (file schema) 'f':'i':'l':'e':':':s' -> Just $ if "//" `isPrefixOf` s' then s else s' - _ | isUrl s -> Just s -- URL - _ -> Nothing + _ -> if isUrl s then Just s else Nothing where isUrl :: String -> Bool isUrl cs = diff --git a/test/Tests/Readers/Org/Inline.hs b/test/Tests/Readers/Org/Inline.hs index 9cfcda79f..7f8873642 100644 --- a/test/Tests/Readers/Org/Inline.hs +++ b/test/Tests/Readers/Org/Inline.hs @@ -184,26 +184,26 @@ tests = , testGroup "Images" [ "Image" =: - "[[./sunset.jpg]]" =?> - para (image "./sunset.jpg" "" "") + "[[./sunset.jpg]]" =?> + para (image "./sunset.jpg" "" "") , "Image with explicit file: prefix" =: - "[[file:sunrise.jpg]]" =?> - para (image "sunrise.jpg" "" "") + "[[file:sunrise.jpg]]" =?> + para (image "sunrise.jpg" "" "") , "Multiple images within a paragraph" =: - T.unlines [ "[[file:sunrise.jpg]]" - , "[[file:sunset.jpg]]" - ] =?> - para ((image "sunrise.jpg" "" "") + T.unlines [ "[[file:sunrise.jpg]]" + , "[[file:sunset.jpg]]" + ] =?> + para (image "sunrise.jpg" "" "" <> softbreak - <> (image "sunset.jpg" "" "")) + <> image "sunset.jpg" "" "") , "Image with html attributes" =: - T.unlines [ "#+ATTR_HTML: :width 50%" - , "[[file:guinea-pig.gif]]" - ] =?> - para (imageWith ("", [], [("width", "50%")]) "guinea-pig.gif" "" "") + T.unlines [ "#+ATTR_HTML: :width 50%" + , "[[file:guinea-pig.gif]]" + ] =?> + para (imageWith ("", [], [("width", "50%")]) "guinea-pig.gif" "" "") ] , "Explicit link" =: @@ -235,6 +235,10 @@ tests = "[[http://example.com][./logo.png]]" =?> para (link "http://example.com" "" (image "./logo.png" "" "")) + , "Link to image" =: + "[[https://example.com/image.jpg][Look!]]" =?> + para (link "https://example.com/image.jpg" "" (str "Look!")) + , "Plain link" =: "Posts on http://zeitlens.com/ can be funny at times." =?> para (spcSep [ "Posts", "on" |