@@ -79,8 +79,6 @@ module Text.Pandoc.Shared (
7979 getDefaultReferenceODT,
8080 readDataFile,
8181 readDataFileUTF8,
82− fetchItem,
83− fetchItem',
8482 openURL,
8583 collapseFilePath,
8684 filteredFilesFromArchive,
@@ -100,7 +98,6 @@ module Text.Pandoc.Shared (
10098
10199import Text.Pandoc.Definition
102100import Text.Pandoc.Walk
103−import Text.Pandoc.MediaBag (MediaBag, lookupMedia)
104101import Text.Pandoc.Builder (Inlines, Blocks, ToMetaValue(..))
105102import qualified Text.Pandoc.Builder as B
106103import qualified Text.Pandoc.UTF8 as UTF8
@@ -111,15 +108,13 @@ import Data.List ( find, stripPrefix, intercalate )
111108import Data.Maybe (mapMaybe)
112109import Data.Version ( showVersion )
113110import qualified Data.Map as M
114−import Network.URI ( escapeURIString, nonStrictRelativeTo,
115− unEscapeString, parseURIReference, isAllowedInURI,
116− parseURI, URI(..) )
111+import Network.URI ( escapeURIString, unEscapeString )
117112import qualified Data.Set as Set
118113import System.Directory
119114import System.FilePath (splitDirectories, isPathSeparator)
120115import qualified System.FilePath.Posix as Posix
121−import Text.Pandoc.MIME (MimeType, getMimeType)
122−import System.FilePath ( (</>), takeExtension, dropExtension)
116+import Text.Pandoc.MIME (MimeType)
117+import System.FilePath ( (</>) )
123118import Data.Generics (Typeable, Data)
124119import qualified Control.Monad.State as S
125120import Control.Monad.Trans (MonadIO (..))
@@ -752,64 +747,6 @@ readDataFileUTF8 :: Maybe FilePath -> FilePath -> IO String
752747readDataFileUTF8 userDir fname =
753748 UTF8.toString `fmap` readDataFile userDir fname
754749
755−-- | Specialized version of parseURIReference that disallows
756−-- single-letter schemes. Reason: these are usually windows absolute
757−-- paths.
758−parseURIReference' :: String -> Maybe URI
759−parseURIReference' s =
760− case parseURIReference s of
761− Just u
762− | length (uriScheme u) > 2 -> Just u
763− | null (uriScheme u) -> Just u -- protocol-relative
764− _ -> Nothing
765−
766−-- | Fetch an image or other item from the local filesystem or the net.
767−-- Returns raw content and maybe mime type.
768−fetchItem :: Maybe String -> String
769− -> IO (Either E.SomeException (BS.ByteString, Maybe MimeType))
770−fetchItem sourceURL s =
771− case (sourceURL >>= parseURIReference' . ensureEscaped, ensureEscaped s) of
772− (Just u, s') -> -- try fetching from relative path at source
773− case parseURIReference' s' of
774− Just u' -> openURL $ show $ u' `nonStrictRelativeTo` u
775− Nothing -> openURL s' -- will throw error
776− (Nothing, s'@('/':'/':_)) -> -- protocol-relative URI
777− case parseURIReference' s' of
778− Just u' -> openURL $ show $ u' `nonStrictRelativeTo` httpcolon
779− Nothing -> openURL s' -- will throw error
780− (Nothing, s') ->
781− case parseURI s' of -- requires absolute URI
782− -- We don't want to treat C:/ as a scheme:
783− Just u' | length (uriScheme u') > 2 -> openURL (show u')
784− Just u' | uriScheme u' == "file:" ->
785− E.try $ readLocalFile $ dropWhile (=='/') (uriPath u')
786− _ -> E.try $ readLocalFile fp -- get from local file system
787− where readLocalFile f = do
788− cont <- BS.readFile f
789− return (cont, mime)
790− httpcolon = URI{ uriScheme = "http:",
791− uriAuthority = Nothing,
792− uriPath = "",
793− uriQuery = "",
794− uriFragment = "" }
795− dropFragmentAndQuery = takeWhile (\c -> c /= '?' && c /= '#')
796− fp = unEscapeString $ dropFragmentAndQuery s
797− mime = case takeExtension fp of
798− ".gz" -> getMimeType $ dropExtension fp
799− ".svgz" -> getMimeType $ dropExtension fp ++ ".svg"
800− x -> getMimeType x
801− ensureEscaped = escapeURIString isAllowedInURI . map convertSlash
802− convertSlash '\\' = '/'
803− convertSlash x = x
804−
805−-- | Like 'fetchItem', but also looks for items in a 'MediaBag'.
806−fetchItem' :: MediaBag -> Maybe String -> String
807− -> IO (Either E.SomeException (BS.ByteString, Maybe MimeType))
808−fetchItem' media sourceURL s = do
809− case lookupMedia s media of
810− Nothing -> fetchItem sourceURL s
811− Just (mime, bs) -> return $ Right (BS.concat $ toChunks bs, Just mime)
812−
813750-- | Read from a URL and return raw data and maybe mime type.
814751openURL :: String -> IO (Either E.SomeException (BS.ByteString, Maybe MimeType))
815752openURL u