Skip to content

Commit

Permalink
Added a splice for automatically inserting script tags for javascript…
Browse files Browse the repository at this point in the history
… files.
  • Loading branch information
mightybyte committed Jul 11, 2012
1 parent 7a4dfa3 commit 07c8102
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@ dist
.DS* .DS*
cabal-dev cabal-dev
dist dist
*.swp
40 changes: 37 additions & 3 deletions src/Snap/Extras/SpliceUtils.hs
Expand Up @@ -13,11 +13,15 @@ module Snap.Extras.SpliceUtils
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
import Control.Monad import Control.Monad
import Control.Monad.Trans.Class import Control.Monad.Trans.Class
import qualified Data.Foldable as F
import Data.List
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T import qualified Data.Text.Encoding as T
import Snap.Core import Snap
import Snap.Snaplet
import Snap.Snaplet.Heist import Snap.Snaplet.Heist
import System.Directory.Tree
import System.FilePath
import Text.Templating.Heist import Text.Templating.Heist
import Text.XmlHtml import Text.XmlHtml
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Expand Down Expand Up @@ -102,4 +106,34 @@ selectSplice nm fid xs defv =
[ ("val", textSplice val) [ ("val", textSplice val)
, ("text", textSplice txt) , ("text", textSplice txt)
, ("ifSelected", ifSplice $ maybe False (== val) defv) , ("ifSelected", ifSplice $ maybe False (== val) defv)
, ("ifNotSelected", ifSplice $ maybe True (/= val) defv) ] , ("ifNotSelected", ifSplice $ maybe True (/= val) defv) ]


------------------------------------------------------------------------------
-- | Searches a directory on disk and all its subdirectories for all files
-- with names that don't begin with an underscore and end with a .js
-- extension. It then returns script tags for each of these files.
--
-- You can use this function to create a splice:
-- ("staticscripts", scriptsSplice "static/js" "/")
--
-- Then when you use the `<staticscripts/>` tag in your templates, it will
-- automatically include all the javascript code in the static/js directory.
scriptsSplice :: MonadIO m
=> FilePath
-- ^ Path to the directory on disk holding the javascript files.
-> String
-- ^ A prefix to add to the src attribute of each script tag.
-> m [Node]
scriptsSplice dir prefix = do
tree <- liftIO $ build dir
let files = F.foldMap ((:[]) . fst) $ zipPaths $ "" :/ free tree
scripts = filter visibleScripts files
return $ concat $ map includeJavascript scripts
where
visibleScripts fname =
isSuffixOf ".js" fname && not (isPrefixOf "_" (takeFileName fname))
includeJavascript script =
[Element "script" [("src", T.pack $ prefix ++ script)] []]


0 comments on commit 07c8102

Please sign in to comment.