module Network.Gitit.Initialize ( initializeGititState
, recompilePageTemplate
, createStaticIfMissing
, createRepoIfMissing
, createTemplateIfMissing )
where
import System.FilePath ((</>), (<.>))
import Data.FileStore
import qualified Data.Map as M
import Network.Gitit.Types
import Network.Gitit.State
import Network.Gitit.Framework
import Network.Gitit.Plugins
import Network.Gitit.Layout (defaultRenderPage)
import Paths_gitit (getDataFileName)
import Control.Exception (throwIO, try)
import System.Directory (copyFile, createDirectoryIfMissing, doesDirectoryExist, doesFileExist)
import Control.Monad (unless, forM_, liftM)
#if MIN_VERSION_base(4,2,0)
#else
import Prelude hiding (readFile)
import System.IO.UTF8
#endif
import Text.Pandoc
import System.Log.Logger (logM, Priority(..))
import qualified Text.StringTemplate as T
initializeGititState :: Config -> IO ()
initializeGititState conf = do
let userFile' = userFile conf
pluginModules' = pluginModules conf
plugins' <- loadPlugins pluginModules'
userFileExists <- doesFileExist userFile'
users' <- if userFileExists
then liftM (M.fromList . read) $ readFile userFile'
else return M.empty
templ <- compilePageTemplate (templatesDir conf)
updateGititState $ \s -> s { sessions = Sessions M.empty
, users = users'
, templatesPath = templatesDir conf
, renderPage = defaultRenderPage templ
, plugins = plugins' }
recompilePageTemplate :: IO ()
recompilePageTemplate = do
tempsDir <- queryGititState templatesPath
ct <- compilePageTemplate tempsDir
updateGititState $ \st -> st{renderPage = defaultRenderPage ct}
compilePageTemplate :: FilePath -> IO (T.StringTemplate String)
compilePageTemplate tempsDir = do
defaultGroup <- getDataFileName ("data" </> "templates") >>= T.directoryGroup
customExists <- doesDirectoryExist tempsDir
combinedGroup <-
if customExists
then do customGroup <- T.directoryGroup tempsDir
return $ T.mergeSTGroups customGroup defaultGroup
else do logM "gitit" WARNING $ "Custom template directory not found"
return defaultGroup
case T.getStringTemplate "page" combinedGroup of
Just t -> return t
Nothing -> error "Could not get string template"
createTemplateIfMissing :: Config -> IO ()
createTemplateIfMissing conf' = do
templateExists <- doesDirectoryExist (templatesDir conf')
unless templateExists $ do
createDirectoryIfMissing True (templatesDir conf')
templatePath <- getDataFileName $ "data" </> "templates"
forM_ ["footer.st"] $ \t -> do
copyFile (templatePath </> t) (templatesDir conf' </> t)
logM "gitit" WARNING $ "Created " ++ (templatesDir conf' </> t)
createRepoIfMissing :: Config -> IO ()
createRepoIfMissing conf = do
let fs = filestoreFromConfig conf
repoExists <- try (initialize fs) >>= \res ->
case res of
Right _ -> do
logM "gitit" WARNING $ "Created repository in " ++ repositoryPath conf
return False
Left RepositoryExists -> return True
Left e -> throwIO e >> return False
let pt = defaultPageType conf
let toPandoc = readMarkdown
defaultParserState{ stateSanitizeHTML = True
, stateSmart = True }
let defOpts = defaultWriterOptions{
writerStandalone = False
, writerHTMLMathMethod = JsMath
(Just "/js/jsMath/easy/load.js")
, writerLiterateHaskell = showLHSBirdTracks conf
}
let converter = case defaultPageType conf of
Markdown -> id
LaTeX -> writeLaTeX defOpts . toPandoc
HTML -> writeHtmlString defOpts . toPandoc
RST -> writeRST defOpts . toPandoc
unless repoExists $ do
welcomepath <- getDataFileName $ "data" </> "FrontPage" <.> "page"
welcomecontents <- liftM converter $ readFile welcomepath
helppath <- getDataFileName $ "data" </> "Help" <.> "page"
helpcontentsInitial <- liftM converter $ readFile helppath
markuppath <- getDataFileName $ "data" </> "markup" <.> show pt
helpcontentsMarkup <- liftM converter $ readFile markuppath
let helpcontents = helpcontentsInitial ++ "\n\n" ++ helpcontentsMarkup
usersguidepath <- getDataFileName "README.markdown"
usersguidecontents <- liftM converter $ readFile usersguidepath
create fs (frontPage conf <.> "page") (Author "Gitit" "") "Default front page" welcomecontents
logM "gitit" WARNING $ "Added " ++ (frontPage conf <.> "page") ++ " to repository"
create fs "Help.page" (Author "Gitit" "") "Default help page" helpcontents
logM "gitit" WARNING $ "Added " ++ "Help.page" ++ " to repository"
create fs "Gitit User's Guide.page" (Author "Gitit" "") "User's guide (README)" usersguidecontents
logM "gitit" WARNING $ "Added " ++ "Gitit User's Guide.page" ++ " to repository"
createStaticIfMissing :: Config -> IO ()
createStaticIfMissing conf = do
let staticdir = staticDir conf
staticExists <- doesDirectoryExist staticdir
unless staticExists $ do
let cssdir = staticdir </> "css"
createDirectoryIfMissing True cssdir
cssDataDir <- getDataFileName $ "data" </> "static" </> "css"
forM_ ["custom.css"] $ \f -> do
copyFile (cssDataDir </> f) (cssdir </> f)
logM "gitit" WARNING $ "Created " ++ (cssdir </> f)
logopath <- getDataFileName $ "data" </> "static" </> "img" </> "logo.png"
createDirectoryIfMissing True $ staticdir </> "img"
copyFile logopath $ staticdir </> "img" </> "logo.png"
logM "gitit" WARNING $ "Created " ++ (staticdir </> "img" </> "logo.png")