@@ -13,14 +13,17 @@ module Text.Hakyll
13 , hakyllWithConfiguration
13 , hakyllWithConfiguration
16−import Control.Monad.Reader (runReaderT, liftIO)
16+import Control.Monad.Reader (runReaderT, liftIO, ask)
17+import Control.Concurrent (forkIO, threadDelay)
17import Control.Monad (when)
18import Control.Monad (when)
18import qualified Data.Map as M
19import qualified Data.Map as M
19import System.Environment (getArgs, getProgName)
20import System.Environment (getArgs, getProgName)
20import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
21import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
22+import System.Time (getClockTime)
22import Network.Hakyll.SimpleServer (simpleServer)
24import Network.Hakyll.SimpleServer (simpleServer)
23import Text.Hakyll.Hakyll
25import Text.Hakyll.Hakyll
26+import Text.Hakyll.File
25-- | The default hakyll configuration.
28-- | The default hakyll configuration.
26defaultHakyllConfiguration :: HakyllConfiguration
29defaultHakyllConfiguration :: HakyllConfiguration
@@ -29,6 +32,7 @@ defaultHakyllConfiguration = HakyllConfiguration
29 , siteDirectory = "_site"
32 , siteDirectory = "_site"
30 , cacheDirectory = "_cache"
33 , cacheDirectory = "_cache"
31 , enableIndexUrl = False
34 , enableIndexUrl = False
35+ , previewPollDelay = 1000000
34-- | Main function to run Hakyll with the default configuration.
38-- | Main function to run Hakyll with the default configuration.
@@ -41,8 +45,8 @@ hakyllWithConfiguration configuration buildFunction = do
41 args <- getArgs
45 args <- getArgs
42 let f = case args of ["build"] -> buildFunction
46 let f = case args of ["build"] -> buildFunction
43 ["clean"] -> clean
47 ["clean"] -> clean
44− ["preview", p] -> buildFunction >> server (read p)
48+ ["preview", p] -> preview buildFunction (read p)
45− ["preview"] -> buildFunction >> server 8000
49+ ["preview"] -> preview buildFunction 8000
46 ["server", p] -> server (read p)
50 ["server", p] -> server (read p)
47 ["server"] -> server 8000
51 ["server"] -> server 8000
@@ -57,6 +61,23 @@ clean = do askHakyll siteDirectory >>= remove'
57 exists <- doesDirectoryExist dir
61 exists <- doesDirectoryExist dir
58 when exists $ removeDirectoryRecursive dir
62 when exists $ removeDirectoryRecursive dir
64+-- | Autocompile mode.
65+preview :: Hakyll () -> Integer -> Hakyll ()
66+preview buildFunction port = do
69+ liftIO getClockTime >>= run
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
60-- | Show usage information.
81-- | Show usage information.
61help :: Hakyll ()
82help :: Hakyll ()
62help = liftIO $ do
83help = liftIO $ do
@@ -68,7 +89,7 @@ help = liftIO $ do
68 ++ name ++ " build Generate the site.\n"
89 ++ name ++ " build Generate the site.\n"
69 ++ name ++ " clean Clean up and remove cache.\n"
90 ++ name ++ " clean Clean up and remove cache.\n"
70 ++ name ++ " help Show this message.\n"
91 ++ 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"
72 ++ name ++ " server [port] Run a local test server.\n"
93 ++ name ++ " server [port] Run a local test server.\n"
74-- | Start a server at the given port number.
95-- | Start a server at the given port number.