hakyll/src/Hakyll/Core/Identifier.hs

105 lines
3.7 KiB
Haskell
Raw Normal View History

2012-11-12 10:22:19 +00:00
--------------------------------------------------------------------------------
2010-12-23 16:19:21 +00:00
-- | An identifier is a type used to uniquely identify a resource, target...
--
-- One can think of an identifier as something similar to a file path. An
-- identifier is a path as well, with the different elements in the path
-- separated by @/@ characters. Examples of identifiers are:
--
-- * @posts/foo.markdown@
--
-- * @index@
--
-- * @error/404@
--
2011-02-12 15:54:31 +00:00
-- The most important difference between an 'Identifier' and a file path is that
-- the identifier for an item is not necesserily the file path.
--
-- For example, we could have an @index@ identifier, generated by Hakyll. The
-- actual file path would be @index.html@, but we identify it using @index@.
--
-- @posts/foo.markdown@ could be an identifier of an item that is rendered to
-- @posts/foo.html@. In this case, the identifier is the name of the source
-- file of the page.
--
2011-05-28 20:33:48 +00:00
-- An `Identifier` carries the type of the value it identifies. This basically
-- means that an @Identifier (Page String)@ refers to a page.
--
-- It is a phantom type parameter, meaning you can safely change this if you
-- know what you are doing. You can change the type using the 'castIdentifier'
-- function.
--
-- If the @a@ type is not known, Hakyll traditionally uses @Identifier ()@.
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
--------------------------------------------------------------------------------
-- | An identifier used to uniquely identify a value
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}