Skip to content

Commit

Permalink
Add ‘compileMustacheDir'’ and ‘getMustacheFilesInDir'’
Browse files Browse the repository at this point in the history
  • Loading branch information
logankaser authored and mrkkrp committed Sep 26, 2017
1 parent 85c1e96 commit 2a6f838
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Stache 1.2.0

* Added `compileMustacheDir'` and `getMusthacheFilesInDir'` that allow for a
custom template predicate. Also exposed `isMustacheFile` as the default
predicate used by functions like `compileMustacheDir`.

## Stache 1.1.2

* Fixed compilation of the test suite with Cabal 2.0/GHC 8.2.1.
Expand Down
1 change: 1 addition & 0 deletions Text/Mustache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ module Text.Mustache
, displayMustacheWarning
-- * Compiling
, compileMustacheDir
, compileMustacheDir'
, compileMustacheFile
, compileMustacheText
-- * Rendering
Expand Down
53 changes: 41 additions & 12 deletions Text/Mustache/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

module Text.Mustache.Compile
( compileMustacheDir
, compileMustacheDir'
, getMustacheFilesInDir
, getMustacheFilesInDir'
, isMustacheFile
, compileMustacheFile
, compileMustacheText )
where
Expand Down Expand Up @@ -43,13 +46,27 @@ import Control.Applicative
--
-- The action can throw the same exceptions as 'getDirectoryContents', and
-- 'T.readFile'.
--
-- > compileMustacheDir = complieMustacheDir' isMustacheFile

compileMustacheDir :: MonadIO m
=> PName -- ^ Which template to select after compiling
-> FilePath -- ^ Directory with templates
-> m Template -- ^ The resulting template
compileMustacheDir pname path =
getMustacheFilesInDir path >>=
compileMustacheDir = compileMustacheDir' isMustacheFile

-- | The same as 'compileMustacheDir', but allows using a custom predicate
-- for template selection.
--
-- @since 1.2.0

compileMustacheDir' :: MonadIO m
=> (FilePath -> Bool) -- ^ Template selection predicate
-> PName -- ^ Which template to select after compiling
-> FilePath -- ^ Directory with templates
-> m Template -- ^ The resulting template
compileMustacheDir' predicate pname path =
getMustacheFilesInDir' predicate path >>=
liftM selectKey . foldM f (Template undefined M.empty)
where
selectKey t = t { templateActual = pname }
Expand All @@ -65,10 +82,30 @@ compileMustacheDir pname path =
getMustacheFilesInDir :: MonadIO m
=> FilePath -- ^ Directory with templates
-> m [FilePath]
getMustacheFilesInDir path = liftIO $
getMustacheFilesInDir = getMustacheFilesInDir' isMustacheFile

-- | Return a list of templates found via a predicate in given directory.
-- The returned paths are absolute.
--
-- @since 1.2.0

getMustacheFilesInDir' :: MonadIO m
=> (FilePath -> Bool) -- ^ Mustache file selection predicate
-> FilePath -- ^ Directory with templates
-> m [FilePath]
getMustacheFilesInDir' predicate path = liftIO $
getDirectoryContents path >>=
filterM isMustacheFile . fmap (F.combine path) >>=
filterM f . fmap (F.combine path) >>=
mapM makeAbsolute
where
f p = (&& predicate p) <$> doesFileExist p

-- | The default Mustache file predicate.
--
-- @since 1.2.0

isMustacheFile :: FilePath -> Bool
isMustacheFile path = F.takeExtension path == ".mustache"

-- | Compile a single Mustache template and select it.
--
Expand Down Expand Up @@ -97,14 +134,6 @@ compileMustacheText pname txt =
----------------------------------------------------------------------------
-- Helpers

-- | Check if a given 'FilePath' points to a mustache file.

isMustacheFile :: FilePath -> IO Bool
isMustacheFile path = do
exists <- doesFileExist path
let rightExtension = F.takeExtension path == ".mustache"
return (exists && rightExtension)

-- | Build a 'PName' from given 'FilePath'.

pathToPName :: FilePath -> PName
Expand Down
21 changes: 19 additions & 2 deletions Text/Mustache/Compile/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

module Text.Mustache.Compile.TH
( compileMustacheDir
, compileMustacheDir'
, compileMustacheFile
, compileMustacheText
, mustache )
Expand Down Expand Up @@ -54,13 +55,29 @@ dataToExpQ _ _ = fail "The feature requires at least GHC 8 to work"
-- recognized. This function /does not/ scan the directory recursively.
--
-- This version compiles the templates at compile time.
--
-- > compileMustacheDir = compileMustacheDir' isMustacheFile

compileMustacheDir
:: PName -- ^ Which template to select after compiling
-> FilePath -- ^ Directory with templates
-> Q Exp -- ^ The resulting template
compileMustacheDir pname path = do
runIO (C.getMustacheFilesInDir path) >>= mapM_ addDependentFile
compileMustacheDir = compileMustacheDir' C.isMustacheFile

-- | The same as 'compileMustacheDir', but allows using a custom predicate
-- for template selection.
--
-- This version compiles the templates at compile time.
--
-- @since 1.2.0

compileMustacheDir'
:: (FilePath -> Bool) -- ^ Template selection predicate
-> PName -- ^ Which template to select after compiling
-> FilePath -- ^ Directory with templates
-> Q Exp -- ^ The resulting template
compileMustacheDir' predicate pname path = do
runIO (C.getMustacheFilesInDir' predicate path) >>= mapM_ addDependentFile
(runIO . try) (C.compileMustacheDir pname path) >>= handleEither

-- | Compile single Mustache template and select it.
Expand Down
4 changes: 2 additions & 2 deletions stack.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
resolver: lts-9.0
resolver: lts-9.6
packages:
- '.'
extra-deps:
- hspec-megaparsec-1.0.0
- megaparsec-6.0.0
- megaparsec-6.2.0

0 comments on commit 2a6f838

Please sign in to comment.