Permalink
Browse files

Added a splice for automatically inserting script tags for javascript…

… files.
  • Loading branch information...
1 parent 7a4dfa3 commit 07c8102ecd6bf4f6e6bfe807d4dd7d7a08a59e57 @mightybyte mightybyte committed Jul 11, 2012
Showing with 38 additions and 3 deletions.
  1. +1 −0 .gitignore
  2. +37 −3 src/Snap/Extras/SpliceUtils.hs
View
@@ -2,3 +2,4 @@ dist
.DS*
cabal-dev
dist
+*.swp
@@ -13,11 +13,15 @@ module Snap.Extras.SpliceUtils
-------------------------------------------------------------------------------
import Control.Monad
import Control.Monad.Trans.Class
+import qualified Data.Foldable as F
+import Data.List
import Data.Text (Text)
+import qualified Data.Text as T
import qualified Data.Text.Encoding as T
-import Snap.Core
-import Snap.Snaplet
+import Snap
import Snap.Snaplet.Heist
+import System.Directory.Tree
+import System.FilePath
import Text.Templating.Heist
import Text.XmlHtml
-------------------------------------------------------------------------------
@@ -102,4 +106,34 @@ selectSplice nm fid xs defv =
[ ("val", textSplice val)
, ("text", textSplice txt)
, ("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.