diff options
author | John MacFarlane <fiddlosopher@gmail.com> | 2013-01-11 11:30:31 -0800 |
---|---|---|
committer | John MacFarlane <fiddlosopher@gmail.com> | 2013-01-11 11:30:31 -0800 |
commit | 77d9ead1b211a87aea8816f86040d269b24671c3 (patch) | |
tree | c716d8ab5fceb8f92656a9f8889aa98f4b35742a /src/Text/Pandoc/Shared.hs | |
parent | 97a3cb2e8612281f68162d9a86e7e9f3c4d88749 (diff) | |
download | pandoc-77d9ead1b211a87aea8816f86040d269b24671c3.tar.gz |
Move getItem from SelfContained to Share; export getItem.
Diffstat (limited to 'src/Text/Pandoc/Shared.hs')
-rw-r--r-- | src/Text/Pandoc/Shared.hs | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs index f23c043e1..15af4cc33 100644 --- a/src/Text/Pandoc/Shared.hs +++ b/src/Text/Pandoc/Shared.hs @@ -67,6 +67,7 @@ module Text.Pandoc.Shared ( inDirectory, readDataFile, readDataFileUTF8, + getItem, -- * Error handling err, warn, @@ -84,9 +85,10 @@ import System.Exit (exitWith, ExitCode(..)) import Data.Char ( toLower, isLower, isUpper, isAlpha, isLetter, isDigit, isSpace ) import Data.List ( find, isPrefixOf, intercalate ) -import Network.URI ( escapeURIString ) +import Network.URI ( escapeURIString, isAbsoluteURI, parseURI ) import System.Directory -import System.FilePath ( (</>) ) +import Text.Pandoc.MIME (getMimeType) +import System.FilePath ( (</>), takeExtension, dropExtension ) import Data.Generics (Typeable, Data) import qualified Control.Monad.State as S import Control.Monad (msum) @@ -97,6 +99,8 @@ import System.IO (stderr) import Text.HTML.TagSoup (renderTagsOptions, RenderOptions(..), Tag(..), renderOptions) import qualified Data.ByteString as B +import Network.HTTP (findHeader, rspBody, simpleHTTP, RequestMethod(..), + HeaderName(..), mkRequest) #ifdef EMBED_DATA_FILES import Data.FileEmbed #else @@ -541,6 +545,28 @@ readDataFileUTF8 :: Maybe FilePath -> FilePath -> IO String readDataFileUTF8 userDir fname = UTF8.toString `fmap` readDataFile userDir fname +getItem :: Maybe FilePath -> String -> IO (B.ByteString, Maybe String) +getItem userdata f = + if isAbsoluteURI f + then openURL f + else do + let mime = case takeExtension f of + ".gz" -> getMimeType $ dropExtension f + x -> getMimeType x + exists <- doesFileExist f + cont <- if exists then B.readFile f else readDataFile userdata f + return (cont, mime) + +-- TODO - have this return mime type too - then it can work for google +-- chart API, e.g. +openURL :: String -> IO (B.ByteString, Maybe String) +openURL u = getBodyAndMimeType =<< simpleHTTP (getReq u) + where getReq v = case parseURI v of + Nothing -> error $ "Could not parse URI: " ++ v + Just u' -> mkRequest GET u' + getBodyAndMimeType (Left e) = fail (show e) + getBodyAndMimeType (Right r) = return (rspBody r, findHeader HdrContentType r) + -- -- Error reporting -- |