@@ -14,20 +14,18 @@ module Text.Hakyll
1414 ) where
1515
1616import Control.Monad.Reader (runReaderT, liftIO, ask)
17−import Control.Concurrent (forkIO, threadDelay)
1817import Control.Monad (when)
1918import qualified Data.Map as M
2019import System.Environment (getArgs, getProgName)
2120import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
22−import System.Time (getClockTime)
2321
2422import Text.Pandoc
2523
2624import Network.Hakyll.SimpleServer (simpleServer)
2725import Text.Hakyll.HakyllMonad
28−import Text.Hakyll.File
2926
3027-- | The default reader options for pandoc parsing.
28+--
3129defaultPandocParserState :: ParserState
3230defaultPandocParserState = defaultParserState
3331 { -- The following option causes pandoc to read smart typography, a nice
@@ -36,6 +34,7 @@ defaultPandocParserState = defaultParserState
3634 }
3735
3836-- | The default writer options for pandoc rendering.
37+--
3938defaultPandocWriterOptions :: WriterOptions
4039defaultPandocWriterOptions = defaultWriterOptions
4140 { -- This option causes literate haskell to be written using '>' marks in
@@ -44,6 +43,7 @@ defaultPandocWriterOptions = defaultWriterOptions
4443 }
4544
4645-- | The default hakyll configuration.
46+--
4747defaultHakyllConfiguration :: HakyllConfiguration
4848defaultHakyllConfiguration = HakyllConfiguration
4949 { absoluteUrl = ""
@@ -51,14 +51,14 @@ defaultHakyllConfiguration = HakyllConfiguration
5151 , siteDirectory = "_site"
5252 , cacheDirectory = "_cache"
5353 , enableIndexUrl = False
54− , previewPollDelay = 1000000
5554 , pandocParserState = defaultPandocParserState
5655 , pandocWriterOptions = defaultPandocWriterOptions
5756 }
5857
5958-- | Main function to run Hakyll with the default configuration. The
60−-- absolute URL is only used in certain cases, for example RSS feeds et
61−-- cetera.
59+-- absolute URL is only used in certain cases, for example RSS feeds et
60+-- cetera.
61+--
6262hakyll :: String -- ^ Absolute URL of your site. Used in certain cases.
6363 -> Hakyll () -- ^ You code.
6464 -> IO ()
@@ -67,20 +67,22 @@ hakyll absolute = hakyllWithConfiguration configuration
6767 configuration = defaultHakyllConfiguration { absoluteUrl = absolute }
6868
6969-- | Main function to run hakyll with a custom configuration.
70+--
7071hakyllWithConfiguration :: HakyllConfiguration -> Hakyll () -> IO ()
7172hakyllWithConfiguration configuration buildFunction = do
7273 args <- getArgs
7374 let f = case args of ["build"] -> buildFunction
7475 ["clean"] -> clean
75− ["preview", p] -> preview buildFunction (read p)
76− ["preview"] -> preview buildFunction 8000
76+ ["preview", p] -> server (read p) buildFunction
77+ ["preview"] -> server 8000 buildFunction
7778 ["rebuild"] -> clean >> buildFunction
78− ["server", p] -> server (read p)
79− ["server"] -> server 8000
79+ ["server", p] -> server (read p) (return ())
80+ ["server"] -> server 8000 (return ())
8081 _ -> help
8182 runReaderT f configuration
8283
8384-- | Clean up directories.
85+--
8486clean :: Hakyll ()
8587clean = do askHakyll siteDirectory >>= remove'
8688 askHakyll cacheDirectory >>= remove'
@@ -89,24 +91,8 @@ clean = do askHakyll siteDirectory >>= remove'
8991 exists <- doesDirectoryExist dir
9092 when exists $ removeDirectoryRecursive dir
9193
92−-- | Autocompile mode.
93−preview :: Hakyll () -> Integer -> Hakyll ()
94−preview buildFunction port = do
95− buildFunction
96− _ <- startServer
97− liftIO getClockTime >>= run
98− where
99− startServer = do configuration <- ask
100− liftIO $ forkIO $ runReaderT (server port) configuration
101− run time = do delay <- askHakyll previewPollDelay
102− liftIO $ threadDelay delay
103− contents <- getRecursiveContents "."
104− valid <- isMoreRecent time contents
105− if valid then run time
106− else do buildFunction
107− liftIO getClockTime >>= run
108−
10994-- | Show usage information.
95+--
11096help :: Hakyll ()
11197help = liftIO $ do
11298 name <- getProgName
@@ -122,5 +108,12 @@ help = liftIO $ do
122108 ++ name ++ " server [port] Run a local test server.\n"
123109
124110-- | Start a server at the given port number.
125−server :: Integer -> Hakyll ()
126−server p = askHakyll siteDirectory >>= liftIO . simpleServer (fromIntegral p)
111+--
112+server :: Integer -- ^ Port number to serve on.
113+ -> Hakyll () -- ^ Pre-respond action.
114+ -> Hakyll ()
115+server port preRespond = do
116+ configuration <- ask
117+ root <- askHakyll siteDirectory
118+ let preRespondIO = runReaderT preRespond configuration
119+ liftIO $ simpleServer (fromIntegral port) root preRespondIO