git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1742 from tarleb/org-fix-links

Org fix links

commit1cd65344b2c9fd1f905c52dc729ae17d03464322 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 5 Nov 2014, 14:48 (12 years ago) parentf3ac419e6cd8c9
2 files changed+36−15
src/Text/Pandoc/Readers/Org.hs+24−15 tests/Tests/Readers/Org.hs+12
src/Text/Pandoc/Readers/Org.hs+24−15
@@ -1099,7 +1099,7 @@ linkOrImage = explicitOrImageLink
10991099explicitOrImageLink :: OrgParser (F Inlines)
11001100explicitOrImageLink = try $ do
11011101 char '['
1102− srcF <- applyCustomLinkFormat =<< linkTarget
1102+ srcF <- applyCustomLinkFormat =<< possiblyEmptyLinkTarget
11031103 title <- enclosedRaw (char '[') (char ']')
11041104 title' <- parseFromString (mconcat <$> many inline) title
11051105 char ']'
@@ -1132,6 +1132,9 @@ selfTarget = try $ char '[' *> linkTarget <* char ']'
11321132linkTarget :: OrgParser String
11331133linkTarget = enclosedByPair '[' ']' (noneOf "\n\r[]")
11341134
1135+possiblyEmptyLinkTarget :: OrgParser String
1136+possiblyEmptyLinkTarget = try linkTarget <|> ("" <$ string "[]")
1137+
11351138applyCustomLinkFormat :: String -> OrgParser (F String)
11361139applyCustomLinkFormat link = do
11371140 let (linkType, rest) = break (== ':') link
@@ -1139,27 +1142,33 @@ applyCustomLinkFormat link = do
11391142 formatter <- M.lookup linkType <$> asksF orgStateLinkFormatters
11401143 return $ maybe link ($ drop 1 rest) formatter
11411144
1142−
11431145linkToInlinesF :: 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)
1146+linkToInlinesF s =
1147+ case s of
1148+ "" -> pure . B.link "" ""
1149+ ('#':_) -> pure . B.link s ""
1150+ _ | isImageFilename s -> const . pure $ B.image s "" ""
1151+ _ | isUri s -> pure . B.link s ""
1152+ _ | isRelativeFilePath s -> pure . B.link s ""
1153+ _ | isAbsoluteFilePath s -> pure . B.link ("file://" ++ s) ""
1154+ _ -> \title -> do
1155+ anchorB <- (s `elem`) <$> asksF orgStateAnchorIds
1156+ if anchorB
1157+ then pure $ B.link ('#':s) "" title
1158+ else pure $ B.emph title
1159+
1160+isRelativeFilePath :: String -> Bool
1161+isRelativeFilePath s = (("./" `isPrefixOf` s) || ("../" `isPrefixOf` s)) &&
1162+ (':' `notElem` s)
11571163
11581164isUri :: String -> Bool
11591165isUri s = let (scheme, path) = break (== ':') s
11601166 in all (\c -> isAlphaNum c || c `elem` ".-") scheme
11611167 && not (null path)
11621168
1169+isAbsoluteFilePath :: String -> Bool
1170+isAbsoluteFilePath = ('/' ==) . head
1171+
11631172isImageFilename :: String -> Bool
11641173isImageFilename filename =
11651174 any (\x -> ('.':x) `isSuffixOf` filename) imageExtensions &&
tests/Tests/Readers/Org.hs+12
@@ -197,6 +197,18 @@ 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+
208+ , "Empty link (for gitit interop)" =:
209+ "[[][New Link]]" =?>
210+ (para $ link "" "" "New Link")
211+
200212 , "Image link" =:
201213 "[[sunset.png][dusk.svg]]" =?>
202214 (para $ link "sunset.png" "" (image "dusk.svg" "" ""))