summaryrefslogtreecommitdiff
path: root/src/Text/Hakyll
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2009-12-26 13:47:40 +0100
committerJasper Van der Jeugt <jaspervdj@gmail.com>2009-12-26 13:47:40 +0100
commit2648115e87f9ad87571b4199cc3b642b0c8079c7 (patch)
treecc56e128066f3edf9575205200c48ca4c448ac2d /src/Text/Hakyll
parentbeeac9b1011dd57bcc12f316bf9483faa5c38744 (diff)
downloadhakyll-2648115e87f9ad87571b4199cc3b642b0c8079c7.tar.gz
Added some tag cloud code.
Diffstat (limited to 'src/Text/Hakyll')
-rw-r--r--src/Text/Hakyll/Tags.hs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/Text/Hakyll/Tags.hs b/src/Text/Hakyll/Tags.hs
index 98fda86..ff373eb 100644
--- a/src/Text/Hakyll/Tags.hs
+++ b/src/Text/Hakyll/Tags.hs
@@ -1,14 +1,17 @@
-- | Module containing some specialized functions to deal with tags.
module Text.Hakyll.Tags
( readTagMap
+ , renderTagCloud
) where
import qualified Data.Map as M
import qualified Data.ByteString.Lazy.Char8 as B
+import qualified Data.List as L
import Control.Monad
import Text.Hakyll.Util
import Text.Hakyll.Page
+import Control.Arrow
-- | Read a tag map. This creates a map from tags to page paths. This function
-- assumes the tags are located in the `tags` metadata field, separated by
@@ -19,3 +22,30 @@ readTagMap paths = foldM addPaths M.empty paths
page <- readPage path
let tags = map trim $ split "," $ B.unpack $ getValue ("tags") page
return $ foldr (\t -> M.insertWith (++) t [path]) current tags
+
+-- | Render a tag cloud.
+renderTagCloud :: M.Map String [FilePath] -- ^ A tag map as produced by 'readTagMap'.
+ -> (String -> String) -- ^ Function that produces an url for a tag.
+ -> Float -- ^ Smallest font size, in percent.
+ -> Float -- ^ Biggest font size, in percent.
+ -> String -- ^ Result of the render.
+renderTagCloud tagMap urlFunction minSize maxSize =
+ L.intercalate " " $ map renderTag tagCount
+ where renderTag :: (String, Float) -> String
+ renderTag (tag, count) = "<a style=\"font-size: "
+ ++ sizeTag count ++ "\" href=\""
+ ++ urlFunction tag ++ "\">"
+ ++ tag ++ "</a>"
+
+ sizeTag :: Float -> String
+ sizeTag count = show size' ++ "%"
+ where size' :: Int
+ size' = floor (minSize + (relative count) * (maxSize - minSize))
+
+ minCount = minimum $ map snd $ tagCount
+ maxCount = maximum $ map snd $ tagCount
+ relative count = (count - minCount) / (maxCount - minCount)
+
+ tagCount :: [(String, Float)]
+ tagCount = map (second $ fromIntegral . length) $ M.toList tagMap
+