on master help sign in
Add a simple static configuration
commit2c8d76cd97e670fdfd216e2288b8cc6633287046 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateMon 2 Aug 2010, 13:48 (16 years ago) parent2066be0
5 files changed+94−17
hakyll.cabal+1 src/Text/Hakyll/Configurations/Static.hs+59 src/Text/Hakyll/File.hs+25−14 src/Text/Hakyll/HakyllAction.hs+1−3 src/Text/Hakyll/HakyllMonad.hs+8
hakyll.cabal+1
@@ -54,6 +54,7 @@ library
5454 Text.Hakyll.Util
5555 Text.Hakyll.Tags
5656 Text.Hakyll.Feed
57+ Text.Hakyll.Configurations.Static
5758 other-modules: Paths_hakyll
5859 Text.Hakyll.Internal.Cache
5960 Text.Hakyll.Internal.CompressCss
src/Text/Hakyll/Configurations/Static.hsnew file+59
@@ -0,0 +1,59 @@
1+-- | Module for a simple static configuration of a website.
2+--
3+-- The configuration works like this:
4+--
5+-- * The @templates/@ directory should contain one template.
6+--
7+-- * Renderable files in the directory tree are rendered using this template.
8+--
9+-- * The @static/@ directory is copied entirely (if it exists).
10+--
11+-- * All files in the @css/@ directory are compressed.
12+--
13+module Text.Hakyll.Configurations.Static
14+ ( staticConfiguration
15+ ) where
16+
17+import Control.Applicative ((<$>))
18+import Control.Monad (filterM, forM_)
19+
20+import Text.Hakyll.File ( getRecursiveContents, inDirectory, inHakyllDirectory
21+ , directory )
22+import Text.Hakyll.Internal.FileType (isRenderableFile)
23+import Text.Hakyll.HakyllMonad (Hakyll, logHakyll)
24+import Text.Hakyll.Render (renderChain, css, static)
25+import Text.Hakyll.CreateContext (createPage)
26+
27+-- | A simple configuration for an entirely static website.
28+--
29+staticConfiguration :: Hakyll ()
30+staticConfiguration = do
31+ -- Find all files not in _site or _cache.
32+ files <- filterM isRenderableFile' =<< getRecursiveContents "."
33+
34+ -- Find a main template to use
35+ mainTemplate <- take 1 <$> getRecursiveContents templateDir
36+ logHakyll $ case mainTemplate of [] -> "Using no template"
37+ (x : _) -> "Using template " ++ x
38+
39+ -- Render all files using this template
40+ forM_ files $ renderChain mainTemplate . createPage
41+
42+ -- Render a static directory
43+ directory static staticDir
44+
45+ -- Render a css directory
46+ directory css cssDir
47+ where
48+ -- A file should have a renderable extension and not be in a hakyll
49+ -- directory, and not in a special directory.
50+ isRenderableFile' file = do
51+ inHakyllDirectory' <- inHakyllDirectory file
52+ return $ isRenderableFile file
53+ && not (any (inDirectory file) [templateDir, cssDir, staticDir])
54+ && not inHakyllDirectory'
55+
56+ -- Directories
57+ templateDir = "templates"
58+ cssDir = "css"
59+ staticDir = "static"
src/Text/Hakyll/File.hs+25−14
@@ -5,6 +5,7 @@ module Text.Hakyll.File
55 , toCache
66 , toUrl
77 , toRoot
8+ , inDirectory
89 , inHakyllDirectory
910 , removeSpaces
1011 , makeDirectories
@@ -87,6 +88,15 @@ toRoot = emptyException . joinPath . map parent . splitPath
8788 emptyException [] = "."
8889 emptyException x = x
8990
91+-- | Check if a file is in a given directory.
92+--
93+inDirectory :: FilePath -- ^ File path
94+ -> FilePath -- ^ Directory
95+ -> Bool -- ^ Result
96+inDirectory path dir = case splitDirectories path of
97+ [] -> False
98+ (x : _) -> x == dir
99+
90100-- | Check if a file is in a Hakyll directory. With a Hakyll directory, we mean
91101-- a directory that should be "ignored" such as the @_site@ or @_cache@
92102-- directory.
@@ -101,11 +111,8 @@ toRoot = emptyException . joinPath . map parent . splitPath
101111--
102112inHakyllDirectory :: FilePath -> Hakyll Bool
103113inHakyllDirectory path =
104− or <$> mapM (liftM inDirectory . askHakyll) [siteDirectory, cacheDirectory]
105− where
106− inDirectory dir = case splitDirectories path of
107− [] -> False
108− (x : _) -> x == dir
114+ or <$> mapM (liftM (inDirectory path) . askHakyll)
115+ [siteDirectory, cacheDirectory]
109116
110117-- | Swaps spaces for '-'.
111118removeSpaces :: FilePath -> FilePath
@@ -123,17 +130,21 @@ makeDirectories path = liftIO $ createDirectoryIfMissing True dir
123130
124131-- | Get all contents of a directory. Note that files starting with a dot (.)
125132-- will be ignored.
133+--
126134getRecursiveContents :: FilePath -> Hakyll [FilePath]
127135getRecursiveContents topdir = do
128− names <- liftIO $ getDirectoryContents topdir
129− let properNames = filter isProper names
130− paths <- forM properNames $ \name -> do
131− let path = topdir </> name
132− isDirectory <- liftIO $ doesDirectoryExist path
133− if isDirectory
134− then getRecursiveContents path
135− else return [path]
136− return (concat paths)
136+ topdirExists <- liftIO $ doesDirectoryExist topdir
137+ if topdirExists
138+ then do names <- liftIO $ getDirectoryContents topdir
139+ let properNames = filter isProper names
140+ paths <- forM properNames $ \name -> do
141+ let path = topdir </> name
142+ isDirectory <- liftIO $ doesDirectoryExist path
143+ if isDirectory
144+ then getRecursiveContents path
145+ else return [normalise path]
146+ return (concat paths)
147+ else return []
137148 where
138149 isProper = not . (== '.') . head
139150
src/Text/Hakyll/HakyllAction.hs+1−3
@@ -12,9 +12,7 @@ module Text.Hakyll.HakyllAction
1212import Control.Arrow
1313import Control.Category
1414import Control.Monad ((<=<), unless)
15−import Control.Monad.Reader (liftIO)
1615import Prelude hiding ((.), id)
17−import System.IO (hPutStrLn, stderr)
1816
1917import Text.Hakyll.File (toDestination, isFileMoreRecent)
2018import Text.Hakyll.HakyllMonad
@@ -65,7 +63,7 @@ runHakyllActionIfNeeded action = do
6563 Right _ -> error "No url when checking dependencies."
6664 destination <- toDestination url
6765 valid <- isFileMoreRecent destination $ actionDependencies action
68− unless valid $ do liftIO $ hPutStrLn stderr $ "Rendering " ++ destination
66+ unless valid $ do logHakyll $ "Rendering " ++ destination
6967 runHakyllAction action
7068
7169-- | Chain a number of @HakyllAction@ computations.
src/Text/Hakyll/HakyllMonad.hs+8
@@ -5,11 +5,14 @@ module Text.Hakyll.HakyllMonad
55 , Hakyll
66 , askHakyll
77 , getAdditionalContext
8+ , logHakyll
89 ) where
910
11+import Control.Monad.Trans (liftIO)
1012import Control.Monad.Reader (ReaderT, ask)
1113import Control.Monad (liftM)
1214import qualified Data.Map as M
15+import System.IO (hPutStrLn, stderr)
1316
1417import Text.Pandoc (ParserState, WriterOptions)
1518
@@ -65,3 +68,8 @@ getAdditionalContext :: HakyllConfiguration -> Context
6568getAdditionalContext configuration =
6669 let (Context c) = additionalContext configuration
6770 in Context $ M.insert "absolute" (absoluteUrl configuration) c
71+
72+-- | Write some log information.
73+--
74+logHakyll :: String -> Hakyll ()
75+logHakyll = liftIO . hPutStrLn stderr