From 2297fc1fb6d71369ff16b14d9ca6656d4a69d1b5 Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Fri, 15 Jul 2011 12:05:54 +0300 Subject: [PATCH] Added embed, other minor fixes --- Yesod/Static.hs | 30 ++++++++++++++++++++++++++---- yesod-static.cabal | 1 + 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Yesod/Static.hs b/Yesod/Static.hs index ede6c47b..36d1fa8a 100644 --- a/Yesod/Static.hs +++ b/Yesod/Static.hs @@ -30,7 +30,7 @@ module Yesod.Static , StaticRoute (..) -- * Smart constructor , static - -- FIXME add embed + , embed -- * Template Haskell helpers , staticFiles -- * Hashing @@ -40,6 +40,7 @@ module Yesod.Static import System.Directory --import qualified System.Time import Control.Monad +import Data.FileEmbed (embedDir) import Yesod.Handler import Yesod.Core @@ -58,12 +59,18 @@ import Data.Monoid (mempty) import qualified Data.Map as M --import Data.IORef (readIORef, newIORef, writeIORef) import Network.Wai (pathInfo) +import Data.Char (isLower, isDigit) import Network.Wai.Application.Static ( StaticSettings (..) , defaultWebAppSettings , fileSystemLookup , staticApp + , embeddedLookup + , toEmbedded + , pathFromPieces + , toPiece + , fixPathName ) newtype Static = Static StaticSettings @@ -79,6 +86,14 @@ static fp = ssFolder = fileSystemLookup fp } +-- | Produces a 'Static' based on embedding file contents in the executable at +-- compile time. +embed :: FilePath -> Q Exp +embed fp = + [|Static (defaultWebAppSettings + { ssFolder = embeddedLookup (toEmbedded $(embedDir fp)) + })|] + {- publicProduction :: String -> FilePath -> IO Public publicProduction root fp = do @@ -196,7 +211,14 @@ mkStaticFiles' fp routeConName makeHash = do | '0' <= c && c <= '9' = c | otherwise = '_' mkRoute f = do - let name = mkName $ intercalate "_" $ map (map replace') f + let name' = intercalate "_" $ map (map replace') f + name = mkName $ + case () of + () + | null name' -> error "null-named file" + | isDigit (head name') -> '_' : name' + | isLower (head name') -> name' + | otherwise -> '_' : name' f' <- [|map pack $(lift f)|] let route = mkName routeConName pack' <- [|pack|] @@ -252,7 +274,7 @@ getStaticHandler static toSubR pieces = do where route = StaticRoute pieces [] toSub _ = static staticSite = getSubSite :: Site (Route Static) (String -> Maybe (GHandler Static y ChooseRep)) - handler = fromMaybe notFound $ handleSite staticSite undefined route "GET" + handler = fromMaybe notFound $ handleSite staticSite (error "Yesod.Static: getSTaticHandler") route "GET" -} @@ -267,4 +289,4 @@ calcHash fname = -- FIXME Greg: Is this correct? Where is this function supposed to be? pathFromRawPieces :: FilePath -> [String] -> FilePath -pathFromRawPieces = undefined +pathFromRawPieces fp = pathFromPieces fp . map (toPiece . pack . fixPathName) diff --git a/yesod-static.cabal b/yesod-static.cabal index 69b9f79c..006772b1 100644 --- a/yesod-static.cabal +++ b/yesod-static.cabal @@ -30,6 +30,7 @@ library , wai-app-static >= 0.3 && < 0.4 , wai >= 0.4 && < 0.5 , text >= 0.5 && < 1.0 + , file-embed >= 0.0.4.1 && < 0.5 exposed-modules: Yesod.Static ghc-options: -Wall