on master help sign in
Documentation++.
6c94ad7Jasper Van der Jeugt
src/Text/Hakyll/Context.hs+22−3
@@ -3,6 +3,7 @@ module Text.Hakyll.Context
3 ( Context
3 ( Context
4 , ContextManipulation
4 , ContextManipulation
5 , renderValue
5 , renderValue
6+ , changeValue
6 , renderDate
7 , renderDate
7 , changeExtension
8 , changeExtension
8 ) where
9 ) where
@@ -23,7 +24,9 @@ type Context = Map String String
23-- | Type for context manipulating functions.
24-- | Type for context manipulating functions.
24type ContextManipulation = Context -> Context
25type ContextManipulation = Context -> Context
25
26
26−-- | Do something with a value of a context.
27+-- | Do something with a value in a "Context", but keep the old value as well.
28+-- This is probably the most common function to construct a
29+-- "ContextManipulation".
27renderValue :: String -- ^ Key of which the value should be copied.
30renderValue :: String -- ^ Key of which the value should be copied.
28 -> String -- ^ Key the value should be copied to.
31 -> String -- ^ Key the value should be copied to.
29 -> (String -> String) -- ^ Function to apply on the value.
32 -> (String -> String) -- ^ Function to apply on the value.
@@ -32,8 +35,23 @@ renderValue src dst f context = case M.lookup src context of
32 Nothing -> context
35 Nothing -> context
33 (Just value) -> M.insert dst (f value) context
36 (Just value) -> M.insert dst (f value) context
34
37
38+-- | Change a value in a "Context".
39+--
40+-- > import Data.Char (toUpper)
41+-- > changeValue "title" (map toUpper)
42+--
43+-- Will put the title in UPPERCASE.
44+changeValue :: String -- ^ Key of which the value should be changed.
45+ -> (String -> String) -- ^ Function to apply on the value.
46+ -> ContextManipulation
47+changeValue key = renderValue key key
48+
35-- | When the context has a key called @path@ in a @yyyy-mm-dd-title.extension@
49-- | When the context has a key called @path@ in a @yyyy-mm-dd-title.extension@
36-- format (default for pages), this function can render the date.
50-- format (default for pages), this function can render the date.
51+--
52+-- > renderDate "date" "%B %e, %Y" "Date unknown"
53+--
54+-- Will render something like @January 32, 2010@.
37renderDate :: String -- ^ Key in which the rendered date should be placed.
55renderDate :: String -- ^ Key in which the rendered date should be placed.
38 -> String -- ^ Format to use on the date.
56 -> String -- ^ Format to use on the date.
39 -> String -- ^ Default value when the date cannot be parsed.
57 -> String -- ^ Default value when the date cannot be parsed.
@@ -57,7 +75,8 @@ renderDate key format defaultValue context = M.insert key value context
57-- > (createPagePath "test.markdown")
75-- > (createPagePath "test.markdown")
58--
76--
59-- Will render to @test.php@ instead of @test.html@.
77-- Will render to @test.php@ instead of @test.html@.
78+changeExtension :: String -- ^ Extension to change to.
60−changeExtension :: String -> ContextManipulation
79+ -> ContextManipulation
61−changeExtension extension = renderValue "url" "url" changeExtension'
80+changeExtension extension = changeValue "url" changeExtension'
62 where
81 where
63 changeExtension' = flip addExtension extension . dropExtension
82 changeExtension' = flip addExtension extension . dropExtension
src/Text/Hakyll/Page.hs−1
@@ -14,7 +14,6 @@ import Data.Maybe (fromMaybe)
14import Control.Monad (liftM)
14import Control.Monad (liftM)
15import Control.Monad.Reader (liftIO)
15import Control.Monad.Reader (liftIO)
16import System.FilePath (takeExtension)
16import System.FilePath (takeExtension)
17−import System.IO
18
17
19import Text.Pandoc
18import Text.Pandoc
20import Data.Binary
19import Data.Binary
src/Text/Hakyll/Render/Internal.hs+1−1
@@ -60,7 +60,7 @@ pureRenderWith manipulation template context =
60 in body `deepseq` M.insert "body" body context
60 in body `deepseq` M.insert "body" body context
61
61
62-- | A pure renderAndConcat function.
62-- | A pure renderAndConcat function.
63−pureRenderAndConcatWith :: ContextManipulation
63+pureRenderAndConcatWith :: ContextManipulation -- ^ Manipulation to apply.
64 -> [String] -- ^ Templates to use.
64 -> [String] -- ^ Templates to use.
65 -> [Context] -- ^ Different renderables.
65 -> [Context] -- ^ Different renderables.
66 -> String
66 -> String
src/Text/Hakyll/Tags.hs+28−7
@@ -1,5 +1,19 @@
1-- | Module containing some specialized functions to deal with tags.
1-- | Module containing some specialized functions to deal with tags.
2-- This Module follows certain conventions. Stick with them.
2-- This Module follows certain conventions. Stick with them.
3+--
4+-- More concrete: all functions in this module assume that the tags are
5+-- located in the @tags@ field, and separated by commas. An example file
6+-- @foo.markdown@ could look like:
7+--
8+-- > ---
9+-- > author: Philip K. Dick
10+-- > title: Do androids dream of electric sheep?
11+-- > tags: future, science fiction, humanoid
12+-- > ---
13+-- > The novel is set in a post-apocalyptic near future, where the Earth and
14+-- > its populations have been damaged greatly by Nuclear...
15+--
16+-- All the following functions would work with such a format.
3module Text.Hakyll.Tags
17module Text.Hakyll.Tags
4 ( readTagMap
18 ( readTagMap
5 , renderTagCloud
19 , renderTagCloud
@@ -12,15 +26,13 @@ import Control.Monad (foldM)
12import Control.Arrow (second)
26import Control.Arrow (second)
13
27
14import Text.Hakyll.Hakyll (Hakyll)
28import Text.Hakyll.Hakyll (Hakyll)
15−import Text.Hakyll.Context (ContextManipulation, renderValue)
29+import Text.Hakyll.Context (ContextManipulation, changeValue)
16import Text.Hakyll.Render.Internal (finalSubstitute)
30import Text.Hakyll.Render.Internal (finalSubstitute)
17import Text.Hakyll.Regex
31import Text.Hakyll.Regex
18import Text.Hakyll.Util
32import Text.Hakyll.Util
19import Text.Hakyll.Page
33import Text.Hakyll.Page
20
34
21−-- | Read a tag map. This creates a map from tags to page paths. This function
35+-- | Read a tag map. This creates a map from tags to page paths.
22−-- assumes the tags are located in the @tags@ metadata field, separated by
23−-- commas.
24readTagMap :: [FilePath] -> Hakyll (M.Map String [FilePath])
36readTagMap :: [FilePath] -> Hakyll (M.Map String [FilePath])
25readTagMap paths = foldM addPaths M.empty paths
37readTagMap paths = foldM addPaths M.empty paths
26 where
38 where
@@ -59,10 +71,19 @@ renderTagCloud tagMap urlFunction minSize maxSize =
59 tagCount :: [(String, Float)]
71 tagCount :: [(String, Float)]
60 tagCount = map (second $ fromIntegral . length) $ M.toList tagMap
72 tagCount = map (second $ fromIntegral . length) $ M.toList tagMap
61
73
62−-- Render all tags to links.
74+-- | Render all tags to links.
75+--
76+-- On your side, it is nice if you can display the tags on a page, but
77+-- naturally, most people would expect these are clickable.
78+--
79+-- So, this function takes a function to produce an url for a given tag, and
80+-- applies it on all tags.
81+--
82+-- Note that it is your own responsibility to ensure a page which such an url
83+-- exists.
63−renderTagLinks :: (String -> String) -- ^ Function that produces an url for a tag.
84+renderTagLinks :: (String -> String) -- ^ Function to produce an url for a tag.
64 -> ContextManipulation
85 -> ContextManipulation
65−renderTagLinks urlFunction = renderValue "tags" "tags" renderTagLinks'
86+renderTagLinks urlFunction = changeValue "tags" renderTagLinks'
66 where
87 where
67 renderTagLinks' = intercalate ", "
88 renderTagLinks' = intercalate ", "
68 . map ((\t -> link t $ urlFunction t) . trim)
89 . map ((\t -> link t $ urlFunction t) . trim)
src/Text/Hakyll/Util.hs+3−3
@@ -6,20 +6,20 @@ module Text.Hakyll.Util
6
6
7import Data.Char (isSpace)
7import Data.Char (isSpace)
8
8
9−-- | Trim a string (drop spaces and tabs at both sides).
9+-- | Trim a string (drop spaces, tabs and newlines at both sides).
10trim :: String -> String
10trim :: String -> String
11trim = reverse . trim' . reverse . trim'
11trim = reverse . trim' . reverse . trim'
12 where
12 where
13 trim' = dropWhile isSpace
13 trim' = dropWhile isSpace
14
14
15−-- | Strip html tags.
15+-- | Strip html tags from the given string.
16stripHTML :: String -> String
16stripHTML :: String -> String
17stripHTML [] = []
17stripHTML [] = []
18stripHTML str = let (beforeTag, rest) = break (== '<') str
18stripHTML str = let (beforeTag, rest) = break (== '<') str
19 (_, afterTag) = break (== '>') rest
19 (_, afterTag) = break (== '>') rest
20 in beforeTag ++ stripHTML (tail' afterTag)
20 in beforeTag ++ stripHTML (tail' afterTag)
21− -- We need a failsafe tail function.
22 where
21 where
22+ -- We need a failsafe tail function.
23 tail' [] = []
23 tail' [] = []
24 tail' xs = tail xs
24 tail' xs = tail xs
25
25