on 9939708 help sign in
hakyll/src/Hakyll/Core/Identifier.hs
47 lines 1.3 kB history raw
1-- | An identifier is a type used to uniquely identify a resource, target...
2--
3-- One can think of an identifier as something similar to a file path. An
4-- identifier is a path as well, with the different elements in the path
5-- separated by @/@ characters. Examples of identifiers are:
6--
7-- * @posts/foo.markdown@
8--
9-- * @index@
10--
11-- * @error/404@
12--
13module Hakyll.Core.Identifier
14 ( Identifier (..)
15 , parseIdentifier
16 , toFilePath
17 ) where
19import Control.Arrow (second)
21import GHC.Exts (IsString, fromString)
22import System.FilePath (joinPath)
24-- | An identifier used to uniquely identify a value
25--
26newtype Identifier = Identifier {unIdentifier :: [String]}
27 deriving (Eq, Ord)
29instance Show Identifier where
30 show = toFilePath
32instance IsString Identifier where
33 fromString = parseIdentifier
35-- | Parse an identifier from a string
36--
37parseIdentifier :: String -> Identifier
38parseIdentifier = Identifier . filter (not . null) . split'
39 where
40 split' [] = [[]]
41 split' str = let (pre, post) = second (drop 1) $ break (== '/') str
42 in pre : split' post
44-- | Convert an identifier to a relative 'FilePath'
45--
46toFilePath :: Identifier -> FilePath
47toFilePath = joinPath . unIdentifier