@@ -45,31 +45,40 @@ import qualified Data.ByteString.Lazy as L
45import Text.Pandoc.Shared (findDataFile)
45import Text.Pandoc.Shared (findDataFile)
46import System.Directory (doesFileExist)
46import System.Directory (doesFileExist)
48−getItem :: Maybe FilePath -> String -> IO ByteString
48+getItem :: Maybe FilePath -> String -> IO (ByteString, Maybe String)
49getItem userdata f =
49getItem userdata f =
50 if isAbsoluteURI f
50 if isAbsoluteURI f
51 then openURL f
51 then openURL f
53+ let ext = case takeExtension f of
54+ ".gz" -> takeExtension $ dropExtension f
53 exists <- doesFileExist f
56 exists <- doesFileExist f
55− then B.readFile f
59+ cont <- B.readFile f
60+ return (cont, mimeTypeFor ext)
57 res <- findDataFile userdata f
62 res <- findDataFile userdata f
58 exists' <- doesFileExist res
63 exists' <- doesFileExist res
59 if exists'
64 if exists'
60− then B.readFile res
66+ cont <- B.readFile res
67+ return (cont, mimeTypeFor ext)
61 else error $ "Could not find `" ++ f ++ "'"
68 else error $ "Could not find `" ++ f ++ "'"
70+-- TODO - have this return mime type too - then it can work for google
63−openURL :: String -> IO ByteString
72+openURL :: String -> IO (ByteString, Maybe String)
64−openURL u = getResponseBody =<< simpleHTTP (getReq u)
73+openURL u = getBodyAndMimeType =<< simpleHTTP (getReq u)
65 where getReq v = case parseURI v of
74 where getReq v = case parseURI v of
66 Nothing -> error $ "Could not parse URI: " ++ v
75 Nothing -> error $ "Could not parse URI: " ++ v
67 Just u' -> mkRequest GET u'
76 Just u' -> mkRequest GET u'
77+ getBodyAndMimeType (Left err) = fail (show err)
78+ getBodyAndMimeType (Right r) = return (rspBody r, findHeader HdrContentType r)
69−mimeTypeFor :: String -> String
80+mimeTypeFor :: String -> Maybe String
70−mimeTypeFor s = case lookup s mimetypes of
81+mimeTypeFor s = lookup (map toLower s) mimetypes
71− Nothing -> error $ "Could not find mime type for " ++ s
73 where mimetypes = [ -- taken from MissingH
82 where mimetypes = [ -- taken from MissingH
74 (".a", "application/octet-stream"),
83 (".a", "application/octet-stream"),
75 (".ai", "application/postscript"),
84 (".ai", "application/postscript"),
@@ -251,16 +260,19 @@ cssURLs userdata d orig =
251getRaw :: Maybe FilePath -> String -> String -> IO (ByteString, String)
260getRaw :: Maybe FilePath -> String -> String -> IO (ByteString, String)
252getRaw userdata mimetype src = do
261getRaw userdata mimetype src = do
253 let ext = map toLower $ takeExtension src
262 let ext = map toLower $ takeExtension src
263+ (raw, respMime) <- getItem userdata src
254− let (ext',decomp) = if ext == ".gz"
264+ let raw' = if ext == ".gz"
255− then (takeExtension $ dropExtension src, B.concat . L.toChunks . Gzip.decompress . L.fromChunks . (:[]))
265+ then B.concat $ L.toChunks $ Gzip.decompress $ L.fromChunks
256− else (ext, id)
267+ else raw
257− let mime = case mimetype of
268+ let mime = case (mimetype, respMime) of
258− [] -> mimeTypeFor ext'
269+ ("",Nothing) -> error
259− x -> x
270+ $ "Could not determine mime type for `" ++ src ++ "'"
260− raw <- getItem userdata src
271+ (x, Nothing) -> x
261 result <- if mime == "text/css"
273 result <- if mime == "text/css"
262− then cssURLs userdata (takeDirectory src) $ decomp raw
274+ then cssURLs userdata (takeDirectory src) raw'
263− else return $ decomp raw
275+ else return raw'
264 return (result, mime)
276 return (result, mime)
266-- | Convert HTML into self-contained HTML, incorporating images,
278-- | Convert HTML into self-contained HTML, incorporating images,