@@ -14,20 +14,18 @@ module Text.Hakyll
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)
24import Text.Pandoc
22import Text.Pandoc
26import Network.Hakyll.SimpleServer (simpleServer)
24import Network.Hakyll.SimpleServer (simpleServer)
27import Text.Hakyll.HakyllMonad
25import Text.Hakyll.HakyllMonad
28−import Text.Hakyll.File
30-- | The default reader options for pandoc parsing.
27-- | The default reader options for pandoc parsing.
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
38-- | The default writer options for pandoc rendering.
36-- | The default writer options for pandoc rendering.
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
46-- | The default hakyll configuration.
45-- | The default hakyll configuration.
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
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.
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.
@@ -67,20 +67,22 @@ hakyll absolute = hakyllWithConfiguration configuration
67 configuration = defaultHakyllConfiguration { absoluteUrl = absolute }
67 configuration = defaultHakyllConfiguration { absoluteUrl = absolute }
69-- | Main function to run hakyll with a custom configuration.
69-- | Main function to run hakyll with a custom configuration.
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 ())
81 runReaderT f configuration
82 runReaderT f configuration
83-- | Clean up directories.
84-- | Clean up directories.
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
92−-- | Autocompile mode.
93−preview :: Hakyll () -> Integer -> Hakyll ()
94−preview buildFunction port = do
97− liftIO getClockTime >>= run
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
109-- | Show usage information.
94-- | Show usage information.
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"
124-- | Start a server at the given port number.
110-- | Start a server at the given port number.
125−server :: Integer -> Hakyll ()
112+server :: Integer -- ^ Port number to serve on.
113+ -> Hakyll () -- ^ Pre-respond action.
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