on b287c82 help sign in
hakyll/src/Hakyll/Core/Identifier.hs
81 lines 2.7 kB history raw
1--------------------------------------------------------------------------------
2-- | An identifier is a type used to uniquely identify an item. An identifier is
3-- conceptually similar to a file path. Examples of identifiers are:
4--
5-- * @posts/foo.markdown@
6--
7-- * @index@
8--
9-- * @error/404@
10{-# LANGUAGE DeriveDataTypeable #-}
11{-# LANGUAGE GeneralizedNewtypeDeriving #-}
12module Hakyll.Core.Identifier
13 ( Identifier
14 , fromFilePath
15 , toFilePath
16 , identifierVersion
17 , setVersion
18 ) where
21--------------------------------------------------------------------------------
22import Control.Applicative ((<$>), (<*>))
23import Control.DeepSeq (NFData (..))
24import Data.List (intercalate)
25import System.FilePath (dropTrailingPathSeparator, splitPath)
28--------------------------------------------------------------------------------
29import Data.Binary (Binary (..))
30import Data.Typeable (Typeable)
31import GHC.Exts (IsString, fromString)
34--------------------------------------------------------------------------------
35data Identifier = Identifier
36 { identifierVersion :: Maybe String
37 , identifierPath :: String
38 } deriving (Eq, Ord, Typeable)
41--------------------------------------------------------------------------------
42instance Binary Identifier where
43 put (Identifier v p) = put v >> put p
44 get = Identifier <$> get <*> get
47--------------------------------------------------------------------------------
48instance IsString Identifier where
49 fromString = fromFilePath
52--------------------------------------------------------------------------------
53instance NFData Identifier where
54 rnf (Identifier v p) = rnf v `seq` rnf p `seq` ()
57--------------------------------------------------------------------------------
58instance Show Identifier where
59 show i = case identifierVersion i of
60 Nothing -> toFilePath i
61 Just v -> toFilePath i ++ " (" ++ v ++ ")"
64--------------------------------------------------------------------------------
65-- | Parse an identifier from a string
66fromFilePath :: String -> Identifier
67fromFilePath = Identifier Nothing .
68 intercalate "/" . filter (not . null) . split'
69 where
70 split' = map dropTrailingPathSeparator . splitPath
73--------------------------------------------------------------------------------
74-- | Convert an identifier to a relative 'FilePath'
75toFilePath :: Identifier -> FilePath
76toFilePath = identifierPath
79--------------------------------------------------------------------------------
80setVersion :: Maybe String -> Identifier -> Identifier
81setVersion v i = i {identifierVersion = v}