git.iko.soy/typeable/pandoc
on master help sign in
Org reader: fix treatment of links to images
c0caaaeAlbert Krewinkel
src/Text/Pandoc/Readers/Org/Inlines.hs+15−11
@@ -432,21 +432,27 @@ explicitOrImageLink :: PandocMonad m => OrgParser m (F Inlines)
432explicitOrImageLink = try $ do
432explicitOrImageLink = try $ do
433 char '['
433 char '['
434 srcF <- applyCustomLinkFormat =<< possiblyEmptyLinkTarget
434 srcF <- applyCustomLinkFormat =<< possiblyEmptyLinkTarget
435− title <- enclosedRaw (char '[') (char ']')
435+ descr <- enclosedRaw (char '[') (char ']')
436− title' <- parseFromString (mconcat <$> many inline) title
436+ titleF <- parseFromString (mconcat <$> many inline) descr
437 char ']'
437 char ']'
438 return $ do
438 return $ do
439 src <- srcF
439 src <- srcF
440+ title <- titleF
440− case cleanLinkString title of
441+ case cleanLinkString descr of
441 Just imgSrc | isImageFilename imgSrc ->
442 Just imgSrc | isImageFilename imgSrc ->
442− pure . B.link src "" $ B.image imgSrc mempty mempty
443+ return . B.link src "" $ B.image imgSrc mempty mempty
443 _ ->
444 _ ->
444− linkToInlinesF src =<< title'
445+ linkToInlinesF src title
445
446
446selflinkOrImage :: PandocMonad m => OrgParser m (F Inlines)
447selflinkOrImage :: PandocMonad m => OrgParser m (F Inlines)
447selflinkOrImage = try $ do
448selflinkOrImage = try $ do
448− src <- char '[' *> linkTarget <* char ']'
449+ target <- char '[' *> linkTarget <* char ']'
449− return $ linkToInlinesF src (B.str src)
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)
450
456
451plainLink :: PandocMonad m => OrgParser m (F Inlines)
457plainLink :: PandocMonad m => OrgParser m (F Inlines)
452plainLink = try $ do
458plainLink = try $ do
@@ -481,10 +487,8 @@ linkToInlinesF linkStr =
481 "" -> pure . B.link mempty "" -- wiki link (empty by convention)
487 "" -> pure . B.link mempty "" -- wiki link (empty by convention)
482 ('#':_) -> pure . B.link linkStr "" -- document-local fraction
488 ('#':_) -> pure . B.link linkStr "" -- document-local fraction
483 _ -> case cleanLinkString linkStr of
489 _ -> case cleanLinkString linkStr of
484− (Just cleanedLink) -> if isImageFilename cleanedLink
490+ Just extTgt -> return . B.link extTgt ""
485− then const . pure $ B.image cleanedLink "" ""
486− else pure . B.link cleanedLink ""
487− Nothing -> internalLink linkStr -- other internal link
491+ Nothing -> internalLink linkStr -- other internal link
488
492
489internalLink :: String -> Inlines -> F Inlines
493internalLink :: String -> Inlines -> F Inlines
490internalLink link title = do
494internalLink link title = do
src/Text/Pandoc/Readers/Org/Shared.hs+1−2
@@ -61,8 +61,7 @@ cleanLinkString s =
61 '.':'.':'/':_ -> Just s -- relative path
61 '.':'.':'/':_ -> Just s -- relative path
62 -- Relative path or URL (file schema)
62 -- Relative path or URL (file schema)
63 'f':'i':'l':'e':':':s' -> Just $ if "//" `isPrefixOf` s' then s else s'
63 'f':'i':'l':'e':':':s' -> Just $ if "//" `isPrefixOf` s' then s else s'
64− _ | isUrl s -> Just s -- URL
64+ _ -> if isUrl s then Just s else Nothing
65− _ -> Nothing
66 where
65 where
67 isUrl :: String -> Bool
66 isUrl :: String -> Bool
68 isUrl cs =
67 isUrl cs =
test/Tests/Readers/Org/Inline.hs+17−13
@@ -184,26 +184,26 @@ tests =
184
184
185 , testGroup "Images"
185 , testGroup "Images"
186 [ "Image" =:
186 [ "Image" =:
187− "[[./sunset.jpg]]" =?>
187+ "[[./sunset.jpg]]" =?>
188− para (image "./sunset.jpg" "" "")
188+ para (image "./sunset.jpg" "" "")
189
189
190 , "Image with explicit file: prefix" =:
190 , "Image with explicit file: prefix" =:
191− "[[file:sunrise.jpg]]" =?>
191+ "[[file:sunrise.jpg]]" =?>
192− para (image "sunrise.jpg" "" "")
192+ para (image "sunrise.jpg" "" "")
193
193
194 , "Multiple images within a paragraph" =:
194 , "Multiple images within a paragraph" =:
195− T.unlines [ "[[file:sunrise.jpg]]"
195+ T.unlines [ "[[file:sunrise.jpg]]"
196− , "[[file:sunset.jpg]]"
196+ , "[[file:sunset.jpg]]"
197− ] =?>
197+ ] =?>
198− para ((image "sunrise.jpg" "" "")
198+ para (image "sunrise.jpg" "" ""
199 <> softbreak
199 <> softbreak
200− <> (image "sunset.jpg" "" ""))
200+ <> image "sunset.jpg" "" "")
201
201
202 , "Image with html attributes" =:
202 , "Image with html attributes" =:
203− T.unlines [ "#+ATTR_HTML: :width 50%"
203+ T.unlines [ "#+ATTR_HTML: :width 50%"
204− , "[[file:guinea-pig.gif]]"
204+ , "[[file:guinea-pig.gif]]"
205− ] =?>
205+ ] =?>
206− para (imageWith ("", [], [("width", "50%")]) "guinea-pig.gif" "" "")
206+ para (imageWith ("", [], [("width", "50%")]) "guinea-pig.gif" "" "")
207 ]
207 ]
208
208
209 , "Explicit link" =:
209 , "Explicit link" =:
@@ -235,6 +235,10 @@ tests =
235 "[[http://example.com][./logo.png]]" =?>
235 "[[http://example.com][./logo.png]]" =?>
236 para (link "http://example.com" "" (image "./logo.png" "" ""))
236 para (link "http://example.com" "" (image "./logo.png" "" ""))
237
237
238+ , "Link to image" =:
239+ "[[https://example.com/image.jpg][Look!]]" =?>
240+ para (link "https://example.com/image.jpg" "" (str "Look!"))
241+
238 , "Plain link" =:
242 , "Plain link" =:
239 "Posts on http://zeitlens.com/ can be funny at times." =?>
243 "Posts on http://zeitlens.com/ can be funny at times." =?>
240 para (spcSep [ "Posts", "on"
244 para (spcSep [ "Posts", "on"