@@ -67,6 +67,7 @@ module Text.Pandoc.Shared (
6767 inDirectory,
6868 readDataFile,
6969 readDataFileUTF8,
70+ getItem,
7071 -- * Error handling
7172 err,
7273 warn,
@@ -84,9 +85,10 @@ import System.Exit (exitWith, ExitCode(..))
8485import Data.Char ( toLower, isLower, isUpper, isAlpha,
8586 isLetter, isDigit, isSpace )
8687import Data.List ( find, isPrefixOf, intercalate )
87−import Network.URI ( escapeURIString )
88+import Network.URI ( escapeURIString, isAbsoluteURI, parseURI )
8889import System.Directory
89−import System.FilePath ( (</>) )
90+import Text.Pandoc.MIME (getMimeType)
91+import System.FilePath ( (</>), takeExtension, dropExtension )
9092import Data.Generics (Typeable, Data)
9193import qualified Control.Monad.State as S
9294import Control.Monad (msum)
@@ -97,6 +99,8 @@ import System.IO (stderr)
9799import Text.HTML.TagSoup (renderTagsOptions, RenderOptions(..), Tag(..),
98100 renderOptions)
99101import qualified Data.ByteString as B
102+import Network.HTTP (findHeader, rspBody, simpleHTTP, RequestMethod(..),
103+ HeaderName(..), mkRequest)
100104#ifdef EMBED_DATA_FILES
101105import Data.FileEmbed
102106#else
@@ -541,6 +545,28 @@ readDataFileUTF8 :: Maybe FilePath -> FilePath -> IO String
541545readDataFileUTF8 userDir fname =
542546 UTF8.toString `fmap` readDataFile userDir fname
543547
548+getItem :: Maybe FilePath -> String -> IO (B.ByteString, Maybe String)
549+getItem userdata f =
550+ if isAbsoluteURI f
551+ then openURL f
552+ else do
553+ let mime = case takeExtension f of
554+ ".gz" -> getMimeType $ dropExtension f
555+ x -> getMimeType x
556+ exists <- doesFileExist f
557+ cont <- if exists then B.readFile f else readDataFile userdata f
558+ return (cont, mime)
559+
560+-- TODO - have this return mime type too - then it can work for google
561+-- chart API, e.g.
562+openURL :: String -> IO (B.ByteString, Maybe String)
563+openURL u = getBodyAndMimeType =<< simpleHTTP (getReq u)
564+ where getReq v = case parseURI v of
565+ Nothing -> error $ "Could not parse URI: " ++ v
566+ Just u' -> mkRequest GET u'
567+ getBodyAndMimeType (Left e) = fail (show e)
568+ getBodyAndMimeType (Right r) = return (rspBody r, findHeader HdrContentType r)
569+
544570--
545571-- Error reporting
546572--