on master help sign in
Option to choose between preview modes
commit1af0421efe7983eb5634440ec162da48518b0b78 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateSat 31 Jul 2010, 12:55 (16 years ago) parent48b1706
2 files changed+40−3
src/Text/Hakyll.hs+29−3 src/Text/Hakyll/HakyllMonad.hs+11
src/Text/Hakyll.hs+29−3
@@ -13,16 +13,19 @@ module Text.Hakyll
1313 , hakyllWithConfiguration
1414 ) where
1515
16+import Control.Concurrent (forkIO, threadDelay)
1617import Control.Monad.Reader (runReaderT, liftIO, ask)
1718import Control.Monad (when)
1819import Data.Monoid (mempty)
1920import System.Environment (getArgs, getProgName)
2021import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
22+import System.Time (getClockTime)
2123
2224import Text.Pandoc
2325
2426import Network.Hakyll.SimpleServer (simpleServer)
2527import Text.Hakyll.HakyllMonad
28+import Text.Hakyll.File
2629
2730-- | The default reader options for pandoc parsing.
2831--
@@ -52,6 +55,7 @@ defaultHakyllConfiguration absoluteUrl' = HakyllConfiguration
5255 , siteDirectory = "_site"
5356 , cacheDirectory = "_cache"
5457 , enableIndexUrl = False
58+ , previewMode = BuildOnRequest
5559 , pandocParserState = defaultPandocParserState
5660 , pandocWriterOptions = defaultPandocWriterOptions
5761 }
@@ -74,13 +78,35 @@ hakyllWithConfiguration configuration buildFunction = do
7478 args <- getArgs
7579 let f = case args of ["build"] -> buildFunction
7680 ["clean"] -> clean
77− ["preview", p] -> server (read p) buildFunction
78− ["preview"] -> server 8000 buildFunction
81+ ["preview", p] -> preview (read p)
82+ ["preview"] -> preview defaultPort
7983 ["rebuild"] -> clean >> buildFunction
8084 ["server", p] -> server (read p) (return ())
81− ["server"] -> server 8000 (return ())
85+ ["server"] -> server defaultPort (return ())
8286 _ -> help
8387 runReaderT f configuration
88+ where
89+ preview port = case previewMode configuration of
90+ BuildOnRequest -> server port buildFunction
91+ BuildOnInterval -> do
92+ let pIO = runReaderT (previewThread buildFunction) configuration
93+ _ <- liftIO $ forkIO pIO
94+ server port (return ())
95+
96+ defaultPort = 8000
97+
98+-- | A preview thread that periodically recompiles the site.
99+--
100+previewThread :: Hakyll () -- ^ Build function
101+ -> Hakyll () -- ^ Result
102+previewThread buildFunction = run =<< liftIO getClockTime
103+ where
104+ delay = 1000000
105+ run time = do liftIO $ threadDelay delay
106+ contents <- getRecursiveContents "."
107+ valid <- isMoreRecent time contents
108+ when valid buildFunction
109+ run =<< liftIO getClockTime
84110
85111-- | Clean up directories.
86112--
src/Text/Hakyll/HakyllMonad.hs+11
@@ -1,6 +1,7 @@
11-- | Module describing the Hakyll monad stack.
22module Text.Hakyll.HakyllMonad
33 ( HakyllConfiguration (..)
4+ , PreviewMode (..)
45 , Hakyll
56 , askHakyll
67 , getAdditionalContext
@@ -15,9 +16,17 @@ import Text.Pandoc (ParserState, WriterOptions)
1516import Text.Hakyll.Context (Context (..))
1617
1718-- | Our custom monad stack.
19+--
1820type Hakyll = ReaderT HakyllConfiguration IO
1921
22+-- | Preview mode.
23+--
24+data PreviewMode = BuildOnRequest
25+ | BuildOnInterval
26+ deriving (Show, Eq, Ord)
27+
2028-- | Hakyll global configuration type.
29+--
2130data HakyllConfiguration = HakyllConfiguration
2231 { -- | Absolute URL of the site.
2332 absoluteUrl :: String
@@ -30,6 +39,8 @@ data HakyllConfiguration = HakyllConfiguration
3039 cacheDirectory :: FilePath
3140 , -- | Enable index links.
3241 enableIndexUrl :: Bool
42+ , -- | The preview mode used
43+ previewMode :: PreviewMode
3344 , -- | Pandoc parsing options
3445 pandocParserState :: ParserState
3546 , -- | Pandoc writer options