on ea1bb41 help sign in
hakyll/src/Text/Hakyll.hs
97 lines 3.7 kB history raw
1-- | This is the main Hakyll module, exporting the important @hakyl@ function.
2--
3-- Most configurations would use this @hakyll@ function more or less as the
4-- main function:
5--
6-- > main = hakyll $ do
7-- > directory css "css"
8-- > directory static "images"
9--
10module Text.Hakyll
11 ( defaultHakyllConfiguration
12 , hakyll
13 , hakyllWithConfiguration
14 ) where
16import Control.Monad.Reader (runReaderT, liftIO, ask)
17import Control.Concurrent (forkIO, threadDelay)
18import Control.Monad (when)
19import qualified Data.Map as M
20import System.Environment (getArgs, getProgName)
21import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
22import System.Time (getClockTime)
24import Network.Hakyll.SimpleServer (simpleServer)
25import Text.Hakyll.Hakyll
26import Text.Hakyll.File
28-- | The default hakyll configuration.
29defaultHakyllConfiguration :: HakyllConfiguration
30defaultHakyllConfiguration = HakyllConfiguration
31 { additionalContext = M.empty
32 , siteDirectory = "_site"
33 , cacheDirectory = "_cache"
34 , enableIndexUrl = False
35 , previewPollDelay = 1000000
36 }
38-- | Main function to run Hakyll with the default configuration.
39hakyll :: Hakyll () -> IO ()
40hakyll = hakyllWithConfiguration defaultHakyllConfiguration
42-- | Main function to run hakyll with a custom configuration.
43hakyllWithConfiguration :: HakyllConfiguration -> Hakyll () -> IO ()
44hakyllWithConfiguration configuration buildFunction = do
45 args <- getArgs
46 let f = case args of ["build"] -> buildFunction
47 ["clean"] -> clean
48 ["preview", p] -> preview buildFunction (read p)
49 ["preview"] -> preview buildFunction 8000
50 ["server", p] -> server (read p)
51 ["server"] -> server 8000
52 _ -> help
53 runReaderT f configuration
55-- | Clean up directories.
56clean :: Hakyll ()
57clean = do askHakyll siteDirectory >>= remove'
58 askHakyll cacheDirectory >>= remove'
59 where
60 remove' dir = liftIO $ do putStrLn $ "Removing " ++ dir ++ "..."
61 exists <- doesDirectoryExist dir
62 when exists $ removeDirectoryRecursive dir
64-- | Autocompile mode.
65preview :: Hakyll () -> Integer -> Hakyll ()
66preview buildFunction port = do
67 buildFunction
68 _ <- startServer
69 liftIO getClockTime >>= run
70 where
71 startServer = do configuration <- ask
72 liftIO $ forkIO $ runReaderT (server port) configuration
73 run time = do delay <- askHakyll previewPollDelay
74 liftIO $ threadDelay delay
75 contents <- getRecursiveContents "."
76 valid <- isMoreRecent time contents
77 if valid then run time
78 else do buildFunction
79 liftIO getClockTime >>= run
81-- | Show usage information.
82help :: Hakyll ()
83help = liftIO $ do
84 name <- getProgName
85 putStrLn $ "This is a Hakyll site generator program. You should always\n"
86 ++ "run it from the project root directory.\n"
87 ++ "\n"
88 ++ "Usage:\n"
89 ++ name ++ " build Generate the site.\n"
90 ++ name ++ " clean Clean up and remove cache.\n"
91 ++ name ++ " help Show this message.\n"
92 ++ name ++ " preview [port] Run a server and autocompile.\n"
93 ++ name ++ " server [port] Run a local test server.\n"
95-- | Start a server at the given port number.
96server :: Integer -> Hakyll ()
97server p = askHakyll siteDirectory >>= liftIO . simpleServer (fromIntegral p)