summaryrefslogtreecommitdiff
path: root/src/Text/Hakyll.hs
blob: c0486e783a9742ee3d2943147ea47e05e5d0918e (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
60
61
62
63
64
65
66
67
68
module Text.Hakyll
    ( defaultHakyllConfiguration
    , hakyll
    , hakyllWithConfiguration
    ) where

import Control.Monad.Reader (runReaderT)
import Control.Monad (when)
import qualified Data.Map as M
import System.Environment (getArgs, getProgName)
import System.Directory (doesDirectoryExist, removeDirectoryRecursive)

import Network.Hakyll.SimpleServer (simpleServer)
import Text.Hakyll.Hakyll

-- | Default hakyll configuration.
defaultHakyllConfiguration :: HakyllConfiguration
defaultHakyllConfiguration = HakyllConfiguration
    { additionalContext = M.empty
    }

-- | Hakyll with a default configuration.
hakyll :: Hakyll () -> IO ()
hakyll = hakyllWithConfiguration defaultHakyllConfiguration

-- | Main function to run hakyll with a configuration.
hakyllWithConfiguration :: HakyllConfiguration -> Hakyll () -> IO ()
hakyllWithConfiguration configuration buildFunction = do
    args <- getArgs
    case args of ["build"]      -> build'
                 ["clean"]      -> clean
                 ["preview", p] -> build' >> server (read p)
                 ["preview"]    -> build' >> server 8000
                 ["server", p]  -> server (read p)
                 ["server"]     -> server 8000
                 _              -> help
  where
    build' = build configuration buildFunction

-- | Build the site.
build :: HakyllConfiguration -> Hakyll () -> IO ()
build configuration buildFunction = do putStrLn "Generating..."
                                       runReaderT buildFunction configuration

-- | Clean up directories.
clean :: IO ()
clean = remove' "_site"
  where
    remove' dir = do putStrLn $ "Removing " ++ dir ++ "..."
                     exists <- doesDirectoryExist dir
                     when exists $ removeDirectoryRecursive dir

-- | Show usage information.
help :: IO ()
help = do
    name <- getProgName
    putStrLn $  "This is a Hakyll site generator program. You should always\n"
             ++ "run it from the project root directory.\n"
             ++ "\n"
             ++ "Usage:\n"
             ++ name ++ " build           Generate the site.\n"
             ++ name ++ " clean           Clean up and remove cache.\n"
             ++ name ++ " help            Show this message.\n"
             ++ name ++ " preview [port]  Generate site, then start a server.\n"
             ++ name ++ " server [port]   Run a local test server.\n"

server :: Integer -> IO ()
server p = simpleServer (fromIntegral p) "_site"