diff options
author | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2011-01-05 13:12:50 +0100 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2011-01-05 13:12:50 +0100 |
commit | 664648c5f9693fa5160a5c67aeabe8a9d38df03d (patch) | |
tree | 73421cfdf6d3042988e21ff0520be6cd81203f9c /src/Hakyll/Web/Tags.hs | |
parent | 77c7d8dc17a86640180b9b233f6e0fd9008c6848 (diff) | |
download | hakyll-664648c5f9693fa5160a5c67aeabe8a9d38df03d.tar.gz |
Proof-of-concept tag module
Diffstat (limited to 'src/Hakyll/Web/Tags.hs')
-rw-r--r-- | src/Hakyll/Web/Tags.hs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/Hakyll/Web/Tags.hs b/src/Hakyll/Web/Tags.hs new file mode 100644 index 0000000..4986a31 --- /dev/null +++ b/src/Hakyll/Web/Tags.hs @@ -0,0 +1,42 @@ +module Hakyll.Web.Tags + ( Tags (..) + , readTagsWith + , readTags + , readCategories + ) where + +import Data.Map (Map) +import qualified Data.Map as M + +import Hakyll.Web.Page +import Hakyll.Web.Util.String + +-- | Data about tags +-- +data Tags a = Tags + { tagsMap :: Map String [Page a] + } deriving (Show) + +-- | Higher-level function to read tags +-- +readTagsWith :: (Page a -> [String]) -- ^ Function extracting tags from a page + -> [Page a] -- ^ Pages + -> Tags a -- ^ Resulting tags +readTagsWith f pages = Tags + { tagsMap = foldl (M.unionWith (++)) M.empty (map readTagsWith' pages) + } + where + -- Create a tag map for one page + readTagsWith' page = + let tags = f page + in M.fromList $ zip tags $ repeat [page] + +-- | Read a tagmap using the @tags@ metadata field +-- +readTags :: [Page a] -> Tags a +readTags = readTagsWith $ map trim . splitAll "," . getField "tags" + +-- | Read a tagmap using the @category@ metadata field +-- +readCategories :: [Page a] -> Tags a +readCategories = readTagsWith $ return . getField "category" |