git.iko.soy/typeable/pandoc
on master help sign in
SelfContained: Get mime type from HTTP request if possible.

--webtex --self-contained now works.

commit42eb96a8b3a7af0e1c25c1e59d9ed3417cdc235c authorJohn MacFarlane <fiddlosopher@gmail.com> dateSun 4 Dec 2011, 15:58 (15 years ago) parentfc4d465
1 file changed+30−18
src/Text/Pandoc/SelfContained.hs+30−18
src/Text/Pandoc/SelfContained.hs+30−18
@@ -45,31 +45,40 @@ import qualified Data.ByteString.Lazy as L
4545import Text.Pandoc.Shared (findDataFile)
4646import System.Directory (doesFileExist)
4747
48−getItem :: Maybe FilePath -> String -> IO ByteString
48+getItem :: Maybe FilePath -> String -> IO (ByteString, Maybe String)
4949getItem userdata f =
5050 if isAbsoluteURI f
5151 then openURL f
5252 else do
53+ let ext = case takeExtension f of
54+ ".gz" -> takeExtension $ dropExtension f
55+ x -> x
5356 exists <- doesFileExist f
5457 if exists
55− then B.readFile f
58+ then do
59+ cont <- B.readFile f
60+ return (cont, mimeTypeFor ext)
5661 else do
5762 res <- findDataFile userdata f
5863 exists' <- doesFileExist res
5964 if exists'
60− then B.readFile res
65+ then do
66+ cont <- B.readFile res
67+ return (cont, mimeTypeFor ext)
6168 else error $ "Could not find `" ++ f ++ "'"
6269
63−openURL :: String -> IO ByteString
64−openURL u = getResponseBody =<< simpleHTTP (getReq u)
70+-- TODO - have this return mime type too - then it can work for google
71+-- chart API, e.g.
72+openURL :: String -> IO (ByteString, Maybe String)
73+openURL u = getBodyAndMimeType =<< simpleHTTP (getReq u)
6574 where getReq v = case parseURI v of
6675 Nothing -> error $ "Could not parse URI: " ++ v
6776 Just u' -> mkRequest GET u'
77+ getBodyAndMimeType (Left err) = fail (show err)
78+ getBodyAndMimeType (Right r) = return (rspBody r, findHeader HdrContentType r)
6879
69−mimeTypeFor :: String -> String
70−mimeTypeFor s = case lookup s mimetypes of
71− Nothing -> error $ "Could not find mime type for " ++ s
72− Just x -> x
80+mimeTypeFor :: String -> Maybe String
81+mimeTypeFor s = lookup (map toLower s) mimetypes
7382 where mimetypes = [ -- taken from MissingH
7483 (".a", "application/octet-stream"),
7584 (".ai", "application/postscript"),
@@ -251,16 +260,19 @@ cssURLs userdata d orig =
251260getRaw :: Maybe FilePath -> String -> String -> IO (ByteString, String)
252261getRaw userdata mimetype src = do
253262 let ext = map toLower $ takeExtension src
254− let (ext',decomp) = if ext == ".gz"
255− then (takeExtension $ dropExtension src, B.concat . L.toChunks . Gzip.decompress . L.fromChunks . (:[]))
256− else (ext, id)
257− let mime = case mimetype of
258− [] -> mimeTypeFor ext'
259− x -> x
260− raw <- getItem userdata src
263+ (raw, respMime) <- getItem userdata src
264+ let raw' = if ext == ".gz"
265+ then B.concat $ L.toChunks $ Gzip.decompress $ L.fromChunks
266+ $ [raw]
267+ else raw
268+ let mime = case (mimetype, respMime) of
269+ ("",Nothing) -> error
270+ $ "Could not determine mime type for `" ++ src ++ "'"
271+ (x, Nothing) -> x
272+ (_, Just x ) -> x
261273 result <- if mime == "text/css"
262− then cssURLs userdata (takeDirectory src) $ decomp raw
263− else return $ decomp raw
274+ then cssURLs userdata (takeDirectory src) raw'
275+ else return raw'
264276 return (result, mime)
265277
266278-- | Convert HTML into self-contained HTML, incorporating images,