git.iko.soy/typeable/pandoc
on master help sign in
Org reader: absolute, relative paths in links

The org reader was to restrictive when parsing links, some relative links and links to files given as absolute paths were not recognized correctly. The org reader's link parsing function was amended to handle such cases properly. This fixes #1741

commitdaaf635806fba3ec459da3c2c97301920cea1270 authorAlbert Krewinkel <albert@zeitkraut.de> dateWed 5 Nov 2014, 22:27 (12 years ago) parentf3ac419
2 files changed+27−14
src/Text/Pandoc/Readers/Org.hs+19−14 tests/Tests/Readers/Org.hs+8
src/Text/Pandoc/Readers/Org.hs+19−14
@@ -1139,27 +1139,32 @@ applyCustomLinkFormat link = do
11391139 formatter <- M.lookup linkType <$> asksF orgStateLinkFormatters
11401140 return $ maybe link ($ drop 1 rest) formatter
11411141
1142−
11431142linkToInlinesF :: String -> Inlines -> F Inlines
1144−linkToInlinesF s@('#':_) = pure . B.link s ""
1145−linkToInlinesF s
1146− | isImageFilename s = const . pure $ B.image s "" ""
1147− | isUri s = pure . B.link s ""
1148− | isRelativeUrl s = pure . B.link s ""
1149−linkToInlinesF s = \title -> do
1150− anchorB <- (s `elem`) <$> asksF orgStateAnchorIds
1151− if anchorB
1152− then pure $ B.link ('#':s) "" title
1153− else pure $ B.emph title
1154−
1155−isRelativeUrl :: String -> Bool
1156−isRelativeUrl s = (':' `notElem` s) && ("./" `isPrefixOf` s)
1143+linkToInlinesF s =
1144+ case s of
1145+ ('#':_) -> pure . B.link s ""
1146+ _ | isImageFilename s -> const . pure $ B.image s "" ""
1147+ _ | isUri s -> pure . B.link s ""
1148+ _ | isRelativeFilePath s -> pure . B.link s ""
1149+ _ | isAbsoluteFilePath s -> pure . B.link ("file://" ++ s) ""
1150+ _ -> \title -> do
1151+ anchorB <- (s `elem`) <$> asksF orgStateAnchorIds
1152+ if anchorB
1153+ then pure $ B.link ('#':s) "" title
1154+ else pure $ B.emph title
1155+
1156+isRelativeFilePath :: String -> Bool
1157+isRelativeFilePath s = (("./" `isPrefixOf` s) || ("../" `isPrefixOf` s)) &&
1158+ (':' `notElem` s)
11571159
11581160isUri :: String -> Bool
11591161isUri s = let (scheme, path) = break (== ':') s
11601162 in all (\c -> isAlphaNum c || c `elem` ".-") scheme
11611163 && not (null path)
11621164
1165+isAbsoluteFilePath :: String -> Bool
1166+isAbsoluteFilePath = ('/' ==) . head
1167+
11631168isImageFilename :: String -> Bool
11641169isImageFilename filename =
11651170 any (\x -> ('.':x) `isSuffixOf` filename) imageExtensions &&
tests/Tests/Readers/Org.hs+8
@@ -197,6 +197,14 @@ tests =
197197 "[[http://zeitlens.com/]]" =?>
198198 (para $ link "http://zeitlens.com/" "" "http://zeitlens.com/")
199199
200+ , "Absolute file link" =:
201+ "[[/url][hi]]" =?>
202+ (para $ link "file:///url" "" "hi")
203+
204+ , "Link to file in parent directory" =:
205+ "[[../file.txt][moin]]" =?>
206+ (para $ link "../file.txt" "" "moin")
207+
200208 , "Image link" =:
201209 "[[sunset.png][dusk.svg]]" =?>
202210 (para $ link "sunset.png" "" (image "dusk.svg" "" ""))