summaryrefslogtreecommitdiff
path: root/src/Text/Hakyll/Configurations/Static.hs
blob: 5a2c1be37c427e9388de4fd72eb57536bf092b47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
-- | Module for a simple static configuration of a website.
--
-- The configuration works like this:
--
-- * The @templates/@ directory should contain one template.
--
-- * Renderable files in the directory tree are rendered using this template.
--
-- * The @static/@ directory is copied entirely (if it exists).
--
-- * All files in the @css/@ directory are compressed.
--
module Text.Hakyll.Configurations.Static
    ( staticConfiguration
    ) where

import Control.Applicative ((<$>))
import Control.Monad (filterM, forM_)

import Text.Hakyll.File ( getRecursiveContents, inDirectory, inHakyllDirectory
                        , directory )
import Text.Hakyll.Internal.FileType (isRenderableFile)
import Text.Hakyll.HakyllMonad (Hakyll, logHakyll)
import Text.Hakyll.Render (renderChain, css, static)
import Text.Hakyll.CreateContext (createPage)

-- | A simple configuration for an entirely static website.
--
staticConfiguration :: Hakyll ()
staticConfiguration = do
    -- Find all files not in _site or _cache.
    files <- filterM isRenderableFile' =<< getRecursiveContents "."

    -- Find a main template to use
    mainTemplate <- take 1 <$> getRecursiveContents templateDir
    logHakyll $ case mainTemplate of [] ->      "Using no template"
                                     (x : _) -> "Using template " ++ x

    -- Render all files using this template
    forM_ files $ renderChain mainTemplate . createPage

    -- Render a static directory
    directory static staticDir

    -- Render a css directory
    directory css cssDir
  where
    -- A file should have a renderable extension and not be in a hakyll
    -- directory, and not in a special directory.
    isRenderableFile' file = do
        inHakyllDirectory' <- inHakyllDirectory file
        return $  isRenderableFile file
               && not (any (inDirectory file) [templateDir, cssDir, staticDir])
               && not inHakyllDirectory'

    -- Directories
    templateDir = "templates"
    cssDir = "css"
    staticDir = "static"