summaryrefslogtreecommitdiff
path: root/src/Text
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2022-08-02 23:16:47 +0200
committerAlbert Krewinkel <albert@zeitkraut.de>2022-08-02 23:49:10 +0200
commit516c827d613d6a9b1f2aaf18610125c3aae0a8ac (patch)
treecacb745e1f21949e5aa876d9338a5487123699b7 /src/Text
parent4633fc3ca05e73d0a2d38e3fa31456f08cf5bb47 (diff)
Org reader: recognize absolute paths on Windows
Fixes: #8201
Diffstat (limited to 'src/Text')
-rw-r--r--src/Text/Pandoc/Readers/Org/Shared.hs16
1 files changed, 13 insertions, 3 deletions
diff --git a/src/Text/Pandoc/Readers/Org/Shared.hs b/src/Text/Pandoc/Readers/Org/Shared.hs
index 7f297cbcc..f3ef290da 100644
--- a/src/Text/Pandoc/Readers/Org/Shared.hs
+++ b/src/Text/Pandoc/Readers/Org/Shared.hs
@@ -20,6 +20,8 @@ import Data.Char (isAlphaNum)
import Data.Text (Text)
import qualified Data.Text as T
import System.FilePath (isValid, takeExtension)
+import qualified System.FilePath.Posix as Posix
+import qualified System.FilePath.Windows as Windows
import Text.Pandoc.Shared (elemText)
-- | Check whether the given string looks like the path to of URL of an image.
@@ -37,13 +39,21 @@ isImageFilename fp = hasImageExtension && (isValid (T.unpack fp) || isKnownProto
-- the string does not appear to be a link.
cleanLinkText :: Text -> Maybe Text
cleanLinkText s
- | Just _ <- T.stripPrefix "/" s = Just $ "file://" <> s -- absolute path
+ | isAbsolute s = Just $ "file://" <> s -- absolute path
| Just _ <- T.stripPrefix "./" s = Just s -- relative path
| Just _ <- T.stripPrefix "../" s = Just s -- relative path
-- Relative path or URL (file schema)
- | Just s' <- T.stripPrefix "file:" s = Just $ if "//" `T.isPrefixOf` s' then s else s'
- | otherwise = if isUrl s then Just s else Nothing
+ | Just s' <- T.stripPrefix "file:" s = Just $
+ if "//" `T.isPrefixOf` s'
+ then s
+ else if isAbsolute s'
+ then "file://" <> s'
+ else s'
+ | isUrl s = Just s
+ | otherwise = Nothing
where
+ isAbsolute :: Text -> Bool
+ isAbsolute = ((||) <$> Posix.isAbsolute <*> Windows.isAbsolute) . T.unpack
isUrl :: Text -> Bool
isUrl cs =
let (scheme, path) = T.break (== ':') cs