Allow sitewide hamlet changes (#377)

This commit is contained in:
Michael Snoyman 2012-07-02 09:37:56 +03:00
parent 3ecbf43f5d
commit e8fb512107
3 changed files with 49 additions and 25 deletions

View File

@ -53,6 +53,7 @@ module Yesod.Widget
, addScriptEither , addScriptEither
-- * Internal -- * Internal
, unGWidget , unGWidget
, whamletFileWithSettings
) where ) where
import Data.Monoid import Data.Monoid
@ -272,6 +273,9 @@ whamlet = NP.hamletWithSettings rules NP.defaultHamletSettings
whamletFile :: FilePath -> Q Exp whamletFile :: FilePath -> Q Exp
whamletFile = NP.hamletFileWithSettings rules NP.defaultHamletSettings whamletFile = NP.hamletFileWithSettings rules NP.defaultHamletSettings
whamletFileWithSettings :: NP.HamletSettings -> FilePath -> Q Exp
whamletFileWithSettings = NP.hamletFileWithSettings rules
rules :: Q NP.HamletRules rules :: Q NP.HamletRules
rules = do rules = do
ah <- [|toWidget|] ah <- [|toWidget|]

View File

@ -7,7 +7,11 @@ module Yesod.Default.Util
, globFile , globFile
, widgetFileNoReload , widgetFileNoReload
, widgetFileReload , widgetFileReload
, widgetFileJsCss , TemplateLanguage (..)
, defaultTemplateLanguages
, WidgetFileSettings
, wfsLanguages
, wfsHamletSettings
) where ) where
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
@ -20,7 +24,9 @@ import Language.Haskell.TH.Syntax
import Text.Lucius (luciusFile, luciusFileReload) import Text.Lucius (luciusFile, luciusFileReload)
import Text.Julius (juliusFile, juliusFileReload) import Text.Julius (juliusFile, juliusFileReload)
import Text.Cassius (cassiusFile, cassiusFileReload) import Text.Cassius (cassiusFile, cassiusFileReload)
import Text.Hamlet (HamletSettings, defaultHamletSettings)
import Data.Maybe (catMaybes) import Data.Maybe (catMaybes)
import Data.Default (Default (def))
-- | An implementation of 'addStaticContent' which stores the contents in an -- | An implementation of 'addStaticContent' which stores the contents in an
-- external file. Files are created in the given static folder with names based -- external file. Files are created in the given static folder with names based
@ -57,34 +63,40 @@ addStaticContentExternal minify hash staticDir toRoute ext' _ content = do
globFile :: String -> String -> FilePath globFile :: String -> String -> FilePath
globFile kind x = "templates/" ++ x ++ "." ++ kind globFile kind x = "templates/" ++ x ++ "." ++ kind
widgetFileNoReload :: FilePath -> Q Exp data TemplateLanguage = TemplateLanguage
widgetFileNoReload x = combine "widgetFileNoReload" x { tlRequiresToWidget :: Bool
[ whenExists x False "hamlet" whamletFile , tlExtension :: String
, whenExists x True "cassius" cassiusFile , tlNoReload :: FilePath -> Q Exp
, whenExists x True "julius" juliusFile , tlReload :: FilePath -> Q Exp
, whenExists x True "lucius" luciusFile }
]
widgetFileReload :: FilePath -> Q Exp defaultTemplateLanguages :: HamletSettings -> [TemplateLanguage]
widgetFileReload x = combine "widgetFileReload" x defaultTemplateLanguages hset =
[ whenExists x False "hamlet" whamletFile [ TemplateLanguage False "hamlet" whamletFile' whamletFile'
, whenExists x True "cassius" cassiusFileReload , TemplateLanguage True "cassius" cassiusFile cassiusFileReload
, whenExists x True "julius" juliusFileReload , TemplateLanguage True "julius" juliusFile juliusFileReload
, whenExists x True "lucius" luciusFileReload , TemplateLanguage True "lucius" luciusFile luciusFileReload
] ]
where
whamletFile' = whamletFileWithSettings hset
widgetFileJsCss :: (String, FilePath -> Q Exp) -- ^ JavaScript file extenstion and loading function. example: ("julius", juliusFileReload) data WidgetFileSettings = WidgetFileSettings
-> (String, FilePath -> Q Exp) -- ^ Css file extenstion and loading function. example: ("cassius", cassiusFileReload) { wfsLanguages :: HamletSettings -> [TemplateLanguage]
-> FilePath -> Q Exp , wfsHamletSettings :: HamletSettings
widgetFileJsCss (jsExt, jsLoad) (csExt, csLoad) x = combine "widgetFileJsCss" x }
[ whenExists x False "hamlet" whamletFile
, whenExists x True csExt csLoad
, whenExists x True jsExt jsLoad
]
combine :: String -> String -> [Q (Maybe Exp)] -> Q Exp instance Default WidgetFileSettings where
combine func file qmexps = do def = WidgetFileSettings defaultTemplateLanguages defaultHamletSettings
mexps <- sequence qmexps
widgetFileNoReload :: WidgetFileSettings -> FilePath -> Q Exp
widgetFileNoReload wfs x = combine "widgetFileNoReload" x False $ wfsLanguages wfs $ wfsHamletSettings wfs
widgetFileReload :: WidgetFileSettings -> FilePath -> Q Exp
widgetFileReload wfs x = combine "widgetFileReload" x True $ wfsLanguages wfs $ wfsHamletSettings wfs
combine :: String -> String -> Bool -> [TemplateLanguage] -> Q Exp
combine func file isReload tls = do
mexps <- qmexps
case catMaybes mexps of case catMaybes mexps of
[] -> error $ concat [] -> error $ concat
[ "Called " [ "Called "
@ -94,6 +106,12 @@ combine func file qmexps = do
, ", but no template were found." , ", but no template were found."
] ]
exps -> return $ DoE $ map NoBindS exps exps -> return $ DoE $ map NoBindS exps
where
qmexps :: Q [Maybe Exp]
qmexps = mapM go tls
go :: TemplateLanguage -> Q (Maybe Exp)
go tl = whenExists file (tlRequiresToWidget tl) (tlExtension tl) ((if isReload then tlReload else tlNoReload) tl)
whenExists :: String whenExists :: String
-> Bool -- ^ requires toWidget wrap -> Bool -- ^ requires toWidget wrap

View File

@ -32,6 +32,8 @@ library
, yaml >= 0.8 && < 0.9 , yaml >= 0.8 && < 0.9
, network-conduit >= 0.5 && < 0.6 , network-conduit >= 0.5 && < 0.6
, unordered-containers , unordered-containers
, hamlet >= 1.1 && < 1.2
, data-default
if !os(windows) if !os(windows)
build-depends: unix build-depends: unix