on master help sign in
Add Item abstraction
commit877cb21d1630d32c6e40eb7c6f0ecc7e1da2bd52 authorJasper Van der Jeugt <m@jaspervdj.be> dateSun 18 Nov 2012, 21:56 (14 years ago) parent1347b0f
26 files changed+393−391
hakyll.cabal+7−7 src/Hakyll.hs+2−2 src/Hakyll/Core/CompiledItem.hs−55 src/Hakyll/Core/Compiler.hs+23−28 src/Hakyll/Core/Compiler/Internal.hs+4−4 src/Hakyll/Core/Item.hs+41 src/Hakyll/Core/Item/SomeItem.hs+23 src/Hakyll/Core/ResourceProvider.hs → src/Hakyll/Core/Provider.hs+8−8 src/Hakyll/Core/ResourceProvider/Internal.hs → src/Hakyll/Core/Provider/Internal.hs+17−17 src/Hakyll/Core/ResourceProvider/Metadata.hs → src/Hakyll/Core/Provider/Metadata.hs+10−10 src/Hakyll/Core/ResourceProvider/MetadataCache.hs → src/Hakyll/Core/Provider/MetadataCache.hs+20−20 src/Hakyll/Core/ResourceProvider/Modified.hs → src/Hakyll/Core/Provider/Modified.hs+16−16 src/Hakyll/Core/Rules.hs+10−9 src/Hakyll/Core/Rules/Internal.hs+10−10 src/Hakyll/Core/Runtime.hs+14−11 src/Hakyll/Core/Writable.hs+32−18 src/Hakyll/Core/Writable/CopyFile.hs+14−8 src/Hakyll/Web/CompressCss.hs+3−2 src/Hakyll/Web/Page.hs+8−17 src/Hakyll/Web/Page/Internal.hs−8 src/Hakyll/Web/Pandoc.hs+30−57 src/Hakyll/Web/Pandoc/Biblio.hs+37−20 src/Hakyll/Web/Pandoc/FileType.hs+4−5 src/Hakyll/Web/Template.hs+28−28 src/Hakyll/Web/Template/Context.hs+20−19 src/Hakyll/Web/Urls/Relativize.hs+12−12
hakyll.cabal+7−7
@@ -96,9 +96,9 @@ Library
9696 Hakyll.Core.Dependencies
9797 Hakyll.Core.Identifier
9898 Hakyll.Core.Identifier.Pattern
99+ Hakyll.Core.Item
99100 Hakyll.Core.Logger
100101 Hakyll.Core.Metadata
101− Hakyll.Core.ResourceProvider
102102 Hakyll.Core.Routes
103103 Hakyll.Core.Rules
104104 Hakyll.Core.Runtime
@@ -126,17 +126,17 @@ Library
126126 Hakyll.Web.Util.Html
127127
128128 Other-Modules:
129− Hakyll.Core.CompiledItem
130129 Hakyll.Core.Compiler.Internal
131130 Hakyll.Core.Compiler.Require
132131 Hakyll.Core.DirectedGraph
133132 Hakyll.Core.DirectedGraph.Dot
134− Hakyll.Core.ResourceProvider.Internal
135− Hakyll.Core.ResourceProvider.Metadata
136− Hakyll.Core.ResourceProvider.MetadataCache
137− Hakyll.Core.ResourceProvider.Modified
133+ Hakyll.Core.Item.SomeItem
134+ Hakyll.Core.Provider
135+ Hakyll.Core.Provider.Internal
136+ Hakyll.Core.Provider.Metadata
137+ Hakyll.Core.Provider.MetadataCache
138+ Hakyll.Core.Provider.Modified
138139 Hakyll.Core.Rules.Internal
139− Hakyll.Web.Page.Internal
140140 Hakyll.Web.Template.Internal
141141 Hakyll.Web.Template.Read.Hakyll
142142 Hakyll.Web.Template.Read.Hamlet
src/Hakyll.hs+2−2
@@ -6,8 +6,8 @@ module Hakyll
66 , module Hakyll.Core.Configuration
77 , module Hakyll.Core.Identifier
88 , module Hakyll.Core.Identifier.Pattern
9+ , module Hakyll.Core.Item
910 , module Hakyll.Core.Metadata
10− , module Hakyll.Core.ResourceProvider
1111 , module Hakyll.Core.Routes
1212 , module Hakyll.Core.Rules
1313#ifdef UNIX_FILTER
@@ -39,8 +39,8 @@ import Hakyll.Core.Compiler
3939import Hakyll.Core.Configuration
4040import Hakyll.Core.Identifier
4141import Hakyll.Core.Identifier.Pattern
42+import Hakyll.Core.Item
4243import Hakyll.Core.Metadata
43−import Hakyll.Core.ResourceProvider
4444import Hakyll.Core.Routes
4545import Hakyll.Core.Rules
4646#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 @@
33{-# LANGUAGE ScopedTypeVariables #-}
44module Hakyll.Core.Compiler
55 ( Compiler
6− , getIdentifier
6+ , getUnderlying
7+ , makeItem
78 , getRoute
8− , getRouteFor
99 , getMetadata
10− , getMetadataFor
1110 , getResourceBody
1211 , getResourceString
1312 , getResourceLBS
@@ -34,42 +33,39 @@ import Hakyll.Core.Compiler.Internal
3433import Hakyll.Core.Compiler.Require
3534import Hakyll.Core.Dependencies
3635import Hakyll.Core.Identifier
36+import Hakyll.Core.Item
3737import Hakyll.Core.Logger as Logger
3838import Hakyll.Core.Metadata
39−import Hakyll.Core.ResourceProvider
39+import Hakyll.Core.Provider
4040import Hakyll.Core.Routes
4141import qualified Hakyll.Core.Store as Store
42−import Hakyll.Core.Writable
4342
4443
4544--------------------------------------------------------------------------------
46−-- | Get the identifier of the item that is currently being compiled
47−getIdentifier :: Compiler Identifier
48−getIdentifier = compilerIdentifier <$> compilerAsk
45+-- | Get the underlying identifier. Only use this if you know what you're doing.
46+getUnderlying :: Compiler Identifier
47+getUnderlying = compilerUnderlying <$> compilerAsk
4948
5049
5150--------------------------------------------------------------------------------
52−-- | Get the route we are using for this item
53−getRoute :: Compiler (Maybe FilePath)
54−getRoute = getIdentifier >>= getRouteFor
51+makeItem :: a -> Compiler (Item a)
52+makeItem x = do
53+ identifier <- getUnderlying
54+ return $ Item identifier x
5555
5656
5757--------------------------------------------------------------------------------
5858-- | Get the route for a specified item
59−getRouteFor :: Identifier -> Compiler (Maybe FilePath)
60−getRouteFor identifier = do
59+getRoute :: Identifier -> Compiler (Maybe FilePath)
60+getRoute identifier = do
6161 routes <- compilerRoutes <$> compilerAsk
6262 return $ runRoutes routes identifier
6363
6464
65−--------------------------------------------------------------------------------
66−getMetadata :: Compiler Metadata
67−getMetadata = getIdentifier >>= getMetadataFor
68−
6965
7066--------------------------------------------------------------------------------
71−getMetadataFor :: Identifier -> Compiler Metadata
72−getMetadataFor identifier = do
67+getMetadata :: Identifier -> Compiler Metadata
68+getMetadata identifier = do
7369 provider <- compilerProvider <$> compilerAsk
7470 compilerTellDependencies [IdentifierDependency identifier]
7571 compilerUnsafeIO $ resourceMetadata provider identifier
@@ -77,32 +73,31 @@ getMetadataFor identifier = do
7773
7874--------------------------------------------------------------------------------
7975-- | Get the body of the underlying resource
80−getResourceBody :: Compiler String
76+getResourceBody :: Compiler (Item String)
8177getResourceBody = getResourceWith resourceBody
8278
8379
8480--------------------------------------------------------------------------------
8581-- | Get the resource we are compiling as a string
86−getResourceString :: Compiler String
82+getResourceString :: Compiler (Item String)
8783getResourceString = getResourceWith $ const resourceString
8884
8985
9086--------------------------------------------------------------------------------
9187-- | Get the resource we are compiling as a lazy bytestring
92−--
93−getResourceLBS :: Compiler ByteString
88+getResourceLBS :: Compiler (Item ByteString)
9489getResourceLBS = getResourceWith $ const resourceLBS
9590
9691
9792--------------------------------------------------------------------------------
9893-- | Overloadable function for 'getResourceString' and 'getResourceLBS'
99−getResourceWith :: (ResourceProvider -> Identifier -> IO a) -> Compiler a
94+getResourceWith :: (Provider -> Identifier -> IO a) -> Compiler (Item a)
10095getResourceWith reader = do
10196 provider <- compilerProvider <$> compilerAsk
102− id' <- compilerIdentifier <$> compilerAsk
97+ id' <- compilerUnderlying <$> compilerAsk
10398 let filePath = toFilePath id'
10499 if resourceExists provider id'
105− then compilerUnsafeIO $ reader provider id'
100+ then compilerUnsafeIO $ Item id' <$> reader provider id'
106101 else compilerThrow $ error' filePath
107102 where
108103 error' fp = "Hakyll.Core.Compiler.getResourceWith: resource " ++
@@ -110,12 +105,12 @@ getResourceWith reader = do
110105
111106
112107--------------------------------------------------------------------------------
113−cached :: (Binary a, Typeable a, Writable a)
108+cached :: (Binary a, Typeable a)
114109 => String
115110 -> Compiler a
116111 -> Compiler a
117112cached name compiler = do
118− id' <- compilerIdentifier <$> compilerAsk
113+ id' <- compilerUnderlying <$> compilerAsk
119114 store <- compilerStore <$> compilerAsk
120115 provider <- compilerProvider <$> compilerAsk
121116 modified <- compilerUnsafeIO $ resourceModified provider id'
src/Hakyll/Core/Compiler/Internal.hs+4−4
@@ -35,7 +35,7 @@ import Data.Monoid (Monoid (..))
3535import Hakyll.Core.Dependencies
3636import Hakyll.Core.Identifier
3737import Hakyll.Core.Logger
38−import Hakyll.Core.ResourceProvider
38+import Hakyll.Core.Provider
3939import Hakyll.Core.Routes
4040import Hakyll.Core.Store
4141
@@ -43,10 +43,10 @@ import Hakyll.Core.Store
4343--------------------------------------------------------------------------------
4444-- | Environment in which a compiler runs
4545data CompilerRead = CompilerRead
46− { -- | Target identifier
47− compilerIdentifier :: Identifier
46+ { -- | Underlying identifier
47+ compilerUnderlying :: Identifier
4848 , -- | Resource provider
49− compilerProvider :: ResourceProvider
49+ compilerProvider :: Provider
5050 , -- | List of all known identifiers
5151 compilerUniverse :: [Identifier]
5252 , -- | 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 @@
11--------------------------------------------------------------------------------
22-- | This module provides an wrapper API around the file system which does some
33-- caching.
4−module Hakyll.Core.ResourceProvider
4+module Hakyll.Core.Provider
55 ( -- * Constructing resource providers
6− ResourceProvider
7− , newResourceProvider
6+ Provider
7+ , newProvider
88
99 -- * Querying resource properties
1010 , resourceList
@@ -25,14 +25,14 @@ module Hakyll.Core.ResourceProvider
2525--------------------------------------------------------------------------------
2626import Hakyll.Core.Identifier
2727import Hakyll.Core.Metadata
28−import Hakyll.Core.ResourceProvider.Internal
29−import qualified Hakyll.Core.ResourceProvider.MetadataCache as Internal
30−import Hakyll.Core.ResourceProvider.Modified
28+import Hakyll.Core.Provider.Internal
29+import qualified Hakyll.Core.Provider.MetadataCache as Internal
30+import Hakyll.Core.Provider.Modified
3131
3232
3333--------------------------------------------------------------------------------
3434-- | Wrapper to ensure metadata cache is invalidated if necessary
35−resourceMetadata :: ResourceProvider -> Identifier -> IO Metadata
35+resourceMetadata :: Provider -> Identifier -> IO Metadata
3636resourceMetadata rp r = do
3737 _ <- resourceModified rp r
3838 Internal.resourceMetadata rp r
@@ -40,7 +40,7 @@ resourceMetadata rp r = do
4040
4141--------------------------------------------------------------------------------
4242-- | Wrapper to ensure metadata cache is invalidated if necessary
43−resourceBody :: ResourceProvider -> Identifier -> IO String
43+resourceBody :: Provider -> Identifier -> IO String
4444resourceBody rp r = do
4545 _ <- resourceModified rp r
4646 Internal.resourceBody rp r
src/Hakyll/Core/ResourceProvider/Internal.hs → src/Hakyll/Core/Provider/Internal.hsrenamed+17−17
@@ -1,7 +1,7 @@
11--------------------------------------------------------------------------------
2−module Hakyll.Core.ResourceProvider.Internal
3− ( ResourceProvider (..)
4− , newResourceProvider
2+module Hakyll.Core.Provider.Internal
3+ ( Provider (..)
4+ , newProvider
55
66 , resourceList
77 , resourceExists
@@ -31,39 +31,39 @@ import Hakyll.Core.Identifier
3131
3232--------------------------------------------------------------------------------
3333-- | Responsible for retrieving and listing resources
34−data ResourceProvider = ResourceProvider
34+data Provider = Provider
3535 { -- | A list of all files found
36− resourceSet :: Set Identifier
36+ providerSet :: Set Identifier
3737 , -- | Cache keeping track of modified files
38− resourceModifiedCache :: IORef (Map Identifier Bool)
38+ providerModifiedCache :: IORef (Map Identifier Bool)
3939 , -- | Underlying persistent store for caching
40− resourceStore :: Store
40+ providerStore :: Store
4141 }
4242
4343
4444--------------------------------------------------------------------------------
4545-- | Create a resource provider
46−newResourceProvider :: Store -- ^ Store to use
47− -> (FilePath -> Bool) -- ^ Should we ignore this file?
48− -> FilePath -- ^ Search directory
49− -> IO ResourceProvider -- ^ Resulting provider
50−newResourceProvider store ignore directory = do
46+newProvider :: Store -- ^ Store to use
47+ -> (FilePath -> Bool) -- ^ Should we ignore this file?
48+ -> FilePath -- ^ Search directory
49+ -> IO Provider -- ^ Resulting provider
50+newProvider store ignore directory = do
5151 list <- map fromFilePath . filter (not . ignore) <$>
5252 getRecursiveContents False directory
5353 cache <- newIORef M.empty
54− return $ ResourceProvider (S.fromList list) cache store
54+ return $ Provider (S.fromList list) cache store
5555
5656
5757--------------------------------------------------------------------------------
58−resourceList :: ResourceProvider -> [Identifier]
59−resourceList = S.toList . resourceSet
58+resourceList :: Provider -> [Identifier]
59+resourceList = S.toList . providerSet
6060
6161
6262--------------------------------------------------------------------------------
6363-- | Check if a given resource exists
64−resourceExists :: ResourceProvider -> Identifier -> Bool
64+resourceExists :: Provider -> Identifier -> Bool
6565resourceExists provider =
66− (`S.member` resourceSet provider) . setVersion Nothing
66+ (`S.member` providerSet provider) . setVersion Nothing
6767
6868
6969--------------------------------------------------------------------------------
src/Hakyll/Core/ResourceProvider/Metadata.hs → src/Hakyll/Core/Provider/Metadata.hsrenamed+10−10
@@ -1,30 +1,30 @@
11--------------------------------------------------------------------------------
22-- | Internal module to parse metadata
3−module Hakyll.Core.ResourceProvider.Metadata
3+module Hakyll.Core.Provider.Metadata
44 ( loadMetadata
55 ) where
66
77
88--------------------------------------------------------------------------------
99import Control.Applicative
10−import Control.Arrow (second)
11−import qualified Data.ByteString.Char8 as BC
12−import qualified Data.Map as M
13−import System.IO as IO
14−import Text.Parsec ((<?>))
15−import qualified Text.Parsec as P
16−import Text.Parsec.String (Parser)
10+import Control.Arrow (second)
11+import qualified Data.ByteString.Char8 as BC
12+import qualified Data.Map as M
13+import System.IO as IO
14+import Text.Parsec ((<?>))
15+import qualified Text.Parsec as P
16+import Text.Parsec.String (Parser)
1717
1818
1919--------------------------------------------------------------------------------
2020import Hakyll.Core.Identifier
2121import Hakyll.Core.Metadata
22−import Hakyll.Core.ResourceProvider.Internal
22+import Hakyll.Core.Provider.Internal
2323import Hakyll.Core.Util.String
2424
2525
2626--------------------------------------------------------------------------------
27−loadMetadata :: ResourceProvider -> Identifier -> IO (Metadata, Maybe String)
27+loadMetadata :: Provider -> Identifier -> IO (Metadata, Maybe String)
2828loadMetadata rp identifier = do
2929 hasHeader <- probablyHasMetadataHeader fp
3030 (md, body) <- if hasHeader
src/Hakyll/Core/ResourceProvider/MetadataCache.hs → src/Hakyll/Core/Provider/MetadataCache.hsrenamed+20−20
@@ -1,5 +1,5 @@
11--------------------------------------------------------------------------------
2−module Hakyll.Core.ResourceProvider.MetadataCache
2+module Hakyll.Core.Provider.MetadataCache
33 ( resourceMetadata
44 , resourceBody
55 , resourceInvalidateMetadataCache
@@ -9,50 +9,50 @@ module Hakyll.Core.ResourceProvider.MetadataCache
99--------------------------------------------------------------------------------
1010import Hakyll.Core.Identifier
1111import Hakyll.Core.Metadata
12−import Hakyll.Core.ResourceProvider.Internal
13−import Hakyll.Core.ResourceProvider.Metadata
14−import qualified Hakyll.Core.Store as Store
12+import Hakyll.Core.Provider.Internal
13+import Hakyll.Core.Provider.Metadata
14+import qualified Hakyll.Core.Store as Store
1515
1616
1717--------------------------------------------------------------------------------
18−resourceMetadata :: ResourceProvider -> Identifier -> IO Metadata
19−resourceMetadata rp r = do
20− load rp r
21− Store.Found md <- Store.get (resourceStore rp)
18+resourceMetadata :: Provider -> Identifier -> IO Metadata
19+resourceMetadata p r = do
20+ load p r
21+ Store.Found md <- Store.get (providerStore p)
2222 [name, toFilePath r, "metadata"]
2323 return md
2424
2525
2626--------------------------------------------------------------------------------
27−resourceBody :: ResourceProvider -> Identifier -> IO String
28−resourceBody rp r = do
29− load rp r
30− Store.Found bd <- Store.get (resourceStore rp)
27+resourceBody :: Provider -> Identifier -> IO String
28+resourceBody p r = do
29+ load p r
30+ Store.Found bd <- Store.get (providerStore p)
3131 [name, toFilePath r, "body"]
3232 maybe (resourceString r) return bd
3333
3434
3535--------------------------------------------------------------------------------
36−resourceInvalidateMetadataCache :: ResourceProvider -> Identifier -> IO ()
37−resourceInvalidateMetadataCache rp r = do
38− Store.delete (resourceStore rp) [name, toFilePath r, "metadata"]
39− Store.delete (resourceStore rp) [name, toFilePath r, "body"]
36+resourceInvalidateMetadataCache :: Provider -> Identifier -> IO ()
37+resourceInvalidateMetadataCache p r = do
38+ Store.delete (providerStore p) [name, toFilePath r, "metadata"]
39+ Store.delete (providerStore p) [name, toFilePath r, "body"]
4040
4141
4242--------------------------------------------------------------------------------
43−load :: ResourceProvider -> Identifier -> IO ()
44−load rp r = do
43+load :: Provider -> Identifier -> IO ()
44+load p r = do
4545 mmd <- Store.get store mdk :: IO (Store.Result Metadata)
4646 case mmd of
4747 -- Already loaded
4848 Store.Found _ -> return ()
4949 -- Not yet loaded
5050 _ -> do
51− (metadata, body) <- loadMetadata rp r
51+ (metadata, body) <- loadMetadata p r
5252 Store.set store mdk metadata
5353 Store.set store bk body
5454 where
55− store = resourceStore rp
55+ store = providerStore p
5656 mdk = [name, toFilePath r, "metadata"]
5757 bk = [name, toFilePath r, "body"]
5858
src/Hakyll/Core/ResourceProvider/Modified.hs → src/Hakyll/Core/Provider/Modified.hsrenamed+16−16
@@ -1,33 +1,33 @@
11--------------------------------------------------------------------------------
2−module Hakyll.Core.ResourceProvider.Modified
2+module Hakyll.Core.Provider.Modified
33 ( resourceModified
44 , resourceModificationTime
55 ) where
66
77
88--------------------------------------------------------------------------------
9−import Control.Applicative ((<$>), (<*>))
10−import Control.Monad (when)
11−import qualified Crypto.Hash.MD5 as MD5
12−import qualified Data.ByteString as B
13−import qualified Data.ByteString.Lazy as BL
9+import Control.Applicative ((<$>), (<*>))
10+import Control.Monad (when)
11+import qualified Crypto.Hash.MD5 as MD5
12+import qualified Data.ByteString as B
13+import qualified Data.ByteString.Lazy as BL
1414import Data.IORef
15−import qualified Data.Map as M
16−import Data.Time (UTCTime)
17−import System.Directory (getModificationTime)
15+import qualified Data.Map as M
16+import Data.Time (UTCTime)
17+import System.Directory (getModificationTime)
1818
1919
2020--------------------------------------------------------------------------------
2121import Hakyll.Core.Identifier
22−import Hakyll.Core.ResourceProvider.Internal
23−import Hakyll.Core.ResourceProvider.MetadataCache
24−import Hakyll.Core.Store (Store)
25−import qualified Hakyll.Core.Store as Store
22+import Hakyll.Core.Provider.Internal
23+import Hakyll.Core.Provider.MetadataCache
24+import Hakyll.Core.Store (Store)
25+import qualified Hakyll.Core.Store as Store
2626
2727
2828--------------------------------------------------------------------------------
2929-- | A resource is modified if it or its metadata has changed
30−resourceModified :: ResourceProvider -> Identifier -> IO Bool
30+resourceModified :: Provider -> Identifier -> IO Bool
3131resourceModified rp r
3232 | not exists = return False
3333 | otherwise = do
@@ -49,8 +49,8 @@ resourceModified rp r
4949 where
5050 normalized = setVersion Nothing r
5151 exists = resourceExists rp r
52− store = resourceStore rp
53− cacheRef = resourceModifiedCache rp
52+ store = providerStore rp
53+ cacheRef = providerModifiedCache rp
5454
5555
5656--------------------------------------------------------------------------------
src/Hakyll/Core/Rules.hs+10−9
@@ -44,11 +44,12 @@ import Data.Typeable (Typeable)
4444
4545
4646--------------------------------------------------------------------------------
47−import Hakyll.Core.CompiledItem
4847import Hakyll.Core.Compiler.Internal
4948import Hakyll.Core.Identifier
5049import 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
5253import Hakyll.Core.Routes
5354import Hakyll.Core.Rules.Internal
5455import Hakyll.Core.Writable
@@ -63,11 +64,11 @@ tellRoute route' = Rules $ tell $ RuleSet route' mempty mempty
6364--------------------------------------------------------------------------------
6465-- | Add a number of compilers
6566tellCompilers :: (Binary a, Typeable a, Writable a)
66− => [(Identifier, Compiler a)]
67+ => [(Identifier, Compiler (Item a))]
6768 -> Rules ()
6869tellCompilers compilers = Rules $ do
6970 -- 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
7172 tell $ RuleSet mempty compilers' mempty
7273
7374
@@ -132,7 +133,7 @@ group g = Rules . local setVersion' . unRules
132133-- no resources match the current selection, nothing will happen. In this case,
133134-- you might want to have a look at 'create'.
134135compile :: (Binary a, Typeable a, Writable a)
135− => Compiler a -> Rules ()
136+ => Compiler (Item a) -> Rules ()
136137compile compiler = do
137138 ids <- resources
138139 tellCompilers [(id', compiler) | id' <- ids]
@@ -149,7 +150,7 @@ compile compiler = do
149150-- replaced by the group set via 'group' (or 'Nothing', if 'group' has not been
150151-- used).
151152create :: (Binary a, Typeable a, Writable a)
152− => Identifier -> Compiler a -> Rules ()
153+ => Identifier -> Compiler (Item a) -> Rules ()
153154create id' compiler = Rules $ do
154155 version' <- rulesVersion <$> ask
155156 let id'' = setVersion version' id'
@@ -175,9 +176,9 @@ route route' = Rules $ do
175176-- the correct group to the identifiers.
176177resources :: Rules [Identifier]
177178resources = Rules $ do
178− pattern <- rulesPattern <$> ask
179− provider <- rulesResourceProvider <$> ask
180− g <- rulesVersion <$> ask
179+ pattern <- rulesPattern <$> ask
180+ provider <- rulesProvider <$> ask
181+ g <- rulesVersion <$> ask
181182 return $ filterMatches pattern $ map (setVersion g) $ resourceList provider
182183
183184
src/Hakyll/Core/Rules/Internal.hs+10−10
@@ -20,11 +20,11 @@ import Data.Set (Set)
2020
2121
2222--------------------------------------------------------------------------------
23−import Hakyll.Core.CompiledItem
2423import Hakyll.Core.Compiler.Internal
2524import Hakyll.Core.Identifier
2625import Hakyll.Core.Identifier.Pattern
27−import Hakyll.Core.ResourceProvider
26+import Hakyll.Core.Item.SomeItem
27+import Hakyll.Core.Provider
2828import Hakyll.Core.Routes
2929
3030
@@ -34,7 +34,7 @@ data RuleSet = RuleSet
3434 { -- | Routes used in the compilation structure
3535 rulesRoutes :: Routes
3636 , -- | Compilation rules
37− rulesCompilers :: [(Identifier, Compiler CompiledItem)]
37+ rulesCompilers :: [(Identifier, Compiler SomeItem)]
3838 , -- | A set of the actually used files
3939 rulesResources :: Set Identifier
4040 }
@@ -57,9 +57,9 @@ data RuleState = RuleState
5757--------------------------------------------------------------------------------
5858-- | Rule environment
5959data RuleEnvironment = RuleEnvironment
60− { rulesResourceProvider :: ResourceProvider
61− , rulesPattern :: Pattern
62− , rulesVersion :: Maybe String
60+ { rulesProvider :: Provider
61+ , rulesPattern :: Pattern
62+ , rulesVersion :: Maybe String
6363 }
6464
6565
@@ -72,16 +72,16 @@ newtype Rules a = Rules
7272
7373--------------------------------------------------------------------------------
7474-- | Run a Rules monad, resulting in a 'RuleSet'
75−runRules :: Rules a -> ResourceProvider -> IO RuleSet
75+runRules :: Rules a -> Provider -> IO RuleSet
7676runRules rules provider = do
7777 (_, _, ruleSet) <- runRWST (unRules rules) env state
7878 return $ nubCompilers ruleSet
7979 where
8080 state = RuleState {rulesNextIdentifier = 0}
8181 env = RuleEnvironment
82− { rulesResourceProvider = provider
83− , rulesPattern = mempty
84− , rulesVersion = Nothing
82+ { rulesProvider = provider
83+ , rulesPattern = mempty
84+ , rulesVersion = Nothing
8585 }
8686
8787
src/Hakyll/Core/Runtime.hs+14−11
@@ -22,15 +22,16 @@ import System.FilePath ((</>))
2222
2323
2424--------------------------------------------------------------------------------
25−import Hakyll.Core.CompiledItem
2625import Hakyll.Core.Compiler.Internal
2726import Hakyll.Core.Compiler.Require
2827import Hakyll.Core.Configuration
2928import Hakyll.Core.Dependencies
3029import Hakyll.Core.Identifier
30+import Hakyll.Core.Item
31+import Hakyll.Core.Item.SomeItem
3132import Hakyll.Core.Logger (Logger)
3233import qualified Hakyll.Core.Logger as Logger
33−import Hakyll.Core.ResourceProvider
34+import Hakyll.Core.Provider
3435import Hakyll.Core.Routes
3536import Hakyll.Core.Rules.Internal
3637import Hakyll.Core.Store (Store)
@@ -49,7 +50,7 @@ run configuration rules = do
4950 store <- Store.new (inMemoryCache configuration) $
5051 storeDirectory configuration
5152 Logger.message logger "Creating provider..."
52− provider <- newResourceProvider store (ignoreFile configuration) "."
53+ provider <- newProvider store (ignoreFile configuration) "."
5354 Logger.message logger "Running rules..."
5455 ruleSet <- runRules rules provider
5556
@@ -94,17 +95,17 @@ run configuration rules = do
9495data RuntimeRead = RuntimeRead
9596 { runtimeConfiguration :: Configuration
9697 , runtimeLogger :: Logger
97− , runtimeProvider :: ResourceProvider
98+ , runtimeProvider :: Provider
9899 , runtimeStore :: Store
99100 , runtimeRoutes :: Routes
100− , runtimeUniverse :: [(Identifier, Compiler CompiledItem)]
101+ , runtimeUniverse :: [(Identifier, Compiler SomeItem)]
101102 }
102103
103104
104105--------------------------------------------------------------------------------
105106data RuntimeState = RuntimeState
106107 { runtimeDone :: Set Identifier
107− , runtimeTodo :: Map Identifier (Compiler CompiledItem)
108+ , runtimeTodo :: Map Identifier (Compiler SomeItem)
108109 , runtimeFacts :: DependencyFacts
109110 }
110111
@@ -178,7 +179,7 @@ chase trail id'
178179
179180 let compiler = todo M.! id'
180181 read' = CompilerRead
181− { compilerIdentifier = id'
182+ { compilerUnderlying = id'
182183 , compilerProvider = provider
183184 , compilerUniverse = map fst universe
184185 , compilerRoutes = routes
@@ -192,8 +193,10 @@ chase trail id'
192193 CompilerError e -> throwError e
193194
194195 -- Huge success
195− CompilerDone (CompiledItem compiled) cwrite -> do
196− let facts = compilerDependencies cwrite
196+ CompilerDone (SomeItem item) cwrite -> do
197+ -- TODO: Sanity check on itemIdentifier?
198+ let body = itemBody item
199+ facts = compilerDependencies cwrite
197200 cacheHits
198201 | compilerCacheHits cwrite <= 0 = "updated"
199202 | otherwise = "cached "
@@ -207,11 +210,11 @@ chase trail id'
207210 Just url -> do
208211 let path = destinationDirectory config </> url
209212 liftIO $ makeDirectories path
210− liftIO $ write path compiled
213+ liftIO $ write path item
211214 Logger.debug logger $ "Routed to " ++ path
212215
213216 -- Save! (For require)
214− liftIO $ save store id' compiled
217+ liftIO $ save store id' body
215218
216219 -- Update state
217220 modify $ \s -> s
src/Hakyll/Core/Writable.hs+32−18
@@ -1,42 +1,56 @@
1+--------------------------------------------------------------------------------
12-- | Describes writable items; items that can be saved to the disk
2−--
3−{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
3+{-# LANGUAGE FlexibleInstances #-}
4+{-# LANGUAGE TypeSynonymInstances #-}
45module Hakyll.Core.Writable
56 ( Writable (..)
67 ) where
78
8−import Data.Word (Word8)
99
10−import qualified Data.ByteString as SB
11−import qualified Data.ByteString.Lazy as LB
12−import Text.Blaze.Html (Html)
13−import Text.Blaze.Html.Renderer.String (renderHtml)
10+--------------------------------------------------------------------------------
11+import qualified Data.ByteString as SB
12+import qualified Data.ByteString.Lazy as LB
13+import Data.Word (Word8)
14+import Text.Blaze.Html (Html)
15+import Text.Blaze.Html.Renderer.String (renderHtml)
1416
15−import Hakyll.Core.Identifier
1617
18+--------------------------------------------------------------------------------
19+import Hakyll.Core.Item
20+
21+
22+--------------------------------------------------------------------------------
1723-- | Describes an item that can be saved to the disk
18−--
1924class Writable a where
2025 -- | Save an item to the given filepath
21− write :: FilePath -> a -> IO ()
26+ write :: FilePath -> Item a -> IO ()
27+
2228
29+--------------------------------------------------------------------------------
2330instance Writable () where
2431 write _ _ = return ()
2532
33+
34+--------------------------------------------------------------------------------
2635instance Writable [Char] where
27− write = writeFile
36+ write p = writeFile p . itemBody
37+
2838
39+--------------------------------------------------------------------------------
2940instance Writable SB.ByteString where
30− write p = SB.writeFile p
41+ write p = SB.writeFile p . itemBody
3142
43+
44+--------------------------------------------------------------------------------
3245instance Writable LB.ByteString where
33− write p = LB.writeFile p
46+ write p = LB.writeFile p . itemBody
47+
3448
49+--------------------------------------------------------------------------------
3550instance Writable [Word8] where
36− write p = write p . SB.pack
51+ write p = write p . fmap SB.pack
3752
38−instance Writable Html where
39− write p html = write p $ renderHtml html
4053
41−instance Writable Identifier where
42− write p = write p . show
54+--------------------------------------------------------------------------------
55+instance Writable Html where
56+ write p = write p . fmap renderHtml
src/Hakyll/Core/Writable/CopyFile.hs+14−8
@@ -9,8 +9,7 @@ module Hakyll.Core.Writable.CopyFile
99
1010
1111--------------------------------------------------------------------------------
12−import Control.Applicative ((<$>))
13−import Data.Binary (Binary)
12+import Data.Binary (Binary (..))
1413import Data.Typeable (Typeable)
1514import System.Directory (copyFile)
1615
@@ -18,20 +17,27 @@ import System.Directory (copyFile)
1817--------------------------------------------------------------------------------
1918import Hakyll.Core.Compiler
2019import Hakyll.Core.Identifier
20+import Hakyll.Core.Item
2121import Hakyll.Core.Writable
2222
2323
2424--------------------------------------------------------------------------------
25−-- | Newtype construct around 'FilePath' which will copy the file directly
26−newtype CopyFile = CopyFile {unCopyFile :: FilePath}
27− deriving (Show, Eq, Ord, Binary, Typeable)
25+-- | This will copy any file directly by using a system call
26+data CopyFile = CopyFile
27+ deriving (Show, Eq, Ord, Typeable)
28+
29+
30+--------------------------------------------------------------------------------
31+instance Binary CopyFile where
32+ put CopyFile = return ()
33+ get = return CopyFile
2834
2935
3036--------------------------------------------------------------------------------
3137instance Writable CopyFile where
32− write dst (CopyFile src) = copyFile src dst
38+ write dst item = copyFile (toFilePath $ itemIdentifier item) dst
3339
3440
3541--------------------------------------------------------------------------------
36−copyFileCompiler :: Compiler CopyFile
37−copyFileCompiler = CopyFile . toFilePath <$> getIdentifier
42+copyFileCompiler :: Compiler (Item CopyFile)
43+copyFileCompiler = makeItem CopyFile
src/Hakyll/Web/CompressCss.hs+3−2
@@ -15,13 +15,14 @@ import Data.List (isPrefixOf)
1515
1616--------------------------------------------------------------------------------
1717import Hakyll.Core.Compiler
18+import Hakyll.Core.Item
1819import Hakyll.Core.Util.String
1920
2021
2122--------------------------------------------------------------------------------
2223-- | Compiler form of 'compressCss'
23−compressCssCompiler :: Compiler String
24−compressCssCompiler = compressCss <$> getResourceString
24+compressCssCompiler :: Compiler (Item String)
25+compressCssCompiler = fmap compressCss <$> getResourceString
2526
2627
2728--------------------------------------------------------------------------------
src/Hakyll/Web/Page.hs+8−17
@@ -49,34 +49,26 @@
4949-- contains three metadata fields and a body. The body is given in markdown
5050-- format, which can be easily rendered to HTML by Hakyll, using pandoc.
5151module Hakyll.Web.Page
52− ( Page
53− , readPageCompiler
54− , pageCompiler
52+ ( pageCompiler
5553 , pageCompilerWith
5654 , pageCompilerWithPandoc
5755 ) where
5856
5957
6058--------------------------------------------------------------------------------
61−import Text.Pandoc (Pandoc, ParserState, WriterOptions)
59+import Control.Applicative ((<$>))
60+import Text.Pandoc (Pandoc, ParserState, WriterOptions)
6261
6362
6463--------------------------------------------------------------------------------
6564import Hakyll.Core.Compiler
66−import Hakyll.Web.Page.Internal
65+import Hakyll.Core.Item
6766import Hakyll.Web.Pandoc
6867
6968
70−--------------------------------------------------------------------------------
71−-- | Read a page (do not render it)
72−readPageCompiler :: Compiler Page
73−readPageCompiler = getResourceBody
74−{-# DEPRECATED readPageCompiler "Use getResourceBody" #-}
75−
76−
7769--------------------------------------------------------------------------------
7870-- | Read a page render using pandoc
79−pageCompiler :: Compiler Page
71+pageCompiler :: Compiler (Item String)
8072pageCompiler =
8173 pageCompilerWith defaultHakyllParserState defaultHakyllWriterOptions
8274
@@ -84,7 +76,7 @@ pageCompiler =
8476--------------------------------------------------------------------------------
8577-- | A version of 'pageCompiler' which allows you to specify your own pandoc
8678-- options
87−pageCompilerWith :: ParserState -> WriterOptions -> Compiler Page
79+pageCompilerWith :: ParserState -> WriterOptions -> Compiler (Item String)
8880pageCompilerWith state options = pageCompilerWithPandoc state options id
8981
9082
@@ -93,9 +85,8 @@ pageCompilerWith state options = pageCompilerWithPandoc state options id
9385-- pandoc transformation for the content
9486pageCompilerWithPandoc :: ParserState -> WriterOptions
9587 -> (Pandoc -> Pandoc)
96− -> Compiler Page
88+ -> Compiler (Item String)
9789pageCompilerWithPandoc state options f = cached cacheName $
98− readPageCompiler >>= pageReadPandocWith state >>=
99− return . writePandocWith options . f
90+ writePandocWith options . fmap f . readPandocWith state <$> getResourceBody
10091 where
10192 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
66 , readPandocWith
77 , writePandoc
88 , writePandocWith
9−
10− -- * Functions working on pages/compilers
11− , pageReadPandoc
12− , pageReadPandocWith
13− , pageRenderPandoc
14− , pageRenderPandocWith
9+ , renderPandoc
10+ , renderPandocWith
1511
1612 -- * Default options
1713 , defaultHakyllParserState
@@ -20,89 +16,66 @@ module Hakyll.Web.Pandoc
2016
2117
2218--------------------------------------------------------------------------------
23−import Control.Applicative ((<$>))
24−import Data.Maybe (fromMaybe)
2519import Text.Pandoc
2620
2721
2822--------------------------------------------------------------------------------
29−import Hakyll.Core.Compiler
30−import Hakyll.Core.Identifier
31−import Hakyll.Web.Page.Internal
23+import Hakyll.Core.Item
3224import Hakyll.Web.Pandoc.FileType
3325
3426
3527--------------------------------------------------------------------------------
3628-- | 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
40− -> Pandoc -- ^ Resulting document
29+readPandoc :: Item String -- ^ String to read
30+ -> Item Pandoc -- ^ Resulting document
4131readPandoc = readPandocWith defaultHakyllParserState
4232
4333
4434--------------------------------------------------------------------------------
4535-- | Read a string using pandoc, with the supplied options
46−readPandocWith :: ParserState -- ^ Parser options
47− -> FileType -- ^ Determines parsing method
48− -> Maybe Identifier -- ^ Optional, for better error messages
49− -> Page -- ^ String to read
50− -> Pandoc -- ^ Resulting document
51−readPandocWith state fileType' id' = case fileType' of
52− Html -> readHtml state
53− LaTeX -> readLaTeX state
54− LiterateHaskell t ->
55− readPandocWith state {stateLiterateHaskell = True} t id'
56− Markdown -> readMarkdown state
57− Rst -> readRST state
58− Textile -> readTextile state
59− t -> error $
60− "Hakyll.Web.readPandocWith: I don't know how to read a file of the " ++
61− "type " ++ show t ++ fromMaybe "" (fmap ((" for: " ++) . show) id')
36+readPandocWith :: ParserState -- ^ Parser options
37+ -> Item String -- ^ String to read
38+ -> Item Pandoc -- ^ Resulting document
39+readPandocWith state item = fmap (reader state (itemFileType item)) item
40+ where
41+ reader s t = case t of
42+ Html -> readHtml s
43+ LaTeX -> readLaTeX s
44+ LiterateHaskell t' -> reader s {stateLiterateHaskell = True} t'
45+ Markdown -> readMarkdown s
46+ Rst -> readRST s
47+ Textile -> readTextile s
48+ _ -> error $
49+ "Hakyll.Web.readPandocWith: I don't know how to read a file of the " ++
50+ "type " ++ show t ++ " for: " ++ show (itemIdentifier item)
6251
6352
6453--------------------------------------------------------------------------------
6554-- | Write a document (as HTML) using pandoc, with the default options
66−writePandoc :: Pandoc -- ^ Document to write
67− -> Page -- ^ Resulting HTML
55+writePandoc :: Item Pandoc -- ^ Document to write
56+ -> Item String -- ^ Resulting HTML
6857writePandoc = writePandocWith defaultHakyllWriterOptions
6958
7059
7160--------------------------------------------------------------------------------
7261-- | Write a document (as HTML) using pandoc, with the supplied options
7362writePandocWith :: WriterOptions -- ^ Writer options for pandoc
74− -> Pandoc -- ^ Document to write
75− -> Page -- ^ Resulting HTML
76−writePandocWith = writeHtmlString
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
63+ -> Item Pandoc -- ^ Document to write
64+ -> Item String -- ^ Resulting HTML
65+writePandocWith options = fmap $ writeHtmlString options
9266
9367
9468--------------------------------------------------------------------------------
9569-- | Render the resource using pandoc
96−pageRenderPandoc :: Page -> Compiler Page
97−pageRenderPandoc =
98− pageRenderPandocWith defaultHakyllParserState defaultHakyllWriterOptions
70+renderPandoc :: Item String -> Item String
71+renderPandoc =
72+ renderPandocWith defaultHakyllParserState defaultHakyllWriterOptions
9973
10074
10175--------------------------------------------------------------------------------
10276-- | Render the resource using pandoc
103−pageRenderPandocWith :: ParserState -> WriterOptions -> Page -> Compiler Page
104−pageRenderPandocWith state options page =
105− writePandocWith options <$> pageReadPandocWith state page
77+renderPandocWith :: ParserState -> WriterOptions -> Item String -> Item String
78+renderPandocWith state options = writePandocWith options . readPandocWith state
10679
10780
10881--------------------------------------------------------------------------------
src/Hakyll/Web/Pandoc/Biblio.hs+37−20
@@ -15,7 +15,7 @@ module Hakyll.Web.Pandoc.Biblio
1515 , cslCompiler
1616 , Biblio (..)
1717 , biblioCompiler
18− , pageReadPandocBiblio
18+ , readPandocBiblio
1919 ) where
2020
2121
@@ -31,19 +31,31 @@ import Text.Pandoc.Biblio (processBiblio)
3131--------------------------------------------------------------------------------
3232import Hakyll.Core.Compiler
3333import Hakyll.Core.Identifier
34+import Hakyll.Core.Item
3435import Hakyll.Core.Writable
35−import Hakyll.Web.Page
3636import Hakyll.Web.Pandoc
3737
3838
3939--------------------------------------------------------------------------------
40−newtype CSL = CSL FilePath
41− deriving (Binary, Show, Typeable, Writable)
40+data CSL = CSL
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 ()
4254
4355
4456--------------------------------------------------------------------------------
45−cslCompiler :: Compiler CSL
46−cslCompiler = CSL . toFilePath <$> getIdentifier
57+cslCompiler :: Compiler (Item CSL)
58+cslCompiler = makeItem CSL
4759
4860
4961--------------------------------------------------------------------------------
@@ -57,29 +69,34 @@ instance Binary Biblio where
5769 get = Biblio . read <$> get
5870 put (Biblio rs) = put $ show rs
5971
72+
73+--------------------------------------------------------------------------------
6074instance Writable Biblio where
75+ -- Shouldn't be written.
6176 write _ _ = return ()
6277
6378
6479--------------------------------------------------------------------------------
65−biblioCompiler :: Compiler Biblio
80+biblioCompiler :: Compiler (Item Biblio)
6681biblioCompiler = do
67− filePath <- toFilePath <$> getIdentifier
68− unsafeCompiler $ Biblio <$> CSL.readBiblioFile filePath
82+ filePath <- toFilePath <$> getUnderlying
83+ makeItem =<< unsafeCompiler (Biblio <$> CSL.readBiblioFile filePath)
6984
7085
7186--------------------------------------------------------------------------------
72−pageReadPandocBiblio :: ParserState
73− -> CSL
74− -> Biblio
75− -> Page
76− -> Compiler Pandoc
77−pageReadPandocBiblio state (CSL csl) (Biblio refs) page = do
87+readPandocBiblio :: ParserState
88+ -> Item CSL
89+ -> Item Biblio
90+ -> (Item String)
91+ -> Compiler (Item Pandoc)
92+readPandocBiblio state csl biblio item = do
7893 -- We need to know the citation keys, add then *before* actually parsing the
7994 -- actual page. If we don't do this, pandoc won't even consider them
8095 -- citations!
81− let cits = map CSL.refId refs
82− state' = state {stateCitations = stateCitations state ++ cits}
83− pandoc <- pageReadPandocWith state' page
84− pandoc' <- unsafeCompiler $ processBiblio csl Nothing refs pandoc
85− return pandoc'
96+ let Biblio refs = itemBody biblio
97+ cits = map CSL.refId refs
98+ state' = state {stateCitations = stateCitations state ++ cits}
99+ pandoc = itemBody $ readPandocWith state' item
100+ cslPath = toFilePath $ itemIdentifier csl
101+ pandoc' <- unsafeCompiler $ processBiblio cslPath Nothing refs pandoc
102+ return $ fmap (const pandoc') item
src/Hakyll/Web/Pandoc/FileType.hs+4−5
@@ -3,18 +3,17 @@
33module Hakyll.Web.Pandoc.FileType
44 ( FileType (..)
55 , fileType
6− , getFileType
6+ , itemFileType
77 ) where
88
99
1010--------------------------------------------------------------------------------
11−import Control.Applicative ((<$>))
1211import System.FilePath (takeExtension)
1312
1413
1514--------------------------------------------------------------------------------
16−import Hakyll.Core.Compiler
1715import Hakyll.Core.Identifier
16+import Hakyll.Core.Item
1817
1918
2019--------------------------------------------------------------------------------
@@ -62,5 +61,5 @@ fileType = fileType' . takeExtension
6261
6362--------------------------------------------------------------------------------
6463-- | Get the file type for the current file
65−getFileType :: Compiler FileType
66−getFileType = fileType . toFilePath <$> getIdentifier
64+itemFileType :: Item a -> FileType
65+itemFileType = fileType . toFilePath . itemIdentifier
src/Hakyll/Web/Template.hs+28−28
@@ -60,10 +60,10 @@
6060-- > #{body}
6161module Hakyll.Web.Template
6262 ( Template
63− , applyTemplate
6463 , templateCompiler
6564 , templateCompilerWith
66− , applyTemplateCompiler
65+ , applyTemplate
66+ , applyTemplateWith
6767 ) where
6868
6969
@@ -78,51 +78,51 @@ import Text.Hamlet (HamletSettings,
7878--------------------------------------------------------------------------------
7979import Hakyll.Core.Compiler
8080import Hakyll.Core.Identifier
81−import Hakyll.Web.Page.Internal
81+import Hakyll.Core.Item
8282import Hakyll.Web.Template.Context
8383import Hakyll.Web.Template.Internal
8484import Hakyll.Web.Template.Read
8585
8686
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−
9887--------------------------------------------------------------------------------
9988-- | Read a template. If the extension of the file we're compiling is
10089-- @.hml@ or @.hamlet@, it will be considered as a Hamlet template, and parsed
10190-- as such.
102−templateCompiler :: Compiler Template
91+templateCompiler :: Compiler (Item Template)
10392templateCompiler = templateCompilerWith defaultHamletSettings
10493
10594
10695--------------------------------------------------------------------------------
10796-- | Version of 'templateCompiler' that enables custom settings.
108−templateCompilerWith :: HamletSettings -> Compiler Template
97+templateCompilerWith :: HamletSettings -> Compiler (Item Template)
10998templateCompilerWith settings =
11099 cached "Hakyll.Web.Template.templateCompilerWith" $ do
111− identifier <- getIdentifier
112− string <- getResourceString
100+ identifier <- getUnderlying
101+ item <- getResourceString
113102 if takeExtension (toFilePath identifier) `elem` [".hml", ".hamlet"]
114103 -- Hamlet template
115− then return $ readHamletTemplateWith settings string
104+ then return $ fmap (readHamletTemplateWith settings) item
116105 -- 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
118118
119119
120120--------------------------------------------------------------------------------
121−applyTemplateCompiler :: Template -- ^ Template
122− -> Context Page -- ^ Context
123− -> Page -- ^ Page
124− -> Compiler Page -- ^ Compiler
125−applyTemplateCompiler tpl context page = do
126− identifier <- getIdentifier
127− let context' k x = unContext context k identifier x
128− applyTemplate context' tpl page
121+applyTemplateWith :: Monad m
122+ => (String -> a -> m String)
123+ -> Template -> a -> m String
124+applyTemplateWith context tpl x = liftM concat $
125+ forM (unTemplate tpl) $ \e -> case e of
126+ Chunk c -> return c
127+ Escaped -> return "$"
128+ Key k -> context k x
src/Hakyll/Web/Template/Context.hs+20−19
@@ -34,36 +34,36 @@ import System.Locale (TimeLocale, defaultTimeLocale)
3434import Hakyll.Core.Compiler
3535import Hakyll.Core.Compiler.Internal
3636import Hakyll.Core.Identifier
37−import Hakyll.Core.ResourceProvider
37+import Hakyll.Core.Item
38+import Hakyll.Core.Provider
3839import Hakyll.Core.Util.String (splitAll)
39−import Hakyll.Web.Page.Internal
4040import Hakyll.Web.Urls
4141
4242
4343--------------------------------------------------------------------------------
4444newtype Context a = Context
45− { unContext :: String -> Identifier -> a -> Compiler String
45+ { unContext :: String -> Item a -> Compiler String
4646 }
4747
4848
4949--------------------------------------------------------------------------------
5050instance Monoid (Context a) where
51− mempty = Context $ \_ _ _ -> empty
52− mappend (Context f) (Context g) = Context $ \k i x -> f k i x <|> g k i x
51+ mempty = Context $ \_ _ -> empty
52+ mappend (Context f) (Context g) = Context $ \k i -> f k i <|> g k i
5353
5454
5555--------------------------------------------------------------------------------
5656mapContext :: (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
5858
5959
6060--------------------------------------------------------------------------------
61−field :: String -> (Identifier -> a -> Compiler String) -> Context a
62−field key value = Context $ \k i x -> if k == key then value i x else empty
61+field :: String -> (Item a -> Compiler String) -> Context a
62+field key value = Context $ \k i -> if k == key then value i else empty
6363
6464
6565--------------------------------------------------------------------------------
66−defaultContext :: Context Page
66+defaultContext :: Context String
6767defaultContext =
6868 bodyField "body" `mappend`
6969 urlField "url" `mappend`
@@ -74,18 +74,19 @@ defaultContext =
7474
7575
7676--------------------------------------------------------------------------------
77−bodyField :: String -> Context Page
78−bodyField key = field key $ \_ x -> return x
77+bodyField :: String -> Context String
78+bodyField key = field key $ return . itemBody
7979
8080
8181--------------------------------------------------------------------------------
8282urlField :: 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
8485
8586
8687--------------------------------------------------------------------------------
8788pathField :: String -> Context a
88−pathField key = field key $ \i _ -> return $ toFilePath i
89+pathField key = field key $ return . toFilePath . itemIdentifier
8990
9091
9192--------------------------------------------------------------------------------
@@ -133,8 +134,8 @@ dateFieldWith :: TimeLocale -- ^ Output time locale
133134 -> String -- ^ Destination key
134135 -> String -- ^ Format to use on the date
135136 -> Context a -- ^ Resulting context
136−dateFieldWith locale key format = field key $ \id' _ -> do
137− time <- getUTC locale id'
137+dateFieldWith locale key format = field key $ \i -> do
138+ time <- getUTC locale $ itemIdentifier i
138139 return $ formatTime locale format time
139140
140141
@@ -145,7 +146,7 @@ getUTC :: TimeLocale -- ^ Output time locale
145146 -> Identifier -- ^ Input page
146147 -> Compiler UTCTime -- ^ Parsed UTCTime
147148getUTC locale id' = do
148− metadata <- getMetadataFor id'
149+ metadata <- getMetadata id'
149150 let tryField k fmt = M.lookup k metadata >>= parseTime' fmt
150151 fn = takeFileName $ toFilePath id'
151152
@@ -177,11 +178,11 @@ modificationTimeFieldWith :: TimeLocale -- ^ Time output locale
177178 -> String -- ^ Key
178179 -> String -- ^ Format
179180 -> Context a -- ^ Resulting context
180−modificationTimeFieldWith locale key fmt = field key $ \id' _ -> do
181− mtime <- compilerUnsafeIO $ resourceModificationTime id'
181+modificationTimeFieldWith locale key fmt = field key $ \i -> do
182+ mtime <- compilerUnsafeIO $ resourceModificationTime $ itemIdentifier i
182183 return $ formatTime locale fmt mtime
183184
184185
185186--------------------------------------------------------------------------------
186187missingField :: 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 @@
1515--
1616-- > <img src="../images/lolcat.png" alt="Funny zomgroflcopter" />
1717module Hakyll.Web.Urls.Relativize
18− ( relativizeUrlsCompiler
19− , relativizeUrls
18+ ( relativizeUrls
19+ , relativizeUrlsWith
2020 ) where
2121
2222
@@ -26,27 +26,27 @@ import Data.List (isPrefixOf)
2626
2727--------------------------------------------------------------------------------
2828import Hakyll.Core.Compiler
29−import Hakyll.Web.Page
29+import Hakyll.Core.Item
3030import Hakyll.Web.Urls
3131
3232
3333--------------------------------------------------------------------------------
3434-- | Compiler form of 'relativizeUrls' which automatically picks the right root
3535-- path
36−relativizeUrlsCompiler :: Page -> Compiler Page
37−relativizeUrlsCompiler page = do
38− route <- getRoute
36+relativizeUrls :: Item String -> Compiler (Item String)
37+relativizeUrls item = do
38+ route <- getRoute $ itemIdentifier item
3939 return $ case route of
40− Nothing -> page
41− Just r -> relativizeUrls (toSiteRoot r) page
40+ Nothing -> item
41+ Just r -> fmap (relativizeUrlsWith $ toSiteRoot r) item
4242
4343
4444--------------------------------------------------------------------------------
4545-- | Relativize URL's in HTML
46−relativizeUrls :: String -- ^ Path to the site root
47− -> Page -- ^ HTML to relativize
48− -> Page -- ^ Resulting HTML
49−relativizeUrls root = withUrls rel
46+relativizeUrlsWith :: String -- ^ Path to the site root
47+ -> String -- ^ HTML to relativize
48+ -> String -- ^ Resulting HTML
49+relativizeUrlsWith root = withUrls rel
5050 where
5151 isRel x = "/" `isPrefixOf` x && not ("//" `isPrefixOf` x)
5252 rel x = if isRel x then root ++ x else x