git.iko.soy/typeable/pandoc
on master help sign in
Org reader: fix image filename recognition

Use a function from the *filepath* library to check whether a string is a valid file name. The custom validity checker that was used before gave wrong results, e.g. for absolute file paths on Windows (kawabata/ox-pandoc#52).

commitd30fbc2879f58be7951eeb1cf8f9acfc83f1ebe6 authorAlbert Krewinkel <albert@zeitkraut.de> dateWed 9 May 2018, 23:03 (8 years ago) parent0eee636
1 file changed+7−6
src/Text/Pandoc/Readers/Org/Shared.hs+7−6
src/Text/Pandoc/Readers/Org/Shared.hs+7−6
@@ -36,17 +36,18 @@ module Text.Pandoc.Readers.Org.Shared
3636
3737import Prelude
3838import Data.Char (isAlphaNum)
39−import Data.List (isPrefixOf, isSuffixOf)
39+import Data.List (isPrefixOf)
40+import System.FilePath (isValid, takeExtension)
4041
4142
4243-- | Check whether the given string looks like the path to of URL of an image.
4344isImageFilename :: String -> Bool
44−isImageFilename filename =
45− any (\x -> ('.':x) `isSuffixOf` filename) imageExtensions &&
46− (any (\x -> (x ++ "://") `isPrefixOf` filename) protocols ||
47− ':' `notElem` filename)
45+isImageFilename fp = hasImageExtension && (isValid fp || isKnownProtocolUri)
4846 where
49− imageExtensions = [ "jpeg" , "jpg" , "png" , "gif" , "svg" ]
47+ hasImageExtension = takeExtension fp `elem` imageExtensions
48+ isKnownProtocolUri = any (\x -> (x ++ "://") `isPrefixOf` fp) protocols
49+
50+ imageExtensions = [ ".jpeg", ".jpg", ".png", ".gif", ".svg" ]
5051 protocols = [ "file", "http", "https" ]
5152
5253-- | Cleanup and canonicalize a string describing a link. Return @Nothing@ if