@@ -24,6 +24,8 @@ import qualified Data.Map as M
2424import Data.List (intercalate)
2525import Control.Monad (foldM)
2626import Control.Arrow (second)
27+import Control.Applicative ((<$>))
28+import System.FilePath ((</>))
2729
2830import Text.Hakyll.Hakyll (Hakyll)
2931import Text.Hakyll.Context (ContextManipulation, changeValue)
@@ -31,11 +33,26 @@ import Text.Hakyll.Render.Internal (finalSubstitute)
3133import Text.Hakyll.Regex
3234import Text.Hakyll.Util
3335import Text.Hakyll.Page
36+import Text.Hakyll.Internal.Cache
3437
3538-- | Read a tag map. This creates a map from tags to page paths.
36−readTagMap :: [FilePath] -> Hakyll (M.Map String [FilePath])
37−readTagMap paths = foldM addPaths M.empty paths
39+--
40+-- You also have to give a unique identifier for every tagmap. This is for
41+-- caching reasons, so the tagmap will be stored in
42+-- @_cache/_tagmap/identifier@.
43+readTagMap :: String -- ^ Unique identifier for the tagmap.
44+ -> [FilePath]
45+ -> Hakyll (M.Map String [FilePath])
46+readTagMap identifier paths = do
47+ isCacheMoreRecent' <- isCacheMoreRecent fileName paths
48+ if isCacheMoreRecent' then M.fromList <$> getFromCache fileName
49+ else do tagMap <- readTagMap'
50+ storeInCache (M.toList tagMap) fileName
51+ return tagMap
3852 where
53+ fileName = "_tagmap" </> identifier
54+
55+ readTagMap' = foldM addPaths M.empty paths
3956 addPaths current path = do
4057 page <- readPage path
4158 let tags = map trim $ splitRegex "," $ getValue "tags" page