{-
Copyright (C) 2009 John MacFarlane <jgm@berkeley.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}{- Utility functions for Gitit.
-}moduleNetwork.Gitit.Util(inDir,withTempDir,orIfNull,splitCategories,trim,yesOrNo,parsePageType)whereimportSystem.DirectoryimportControl.Exception(bracket)importSystem.FilePath((</>),(<.>))importSystem.IO.Error(isAlreadyExistsError)importControl.Monad.Trans(liftIO)importData.Char(toLower)importNetwork.Gitit.Types-- | Perform a function a directory and return to working directory.inDir::FilePath->IOa->IOainDirdaction=dow<-getCurrentDirectorysetCurrentDirectorydresult<-actionsetCurrentDirectorywreturnresult-- | Perform a function in a temporary directory and clean up.withTempDir::FilePath->(FilePath->IOa)->IOawithTempDirbaseName=bracket(createTempDir0baseName)(removeDirectoryRecursive)-- | Create a temporary directory with a unique name.createTempDir::Integer->FilePath->IOFilePathcreateTempDirnumbaseName=dosysTempDir<-getTemporaryDirectoryletdirName=sysTempDir</>baseName<.>shownumliftIO$catch(createDirectorydirName>>returndirName)$\e->ifisAlreadyExistsErrorethencreateTempDir(num+1)baseNameelseioErrore-- | Returns a list, if it is not null, or a backup, if it is.orIfNull::[a]->[a]->[a]orIfNulllstbackup=ifnulllstthenbackupelselst-- | Split a string containing a list of categories.splitCategories::String->[String]splitCategories=words.mappuncToSpace.trimwherepuncToSpacex|x`elem`".,;:"=' 'puncToSpacex=x-- | Trim leading and trailing spaces.trim::String->Stringtrim=reverse.trimLeft.reverse.trimLeftwheretrimLeft=dropWhile(`elem`" \t")-- | Show Bool as "yes" or "no".yesOrNo::Bool->StringyesOrNoTrue="yes"yesOrNoFalse="no"parsePageType::String->(PageType,Bool)parsePageTypes=casemaptoLowersof"markdown"->(Markdown,False)"markdown+lhs"->(Markdown,True)"rst"->(RST,False)"rst+lhs"->(RST,True)"html"->(HTML,False)"latex"->(LaTeX,False)"latex+lhs"->(LaTeX,True)x->error$"Unknown page type: "++x