hakyll/src/Hakyll/Core/Identifier.hs

82 lines
2.7 KiB
Haskell
Raw Normal View History

2012-11-12 10:22:19 +00:00
--------------------------------------------------------------------------------
2012-11-25 09:45:55 +00:00
-- | An identifier is a type used to uniquely identify an item. An identifier is
-- conceptually similar to a file path. Examples of identifiers are:
2010-12-23 16:19:21 +00:00
--
-- * @posts/foo.markdown@
--
-- * @index@
--
-- * @error/404@
2012-11-12 10:22:19 +00:00
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2010-12-23 16:19:21 +00:00
module Hakyll.Core.Identifier
2012-11-12 10:22:19 +00:00
( Identifier
2012-11-09 15:34:45 +00:00
, fromFilePath
2010-12-23 16:19:21 +00:00
, toFilePath
2012-11-12 11:23:34 +00:00
, identifierVersion
, setVersion
2010-12-23 16:19:21 +00:00
) where
2012-11-12 10:22:19 +00:00
--------------------------------------------------------------------------------
2012-11-12 11:23:34 +00:00
import Control.Applicative ((<$>), (<*>))
import Control.DeepSeq (NFData (..))
import Data.List (intercalate)
import System.FilePath (dropTrailingPathSeparator, splitPath)
2010-12-23 16:19:21 +00:00
2012-11-12 10:22:19 +00:00
--------------------------------------------------------------------------------
2012-11-12 11:23:34 +00:00
import Data.Binary (Binary (..))
import Data.Typeable (Typeable)
import GHC.Exts (IsString, fromString)
2010-12-23 16:19:21 +00:00
2012-11-12 10:22:19 +00:00
--------------------------------------------------------------------------------
2012-11-13 12:13:17 +00:00
data Identifier = Identifier
2012-11-12 11:23:34 +00:00
{ identifierVersion :: Maybe String
, identifierPath :: String
} deriving (Eq, Ord, Typeable)
2010-12-23 16:19:21 +00:00
2012-11-12 10:22:19 +00:00
--------------------------------------------------------------------------------
2012-11-13 12:13:17 +00:00
instance Binary Identifier where
2012-11-12 11:23:34 +00:00
put (Identifier v p) = put v >> put p
get = Identifier <$> get <*> get
2012-11-12 10:22:19 +00:00
--------------------------------------------------------------------------------
2012-11-13 12:13:17 +00:00
instance IsString Identifier where
2012-11-12 10:22:19 +00:00
fromString = fromFilePath
2011-05-24 09:58:13 +00:00
2012-11-09 15:34:45 +00:00
2012-11-12 11:23:34 +00:00
--------------------------------------------------------------------------------
2012-11-13 12:13:17 +00:00
instance NFData Identifier where
2012-11-12 11:23:34 +00:00
rnf (Identifier v p) = rnf v `seq` rnf p `seq` ()
--------------------------------------------------------------------------------
2012-11-13 12:13:17 +00:00
instance Show Identifier where
2012-11-12 11:23:34 +00:00
show i = case identifierVersion i of
Nothing -> toFilePath i
Just v -> toFilePath i ++ " (" ++ v ++ ")"
2012-11-09 15:34:45 +00:00
--------------------------------------------------------------------------------
2010-12-23 16:19:21 +00:00
-- | Parse an identifier from a string
2012-11-13 12:13:17 +00:00
fromFilePath :: String -> Identifier
2012-11-12 11:23:34 +00:00
fromFilePath = Identifier Nothing .
intercalate "/" . filter (not . null) . split'
2010-12-23 16:19:21 +00:00
where
2012-05-18 15:17:51 +00:00
split' = map dropTrailingPathSeparator . splitPath
2010-12-23 16:19:21 +00:00
2012-11-09 15:34:45 +00:00
--------------------------------------------------------------------------------
2010-12-23 16:19:21 +00:00
-- | Convert an identifier to a relative 'FilePath'
2012-11-13 12:13:17 +00:00
toFilePath :: Identifier -> FilePath
2012-11-12 11:23:34 +00:00
toFilePath = identifierPath
2011-04-11 16:10:45 +00:00
2012-11-12 10:22:19 +00:00
--------------------------------------------------------------------------------
2012-11-13 12:13:17 +00:00
setVersion :: Maybe String -> Identifier -> Identifier
2012-11-12 11:23:34 +00:00
setVersion v i = i {identifierVersion = v}