@@ -16,35 +16,42 @@
16-- All the following functions would work with such a format.
16-- All the following functions would work with such a format.
17module Text.Hakyll.Tags
17module Text.Hakyll.Tags
18 ( readTagMap
18 ( readTagMap
19 , renderTagCloud
20 , renderTagCloud
20 , renderTagLinks
21 , renderTagLinks
23import qualified Data.Map as M
24import qualified Data.Map as M
24import Data.List (intercalate)
25import Data.List (intercalate)
26+import Data.Maybe (fromMaybe, maybeToList)
25import Control.Monad (foldM)
27import Control.Monad (foldM)
26import Control.Arrow (second)
28import Control.Arrow (second)
27import Control.Applicative ((<$>))
29import Control.Applicative ((<$>))
28import System.FilePath
30import System.FilePath
30import Text.Hakyll.Hakyll
32import Text.Hakyll.Hakyll
31−import Text.Hakyll.Context (ContextManipulation, changeValue)
33+import Text.Hakyll.Context
32import Text.Hakyll.Regex
34import Text.Hakyll.Regex
35+import Text.Hakyll.Renderable
36+import Text.Hakyll.Renderables
33import Text.Hakyll.Util
37import Text.Hakyll.Util
34−import Text.Hakyll.Page
35import Text.Hakyll.Internal.Cache
38import Text.Hakyll.Internal.Cache
36import Text.Hakyll.Internal.Template
39import Text.Hakyll.Internal.Template
41+-- | Type for a tag map.
42+type TagMap = M.Map String [PagePath]
38-- | Read a tag map. This creates a map from tags to page paths.
44-- | Read a tag map. This creates a map from tags to page paths.
40-- You also have to give a unique identifier for every tagmap. This is for
46-- You also have to give a unique identifier for every tagmap. This is for
41-- caching reasons, so the tagmap will be stored in
47-- caching reasons, so the tagmap will be stored in
42-- @_cache/_tagmap/identifier@.
48-- @_cache/_tagmap/identifier@.
49+readMap :: (Context -> [String]) -- ^ Function to get tags from a context.
43−readTagMap :: String -- ^ Unique identifier for the tagmap.
50+ -> String -- ^ Unique identifier for the tagmap.
44− -> [FilePath]
51+ -> [PagePath]
45− -> Hakyll (M.Map String [FilePath])
52+ -> Hakyll TagMap
46−readTagMap identifier paths = do
53+readMap getTagsFunction identifier paths = do
47− isCacheMoreRecent' <- isCacheMoreRecent fileName paths
54+ isCacheMoreRecent' <- isCacheMoreRecent fileName (getDependencies =<< paths)
48 if isCacheMoreRecent' then M.fromAscList <$> getFromCache fileName
55 if isCacheMoreRecent' then M.fromAscList <$> getFromCache fileName
49 else do tagMap <- readTagMap'
56 else do tagMap <- readTagMap'
50 storeInCache (M.toAscList tagMap) fileName
57 storeInCache (M.toAscList tagMap) fileName
@@ -54,17 +61,26 @@ readTagMap identifier paths = do
55 readTagMap' = foldM addPaths M.empty paths
62 readTagMap' = foldM addPaths M.empty paths
56 addPaths current path = do
63 addPaths current path = do
57− page <- readPage path
64+ context <- toContext path
58− categoriesEnabled <- askHakyll enableCategories
65+ let tags = getTagsFunction context
59− let tags = map trim $ splitRegex "," $ getValue "tags" page
60− category = [getCategory path | categoriesEnabled]
61 addPaths' = flip (M.insertWith (++)) [path]
66 addPaths' = flip (M.insertWith (++)) [path]
62− return $ foldr addPaths' current (category ++ tags)
67+ return $ foldr addPaths' current tags
72+readTagMap = readMap getTagsFunction
74+ getTagsFunction = map trim . splitRegex ","
75+ . fromMaybe [] . M.lookup "tags"
64− getCategory = last . splitDirectories . takeDirectory
77+readCategoryMap :: String
80+readCategoryMap = readMap $ maybeToList . M.lookup "category"
66-- | Render a tag cloud.
82-- | Render a tag cloud.
67−renderTagCloud :: M.Map String [FilePath] -- ^ Map as produced by @readTagMap@.
83+renderTagCloud :: TagMap -- ^ Map as produced by @readTagMap@.
68 -> (String -> String) -- ^ Function to produce an url for a tag.
84 -> (String -> String) -- ^ Function to produce an url for a tag.
69 -> Float -- ^ Smallest font size, in percent.
85 -> Float -- ^ Smallest font size, in percent.
70 -> Float -- ^ Biggest font size, in percent.
86 -> Float -- ^ Biggest font size, in percent.