/
Util.hs
113 lines (101 loc) · 3.83 KB
/
Util.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{-# LANGUAGE CPP, ScopedTypeVariables #-}
{-
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.
-}
module Network.Gitit.Util ( readFileUTF8
, inDir
, withTempDir
, orIfNull
, splitCategories
, trim
, yesOrNo
, parsePageType
)
where
import System.Directory
import Control.Exception (bracket)
import System.FilePath ((</>), (<.>))
import System.IO.Error (isAlreadyExistsError)
import Control.Monad.Trans (liftIO)
import Data.Char (toLower)
import Data.ByteString.Lazy.UTF8 (toString)
import qualified Data.ByteString.Lazy as B
import Network.Gitit.Types
import qualified Control.Exception as E
import Control.Monad (liftM)
#if MIN_VERSION_base(4,5,0)
#else
import Codec.Binary.UTF8.String (encodeString)
#endif
-- | Read file as UTF-8 string. Encode filename as UTF-8.
readFileUTF8 :: FilePath -> IO String
#if MIN_VERSION_base(4,5,0)
readFileUTF8 f = liftM toString $ B.readFile f
#else
readFileUTF8 f = liftM toString $ B.readFile $ encodeString f
#endif
-- | Perform a function a directory and return to working directory.
inDir :: FilePath -> IO a -> IO a
inDir d action = do
w <- getCurrentDirectory
setCurrentDirectory d
result <- action
setCurrentDirectory w
return result
-- | Perform a function in a temporary directory and clean up.
withTempDir :: FilePath -> (FilePath -> IO a) -> IO a
withTempDir baseName f = do
oldDir <- getCurrentDirectory
bracket (createTempDir 0 baseName)
(\tmp -> setCurrentDirectory oldDir >> removeDirectoryRecursive tmp)
f
-- | Create a temporary directory with a unique name.
createTempDir :: Integer -> FilePath -> IO FilePath
createTempDir num baseName = do
sysTempDir <- getTemporaryDirectory
let dirName = sysTempDir </> baseName <.> show num
liftIO $ E.catch (createDirectory dirName >> return dirName) $
\e -> if isAlreadyExistsError e
then createTempDir (num + 1) baseName
else ioError e
-- | Returns a list, if it is not null, or a backup, if it is.
orIfNull :: [a] -> [a] -> [a]
orIfNull lst backup = if null lst then backup else lst
-- | Split a string containing a list of categories.
splitCategories :: String -> [String]
splitCategories = words . map puncToSpace . trim
where puncToSpace x | x `elem` ".,;:" = ' '
puncToSpace x = x
-- | Trim leading and trailing spaces.
trim :: String -> String
trim = reverse . trimLeft . reverse . trimLeft
where trimLeft = dropWhile (`elem` " \t")
-- | Show Bool as "yes" or "no".
yesOrNo :: Bool -> String
yesOrNo True = "yes"
yesOrNo False = "no"
parsePageType :: String -> (PageType, Bool)
parsePageType s =
case map toLower s of
"markdown" -> (Markdown,False)
"markdown+lhs" -> (Markdown,True)
"rst" -> (RST,False)
"rst+lhs" -> (RST,True)
"html" -> (HTML,False)
"textile" -> (Textile,False)
"latex" -> (LaTeX,False)
"latex+lhs" -> (LaTeX,True)
x -> error $ "Unknown page type: " ++ x