@@ -16,35 +16,42 @@
1616-- All the following functions would work with such a format.
1717module Text.Hakyll.Tags
1818 ( readTagMap
19+ , readCategoryMap
1920 , renderTagCloud
2021 , renderTagLinks
2122 ) where
2223
2324import qualified Data.Map as M
2425import Data.List (intercalate)
26+import Data.Maybe (fromMaybe, maybeToList)
2527import Control.Monad (foldM)
2628import Control.Arrow (second)
2729import Control.Applicative ((<$>))
2830import System.FilePath
2931
3032import Text.Hakyll.Hakyll
31−import Text.Hakyll.Context (ContextManipulation, changeValue)
33+import Text.Hakyll.Context
3234import Text.Hakyll.Regex
35+import Text.Hakyll.Renderable
36+import Text.Hakyll.Renderables
3337import Text.Hakyll.Util
34−import Text.Hakyll.Page
3538import Text.Hakyll.Internal.Cache
3639import Text.Hakyll.Internal.Template
3740
41+-- | Type for a tag map.
42+type TagMap = M.Map String [PagePath]
43+
3844-- | Read a tag map. This creates a map from tags to page paths.
3945--
4046-- You also have to give a unique identifier for every tagmap. This is for
4147-- caching reasons, so the tagmap will be stored in
4248-- @_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
49+readMap :: (Context -> [String]) -- ^ Function to get tags from a context.
50+ -> String -- ^ Unique identifier for the tagmap.
51+ -> [PagePath]
52+ -> Hakyll TagMap
53+readMap getTagsFunction identifier paths = do
54+ isCacheMoreRecent' <- isCacheMoreRecent fileName (getDependencies =<< paths)
4855 if isCacheMoreRecent' then M.fromAscList <$> getFromCache fileName
4956 else do tagMap <- readTagMap'
5057 storeInCache (M.toAscList tagMap) fileName
@@ -54,17 +61,26 @@ readTagMap identifier paths = do
5461
5562 readTagMap' = foldM addPaths M.empty paths
5663 addPaths current path = do
57− page <- readPage path
58− categoriesEnabled <- askHakyll enableCategories
59− let tags = map trim $ splitRegex "," $ getValue "tags" page
60− category = [getCategory path | categoriesEnabled]
64+ context <- toContext path
65+ let tags = getTagsFunction context
6166 addPaths' = flip (M.insertWith (++)) [path]
62− return $ foldr addPaths' current (category ++ tags)
67+ return $ foldr addPaths' current tags
68+
69+readTagMap :: String
70+ -> [PagePath]
71+ -> Hakyll TagMap
72+readTagMap = readMap getTagsFunction
73+ where
74+ getTagsFunction = map trim . splitRegex ","
75+ . fromMaybe [] . M.lookup "tags"
6376
64− getCategory = last . splitDirectories . takeDirectory
77+readCategoryMap :: String
78+ -> [PagePath]
79+ -> Hakyll TagMap
80+readCategoryMap = readMap $ maybeToList . M.lookup "category"
6581
6682-- | Render a tag cloud.
67−renderTagCloud :: M.Map String [FilePath] -- ^ Map as produced by @readTagMap@.
83+renderTagCloud :: TagMap -- ^ Map as produced by @readTagMap@.
6884 -> (String -> String) -- ^ Function to produce an url for a tag.
6985 -> Float -- ^ Smallest font size, in percent.
7086 -> Float -- ^ Biggest font size, in percent.