@@ -13,14 +13,17 @@ module Text.Hakyll
1313 , hakyllWithConfiguration
1414 ) where
1515
16−import Control.Monad.Reader (runReaderT, liftIO)
16+import Control.Monad.Reader (runReaderT, liftIO, ask)
17+import Control.Concurrent (forkIO, threadDelay)
1718import Control.Monad (when)
1819import qualified Data.Map as M
1920import System.Environment (getArgs, getProgName)
2021import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
22+import System.Time (getClockTime)
2123
2224import Network.Hakyll.SimpleServer (simpleServer)
2325import Text.Hakyll.Hakyll
26+import Text.Hakyll.File
2427
2528-- | The default hakyll configuration.
2629defaultHakyllConfiguration :: HakyllConfiguration
@@ -29,6 +32,7 @@ defaultHakyllConfiguration = HakyllConfiguration
2932 , siteDirectory = "_site"
3033 , cacheDirectory = "_cache"
3134 , enableIndexUrl = False
35+ , previewPollDelay = 1000000
3236 }
3337
3438-- | Main function to run Hakyll with the default configuration.
@@ -41,8 +45,8 @@ hakyllWithConfiguration configuration buildFunction = do
4145 args <- getArgs
4246 let f = case args of ["build"] -> buildFunction
4347 ["clean"] -> clean
44− ["preview", p] -> buildFunction >> server (read p)
45− ["preview"] -> buildFunction >> server 8000
48+ ["preview", p] -> preview buildFunction (read p)
49+ ["preview"] -> preview buildFunction 8000
4650 ["server", p] -> server (read p)
4751 ["server"] -> server 8000
4852 _ -> help
@@ -57,6 +61,23 @@ clean = do askHakyll siteDirectory >>= remove'
5761 exists <- doesDirectoryExist dir
5862 when exists $ removeDirectoryRecursive dir
5963
64+-- | Autocompile mode.
65+preview :: Hakyll () -> Integer -> Hakyll ()
66+preview 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
80+
6081-- | Show usage information.
6182help :: Hakyll ()
6283help = liftIO $ do
@@ -68,7 +89,7 @@ help = liftIO $ do
6889 ++ name ++ " build Generate the site.\n"
6990 ++ name ++ " clean Clean up and remove cache.\n"
7091 ++ name ++ " help Show this message.\n"
71− ++ name ++ " preview [port] Generate site, then start a server.\n"
92+ ++ name ++ " preview [port] Run a server and autocompile.\n"
7293 ++ name ++ " server [port] Run a local test server.\n"
7394
7495-- | Start a server at the given port number.