on master help sign in
Rewrote preview mode.
e3253e7Jasper Van der Jeugt
src/Network/Hakyll/SimpleServer.hs+7−2
@@ -188,8 +188,12 @@ respond handle = do
188
188
189-- | Start a simple http server on the given 'PortNumber', serving the given
189-- | Start a simple http server on the given 'PortNumber', serving the given
190-- directory.
190-- directory.
191+--
191−simpleServer :: PortNumber -> FilePath -> IO ()
192+simpleServer :: PortNumber -- ^ Port to listen on.
193+ -> FilePath -- ^ Root directory to serve.
194+ -> IO () -- ^ Optional pre-respond action.
195+ -> IO ()
192−simpleServer port root = do
196+simpleServer port root preRespond = do
193 -- Channel to send logs to
197 -- Channel to send logs to
194 logChan <- newChan
198 logChan <- newChan
195
199
@@ -200,6 +204,7 @@ simpleServer port root = do
200
204
201 -- When a client connects, respond in a separate thread.
205 -- When a client connects, respond in a separate thread.
202 listen socket = do (handle, _, _) <- accept socket
206 listen socket = do (handle, _, _) <- accept socket
207+ preRespond
203 forkIO (runReaderT (respond handle) config)
208 forkIO (runReaderT (respond handle) config)
204
209
205 -- Handle logging in a separate thread
210 -- Handle logging in a separate thread
src/Text/Hakyll.hs+22−29
@@ -14,20 +14,18 @@ module Text.Hakyll
14 ) where
14 ) where
15
15
16import Control.Monad.Reader (runReaderT, liftIO, ask)
16import Control.Monad.Reader (runReaderT, liftIO, ask)
17−import Control.Concurrent (forkIO, threadDelay)
18import Control.Monad (when)
17import Control.Monad (when)
19import qualified Data.Map as M
18import qualified Data.Map as M
20import System.Environment (getArgs, getProgName)
19import System.Environment (getArgs, getProgName)
21import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
20import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
22−import System.Time (getClockTime)
23
21
24import Text.Pandoc
22import Text.Pandoc
25
23
26import Network.Hakyll.SimpleServer (simpleServer)
24import Network.Hakyll.SimpleServer (simpleServer)
27import Text.Hakyll.HakyllMonad
25import Text.Hakyll.HakyllMonad
28−import Text.Hakyll.File
29
26
30-- | The default reader options for pandoc parsing.
27-- | The default reader options for pandoc parsing.
28+--
31defaultPandocParserState :: ParserState
29defaultPandocParserState :: ParserState
32defaultPandocParserState = defaultParserState
30defaultPandocParserState = defaultParserState
33 { -- The following option causes pandoc to read smart typography, a nice
31 { -- The following option causes pandoc to read smart typography, a nice
@@ -36,6 +34,7 @@ defaultPandocParserState = defaultParserState
36 }
34 }
37
35
38-- | The default writer options for pandoc rendering.
36-- | The default writer options for pandoc rendering.
37+--
39defaultPandocWriterOptions :: WriterOptions
38defaultPandocWriterOptions :: WriterOptions
40defaultPandocWriterOptions = defaultWriterOptions
39defaultPandocWriterOptions = defaultWriterOptions
41 { -- This option causes literate haskell to be written using '>' marks in
40 { -- This option causes literate haskell to be written using '>' marks in
@@ -44,6 +43,7 @@ defaultPandocWriterOptions = defaultWriterOptions
44 }
43 }
45
44
46-- | The default hakyll configuration.
45-- | The default hakyll configuration.
46+--
47defaultHakyllConfiguration :: HakyllConfiguration
47defaultHakyllConfiguration :: HakyllConfiguration
48defaultHakyllConfiguration = HakyllConfiguration
48defaultHakyllConfiguration = HakyllConfiguration
49 { absoluteUrl = ""
49 { absoluteUrl = ""
@@ -51,14 +51,14 @@ defaultHakyllConfiguration = HakyllConfiguration
51 , siteDirectory = "_site"
51 , siteDirectory = "_site"
52 , cacheDirectory = "_cache"
52 , cacheDirectory = "_cache"
53 , enableIndexUrl = False
53 , enableIndexUrl = False
54− , previewPollDelay = 1000000
55 , pandocParserState = defaultPandocParserState
54 , pandocParserState = defaultPandocParserState
56 , pandocWriterOptions = defaultPandocWriterOptions
55 , pandocWriterOptions = defaultPandocWriterOptions
57 }
56 }
58
57
59-- | Main function to run Hakyll with the default configuration. The
58-- | Main function to run Hakyll with the default configuration. The
60−-- absolute URL is only used in certain cases, for example RSS feeds et
59+-- absolute URL is only used in certain cases, for example RSS feeds et
61−-- cetera.
60+-- cetera.
61+--
62hakyll :: String -- ^ Absolute URL of your site. Used in certain cases.
62hakyll :: String -- ^ Absolute URL of your site. Used in certain cases.
63 -> Hakyll () -- ^ You code.
63 -> Hakyll () -- ^ You code.
64 -> IO ()
64 -> IO ()
@@ -67,20 +67,22 @@ hakyll absolute = hakyllWithConfiguration configuration
67 configuration = defaultHakyllConfiguration { absoluteUrl = absolute }
67 configuration = defaultHakyllConfiguration { absoluteUrl = absolute }
68
68
69-- | Main function to run hakyll with a custom configuration.
69-- | Main function to run hakyll with a custom configuration.
70+--
70hakyllWithConfiguration :: HakyllConfiguration -> Hakyll () -> IO ()
71hakyllWithConfiguration :: HakyllConfiguration -> Hakyll () -> IO ()
71hakyllWithConfiguration configuration buildFunction = do
72hakyllWithConfiguration configuration buildFunction = do
72 args <- getArgs
73 args <- getArgs
73 let f = case args of ["build"] -> buildFunction
74 let f = case args of ["build"] -> buildFunction
74 ["clean"] -> clean
75 ["clean"] -> clean
75− ["preview", p] -> preview buildFunction (read p)
76+ ["preview", p] -> server (read p) buildFunction
76− ["preview"] -> preview buildFunction 8000
77+ ["preview"] -> server 8000 buildFunction
77 ["rebuild"] -> clean >> buildFunction
78 ["rebuild"] -> clean >> buildFunction
78− ["server", p] -> server (read p)
79+ ["server", p] -> server (read p) (return ())
79− ["server"] -> server 8000
80+ ["server"] -> server 8000 (return ())
80 _ -> help
81 _ -> help
81 runReaderT f configuration
82 runReaderT f configuration
82
83
83-- | Clean up directories.
84-- | Clean up directories.
85+--
84clean :: Hakyll ()
86clean :: Hakyll ()
85clean = do askHakyll siteDirectory >>= remove'
87clean = do askHakyll siteDirectory >>= remove'
86 askHakyll cacheDirectory >>= remove'
88 askHakyll cacheDirectory >>= remove'
@@ -89,24 +91,8 @@ clean = do askHakyll siteDirectory >>= remove'
89 exists <- doesDirectoryExist dir
91 exists <- doesDirectoryExist dir
90 when exists $ removeDirectoryRecursive dir
92 when exists $ removeDirectoryRecursive dir
91
93
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−
109-- | Show usage information.
94-- | Show usage information.
95+--
110help :: Hakyll ()
96help :: Hakyll ()
111help = liftIO $ do
97help = liftIO $ do
112 name <- getProgName
98 name <- getProgName
@@ -122,5 +108,12 @@ help = liftIO $ do
122 ++ name ++ " server [port] Run a local test server.\n"
108 ++ name ++ " server [port] Run a local test server.\n"
123
109
124-- | Start a server at the given port number.
110-- | Start a server at the given port number.
111+--
125−server :: Integer -> Hakyll ()
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
126−server p = askHakyll siteDirectory >>= liftIO . simpleServer (fromIntegral p)
119+ liftIO $ simpleServer (fromIntegral port) root preRespondIO
src/Text/Hakyll/HakyllMonad.hs−2
@@ -30,8 +30,6 @@ data HakyllConfiguration = HakyllConfiguration
30 cacheDirectory :: FilePath
30 cacheDirectory :: FilePath
31 , -- | Enable index links.
31 , -- | Enable index links.
32 enableIndexUrl :: Bool
32 enableIndexUrl :: Bool
33− , -- | Delay between polls in preview mode.
34− previewPollDelay :: Int
35 , -- | Pandoc parsing options
33 , -- | Pandoc parsing options
36 pandocParserState :: ParserState
34 pandocParserState :: ParserState
37 , -- | Pandoc writer options
35 , -- | Pandoc writer options