{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Network.Gitit.Initialize ( initializeGititState
, recompilePageTemplate
, compilePageTemplate
, createStaticIfMissing
, createRepoIfMissing
, createDefaultPages
, createTemplateIfMissing )
where
import System.FilePath ((</>), (<.>))
import Data.Semigroup ((<>))
import Data.Text (Text)
import qualified Data.Text as T
import Data.FileStore
import qualified Data.Map as M
import Network.Gitit.Util (readFileUTF8)
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)
import Text.Pandoc hiding (getDataFileName, WARNING)
import System.Log.Logger (logM, Priority(..))
import qualified Text.StringTemplate as ST
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 . T.unpack) $ readFileUTF8 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 (ST.StringTemplate String)
compilePageTemplate tempsDir = do
defaultGroup <- getDataFileName ("data" </> "templates") >>= ST.directoryGroup
customExists <- doesDirectoryExist tempsDir
combinedGroup <-
if customExists
then do customGroup <- ST.directoryGroup tempsDir
return $ ST.mergeSTGroups customGroup defaultGroup
else do logM "gitit" WARNING $ "Custom template directory not found"
return defaultGroup
case ST.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
unless repoExists $ createDefaultPages conf
createDefaultPages :: Config -> IO ()
createDefaultPages conf = do
let fs = filestoreFromConfig conf
pt = defaultPageType conf
toPandoc = readMarkdown def{ readerExtensions = enableExtension Ext_smart (readerExtensions def) }
defOpts = def{ writerExtensions = if showLHSBirdTracks conf
then enableExtension
Ext_literate_haskell
$ writerExtensions def
else writerExtensions def
}
converter = handleError . runPure . case pt of
Markdown -> return
LaTeX -> writeLaTeX defOpts <=< toPandoc
HTML -> writeHtml5String defOpts <=< toPandoc
RST -> writeRST defOpts <=< toPandoc
Textile -> writeTextile defOpts <=< toPandoc
Org -> writeOrg defOpts <=< toPandoc
DocBook -> writeDocbook5 defOpts <=< toPandoc
MediaWiki -> writeMediaWiki defOpts <=< toPandoc
CommonMark -> writeCommonMark defOpts <=< toPandoc
welcomepath <- getDataFileName $ "data" </> "FrontPage" <.> "page"
welcomecontents <- converter =<< readFileUTF8 welcomepath
helppath <- getDataFileName $ "data" </> "Help" <.> "page"
helpcontentsInitial <- converter =<< readFileUTF8 helppath
markuppath <- getDataFileName $ "data" </> "markup" <.> show pt
helpcontentsMarkup <- converter =<< readFileUTF8 markuppath
let helpcontents = helpcontentsInitial <> "\n\n" <> helpcontentsMarkup
usersguidepath <- getDataFileName "README.markdown"
usersguidecontents <- converter =<< readFileUTF8 usersguidepath
let header = "---\nformat: " <>
T.pack (show pt) <> (if defaultLHS conf then "+lhs" else "") <>
"\n...\n\n"
let auth = Author "Gitit" ""
createIfMissing fs (frontPage conf <.> defaultExtension conf) auth "Default front page"
$ header <> welcomecontents
createIfMissing fs ("Help" <.> defaultExtension conf) auth "Default help page"
$ header <> helpcontents
createIfMissing fs ("Gitit User’s Guide" <.> defaultExtension conf) auth "User’s guide (README)"
$ header <> usersguidecontents
createIfMissing :: FileStore -> FilePath -> Author -> Description -> Text -> IO ()
createIfMissing fs p a comm cont = do
res <- try $ create fs p a comm (T.unpack cont)
case res of
Right _ -> logM "gitit" WARNING ("Added " ++ p ++ " to repository")
Left ResourceExists -> return ()
Left e -> throwIO e >> return ()
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")