on master help sign in
Add Item abstraction
877cb21Jasper Van der Jeugt
hakyll.cabal+7−7
@@ -96,9 +96,9 @@ Library
96 Hakyll.Core.Dependencies
96 Hakyll.Core.Dependencies
97 Hakyll.Core.Identifier
97 Hakyll.Core.Identifier
98 Hakyll.Core.Identifier.Pattern
98 Hakyll.Core.Identifier.Pattern
99+ Hakyll.Core.Item
99 Hakyll.Core.Logger
100 Hakyll.Core.Logger
100 Hakyll.Core.Metadata
101 Hakyll.Core.Metadata
101− Hakyll.Core.ResourceProvider
102 Hakyll.Core.Routes
102 Hakyll.Core.Routes
103 Hakyll.Core.Rules
103 Hakyll.Core.Rules
104 Hakyll.Core.Runtime
104 Hakyll.Core.Runtime
@@ -126,17 +126,17 @@ Library
126 Hakyll.Web.Util.Html
126 Hakyll.Web.Util.Html
127
127
128 Other-Modules:
128 Other-Modules:
129− Hakyll.Core.CompiledItem
130 Hakyll.Core.Compiler.Internal
129 Hakyll.Core.Compiler.Internal
131 Hakyll.Core.Compiler.Require
130 Hakyll.Core.Compiler.Require
132 Hakyll.Core.DirectedGraph
131 Hakyll.Core.DirectedGraph
133 Hakyll.Core.DirectedGraph.Dot
132 Hakyll.Core.DirectedGraph.Dot
134− Hakyll.Core.ResourceProvider.Internal
133+ Hakyll.Core.Item.SomeItem
134+ Hakyll.Core.Provider
135+ Hakyll.Core.Provider.Internal
135− Hakyll.Core.ResourceProvider.Metadata
136+ Hakyll.Core.Provider.Metadata
136− Hakyll.Core.ResourceProvider.MetadataCache
137+ Hakyll.Core.Provider.MetadataCache
137− Hakyll.Core.ResourceProvider.Modified
138+ Hakyll.Core.Provider.Modified
138 Hakyll.Core.Rules.Internal
139 Hakyll.Core.Rules.Internal
139− Hakyll.Web.Page.Internal
140 Hakyll.Web.Template.Internal
140 Hakyll.Web.Template.Internal
141 Hakyll.Web.Template.Read.Hakyll
141 Hakyll.Web.Template.Read.Hakyll
142 Hakyll.Web.Template.Read.Hamlet
142 Hakyll.Web.Template.Read.Hamlet
src/Hakyll.hs+2−2
@@ -6,8 +6,8 @@ module Hakyll
6 , module Hakyll.Core.Configuration
6 , module Hakyll.Core.Configuration
7 , module Hakyll.Core.Identifier
7 , module Hakyll.Core.Identifier
8 , module Hakyll.Core.Identifier.Pattern
8 , module Hakyll.Core.Identifier.Pattern
9+ , module Hakyll.Core.Item
9 , module Hakyll.Core.Metadata
10 , module Hakyll.Core.Metadata
10− , module Hakyll.Core.ResourceProvider
11 , module Hakyll.Core.Routes
11 , module Hakyll.Core.Routes
12 , module Hakyll.Core.Rules
12 , module Hakyll.Core.Rules
13#ifdef UNIX_FILTER
13#ifdef UNIX_FILTER
@@ -39,8 +39,8 @@ import Hakyll.Core.Compiler
39import Hakyll.Core.Configuration
39import Hakyll.Core.Configuration
40import Hakyll.Core.Identifier
40import Hakyll.Core.Identifier
41import Hakyll.Core.Identifier.Pattern
41import Hakyll.Core.Identifier.Pattern
42+import Hakyll.Core.Item
42import Hakyll.Core.Metadata
43import Hakyll.Core.Metadata
43−import Hakyll.Core.ResourceProvider
44import Hakyll.Core.Routes
44import Hakyll.Core.Routes
45import Hakyll.Core.Rules
45import Hakyll.Core.Rules
46#ifdef UNIX_FILTER
46#ifdef UNIX_FILTER
src/Hakyll/Core/CompiledItem.hsdeleted−55
@@ -1,55 +0,0 @@
1−--------------------------------------------------------------------------------
2−-- | A module containing a box datatype representing a compiled item. This
3−-- item can be of any type, given that a few restrictions hold:
4−--
5−-- * we need a 'Typeable' instance to perform type-safe casts;
6−--
7−-- * we need a 'Binary' instance so we can serialize these items to the cache;
8−--
9−-- * we need a 'Writable' instance so the results can be saved.
10−{-# LANGUAGE DeriveDataTypeable #-}
11−{-# LANGUAGE ExistentialQuantification #-}
12−module Hakyll.Core.CompiledItem
13− ( CompiledItem (..)
14− , compiledItem
15− , unCompiledItem
16− ) where
17−
18−
19−--------------------------------------------------------------------------------
20−import Data.Binary (Binary)
21−import Data.Maybe (fromMaybe)
22−import Data.Typeable (Typeable, cast, typeOf)
23−
24−
25−--------------------------------------------------------------------------------
26−import Hakyll.Core.Writable
27−
28−
29−--------------------------------------------------------------------------------
30−-- | Box type for a compiled item
31−--
32−data CompiledItem = forall a. (Binary a, Typeable a, Writable a)
33− => CompiledItem a
34− deriving (Typeable)
35−
36−
37−--------------------------------------------------------------------------------
38−instance Writable CompiledItem where
39− write p (CompiledItem x) = write p x
40−
41−
42−--------------------------------------------------------------------------------
43−-- | Box a value into a 'CompiledItem'
44−compiledItem :: (Binary a, Typeable a, Writable a) => a -> CompiledItem
45−compiledItem = CompiledItem
46−
47−
48−--------------------------------------------------------------------------------
49−-- | Unbox a value from a 'CompiledItem'
50−unCompiledItem :: (Binary a, Typeable a, Writable a) => CompiledItem -> a
51−unCompiledItem (CompiledItem x) = fromMaybe error' $ cast x
52− where
53− error' = error $
54− "Hakyll.Core.CompiledItem.unCompiledItem: " ++
55− "unsupported type (got " ++ show (typeOf x) ++ ")"
src/Hakyll/Core/Compiler.hs+23−28
@@ -3,11 +3,10 @@
3{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4module Hakyll.Core.Compiler
4module Hakyll.Core.Compiler
5 ( Compiler
5 ( Compiler
6− , getIdentifier
6+ , getUnderlying
7+ , makeItem
7 , getRoute
8 , getRoute
8− , getRouteFor
9 , getMetadata
9 , getMetadata
10− , getMetadataFor
11 , getResourceBody
10 , getResourceBody
12 , getResourceString
11 , getResourceString
13 , getResourceLBS
12 , getResourceLBS
@@ -34,42 +33,39 @@ import Hakyll.Core.Compiler.Internal
34import Hakyll.Core.Compiler.Require
33import Hakyll.Core.Compiler.Require
35import Hakyll.Core.Dependencies
34import Hakyll.Core.Dependencies
36import Hakyll.Core.Identifier
35import Hakyll.Core.Identifier
36+import Hakyll.Core.Item
37import Hakyll.Core.Logger as Logger
37import Hakyll.Core.Logger as Logger
38import Hakyll.Core.Metadata
38import Hakyll.Core.Metadata
39−import Hakyll.Core.ResourceProvider
39+import Hakyll.Core.Provider
40import Hakyll.Core.Routes
40import Hakyll.Core.Routes
41import qualified Hakyll.Core.Store as Store
41import qualified Hakyll.Core.Store as Store
42−import Hakyll.Core.Writable
43
42
44
43
45--------------------------------------------------------------------------------
44--------------------------------------------------------------------------------
46−-- | Get the identifier of the item that is currently being compiled
45+-- | Get the underlying identifier. Only use this if you know what you're doing.
47−getIdentifier :: Compiler Identifier
46+getUnderlying :: Compiler Identifier
48−getIdentifier = compilerIdentifier <$> compilerAsk
47+getUnderlying = compilerUnderlying <$> compilerAsk
49
48
50
49
51--------------------------------------------------------------------------------
50--------------------------------------------------------------------------------
52−-- | Get the route we are using for this item
51+makeItem :: a -> Compiler (Item a)
53−getRoute :: Compiler (Maybe FilePath)
52+makeItem x = do
54−getRoute = getIdentifier >>= getRouteFor
53+ identifier <- getUnderlying
54+ return $ Item identifier x
55
55
56
56
57--------------------------------------------------------------------------------
57--------------------------------------------------------------------------------
58-- | Get the route for a specified item
58-- | Get the route for a specified item
59−getRouteFor :: Identifier -> Compiler (Maybe FilePath)
59+getRoute :: Identifier -> Compiler (Maybe FilePath)
60−getRouteFor identifier = do
60+getRoute identifier = do
61 routes <- compilerRoutes <$> compilerAsk
61 routes <- compilerRoutes <$> compilerAsk
62 return $ runRoutes routes identifier
62 return $ runRoutes routes identifier
63
63
64
64
65−--------------------------------------------------------------------------------
66−getMetadata :: Compiler Metadata
67−getMetadata = getIdentifier >>= getMetadataFor
68−
69
65
70--------------------------------------------------------------------------------
66--------------------------------------------------------------------------------
71−getMetadataFor :: Identifier -> Compiler Metadata
67+getMetadata :: Identifier -> Compiler Metadata
72−getMetadataFor identifier = do
68+getMetadata identifier = do
73 provider <- compilerProvider <$> compilerAsk
69 provider <- compilerProvider <$> compilerAsk
74 compilerTellDependencies [IdentifierDependency identifier]
70 compilerTellDependencies [IdentifierDependency identifier]
75 compilerUnsafeIO $ resourceMetadata provider identifier
71 compilerUnsafeIO $ resourceMetadata provider identifier
@@ -77,32 +73,31 @@ getMetadataFor identifier = do
77
73
78--------------------------------------------------------------------------------
74--------------------------------------------------------------------------------
79-- | Get the body of the underlying resource
75-- | Get the body of the underlying resource
80−getResourceBody :: Compiler String
76+getResourceBody :: Compiler (Item String)
81getResourceBody = getResourceWith resourceBody
77getResourceBody = getResourceWith resourceBody
82
78
83
79
84--------------------------------------------------------------------------------
80--------------------------------------------------------------------------------
85-- | Get the resource we are compiling as a string
81-- | Get the resource we are compiling as a string
86−getResourceString :: Compiler String
82+getResourceString :: Compiler (Item String)
87getResourceString = getResourceWith $ const resourceString
83getResourceString = getResourceWith $ const resourceString
88
84
89
85
90--------------------------------------------------------------------------------
86--------------------------------------------------------------------------------
91-- | Get the resource we are compiling as a lazy bytestring
87-- | Get the resource we are compiling as a lazy bytestring
92−--
93−getResourceLBS :: Compiler ByteString
88+getResourceLBS :: Compiler (Item ByteString)
94getResourceLBS = getResourceWith $ const resourceLBS
89getResourceLBS = getResourceWith $ const resourceLBS
95
90
96
91
97--------------------------------------------------------------------------------
92--------------------------------------------------------------------------------
98-- | Overloadable function for 'getResourceString' and 'getResourceLBS'
93-- | Overloadable function for 'getResourceString' and 'getResourceLBS'
99−getResourceWith :: (ResourceProvider -> Identifier -> IO a) -> Compiler a
94+getResourceWith :: (Provider -> Identifier -> IO a) -> Compiler (Item a)
100getResourceWith reader = do
95getResourceWith reader = do
101 provider <- compilerProvider <$> compilerAsk
96 provider <- compilerProvider <$> compilerAsk
102− id' <- compilerIdentifier <$> compilerAsk
97+ id' <- compilerUnderlying <$> compilerAsk
103 let filePath = toFilePath id'
98 let filePath = toFilePath id'
104 if resourceExists provider id'
99 if resourceExists provider id'
105− then compilerUnsafeIO $ reader provider id'
100+ then compilerUnsafeIO $ Item id' <$> reader provider id'
106 else compilerThrow $ error' filePath
101 else compilerThrow $ error' filePath
107 where
102 where
108 error' fp = "Hakyll.Core.Compiler.getResourceWith: resource " ++
103 error' fp = "Hakyll.Core.Compiler.getResourceWith: resource " ++
@@ -110,12 +105,12 @@ getResourceWith reader = do
110
105
111
106
112--------------------------------------------------------------------------------
107--------------------------------------------------------------------------------
113−cached :: (Binary a, Typeable a, Writable a)
108+cached :: (Binary a, Typeable a)
114 => String
109 => String
115 -> Compiler a
110 -> Compiler a
116 -> Compiler a
111 -> Compiler a
117cached name compiler = do
112cached name compiler = do
118− id' <- compilerIdentifier <$> compilerAsk
113+ id' <- compilerUnderlying <$> compilerAsk
119 store <- compilerStore <$> compilerAsk
114 store <- compilerStore <$> compilerAsk
120 provider <- compilerProvider <$> compilerAsk
115 provider <- compilerProvider <$> compilerAsk
121 modified <- compilerUnsafeIO $ resourceModified provider id'
116 modified <- compilerUnsafeIO $ resourceModified provider id'
src/Hakyll/Core/Compiler/Internal.hs+4−4
@@ -35,7 +35,7 @@ import Data.Monoid (Monoid (..))
35import Hakyll.Core.Dependencies
35import Hakyll.Core.Dependencies
36import Hakyll.Core.Identifier
36import Hakyll.Core.Identifier
37import Hakyll.Core.Logger
37import Hakyll.Core.Logger
38−import Hakyll.Core.ResourceProvider
38+import Hakyll.Core.Provider
39import Hakyll.Core.Routes
39import Hakyll.Core.Routes
40import Hakyll.Core.Store
40import Hakyll.Core.Store
41
41
@@ -43,10 +43,10 @@ import Hakyll.Core.Store
43--------------------------------------------------------------------------------
43--------------------------------------------------------------------------------
44-- | Environment in which a compiler runs
44-- | Environment in which a compiler runs
45data CompilerRead = CompilerRead
45data CompilerRead = CompilerRead
46− { -- | Target identifier
46+ { -- | Underlying identifier
47− compilerIdentifier :: Identifier
47+ compilerUnderlying :: Identifier
48 , -- | Resource provider
48 , -- | Resource provider
49− compilerProvider :: ResourceProvider
49+ compilerProvider :: Provider
50 , -- | List of all known identifiers
50 , -- | List of all known identifiers
51 compilerUniverse :: [Identifier]
51 compilerUniverse :: [Identifier]
52 , -- | Site routes
52 , -- | Site routes
src/Hakyll/Core/Item.hsnew file+41
@@ -0,0 +1,41 @@
1+--------------------------------------------------------------------------------
2+-- | An item is a combination of some content and its 'Identifier'. This way, we
3+-- can still use the 'Identifier' to access metadata.
4+{-# LANGUAGE DeriveDataTypeable #-}
5+module Hakyll.Core.Item
6+ ( Item (..)
7+ , itemSetBody
8+ ) where
9+
10+
11+--------------------------------------------------------------------------------
12+import Control.Applicative ((<$>), (<*>))
13+import Data.Binary (Binary (..))
14+import Data.Typeable (Typeable)
15+
16+
17+--------------------------------------------------------------------------------
18+import Hakyll.Core.Identifier
19+
20+
21+--------------------------------------------------------------------------------
22+data Item a = Item
23+ { itemIdentifier :: Identifier
24+ , itemBody :: a
25+ } deriving (Show, Typeable)
26+
27+
28+--------------------------------------------------------------------------------
29+instance Functor Item where
30+ fmap f (Item i x) = Item i (f x)
31+
32+
33+--------------------------------------------------------------------------------
34+instance Binary a => Binary (Item a) where
35+ put (Item i x) = put i >> put x
36+ get = Item <$> get <*> get
37+
38+
39+--------------------------------------------------------------------------------
40+itemSetBody :: a -> Item b -> Item a
41+itemSetBody x (Item i _) = Item i x
src/Hakyll/Core/Item/SomeItem.hsnew file+23
@@ -0,0 +1,23 @@
1+--------------------------------------------------------------------------------
2+{-# LANGUAGE DeriveDataTypeable #-}
3+{-# LANGUAGE ExistentialQuantification #-}
4+module Hakyll.Core.Item.SomeItem
5+ ( SomeItem (..)
6+ ) where
7+
8+
9+--------------------------------------------------------------------------------
10+import Data.Binary (Binary)
11+import Data.Typeable (Typeable)
12+
13+
14+--------------------------------------------------------------------------------
15+import Hakyll.Core.Item
16+import Hakyll.Core.Writable
17+
18+
19+--------------------------------------------------------------------------------
20+-- | An existential type, mostly for internal usage.
21+data SomeItem = forall a.
22+ (Binary a, Typeable a, Writable a) => SomeItem (Item a)
23+ deriving (Typeable)
src/Hakyll/Core/ResourceProvider.hs → src/Hakyll/Core/Provider.hsrenamed+8−8
@@ -1,10 +1,10 @@
1--------------------------------------------------------------------------------
1--------------------------------------------------------------------------------
2-- | This module provides an wrapper API around the file system which does some
2-- | This module provides an wrapper API around the file system which does some
3-- caching.
3-- caching.
4−module Hakyll.Core.ResourceProvider
4+module Hakyll.Core.Provider
5 ( -- * Constructing resource providers
5 ( -- * Constructing resource providers
6− ResourceProvider
6+ Provider
7− , newResourceProvider
7+ , newProvider
8
8
9 -- * Querying resource properties
9 -- * Querying resource properties
10 , resourceList
10 , resourceList
@@ -25,14 +25,14 @@ module Hakyll.Core.ResourceProvider
25--------------------------------------------------------------------------------
25--------------------------------------------------------------------------------
26import Hakyll.Core.Identifier
26import Hakyll.Core.Identifier
27import Hakyll.Core.Metadata
27import Hakyll.Core.Metadata
28−import Hakyll.Core.ResourceProvider.Internal
28+import Hakyll.Core.Provider.Internal
29−import qualified Hakyll.Core.ResourceProvider.MetadataCache as Internal
29+import qualified Hakyll.Core.Provider.MetadataCache as Internal
30−import Hakyll.Core.ResourceProvider.Modified
30+import Hakyll.Core.Provider.Modified
31
31
32
32
33--------------------------------------------------------------------------------
33--------------------------------------------------------------------------------
34-- | Wrapper to ensure metadata cache is invalidated if necessary
34-- | Wrapper to ensure metadata cache is invalidated if necessary
35−resourceMetadata :: ResourceProvider -> Identifier -> IO Metadata
35+resourceMetadata :: Provider -> Identifier -> IO Metadata
36resourceMetadata rp r = do
36resourceMetadata rp r = do
37 _ <- resourceModified rp r
37 _ <- resourceModified rp r
38 Internal.resourceMetadata rp r
38 Internal.resourceMetadata rp r
@@ -40,7 +40,7 @@ resourceMetadata rp r = do
40
40
41--------------------------------------------------------------------------------
41--------------------------------------------------------------------------------
42-- | Wrapper to ensure metadata cache is invalidated if necessary
42-- | Wrapper to ensure metadata cache is invalidated if necessary
43−resourceBody :: ResourceProvider -> Identifier -> IO String
43+resourceBody :: Provider -> Identifier -> IO String
44resourceBody rp r = do
44resourceBody rp r = do
45 _ <- resourceModified rp r
45 _ <- resourceModified rp r
46 Internal.resourceBody rp r
46 Internal.resourceBody rp r
src/Hakyll/Core/ResourceProvider/Internal.hs → src/Hakyll/Core/Provider/Internal.hsrenamed+17−17
@@ -1,7 +1,7 @@
1--------------------------------------------------------------------------------
1--------------------------------------------------------------------------------
2−module Hakyll.Core.ResourceProvider.Internal
2+module Hakyll.Core.Provider.Internal
3− ( ResourceProvider (..)
3+ ( Provider (..)
4− , newResourceProvider
4+ , newProvider
5
5
6 , resourceList
6 , resourceList
7 , resourceExists
7 , resourceExists
@@ -31,39 +31,39 @@ import Hakyll.Core.Identifier
31
31
32--------------------------------------------------------------------------------
32--------------------------------------------------------------------------------
33-- | Responsible for retrieving and listing resources
33-- | Responsible for retrieving and listing resources
34−data ResourceProvider = ResourceProvider
34+data Provider = Provider
35 { -- | A list of all files found
35 { -- | A list of all files found
36− resourceSet :: Set Identifier
36+ providerSet :: Set Identifier
37 , -- | Cache keeping track of modified files
37 , -- | Cache keeping track of modified files
38− resourceModifiedCache :: IORef (Map Identifier Bool)
38+ providerModifiedCache :: IORef (Map Identifier Bool)
39 , -- | Underlying persistent store for caching
39 , -- | Underlying persistent store for caching
40− resourceStore :: Store
40+ providerStore :: Store
41 }
41 }
42
42
43
43
44--------------------------------------------------------------------------------
44--------------------------------------------------------------------------------
45-- | Create a resource provider
45-- | Create a resource provider
46−newResourceProvider :: Store -- ^ Store to use
46+newProvider :: Store -- ^ Store to use
47− -> (FilePath -> Bool) -- ^ Should we ignore this file?
47+ -> (FilePath -> Bool) -- ^ Should we ignore this file?
48− -> FilePath -- ^ Search directory
48+ -> FilePath -- ^ Search directory
49− -> IO ResourceProvider -- ^ Resulting provider
49+ -> IO Provider -- ^ Resulting provider
50−newResourceProvider store ignore directory = do
50+newProvider store ignore directory = do
51 list <- map fromFilePath . filter (not . ignore) <$>
51 list <- map fromFilePath . filter (not . ignore) <$>
52 getRecursiveContents False directory
52 getRecursiveContents False directory
53 cache <- newIORef M.empty
53 cache <- newIORef M.empty
54− return $ ResourceProvider (S.fromList list) cache store
54+ return $ Provider (S.fromList list) cache store
55
55
56
56
57--------------------------------------------------------------------------------
57--------------------------------------------------------------------------------
58−resourceList :: ResourceProvider -> [Identifier]
58+resourceList :: Provider -> [Identifier]
59−resourceList = S.toList . resourceSet
59+resourceList = S.toList . providerSet
60
60
61
61
62--------------------------------------------------------------------------------
62--------------------------------------------------------------------------------
63-- | Check if a given resource exists
63-- | Check if a given resource exists
64−resourceExists :: ResourceProvider -> Identifier -> Bool
64+resourceExists :: Provider -> Identifier -> Bool
65resourceExists provider =
65resourceExists provider =
66− (`S.member` resourceSet provider) . setVersion Nothing
66+ (`S.member` providerSet provider) . setVersion Nothing
67
67
68
68
69--------------------------------------------------------------------------------
69--------------------------------------------------------------------------------
src/Hakyll/Core/ResourceProvider/Metadata.hs → src/Hakyll/Core/Provider/Metadata.hsrenamed+10−10
@@ -1,30 +1,30 @@
1--------------------------------------------------------------------------------
1--------------------------------------------------------------------------------
2-- | Internal module to parse metadata
2-- | Internal module to parse metadata
3−module Hakyll.Core.ResourceProvider.Metadata
3+module Hakyll.Core.Provider.Metadata
4 ( loadMetadata
4 ( loadMetadata
5 ) where
5 ) where
6
6
7
7
8--------------------------------------------------------------------------------
8--------------------------------------------------------------------------------
9import Control.Applicative
9import Control.Applicative
10−import Control.Arrow (second)
10+import Control.Arrow (second)
11−import qualified Data.ByteString.Char8 as BC
11+import qualified Data.ByteString.Char8 as BC
12−import qualified Data.Map as M
12+import qualified Data.Map as M
13−import System.IO as IO
13+import System.IO as IO
14−import Text.Parsec ((<?>))
14+import Text.Parsec ((<?>))
15−import qualified Text.Parsec as P
15+import qualified Text.Parsec as P
16−import Text.Parsec.String (Parser)
16+import Text.Parsec.String (Parser)
17
17
18
18
19--------------------------------------------------------------------------------
19--------------------------------------------------------------------------------
20import Hakyll.Core.Identifier
20import Hakyll.Core.Identifier
21import Hakyll.Core.Metadata
21import Hakyll.Core.Metadata
22−import Hakyll.Core.ResourceProvider.Internal
22+import Hakyll.Core.Provider.Internal
23import Hakyll.Core.Util.String
23import Hakyll.Core.Util.String
24
24
25
25
26--------------------------------------------------------------------------------
26--------------------------------------------------------------------------------
27−loadMetadata :: ResourceProvider -> Identifier -> IO (Metadata, Maybe String)
27+loadMetadata :: Provider -> Identifier -> IO (Metadata, Maybe String)
28loadMetadata rp identifier = do
28loadMetadata rp identifier = do
29 hasHeader <- probablyHasMetadataHeader fp
29 hasHeader <- probablyHasMetadataHeader fp
30 (md, body) <- if hasHeader
30 (md, body) <- if hasHeader
src/Hakyll/Core/ResourceProvider/MetadataCache.hs → src/Hakyll/Core/Provider/MetadataCache.hsrenamed+20−20
@@ -1,5 +1,5 @@
1--------------------------------------------------------------------------------
1--------------------------------------------------------------------------------
2−module Hakyll.Core.ResourceProvider.MetadataCache
2+module Hakyll.Core.Provider.MetadataCache
3 ( resourceMetadata
3 ( resourceMetadata
4 , resourceBody
4 , resourceBody
5 , resourceInvalidateMetadataCache
5 , resourceInvalidateMetadataCache
@@ -9,50 +9,50 @@ module Hakyll.Core.ResourceProvider.MetadataCache
9--------------------------------------------------------------------------------
9--------------------------------------------------------------------------------
10import Hakyll.Core.Identifier
10import Hakyll.Core.Identifier
11import Hakyll.Core.Metadata
11import Hakyll.Core.Metadata
12−import Hakyll.Core.ResourceProvider.Internal
12+import Hakyll.Core.Provider.Internal
13−import Hakyll.Core.ResourceProvider.Metadata
13+import Hakyll.Core.Provider.Metadata
14−import qualified Hakyll.Core.Store as Store
14+import qualified Hakyll.Core.Store as Store
15
15
16
16
17--------------------------------------------------------------------------------
17--------------------------------------------------------------------------------
18−resourceMetadata :: ResourceProvider -> Identifier -> IO Metadata
18+resourceMetadata :: Provider -> Identifier -> IO Metadata
19−resourceMetadata rp r = do
19+resourceMetadata p r = do
20− load rp r
20+ load p r
21− Store.Found md <- Store.get (resourceStore rp)
21+ Store.Found md <- Store.get (providerStore p)
22 [name, toFilePath r, "metadata"]
22 [name, toFilePath r, "metadata"]
23 return md
23 return md
24
24
25
25
26--------------------------------------------------------------------------------
26--------------------------------------------------------------------------------
27−resourceBody :: ResourceProvider -> Identifier -> IO String
27+resourceBody :: Provider -> Identifier -> IO String
28−resourceBody rp r = do
28+resourceBody p r = do
29− load rp r
29+ load p r
30− Store.Found bd <- Store.get (resourceStore rp)
30+ Store.Found bd <- Store.get (providerStore p)
31 [name, toFilePath r, "body"]
31 [name, toFilePath r, "body"]
32 maybe (resourceString r) return bd
32 maybe (resourceString r) return bd
33
33
34
34
35--------------------------------------------------------------------------------
35--------------------------------------------------------------------------------
36−resourceInvalidateMetadataCache :: ResourceProvider -> Identifier -> IO ()
36+resourceInvalidateMetadataCache :: Provider -> Identifier -> IO ()
37−resourceInvalidateMetadataCache rp r = do
37+resourceInvalidateMetadataCache p r = do
38− Store.delete (resourceStore rp) [name, toFilePath r, "metadata"]
38+ Store.delete (providerStore p) [name, toFilePath r, "metadata"]
39− Store.delete (resourceStore rp) [name, toFilePath r, "body"]
39+ Store.delete (providerStore p) [name, toFilePath r, "body"]
40
40
41
41
42--------------------------------------------------------------------------------
42--------------------------------------------------------------------------------
43−load :: ResourceProvider -> Identifier -> IO ()
43+load :: Provider -> Identifier -> IO ()
44−load rp r = do
44+load p r = do
45 mmd <- Store.get store mdk :: IO (Store.Result Metadata)
45 mmd <- Store.get store mdk :: IO (Store.Result Metadata)
46 case mmd of
46 case mmd of
47 -- Already loaded
47 -- Already loaded
48 Store.Found _ -> return ()
48 Store.Found _ -> return ()
49 -- Not yet loaded
49 -- Not yet loaded
50 _ -> do
50 _ -> do
51− (metadata, body) <- loadMetadata rp r
51+ (metadata, body) <- loadMetadata p r
52 Store.set store mdk metadata
52 Store.set store mdk metadata
53 Store.set store bk body
53 Store.set store bk body
54 where
54 where
55− store = resourceStore rp
55+ store = providerStore p
56 mdk = [name, toFilePath r, "metadata"]
56 mdk = [name, toFilePath r, "metadata"]
57 bk = [name, toFilePath r, "body"]
57 bk = [name, toFilePath r, "body"]
58
58
src/Hakyll/Core/ResourceProvider/Modified.hs → src/Hakyll/Core/Provider/Modified.hsrenamed+16−16
@@ -1,33 +1,33 @@
1--------------------------------------------------------------------------------
1--------------------------------------------------------------------------------
2−module Hakyll.Core.ResourceProvider.Modified
2+module Hakyll.Core.Provider.Modified
3 ( resourceModified
3 ( resourceModified
4 , resourceModificationTime
4 , resourceModificationTime
5 ) where
5 ) where
6
6
7
7
8--------------------------------------------------------------------------------
8--------------------------------------------------------------------------------
9−import Control.Applicative ((<$>), (<*>))
9+import Control.Applicative ((<$>), (<*>))
10−import Control.Monad (when)
10+import Control.Monad (when)
11−import qualified Crypto.Hash.MD5 as MD5
11+import qualified Crypto.Hash.MD5 as MD5
12−import qualified Data.ByteString as B
12+import qualified Data.ByteString as B
13−import qualified Data.ByteString.Lazy as BL
13+import qualified Data.ByteString.Lazy as BL
14import Data.IORef
14import Data.IORef
15−import qualified Data.Map as M
15+import qualified Data.Map as M
16−import Data.Time (UTCTime)
16+import Data.Time (UTCTime)
17−import System.Directory (getModificationTime)
17+import System.Directory (getModificationTime)
18
18
19
19
20--------------------------------------------------------------------------------
20--------------------------------------------------------------------------------
21import Hakyll.Core.Identifier
21import Hakyll.Core.Identifier
22−import Hakyll.Core.ResourceProvider.Internal
22+import Hakyll.Core.Provider.Internal
23−import Hakyll.Core.ResourceProvider.MetadataCache
23+import Hakyll.Core.Provider.MetadataCache
24−import Hakyll.Core.Store (Store)
24+import Hakyll.Core.Store (Store)
25−import qualified Hakyll.Core.Store as Store
25+import qualified Hakyll.Core.Store as Store
26
26
27
27
28--------------------------------------------------------------------------------
28--------------------------------------------------------------------------------
29-- | A resource is modified if it or its metadata has changed
29-- | A resource is modified if it or its metadata has changed
30−resourceModified :: ResourceProvider -> Identifier -> IO Bool
30+resourceModified :: Provider -> Identifier -> IO Bool
31resourceModified rp r
31resourceModified rp r
32 | not exists = return False
32 | not exists = return False
33 | otherwise = do
33 | otherwise = do
@@ -49,8 +49,8 @@ resourceModified rp r
49 where
49 where
50 normalized = setVersion Nothing r
50 normalized = setVersion Nothing r
51 exists = resourceExists rp r
51 exists = resourceExists rp r
52− store = resourceStore rp
52+ store = providerStore rp
53− cacheRef = resourceModifiedCache rp
53+ cacheRef = providerModifiedCache rp
54
54
55
55
56--------------------------------------------------------------------------------
56--------------------------------------------------------------------------------
src/Hakyll/Core/Rules.hs+10−9
@@ -44,11 +44,12 @@ import Data.Typeable (Typeable)
44
44
45
45
46--------------------------------------------------------------------------------
46--------------------------------------------------------------------------------
47−import Hakyll.Core.CompiledItem
48import Hakyll.Core.Compiler.Internal
47import Hakyll.Core.Compiler.Internal
49import Hakyll.Core.Identifier
48import Hakyll.Core.Identifier
50import Hakyll.Core.Identifier.Pattern
49import Hakyll.Core.Identifier.Pattern
51−import Hakyll.Core.ResourceProvider
50+import Hakyll.Core.Item
51+import Hakyll.Core.Item.SomeItem
52+import Hakyll.Core.Provider
52import Hakyll.Core.Routes
53import Hakyll.Core.Routes
53import Hakyll.Core.Rules.Internal
54import Hakyll.Core.Rules.Internal
54import Hakyll.Core.Writable
55import Hakyll.Core.Writable
@@ -63,11 +64,11 @@ tellRoute route' = Rules $ tell $ RuleSet route' mempty mempty
63--------------------------------------------------------------------------------
64--------------------------------------------------------------------------------
64-- | Add a number of compilers
65-- | Add a number of compilers
65tellCompilers :: (Binary a, Typeable a, Writable a)
66tellCompilers :: (Binary a, Typeable a, Writable a)
66− => [(Identifier, Compiler a)]
67+ => [(Identifier, Compiler (Item a))]
67 -> Rules ()
68 -> Rules ()
68tellCompilers compilers = Rules $ do
69tellCompilers compilers = Rules $ do
69 -- We box the compilers so they have a more simple type
70 -- We box the compilers so they have a more simple type
70− let compilers' = map (second $ fmap compiledItem) compilers
71+ let compilers' = map (second $ fmap SomeItem) compilers
71 tell $ RuleSet mempty compilers' mempty
72 tell $ RuleSet mempty compilers' mempty
72
73
73
74
@@ -132,7 +133,7 @@ group g = Rules . local setVersion' . unRules
132-- no resources match the current selection, nothing will happen. In this case,
133-- no resources match the current selection, nothing will happen. In this case,
133-- you might want to have a look at 'create'.
134-- you might want to have a look at 'create'.
134compile :: (Binary a, Typeable a, Writable a)
135compile :: (Binary a, Typeable a, Writable a)
135− => Compiler a -> Rules ()
136+ => Compiler (Item a) -> Rules ()
136compile compiler = do
137compile compiler = do
137 ids <- resources
138 ids <- resources
138 tellCompilers [(id', compiler) | id' <- ids]
139 tellCompilers [(id', compiler) | id' <- ids]
@@ -149,7 +150,7 @@ compile compiler = do
149-- replaced by the group set via 'group' (or 'Nothing', if 'group' has not been
150-- replaced by the group set via 'group' (or 'Nothing', if 'group' has not been
150-- used).
151-- used).
151create :: (Binary a, Typeable a, Writable a)
152create :: (Binary a, Typeable a, Writable a)
152− => Identifier -> Compiler a -> Rules ()
153+ => Identifier -> Compiler (Item a) -> Rules ()
153create id' compiler = Rules $ do
154create id' compiler = Rules $ do
154 version' <- rulesVersion <$> ask
155 version' <- rulesVersion <$> ask
155 let id'' = setVersion version' id'
156 let id'' = setVersion version' id'
@@ -175,9 +176,9 @@ route route' = Rules $ do
175-- the correct group to the identifiers.
176-- the correct group to the identifiers.
176resources :: Rules [Identifier]
177resources :: Rules [Identifier]
177resources = Rules $ do
178resources = Rules $ do
178− pattern <- rulesPattern <$> ask
179+ pattern <- rulesPattern <$> ask
179− provider <- rulesResourceProvider <$> ask
180+ provider <- rulesProvider <$> ask
180− g <- rulesVersion <$> ask
181+ g <- rulesVersion <$> ask
181 return $ filterMatches pattern $ map (setVersion g) $ resourceList provider
182 return $ filterMatches pattern $ map (setVersion g) $ resourceList provider
182
183
183
184
src/Hakyll/Core/Rules/Internal.hs+10−10
@@ -20,11 +20,11 @@ import Data.Set (Set)
20
20
21
21
22--------------------------------------------------------------------------------
22--------------------------------------------------------------------------------
23−import Hakyll.Core.CompiledItem
24import Hakyll.Core.Compiler.Internal
23import Hakyll.Core.Compiler.Internal
25import Hakyll.Core.Identifier
24import Hakyll.Core.Identifier
26import Hakyll.Core.Identifier.Pattern
25import Hakyll.Core.Identifier.Pattern
27−import Hakyll.Core.ResourceProvider
26+import Hakyll.Core.Item.SomeItem
27+import Hakyll.Core.Provider
28import Hakyll.Core.Routes
28import Hakyll.Core.Routes
29
29
30
30
@@ -34,7 +34,7 @@ data RuleSet = RuleSet
34 { -- | Routes used in the compilation structure
34 { -- | Routes used in the compilation structure
35 rulesRoutes :: Routes
35 rulesRoutes :: Routes
36 , -- | Compilation rules
36 , -- | Compilation rules
37− rulesCompilers :: [(Identifier, Compiler CompiledItem)]
37+ rulesCompilers :: [(Identifier, Compiler SomeItem)]
38 , -- | A set of the actually used files
38 , -- | A set of the actually used files
39 rulesResources :: Set Identifier
39 rulesResources :: Set Identifier
40 }
40 }
@@ -57,9 +57,9 @@ data RuleState = RuleState
57--------------------------------------------------------------------------------
57--------------------------------------------------------------------------------
58-- | Rule environment
58-- | Rule environment
59data RuleEnvironment = RuleEnvironment
59data RuleEnvironment = RuleEnvironment
60− { rulesResourceProvider :: ResourceProvider
60+ { rulesProvider :: Provider
61− , rulesPattern :: Pattern
61+ , rulesPattern :: Pattern
62− , rulesVersion :: Maybe String
62+ , rulesVersion :: Maybe String
63 }
63 }
64
64
65
65
@@ -72,16 +72,16 @@ newtype Rules a = Rules
72
72
73--------------------------------------------------------------------------------
73--------------------------------------------------------------------------------
74-- | Run a Rules monad, resulting in a 'RuleSet'
74-- | Run a Rules monad, resulting in a 'RuleSet'
75−runRules :: Rules a -> ResourceProvider -> IO RuleSet
75+runRules :: Rules a -> Provider -> IO RuleSet
76runRules rules provider = do
76runRules rules provider = do
77 (_, _, ruleSet) <- runRWST (unRules rules) env state
77 (_, _, ruleSet) <- runRWST (unRules rules) env state
78 return $ nubCompilers ruleSet
78 return $ nubCompilers ruleSet
79 where
79 where
80 state = RuleState {rulesNextIdentifier = 0}
80 state = RuleState {rulesNextIdentifier = 0}
81 env = RuleEnvironment
81 env = RuleEnvironment
82− { rulesResourceProvider = provider
82+ { rulesProvider = provider
83− , rulesPattern = mempty
83+ , rulesPattern = mempty
84− , rulesVersion = Nothing
84+ , rulesVersion = Nothing
85 }
85 }
86
86
87
87
src/Hakyll/Core/Runtime.hs+14−11
@@ -22,15 +22,16 @@ import System.FilePath ((</>))
22
22
23
23
24--------------------------------------------------------------------------------
24--------------------------------------------------------------------------------
25−import Hakyll.Core.CompiledItem
26import Hakyll.Core.Compiler.Internal
25import Hakyll.Core.Compiler.Internal
27import Hakyll.Core.Compiler.Require
26import Hakyll.Core.Compiler.Require
28import Hakyll.Core.Configuration
27import Hakyll.Core.Configuration
29import Hakyll.Core.Dependencies
28import Hakyll.Core.Dependencies
30import Hakyll.Core.Identifier
29import Hakyll.Core.Identifier
30+import Hakyll.Core.Item
31+import Hakyll.Core.Item.SomeItem
31import Hakyll.Core.Logger (Logger)
32import Hakyll.Core.Logger (Logger)
32import qualified Hakyll.Core.Logger as Logger
33import qualified Hakyll.Core.Logger as Logger
33−import Hakyll.Core.ResourceProvider
34+import Hakyll.Core.Provider
34import Hakyll.Core.Routes
35import Hakyll.Core.Routes
35import Hakyll.Core.Rules.Internal
36import Hakyll.Core.Rules.Internal
36import Hakyll.Core.Store (Store)
37import Hakyll.Core.Store (Store)
@@ -49,7 +50,7 @@ run configuration rules = do
49 store <- Store.new (inMemoryCache configuration) $
50 store <- Store.new (inMemoryCache configuration) $
50 storeDirectory configuration
51 storeDirectory configuration
51 Logger.message logger "Creating provider..."
52 Logger.message logger "Creating provider..."
52− provider <- newResourceProvider store (ignoreFile configuration) "."
53+ provider <- newProvider store (ignoreFile configuration) "."
53 Logger.message logger "Running rules..."
54 Logger.message logger "Running rules..."
54 ruleSet <- runRules rules provider
55 ruleSet <- runRules rules provider
55
56
@@ -94,17 +95,17 @@ run configuration rules = do
94data RuntimeRead = RuntimeRead
95data RuntimeRead = RuntimeRead
95 { runtimeConfiguration :: Configuration
96 { runtimeConfiguration :: Configuration
96 , runtimeLogger :: Logger
97 , runtimeLogger :: Logger
97− , runtimeProvider :: ResourceProvider
98+ , runtimeProvider :: Provider
98 , runtimeStore :: Store
99 , runtimeStore :: Store
99 , runtimeRoutes :: Routes
100 , runtimeRoutes :: Routes
100− , runtimeUniverse :: [(Identifier, Compiler CompiledItem)]
101+ , runtimeUniverse :: [(Identifier, Compiler SomeItem)]
101 }
102 }
102
103
103
104
104--------------------------------------------------------------------------------
105--------------------------------------------------------------------------------
105data RuntimeState = RuntimeState
106data RuntimeState = RuntimeState
106 { runtimeDone :: Set Identifier
107 { runtimeDone :: Set Identifier
107− , runtimeTodo :: Map Identifier (Compiler CompiledItem)
108+ , runtimeTodo :: Map Identifier (Compiler SomeItem)
108 , runtimeFacts :: DependencyFacts
109 , runtimeFacts :: DependencyFacts
109 }
110 }
110
111
@@ -178,7 +179,7 @@ chase trail id'
178
179
179 let compiler = todo M.! id'
180 let compiler = todo M.! id'
180 read' = CompilerRead
181 read' = CompilerRead
181− { compilerIdentifier = id'
182+ { compilerUnderlying = id'
182 , compilerProvider = provider
183 , compilerProvider = provider
183 , compilerUniverse = map fst universe
184 , compilerUniverse = map fst universe
184 , compilerRoutes = routes
185 , compilerRoutes = routes
@@ -192,8 +193,10 @@ chase trail id'
192 CompilerError e -> throwError e
193 CompilerError e -> throwError e
193
194
194 -- Huge success
195 -- Huge success
195− CompilerDone (CompiledItem compiled) cwrite -> do
196+ CompilerDone (SomeItem item) cwrite -> do
197+ -- TODO: Sanity check on itemIdentifier?
198+ let body = itemBody item
196− let facts = compilerDependencies cwrite
199+ facts = compilerDependencies cwrite
197 cacheHits
200 cacheHits
198 | compilerCacheHits cwrite <= 0 = "updated"
201 | compilerCacheHits cwrite <= 0 = "updated"
199 | otherwise = "cached "
202 | otherwise = "cached "
@@ -207,11 +210,11 @@ chase trail id'
207 Just url -> do
210 Just url -> do
208 let path = destinationDirectory config </> url
211 let path = destinationDirectory config </> url
209 liftIO $ makeDirectories path
212 liftIO $ makeDirectories path
210− liftIO $ write path compiled
213+ liftIO $ write path item
211 Logger.debug logger $ "Routed to " ++ path
214 Logger.debug logger $ "Routed to " ++ path
212
215
213 -- Save! (For require)
216 -- Save! (For require)
214− liftIO $ save store id' compiled
217+ liftIO $ save store id' body
215
218
216 -- Update state
219 -- Update state
217 modify $ \s -> s
220 modify $ \s -> s
src/Hakyll/Core/Writable.hs+32−18
@@ -1,42 +1,56 @@
1+--------------------------------------------------------------------------------
1-- | Describes writable items; items that can be saved to the disk
2-- | Describes writable items; items that can be saved to the disk
2−--
3+{-# LANGUAGE FlexibleInstances #-}
3−{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
4+{-# LANGUAGE TypeSynonymInstances #-}
4module Hakyll.Core.Writable
5module Hakyll.Core.Writable
5 ( Writable (..)
6 ( Writable (..)
6 ) where
7 ) where
7
8
8−import Data.Word (Word8)
9
9
10+--------------------------------------------------------------------------------
10−import qualified Data.ByteString as SB
11+import qualified Data.ByteString as SB
11−import qualified Data.ByteString.Lazy as LB
12+import qualified Data.ByteString.Lazy as LB
13+import Data.Word (Word8)
12−import Text.Blaze.Html (Html)
14+import Text.Blaze.Html (Html)
13−import Text.Blaze.Html.Renderer.String (renderHtml)
15+import Text.Blaze.Html.Renderer.String (renderHtml)
14
16
15−import Hakyll.Core.Identifier
16
17
18+--------------------------------------------------------------------------------
19+import Hakyll.Core.Item
20+
21+
22+--------------------------------------------------------------------------------
17-- | Describes an item that can be saved to the disk
23-- | Describes an item that can be saved to the disk
18−--
19class Writable a where
24class Writable a where
20 -- | Save an item to the given filepath
25 -- | Save an item to the given filepath
21− write :: FilePath -> a -> IO ()
26+ write :: FilePath -> Item a -> IO ()
27+
22
28
29+--------------------------------------------------------------------------------
23instance Writable () where
30instance Writable () where
24 write _ _ = return ()
31 write _ _ = return ()
25
32
33+
34+--------------------------------------------------------------------------------
26instance Writable [Char] where
35instance Writable [Char] where
27− write = writeFile
36+ write p = writeFile p . itemBody
37+
28
38
39+--------------------------------------------------------------------------------
29instance Writable SB.ByteString where
40instance Writable SB.ByteString where
30− write p = SB.writeFile p
41+ write p = SB.writeFile p . itemBody
31
42
43+
44+--------------------------------------------------------------------------------
32instance Writable LB.ByteString where
45instance Writable LB.ByteString where
33− write p = LB.writeFile p
46+ write p = LB.writeFile p . itemBody
47+
34
48
49+--------------------------------------------------------------------------------
35instance Writable [Word8] where
50instance Writable [Word8] where
36− write p = write p . SB.pack
51+ write p = write p . fmap SB.pack
37
52
38−instance Writable Html where
39− write p html = write p $ renderHtml html
40
53
54+--------------------------------------------------------------------------------
41−instance Writable Identifier where
55+instance Writable Html where
42− write p = write p . show
56+ write p = write p . fmap renderHtml
src/Hakyll/Core/Writable/CopyFile.hs+14−8
@@ -9,8 +9,7 @@ module Hakyll.Core.Writable.CopyFile
9
9
10
10
11--------------------------------------------------------------------------------
11--------------------------------------------------------------------------------
12−import Control.Applicative ((<$>))
13−import Data.Binary (Binary)
12+import Data.Binary (Binary (..))
14import Data.Typeable (Typeable)
13import Data.Typeable (Typeable)
15import System.Directory (copyFile)
14import System.Directory (copyFile)
16
15
@@ -18,20 +17,27 @@ import System.Directory (copyFile)
18--------------------------------------------------------------------------------
17--------------------------------------------------------------------------------
19import Hakyll.Core.Compiler
18import Hakyll.Core.Compiler
20import Hakyll.Core.Identifier
19import Hakyll.Core.Identifier
20+import Hakyll.Core.Item
21import Hakyll.Core.Writable
21import Hakyll.Core.Writable
22
22
23
23
24--------------------------------------------------------------------------------
24--------------------------------------------------------------------------------
25−-- | Newtype construct around 'FilePath' which will copy the file directly
25+-- | This will copy any file directly by using a system call
26−newtype CopyFile = CopyFile {unCopyFile :: FilePath}
26+data CopyFile = CopyFile
27− deriving (Show, Eq, Ord, Binary, Typeable)
27+ deriving (Show, Eq, Ord, Typeable)
28+
29+
30+--------------------------------------------------------------------------------
31+instance Binary CopyFile where
32+ put CopyFile = return ()
33+ get = return CopyFile
28
34
29
35
30--------------------------------------------------------------------------------
36--------------------------------------------------------------------------------
31instance Writable CopyFile where
37instance Writable CopyFile where
32− write dst (CopyFile src) = copyFile src dst
38+ write dst item = copyFile (toFilePath $ itemIdentifier item) dst
33
39
34
40
35--------------------------------------------------------------------------------
41--------------------------------------------------------------------------------
36−copyFileCompiler :: Compiler CopyFile
42+copyFileCompiler :: Compiler (Item CopyFile)
37−copyFileCompiler = CopyFile . toFilePath <$> getIdentifier
43+copyFileCompiler = makeItem CopyFile
src/Hakyll/Web/CompressCss.hs+3−2
@@ -15,13 +15,14 @@ import Data.List (isPrefixOf)
15
15
16--------------------------------------------------------------------------------
16--------------------------------------------------------------------------------
17import Hakyll.Core.Compiler
17import Hakyll.Core.Compiler
18+import Hakyll.Core.Item
18import Hakyll.Core.Util.String
19import Hakyll.Core.Util.String
19
20
20
21
21--------------------------------------------------------------------------------
22--------------------------------------------------------------------------------
22-- | Compiler form of 'compressCss'
23-- | Compiler form of 'compressCss'
23−compressCssCompiler :: Compiler String
24+compressCssCompiler :: Compiler (Item String)
24−compressCssCompiler = compressCss <$> getResourceString
25+compressCssCompiler = fmap compressCss <$> getResourceString
25
26
26
27
27--------------------------------------------------------------------------------
28--------------------------------------------------------------------------------
src/Hakyll/Web/Page.hs+8−17
@@ -49,34 +49,26 @@
49-- contains three metadata fields and a body. The body is given in markdown
49-- contains three metadata fields and a body. The body is given in markdown
50-- format, which can be easily rendered to HTML by Hakyll, using pandoc.
50-- format, which can be easily rendered to HTML by Hakyll, using pandoc.
51module Hakyll.Web.Page
51module Hakyll.Web.Page
52− ( Page
53− , readPageCompiler
54− , pageCompiler
52+ ( pageCompiler
55 , pageCompilerWith
53 , pageCompilerWith
56 , pageCompilerWithPandoc
54 , pageCompilerWithPandoc
57 ) where
55 ) where
58
56
59
57
60--------------------------------------------------------------------------------
58--------------------------------------------------------------------------------
59+import Control.Applicative ((<$>))
61−import Text.Pandoc (Pandoc, ParserState, WriterOptions)
60+import Text.Pandoc (Pandoc, ParserState, WriterOptions)
62
61
63
62
64--------------------------------------------------------------------------------
63--------------------------------------------------------------------------------
65import Hakyll.Core.Compiler
64import Hakyll.Core.Compiler
66−import Hakyll.Web.Page.Internal
65+import Hakyll.Core.Item
67import Hakyll.Web.Pandoc
66import Hakyll.Web.Pandoc
68
67
69
68
70−--------------------------------------------------------------------------------
71−-- | Read a page (do not render it)
72−readPageCompiler :: Compiler Page
73−readPageCompiler = getResourceBody
74−{-# DEPRECATED readPageCompiler "Use getResourceBody" #-}
75−
76−
77--------------------------------------------------------------------------------
69--------------------------------------------------------------------------------
78-- | Read a page render using pandoc
70-- | Read a page render using pandoc
79−pageCompiler :: Compiler Page
71+pageCompiler :: Compiler (Item String)
80pageCompiler =
72pageCompiler =
81 pageCompilerWith defaultHakyllParserState defaultHakyllWriterOptions
73 pageCompilerWith defaultHakyllParserState defaultHakyllWriterOptions
82
74
@@ -84,7 +76,7 @@ pageCompiler =
84--------------------------------------------------------------------------------
76--------------------------------------------------------------------------------
85-- | A version of 'pageCompiler' which allows you to specify your own pandoc
77-- | A version of 'pageCompiler' which allows you to specify your own pandoc
86-- options
78-- options
87−pageCompilerWith :: ParserState -> WriterOptions -> Compiler Page
79+pageCompilerWith :: ParserState -> WriterOptions -> Compiler (Item String)
88pageCompilerWith state options = pageCompilerWithPandoc state options id
80pageCompilerWith state options = pageCompilerWithPandoc state options id
89
81
90
82
@@ -93,9 +85,8 @@ pageCompilerWith state options = pageCompilerWithPandoc state options id
93-- pandoc transformation for the content
85-- pandoc transformation for the content
94pageCompilerWithPandoc :: ParserState -> WriterOptions
86pageCompilerWithPandoc :: ParserState -> WriterOptions
95 -> (Pandoc -> Pandoc)
87 -> (Pandoc -> Pandoc)
96− -> Compiler Page
88+ -> Compiler (Item String)
97pageCompilerWithPandoc state options f = cached cacheName $
89pageCompilerWithPandoc state options f = cached cacheName $
98− readPageCompiler >>= pageReadPandocWith state >>=
99− return . writePandocWith options . f
90+ writePandocWith options . fmap f . readPandocWith state <$> getResourceBody
100 where
91 where
101 cacheName = "Hakyll.Web.Page.pageCompilerWithPandoc"
92 cacheName = "Hakyll.Web.Page.pageCompilerWithPandoc"
src/Hakyll/Web/Page/Internal.hsdeleted−8
@@ -1,8 +0,0 @@
1−--------------------------------------------------------------------------------
2−module Hakyll.Web.Page.Internal
3− ( Page
4− ) where
5−
6−
7−--------------------------------------------------------------------------------
8−type Page = String
src/Hakyll/Web/Pandoc.hs+30−57
@@ -6,12 +6,8 @@ module Hakyll.Web.Pandoc
6 , readPandocWith
6 , readPandocWith
7 , writePandoc
7 , writePandoc
8 , writePandocWith
8 , writePandocWith
9−
9+ , renderPandoc
10− -- * Functions working on pages/compilers
10+ , renderPandocWith
11− , pageReadPandoc
12− , pageReadPandocWith
13− , pageRenderPandoc
14− , pageRenderPandocWith
15
11
16 -- * Default options
12 -- * Default options
17 , defaultHakyllParserState
13 , defaultHakyllParserState
@@ -20,89 +16,66 @@ module Hakyll.Web.Pandoc
20
16
21
17
22--------------------------------------------------------------------------------
18--------------------------------------------------------------------------------
23−import Control.Applicative ((<$>))
24−import Data.Maybe (fromMaybe)
25import Text.Pandoc
19import Text.Pandoc
26
20
27
21
28--------------------------------------------------------------------------------
22--------------------------------------------------------------------------------
29−import Hakyll.Core.Compiler
23+import Hakyll.Core.Item
30−import Hakyll.Core.Identifier
31−import Hakyll.Web.Page.Internal
32import Hakyll.Web.Pandoc.FileType
24import Hakyll.Web.Pandoc.FileType
33
25
34
26
35--------------------------------------------------------------------------------
27--------------------------------------------------------------------------------
36-- | Read a string using pandoc, with the default options
28-- | Read a string using pandoc, with the default options
37−readPandoc :: FileType -- ^ Determines how parsing happens
38− -> Maybe Identifier -- ^ Optional, for better error messages
39− -> Page -- ^ String to read
29+readPandoc :: Item String -- ^ String to read
40− -> Pandoc -- ^ Resulting document
30+ -> Item Pandoc -- ^ Resulting document
41readPandoc = readPandocWith defaultHakyllParserState
31readPandoc = readPandocWith defaultHakyllParserState
42
32
43
33
44--------------------------------------------------------------------------------
34--------------------------------------------------------------------------------
45-- | Read a string using pandoc, with the supplied options
35-- | Read a string using pandoc, with the supplied options
46−readPandocWith :: ParserState -- ^ Parser options
36+readPandocWith :: ParserState -- ^ Parser options
47− -> FileType -- ^ Determines parsing method
48− -> Maybe Identifier -- ^ Optional, for better error messages
49− -> Page -- ^ String to read
37+ -> Item String -- ^ String to read
50− -> Pandoc -- ^ Resulting document
38+ -> Item Pandoc -- ^ Resulting document
51−readPandocWith state fileType' id' = case fileType' of
39+readPandocWith state item = fmap (reader state (itemFileType item)) item
40+ where
41+ reader s t = case t of
52− Html -> readHtml state
42+ Html -> readHtml s
53− LaTeX -> readLaTeX state
43+ LaTeX -> readLaTeX s
54− LiterateHaskell t ->
55− readPandocWith state {stateLiterateHaskell = True} t id'
44+ LiterateHaskell t' -> reader s {stateLiterateHaskell = True} t'
56− Markdown -> readMarkdown state
45+ Markdown -> readMarkdown s
57− Rst -> readRST state
46+ Rst -> readRST s
58− Textile -> readTextile state
47+ Textile -> readTextile s
59− t -> error $
48+ _ -> error $
60− "Hakyll.Web.readPandocWith: I don't know how to read a file of the " ++
49+ "Hakyll.Web.readPandocWith: I don't know how to read a file of the " ++
61− "type " ++ show t ++ fromMaybe "" (fmap ((" for: " ++) . show) id')
50+ "type " ++ show t ++ " for: " ++ show (itemIdentifier item)
62
51
63
52
64--------------------------------------------------------------------------------
53--------------------------------------------------------------------------------
65-- | Write a document (as HTML) using pandoc, with the default options
54-- | Write a document (as HTML) using pandoc, with the default options
66−writePandoc :: Pandoc -- ^ Document to write
55+writePandoc :: Item Pandoc -- ^ Document to write
67− -> Page -- ^ Resulting HTML
56+ -> Item String -- ^ Resulting HTML
68writePandoc = writePandocWith defaultHakyllWriterOptions
57writePandoc = writePandocWith defaultHakyllWriterOptions
69
58
70
59
71--------------------------------------------------------------------------------
60--------------------------------------------------------------------------------
72-- | Write a document (as HTML) using pandoc, with the supplied options
61-- | Write a document (as HTML) using pandoc, with the supplied options
73writePandocWith :: WriterOptions -- ^ Writer options for pandoc
62writePandocWith :: WriterOptions -- ^ Writer options for pandoc
74− -> Pandoc -- ^ Document to write
63+ -> Item Pandoc -- ^ Document to write
75− -> Page -- ^ Resulting HTML
64+ -> Item String -- ^ Resulting HTML
76−writePandocWith = writeHtmlString
65+writePandocWith options = fmap $ writeHtmlString options
77−
78−
79−--------------------------------------------------------------------------------
80−-- | Read the resource using pandoc
81−pageReadPandoc :: Page -> Compiler Pandoc
82−pageReadPandoc = pageReadPandocWith defaultHakyllParserState
83−
84−
85−--------------------------------------------------------------------------------
86−-- | Read the resource using pandoc
87−pageReadPandocWith :: ParserState -> Page -> Compiler Pandoc
88−pageReadPandocWith state page = do
89− identifier <- getIdentifier
90− fileType' <- getFileType
91− return $ readPandocWith state fileType' (Just identifier) page
92
66
93
67
94--------------------------------------------------------------------------------
68--------------------------------------------------------------------------------
95-- | Render the resource using pandoc
69-- | Render the resource using pandoc
96−pageRenderPandoc :: Page -> Compiler Page
70+renderPandoc :: Item String -> Item String
97−pageRenderPandoc =
71+renderPandoc =
98− pageRenderPandocWith defaultHakyllParserState defaultHakyllWriterOptions
72+ renderPandocWith defaultHakyllParserState defaultHakyllWriterOptions
99
73
100
74
101--------------------------------------------------------------------------------
75--------------------------------------------------------------------------------
102-- | Render the resource using pandoc
76-- | Render the resource using pandoc
103−pageRenderPandocWith :: ParserState -> WriterOptions -> Page -> Compiler Page
77+renderPandocWith :: ParserState -> WriterOptions -> Item String -> Item String
104−pageRenderPandocWith state options page =
105− writePandocWith options <$> pageReadPandocWith state page
78+renderPandocWith state options = writePandocWith options . readPandocWith state
106
79
107
80
108--------------------------------------------------------------------------------
81--------------------------------------------------------------------------------
src/Hakyll/Web/Pandoc/Biblio.hs+37−20
@@ -15,7 +15,7 @@ module Hakyll.Web.Pandoc.Biblio
15 , cslCompiler
15 , cslCompiler
16 , Biblio (..)
16 , Biblio (..)
17 , biblioCompiler
17 , biblioCompiler
18− , pageReadPandocBiblio
18+ , readPandocBiblio
19 ) where
19 ) where
20
20
21
21
@@ -31,19 +31,31 @@ import Text.Pandoc.Biblio (processBiblio)
31--------------------------------------------------------------------------------
31--------------------------------------------------------------------------------
32import Hakyll.Core.Compiler
32import Hakyll.Core.Compiler
33import Hakyll.Core.Identifier
33import Hakyll.Core.Identifier
34+import Hakyll.Core.Item
34import Hakyll.Core.Writable
35import Hakyll.Core.Writable
35−import Hakyll.Web.Page
36import Hakyll.Web.Pandoc
36import Hakyll.Web.Pandoc
37
37
38
38
39--------------------------------------------------------------------------------
39--------------------------------------------------------------------------------
40−newtype CSL = CSL FilePath
40+data CSL = CSL
41− deriving (Binary, Show, Typeable, Writable)
41+ deriving (Show, Typeable)
42+
43+
44+--------------------------------------------------------------------------------
45+instance Binary CSL where
46+ put CSL = return ()
47+ get = return CSL
48+
49+
50+--------------------------------------------------------------------------------
51+instance Writable CSL where
52+ -- Shouldn't be written.
53+ write _ _ = return ()
42
54
43
55
44--------------------------------------------------------------------------------
56--------------------------------------------------------------------------------
45−cslCompiler :: Compiler CSL
57+cslCompiler :: Compiler (Item CSL)
46−cslCompiler = CSL . toFilePath <$> getIdentifier
58+cslCompiler = makeItem CSL
47
59
48
60
49--------------------------------------------------------------------------------
61--------------------------------------------------------------------------------
@@ -57,29 +69,34 @@ instance Binary Biblio where
57 get = Biblio . read <$> get
69 get = Biblio . read <$> get
58 put (Biblio rs) = put $ show rs
70 put (Biblio rs) = put $ show rs
59
71
72+
73+--------------------------------------------------------------------------------
60instance Writable Biblio where
74instance Writable Biblio where
75+ -- Shouldn't be written.
61 write _ _ = return ()
76 write _ _ = return ()
62
77
63
78
64--------------------------------------------------------------------------------
79--------------------------------------------------------------------------------
65−biblioCompiler :: Compiler Biblio
80+biblioCompiler :: Compiler (Item Biblio)
66biblioCompiler = do
81biblioCompiler = do
67− filePath <- toFilePath <$> getIdentifier
82+ filePath <- toFilePath <$> getUnderlying
68− unsafeCompiler $ Biblio <$> CSL.readBiblioFile filePath
83+ makeItem =<< unsafeCompiler (Biblio <$> CSL.readBiblioFile filePath)
69
84
70
85
71--------------------------------------------------------------------------------
86--------------------------------------------------------------------------------
72−pageReadPandocBiblio :: ParserState
87+readPandocBiblio :: ParserState
73− -> CSL
88+ -> Item CSL
74− -> Biblio
89+ -> Item Biblio
75− -> Page
90+ -> (Item String)
76− -> Compiler Pandoc
91+ -> Compiler (Item Pandoc)
77−pageReadPandocBiblio state (CSL csl) (Biblio refs) page = do
92+readPandocBiblio state csl biblio item = do
78 -- We need to know the citation keys, add then *before* actually parsing the
93 -- We need to know the citation keys, add then *before* actually parsing the
79 -- actual page. If we don't do this, pandoc won't even consider them
94 -- actual page. If we don't do this, pandoc won't even consider them
80 -- citations!
95 -- citations!
96+ let Biblio refs = itemBody biblio
81− let cits = map CSL.refId refs
97+ cits = map CSL.refId refs
82− state' = state {stateCitations = stateCitations state ++ cits}
98+ state' = state {stateCitations = stateCitations state ++ cits}
83− pandoc <- pageReadPandocWith state' page
99+ pandoc = itemBody $ readPandocWith state' item
100+ cslPath = toFilePath $ itemIdentifier csl
84− pandoc' <- unsafeCompiler $ processBiblio csl Nothing refs pandoc
101+ pandoc' <- unsafeCompiler $ processBiblio cslPath Nothing refs pandoc
85− return pandoc'
102+ return $ fmap (const pandoc') item
src/Hakyll/Web/Pandoc/FileType.hs+4−5
@@ -3,18 +3,17 @@
3module Hakyll.Web.Pandoc.FileType
3module Hakyll.Web.Pandoc.FileType
4 ( FileType (..)
4 ( FileType (..)
5 , fileType
5 , fileType
6− , getFileType
6+ , itemFileType
7 ) where
7 ) where
8
8
9
9
10--------------------------------------------------------------------------------
10--------------------------------------------------------------------------------
11−import Control.Applicative ((<$>))
12import System.FilePath (takeExtension)
11import System.FilePath (takeExtension)
13
12
14
13
15--------------------------------------------------------------------------------
14--------------------------------------------------------------------------------
16−import Hakyll.Core.Compiler
17import Hakyll.Core.Identifier
15import Hakyll.Core.Identifier
16+import Hakyll.Core.Item
18
17
19
18
20--------------------------------------------------------------------------------
19--------------------------------------------------------------------------------
@@ -62,5 +61,5 @@ fileType = fileType' . takeExtension
62
61
63--------------------------------------------------------------------------------
62--------------------------------------------------------------------------------
64-- | Get the file type for the current file
63-- | Get the file type for the current file
65−getFileType :: Compiler FileType
64+itemFileType :: Item a -> FileType
66−getFileType = fileType . toFilePath <$> getIdentifier
65+itemFileType = fileType . toFilePath . itemIdentifier
src/Hakyll/Web/Template.hs+28−28
@@ -60,10 +60,10 @@
60-- > #{body}
60-- > #{body}
61module Hakyll.Web.Template
61module Hakyll.Web.Template
62 ( Template
62 ( Template
63− , applyTemplate
64 , templateCompiler
63 , templateCompiler
65 , templateCompilerWith
64 , templateCompilerWith
66− , applyTemplateCompiler
65+ , applyTemplate
66+ , applyTemplateWith
67 ) where
67 ) where
68
68
69
69
@@ -78,51 +78,51 @@ import Text.Hamlet (HamletSettings,
78--------------------------------------------------------------------------------
78--------------------------------------------------------------------------------
79import Hakyll.Core.Compiler
79import Hakyll.Core.Compiler
80import Hakyll.Core.Identifier
80import Hakyll.Core.Identifier
81−import Hakyll.Web.Page.Internal
81+import Hakyll.Core.Item
82import Hakyll.Web.Template.Context
82import Hakyll.Web.Template.Context
83import Hakyll.Web.Template.Internal
83import Hakyll.Web.Template.Internal
84import Hakyll.Web.Template.Read
84import Hakyll.Web.Template.Read
85
85
86
86
87−--------------------------------------------------------------------------------
88−applyTemplate :: Monad m
89− => (String -> a -> m String)
90− -> Template -> a -> m String
91−applyTemplate context tpl x = liftM concat $
92− forM (unTemplate tpl) $ \e -> case e of
93− Chunk c -> return c
94− Escaped -> return "$"
95− Key k -> context k x
96−
97−
98--------------------------------------------------------------------------------
87--------------------------------------------------------------------------------
99-- | Read a template. If the extension of the file we're compiling is
88-- | Read a template. If the extension of the file we're compiling is
100-- @.hml@ or @.hamlet@, it will be considered as a Hamlet template, and parsed
89-- @.hml@ or @.hamlet@, it will be considered as a Hamlet template, and parsed
101-- as such.
90-- as such.
102−templateCompiler :: Compiler Template
91+templateCompiler :: Compiler (Item Template)
103templateCompiler = templateCompilerWith defaultHamletSettings
92templateCompiler = templateCompilerWith defaultHamletSettings
104
93
105
94
106--------------------------------------------------------------------------------
95--------------------------------------------------------------------------------
107-- | Version of 'templateCompiler' that enables custom settings.
96-- | Version of 'templateCompiler' that enables custom settings.
108−templateCompilerWith :: HamletSettings -> Compiler Template
97+templateCompilerWith :: HamletSettings -> Compiler (Item Template)
109templateCompilerWith settings =
98templateCompilerWith settings =
110 cached "Hakyll.Web.Template.templateCompilerWith" $ do
99 cached "Hakyll.Web.Template.templateCompilerWith" $ do
111− identifier <- getIdentifier
100+ identifier <- getUnderlying
112− string <- getResourceString
101+ item <- getResourceString
113 if takeExtension (toFilePath identifier) `elem` [".hml", ".hamlet"]
102 if takeExtension (toFilePath identifier) `elem` [".hml", ".hamlet"]
114 -- Hamlet template
103 -- Hamlet template
115− then return $ readHamletTemplateWith settings string
104+ then return $ fmap (readHamletTemplateWith settings) item
116 -- Hakyll template
105 -- Hakyll template
117− else return $ readTemplate string
106+ else return $ fmap readTemplate item
107+
108+
109+--------------------------------------------------------------------------------
110+applyTemplate :: Template -- ^ Template
111+ -> Context a -- ^ Context
112+ -> Item a -- ^ Page
113+ -> Compiler (Item String) -- ^ Resulting item
114+applyTemplate tpl context item = do
115+ let context' k x = unContext context k x
116+ body <- applyTemplateWith context' tpl item
117+ return $ itemSetBody body item
118
118
119
119
120--------------------------------------------------------------------------------
120--------------------------------------------------------------------------------
121−applyTemplateCompiler :: Template -- ^ Template
121+applyTemplateWith :: Monad m
122− -> Context Page -- ^ Context
122+ => (String -> a -> m String)
123− -> Page -- ^ Page
123+ -> Template -> a -> m String
124− -> Compiler Page -- ^ Compiler
124+applyTemplateWith context tpl x = liftM concat $
125−applyTemplateCompiler tpl context page = do
125+ forM (unTemplate tpl) $ \e -> case e of
126− identifier <- getIdentifier
126+ Chunk c -> return c
127− let context' k x = unContext context k identifier x
127+ Escaped -> return "$"
128− applyTemplate context' tpl page
128+ Key k -> context k x
src/Hakyll/Web/Template/Context.hs+20−19
@@ -34,36 +34,36 @@ import System.Locale (TimeLocale, defaultTimeLocale)
34import Hakyll.Core.Compiler
34import Hakyll.Core.Compiler
35import Hakyll.Core.Compiler.Internal
35import Hakyll.Core.Compiler.Internal
36import Hakyll.Core.Identifier
36import Hakyll.Core.Identifier
37−import Hakyll.Core.ResourceProvider
37+import Hakyll.Core.Item
38+import Hakyll.Core.Provider
38import Hakyll.Core.Util.String (splitAll)
39import Hakyll.Core.Util.String (splitAll)
39−import Hakyll.Web.Page.Internal
40import Hakyll.Web.Urls
40import Hakyll.Web.Urls
41
41
42
42
43--------------------------------------------------------------------------------
43--------------------------------------------------------------------------------
44newtype Context a = Context
44newtype Context a = Context
45− { unContext :: String -> Identifier -> a -> Compiler String
45+ { unContext :: String -> Item a -> Compiler String
46 }
46 }
47
47
48
48
49--------------------------------------------------------------------------------
49--------------------------------------------------------------------------------
50instance Monoid (Context a) where
50instance Monoid (Context a) where
51− mempty = Context $ \_ _ _ -> empty
51+ mempty = Context $ \_ _ -> empty
52− mappend (Context f) (Context g) = Context $ \k i x -> f k i x <|> g k i x
52+ mappend (Context f) (Context g) = Context $ \k i -> f k i <|> g k i
53
53
54
54
55--------------------------------------------------------------------------------
55--------------------------------------------------------------------------------
56mapContext :: (String -> String) -> Context a -> Context a
56mapContext :: (String -> String) -> Context a -> Context a
57−mapContext f (Context g) = Context $ \k i x -> f <$> g k i x
57+mapContext f (Context g) = Context $ \k i -> f <$> g k i
58
58
59
59
60--------------------------------------------------------------------------------
60--------------------------------------------------------------------------------
61−field :: String -> (Identifier -> a -> Compiler String) -> Context a
61+field :: String -> (Item a -> Compiler String) -> Context a
62−field key value = Context $ \k i x -> if k == key then value i x else empty
62+field key value = Context $ \k i -> if k == key then value i else empty
63
63
64
64
65--------------------------------------------------------------------------------
65--------------------------------------------------------------------------------
66−defaultContext :: Context Page
66+defaultContext :: Context String
67defaultContext =
67defaultContext =
68 bodyField "body" `mappend`
68 bodyField "body" `mappend`
69 urlField "url" `mappend`
69 urlField "url" `mappend`
@@ -74,18 +74,19 @@ defaultContext =
74
74
75
75
76--------------------------------------------------------------------------------
76--------------------------------------------------------------------------------
77−bodyField :: String -> Context Page
77+bodyField :: String -> Context String
78−bodyField key = field key $ \_ x -> return x
78+bodyField key = field key $ return . itemBody
79
79
80
80
81--------------------------------------------------------------------------------
81--------------------------------------------------------------------------------
82urlField :: String -> Context a
82urlField :: String -> Context a
83−urlField key = field key $ \i _ -> maybe empty toUrl <$> getRouteFor i
83+urlField key = field key $
84+ fmap (maybe empty toUrl) . getRoute . itemIdentifier
84
85
85
86
86--------------------------------------------------------------------------------
87--------------------------------------------------------------------------------
87pathField :: String -> Context a
88pathField :: String -> Context a
88−pathField key = field key $ \i _ -> return $ toFilePath i
89+pathField key = field key $ return . toFilePath . itemIdentifier
89
90
90
91
91--------------------------------------------------------------------------------
92--------------------------------------------------------------------------------
@@ -133,8 +134,8 @@ dateFieldWith :: TimeLocale -- ^ Output time locale
133 -> String -- ^ Destination key
134 -> String -- ^ Destination key
134 -> String -- ^ Format to use on the date
135 -> String -- ^ Format to use on the date
135 -> Context a -- ^ Resulting context
136 -> Context a -- ^ Resulting context
136−dateFieldWith locale key format = field key $ \id' _ -> do
137+dateFieldWith locale key format = field key $ \i -> do
137− time <- getUTC locale id'
138+ time <- getUTC locale $ itemIdentifier i
138 return $ formatTime locale format time
139 return $ formatTime locale format time
139
140
140
141
@@ -145,7 +146,7 @@ getUTC :: TimeLocale -- ^ Output time locale
145 -> Identifier -- ^ Input page
146 -> Identifier -- ^ Input page
146 -> Compiler UTCTime -- ^ Parsed UTCTime
147 -> Compiler UTCTime -- ^ Parsed UTCTime
147getUTC locale id' = do
148getUTC locale id' = do
148− metadata <- getMetadataFor id'
149+ metadata <- getMetadata id'
149 let tryField k fmt = M.lookup k metadata >>= parseTime' fmt
150 let tryField k fmt = M.lookup k metadata >>= parseTime' fmt
150 fn = takeFileName $ toFilePath id'
151 fn = takeFileName $ toFilePath id'
151
152
@@ -177,11 +178,11 @@ modificationTimeFieldWith :: TimeLocale -- ^ Time output locale
177 -> String -- ^ Key
178 -> String -- ^ Key
178 -> String -- ^ Format
179 -> String -- ^ Format
179 -> Context a -- ^ Resulting context
180 -> Context a -- ^ Resulting context
180−modificationTimeFieldWith locale key fmt = field key $ \id' _ -> do
181+modificationTimeFieldWith locale key fmt = field key $ \i -> do
181− mtime <- compilerUnsafeIO $ resourceModificationTime id'
182+ mtime <- compilerUnsafeIO $ resourceModificationTime $ itemIdentifier i
182 return $ formatTime locale fmt mtime
183 return $ formatTime locale fmt mtime
183
184
184
185
185--------------------------------------------------------------------------------
186--------------------------------------------------------------------------------
186missingField :: Context a
187missingField :: Context a
187−missingField = Context $ \k _ _ -> return $ "$" ++ k ++ "$"
188+missingField = Context $ \k _ -> return $ "$" ++ k ++ "$"
src/Hakyll/Web/Urls/Relativize.hs+12−12
@@ -15,8 +15,8 @@
15--
15--
16-- > <img src="../images/lolcat.png" alt="Funny zomgroflcopter" />
16-- > <img src="../images/lolcat.png" alt="Funny zomgroflcopter" />
17module Hakyll.Web.Urls.Relativize
17module Hakyll.Web.Urls.Relativize
18− ( relativizeUrlsCompiler
18+ ( relativizeUrls
19− , relativizeUrls
19+ , relativizeUrlsWith
20 ) where
20 ) where
21
21
22
22
@@ -26,27 +26,27 @@ import Data.List (isPrefixOf)
26
26
27--------------------------------------------------------------------------------
27--------------------------------------------------------------------------------
28import Hakyll.Core.Compiler
28import Hakyll.Core.Compiler
29−import Hakyll.Web.Page
29+import Hakyll.Core.Item
30import Hakyll.Web.Urls
30import Hakyll.Web.Urls
31
31
32
32
33--------------------------------------------------------------------------------
33--------------------------------------------------------------------------------
34-- | Compiler form of 'relativizeUrls' which automatically picks the right root
34-- | Compiler form of 'relativizeUrls' which automatically picks the right root
35-- path
35-- path
36−relativizeUrlsCompiler :: Page -> Compiler Page
36+relativizeUrls :: Item String -> Compiler (Item String)
37−relativizeUrlsCompiler page = do
37+relativizeUrls item = do
38− route <- getRoute
38+ route <- getRoute $ itemIdentifier item
39 return $ case route of
39 return $ case route of
40− Nothing -> page
40+ Nothing -> item
41− Just r -> relativizeUrls (toSiteRoot r) page
41+ Just r -> fmap (relativizeUrlsWith $ toSiteRoot r) item
42
42
43
43
44--------------------------------------------------------------------------------
44--------------------------------------------------------------------------------
45-- | Relativize URL's in HTML
45-- | Relativize URL's in HTML
46−relativizeUrls :: String -- ^ Path to the site root
46+relativizeUrlsWith :: String -- ^ Path to the site root
47− -> Page -- ^ HTML to relativize
47+ -> String -- ^ HTML to relativize
48− -> Page -- ^ Resulting HTML
48+ -> String -- ^ Resulting HTML
49−relativizeUrls root = withUrls rel
49+relativizeUrlsWith root = withUrls rel
50 where
50 where
51 isRel x = "/" `isPrefixOf` x && not ("//" `isPrefixOf` x)
51 isRel x = "/" `isPrefixOf` x && not ("//" `isPrefixOf` x)
52 rel x = if isRel x then root ++ x else x
52 rel x = if isRel x then root ++ x else x