module Text.Hakyll
( defaultHakyllConfiguration
, hakyll
, hakyllWithConfiguration
) where
import Control.Monad.Reader (runReaderT, liftIO, ask)
import Control.Concurrent (forkIO, threadDelay)
import Control.Monad (when)
import qualified Data.Map as M
import System.Environment (getArgs, getProgName)
import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
import System.Time (getClockTime)
import Network.Hakyll.SimpleServer (simpleServer)
import Text.Hakyll.Hakyll
import Text.Hakyll.File
defaultHakyllConfiguration :: HakyllConfiguration
defaultHakyllConfiguration = HakyllConfiguration
{ additionalContext = M.empty
, siteDirectory = "_site"
, cacheDirectory = "_cache"
, enableIndexUrl = False
, previewPollDelay = 1000000
}
hakyll :: Hakyll () -> IO ()
hakyll = hakyllWithConfiguration defaultHakyllConfiguration
hakyllWithConfiguration :: HakyllConfiguration -> Hakyll () -> IO ()
hakyllWithConfiguration configuration buildFunction = do
args <- getArgs
let f = case args of ["build"] -> buildFunction
["clean"] -> clean
["preview", p] -> preview buildFunction (read p)
["preview"] -> preview buildFunction 8000
["server", p] -> server (read p)
["server"] -> server 8000
_ -> help
runReaderT f configuration
clean :: Hakyll ()
clean = do askHakyll siteDirectory >>= remove'
askHakyll cacheDirectory >>= remove'
where
remove' dir = liftIO $ do putStrLn $ "Removing " ++ dir ++ "..."
exists <- doesDirectoryExist dir
when exists $ removeDirectoryRecursive dir
preview :: Hakyll () -> Integer -> Hakyll ()
preview buildFunction port = do
buildFunction
_ <- startServer
liftIO getClockTime >>= run
where
startServer = do configuration <- ask
liftIO $ forkIO $ runReaderT (server port) configuration
run time = do delay <- askHakyll previewPollDelay
liftIO $ threadDelay delay
contents <- getRecursiveContents "."
valid <- isMoreRecent time contents
if valid then run time
else do buildFunction
liftIO getClockTime >>= run
help :: Hakyll ()
help = liftIO $ do
name <- getProgName
putStrLn $ "This is a Hakyll site generator program. You should always\n"
++ "run it from the project root directory.\n"
++ "\n"
++ "Usage:\n"
++ name ++ " build Generate the site.\n"
++ name ++ " clean Clean up and remove cache.\n"
++ name ++ " help Show this message.\n"
++ name ++ " preview [port] Run a server and autocompile.\n"
++ name ++ " server [port] Run a local test server.\n"
server :: Integer -> Hakyll ()
server p = askHakyll siteDirectory >>= liftIO . simpleServer (fromIntegral p)