@@ -0,0 +1,61 @@
1+{-# LANGUAGE TemplateHaskell #-}
7+import Language.Haskell.TH (runQ,
8+ Exp(AppE, ListE, LitE, TupE),
9+ Lit(IntegerL, StringL),
12+import System.Directory (doesDirectoryExist, doesFileExist,
13+ getDirectoryContents)
14+import Control.Monad (filterM)
15+import qualified Data.ByteString as B
16+import Control.Arrow ((&&&), second, first)
17+import Control.Applicative ((<$>))
18+import Data.Monoid (mappend)
20+embedFile :: FilePath -> Q Exp
21+embedFile fp = (runIO $ B.readFile fp) >>= bsToExp
23+embedDir :: FilePath -> Q Exp
24+embedDir fp = ListE <$> ((runIO $ fileList fp) >>= mapM pairToExp)
26+pairToExp :: (FilePath, B.ByteString) -> Q Exp
27+pairToExp (path, bs) = do
29+ return $! TupE [LitE $ StringL path, exp']
31+bsToExp :: B.ByteString -> Q Exp
33+ pack <- runQ [| B.pack |]
37+ map (LitE . IntegerL . fromIntegral) .
41+notHidden :: FilePath -> Bool
42+notHidden ('.':_) = False
45+fileList :: FilePath -> IO [(FilePath, B.ByteString)]
46+fileList top = map (first tail) <$> fileList' top ""
48+fileList' :: FilePath -> FilePath -> IO [(FilePath, B.ByteString)]
49+fileList' realTop top = do
50+ let prefix1 = top ++ "/"
51+ prefix2 = realTop ++ prefix1
52+ allContents <- filter notHidden <$> getDirectoryContents prefix2
53+ let all' = map (mappend prefix1 &&& mappend prefix2) allContents
54+ files <- filterM (doesFileExist . snd) all' >>=
55+ mapM (liftPair2 . second B.readFile)
56+ dirs <- filterM (doesDirectoryExist . snd) all' >>=
57+ mapM (fileList' realTop . fst)
58+ return $ concat $ files : dirs
60+liftPair2 :: Monad m => (a, m b) -> m (a, b)
61+liftPair2 (a, b) = b >>= \b' -> return (a, b')