git.iko.soy/typeable/pandoc
on master help sign in
Move getItem from SelfContained to Share; export getItem.
commit77d9ead1b211a87aea8816f86040d269b24671c3 authorJohn MacFarlane <fiddlosopher@gmail.com> dateFri 11 Jan 2013, 11:30 (14 years ago) parent97a3cb2
2 files changed+31−30
src/Text/Pandoc/SelfContained.hs+3−28 src/Text/Pandoc/Shared.hs+28−2
src/Text/Pandoc/SelfContained.hs+3−28
@@ -32,42 +32,17 @@ the HTML using data URIs.
3232-}
3333module Text.Pandoc.SelfContained ( makeSelfContained ) where
3434import Text.HTML.TagSoup
35−import Network.URI (isAbsoluteURI, parseURI, escapeURIString)
36−import Network.HTTP
35+import Network.URI (isAbsoluteURI, escapeURIString)
3736import Data.ByteString.Base64
3837import qualified Data.ByteString.Char8 as B
3938import Data.ByteString (ByteString)
40−import System.FilePath (takeExtension, dropExtension, takeDirectory, (</>))
39+import System.FilePath (takeExtension, takeDirectory, (</>))
4140import Data.Char (toLower, isAscii, isAlphaNum)
4241import Codec.Compression.GZip as Gzip
4342import qualified Data.ByteString.Lazy as L
44−import Text.Pandoc.Shared (readDataFile, renderTags')
45−import Text.Pandoc.MIME (getMimeType)
46−import System.Directory (doesFileExist)
43+import Text.Pandoc.Shared (renderTags', getItem)
4744import Text.Pandoc.UTF8 (toString, fromString)
4845
49−getItem :: Maybe FilePath -> String -> IO (ByteString, Maybe String)
50−getItem userdata f =
51− if isAbsoluteURI f
52− then openURL f
53− else do
54− let mime = case takeExtension f of
55− ".gz" -> getMimeType $ dropExtension f
56− x -> getMimeType x
57− exists <- doesFileExist f
58− cont <- if exists then B.readFile f else readDataFile userdata f
59− return (cont, mime)
60−
61−-- TODO - have this return mime type too - then it can work for google
62−-- chart API, e.g.
63−openURL :: String -> IO (ByteString, Maybe String)
64−openURL u = getBodyAndMimeType =<< simpleHTTP (getReq u)
65− where getReq v = case parseURI v of
66− Nothing -> error $ "Could not parse URI: " ++ v
67− Just u' -> mkRequest GET u'
68− getBodyAndMimeType (Left err) = fail (show err)
69− getBodyAndMimeType (Right r) = return (rspBody r, findHeader HdrContentType r)
70−
7146isOk :: Char -> Bool
7247isOk c = isAscii c && isAlphaNum c
7348
src/Text/Pandoc/Shared.hs+28−2
@@ -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--