on master help sign in
Rewrote preview mode.

Preview mode is now based on server requests instead of time intervals. This should solve some problems out there.

commite3253e79e3a8dfe274fa7e829b11c050029c3195 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateWed 16 Jun 2010, 18:00 (16 years ago) parentb1d4f48
3 files changed+29−33
src/Network/Hakyll/SimpleServer.hs+7−2 src/Text/Hakyll.hs+22−29 src/Text/Hakyll/HakyllMonad.hs−2
src/Network/Hakyll/SimpleServer.hs+7−2
@@ -188,8 +188,12 @@ respond handle = do
188188
189189-- | Start a simple http server on the given 'PortNumber', serving the given
190190-- directory.
191−simpleServer :: PortNumber -> FilePath -> IO ()
192−simpleServer port root = do
191+--
192+simpleServer :: PortNumber -- ^ Port to listen on.
193+ -> FilePath -- ^ Root directory to serve.
194+ -> IO () -- ^ Optional pre-respond action.
195+ -> IO ()
196+simpleServer port root preRespond = do
193197 -- Channel to send logs to
194198 logChan <- newChan
195199
@@ -200,6 +204,7 @@ simpleServer port root = do
200204
201205 -- When a client connects, respond in a separate thread.
202206 listen socket = do (handle, _, _) <- accept socket
207+ preRespond
203208 forkIO (runReaderT (respond handle) config)
204209
205210 -- Handle logging in a separate thread
src/Text/Hakyll.hs+22−29
@@ -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
src/Text/Hakyll/HakyllMonad.hs−2
@@ -30,8 +30,6 @@ data HakyllConfiguration = HakyllConfiguration
3030 cacheDirectory :: FilePath
3131 , -- | Enable index links.
3232 enableIndexUrl :: Bool
33− , -- | Delay between polls in preview mode.
34− previewPollDelay :: Int
3533 , -- | Pandoc parsing options
3634 pandocParserState :: ParserState
3735 , -- | Pandoc writer options