hakyll/src/Hakyll/Core/Identifier.hs

89 lines
3.2 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 10:22:19 +00:00
, castIdentifier
2010-12-23 16:19:21 +00:00
) where
2012-11-12 10:22:19 +00:00
--------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------
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
newtype Identifier a = Identifier {unIdentifier :: String}
deriving (Binary, Eq, NFData, Ord, Typeable)
2010-12-23 16:19:21 +00:00
2012-11-12 10:22:19 +00:00
--------------------------------------------------------------------------------
instance Show (Identifier a) where
show = toFilePath
2012-11-12 10:22:19 +00:00
--------------------------------------------------------------------------------
instance IsString (Identifier a) where
fromString = fromFilePath
2011-05-24 09:58:13 +00:00
2012-11-09 15:34:45 +00:00
--------------------------------------------------------------------------------
2010-12-23 16:19:21 +00:00
-- | Parse an identifier from a string
2012-11-12 10:22:19 +00:00
fromFilePath :: String -> Identifier a
fromFilePath = Identifier . 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'
2011-05-24 09:58:13 +00:00
toFilePath :: Identifier a -> FilePath
2012-11-12 10:22:19 +00:00
toFilePath = unIdentifier
2011-04-11 16:10:45 +00:00
2012-11-12 10:22:19 +00:00
--------------------------------------------------------------------------------
-- | Discard the phantom type parameter of an identifier
castIdentifier :: Identifier a -> Identifier b
castIdentifier (Identifier x) = Identifier x
{-# INLINE castIdentifier #-}