@@ -13,13 +13,26 @@ import System.Directory (doesDirectoryExist, doesFileExist,
13 getDirectoryContents)
13 getDirectoryContents)
14import Control.Monad (filterM)
14import Control.Monad (filterM)
15import qualified Data.ByteString as B
15import qualified Data.ByteString as B
16+import qualified Data.ByteString.Char8 as B8
16import Control.Arrow ((&&&), second, first)
17import Control.Arrow ((&&&), second, first)
17import Control.Applicative ((<$>))
18import Control.Applicative ((<$>))
18import Data.Monoid (mappend)
19import Data.Monoid (mappend)
21+-- | Embed a single file in your source code.
23+-- > import qualified Data.ByteString
25+-- > myFile :: Data.ByteString.ByteString
26+-- > myFile = $(embedFile "dirName/fileName")
20embedFile :: FilePath -> Q Exp
27embedFile :: FilePath -> Q Exp
21embedFile fp = (runIO $ B.readFile fp) >>= bsToExp
28embedFile fp = (runIO $ B.readFile fp) >>= bsToExp
30+-- | Embed a directory recusrively in your source code.
32+-- > import qualified Data.ByteString
34+-- > myDir :: [(FilePath, Data.ByteString.ByteString)]
35+-- > myDir = $(embedDir "dirName")
23embedDir :: FilePath -> Q Exp
36embedDir :: FilePath -> Q Exp
24embedDir fp = ListE <$> ((runIO $ fileList fp) >>= mapM pairToExp)
37embedDir fp = ListE <$> ((runIO $ fileList fp) >>= mapM pairToExp)
@@ -31,12 +44,11 @@ pairToExp (path, bs) = do
31bsToExp :: B.ByteString -> Q Exp
44bsToExp :: B.ByteString -> Q Exp
32bsToExp bs = do
45bsToExp bs = do
33 helper <- runQ [| stringToBs |]
46 helper <- runQ [| stringToBs |]
34− let octets = B.unpack bs
47+ let chars = B8.unpack bs
35− let chars = map (toEnum . fromEnum) octets
36 return $! AppE helper $! LitE $! StringL chars
48 return $! AppE helper $! LitE $! StringL chars
38stringToBs :: String -> B.ByteString
50stringToBs :: String -> B.ByteString
39−stringToBs = B.pack . map (toEnum . fromEnum)
51+stringToBs = B8.pack
41notHidden :: FilePath -> Bool
53notHidden :: FilePath -> Bool
42notHidden ('.':_) = False
54notHidden ('.':_) = False