on master help sign in
Documentation++.
commit6c94ad79926d9808ad1817d06c7e9aa69679318f authorJasper Van der Jeugt <jaspervdj@gmail.com> dateSat 23 Jan 2010, 18:20 (17 years ago) parent5f8ea06
5 files changed+54−15
src/Text/Hakyll/Context.hs+22−3 src/Text/Hakyll/Page.hs−1 src/Text/Hakyll/Render/Internal.hs+1−1 src/Text/Hakyll/Tags.hs+28−7 src/Text/Hakyll/Util.hs+3−3
src/Text/Hakyll/Context.hs+22−3
@@ -3,6 +3,7 @@ module Text.Hakyll.Context
33 ( Context
44 , ContextManipulation
55 , renderValue
6+ , changeValue
67 , renderDate
78 , changeExtension
89 ) where
@@ -23,7 +24,9 @@ type Context = Map String String
2324-- | Type for context manipulating functions.
2425type ContextManipulation = Context -> Context
2526
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".
2730renderValue :: String -- ^ Key of which the value should be copied.
2831 -> String -- ^ Key the value should be copied to.
2932 -> (String -> String) -- ^ Function to apply on the value.
@@ -32,8 +35,23 @@ renderValue src dst f context = case M.lookup src context of
3235 Nothing -> context
3336 (Just value) -> M.insert dst (f value) context
3437
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+
3549-- | When the context has a key called @path@ in a @yyyy-mm-dd-title.extension@
3650-- 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@.
3755renderDate :: String -- ^ Key in which the rendered date should be placed.
3856 -> String -- ^ Format to use on the date.
3957 -> String -- ^ Default value when the date cannot be parsed.
@@ -57,7 +75,8 @@ renderDate key format defaultValue context = M.insert key value context
5775-- > (createPagePath "test.markdown")
5876--
5977-- Will render to @test.php@ instead of @test.html@.
60−changeExtension :: String -> ContextManipulation
61−changeExtension extension = renderValue "url" "url" changeExtension'
78+changeExtension :: String -- ^ Extension to change to.
79+ -> ContextManipulation
80+changeExtension extension = changeValue "url" changeExtension'
6281 where
6382 changeExtension' = flip addExtension extension . dropExtension
src/Text/Hakyll/Page.hs−1
@@ -14,7 +14,6 @@ import Data.Maybe (fromMaybe)
1414import Control.Monad (liftM)
1515import Control.Monad.Reader (liftIO)
1616import System.FilePath (takeExtension)
17−import System.IO
1817
1918import Text.Pandoc
2019import Data.Binary
src/Text/Hakyll/Render/Internal.hs+1−1
@@ -60,7 +60,7 @@ pureRenderWith manipulation template context =
6060 in body `deepseq` M.insert "body" body context
6161
6262-- | A pure renderAndConcat function.
63−pureRenderAndConcatWith :: ContextManipulation
63+pureRenderAndConcatWith :: ContextManipulation -- ^ Manipulation to apply.
6464 -> [String] -- ^ Templates to use.
6565 -> [Context] -- ^ Different renderables.
6666 -> String
src/Text/Hakyll/Tags.hs+28−7
@@ -1,5 +1,19 @@
11-- | Module containing some specialized functions to deal with tags.
22-- 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.
317module Text.Hakyll.Tags
418 ( readTagMap
519 , renderTagCloud
@@ -12,15 +26,13 @@ import Control.Monad (foldM)
1226import Control.Arrow (second)
1327
1428import Text.Hakyll.Hakyll (Hakyll)
15−import Text.Hakyll.Context (ContextManipulation, renderValue)
29+import Text.Hakyll.Context (ContextManipulation, changeValue)
1630import Text.Hakyll.Render.Internal (finalSubstitute)
1731import Text.Hakyll.Regex
1832import Text.Hakyll.Util
1933import Text.Hakyll.Page
2034
21−-- | Read a tag map. This creates a map from tags to page paths. This function
22−-- assumes the tags are located in the @tags@ metadata field, separated by
23−-- commas.
35+-- | Read a tag map. This creates a map from tags to page paths.
2436readTagMap :: [FilePath] -> Hakyll (M.Map String [FilePath])
2537readTagMap paths = foldM addPaths M.empty paths
2638 where
@@ -59,10 +71,19 @@ renderTagCloud tagMap urlFunction minSize maxSize =
5971 tagCount :: [(String, Float)]
6072 tagCount = map (second $ fromIntegral . length) $ M.toList tagMap
6173
62−-- Render all tags to links.
63−renderTagLinks :: (String -> String) -- ^ Function that produces an url for a tag.
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.
84+renderTagLinks :: (String -> String) -- ^ Function to produce an url for a tag.
6485 -> ContextManipulation
65−renderTagLinks urlFunction = renderValue "tags" "tags" renderTagLinks'
86+renderTagLinks urlFunction = changeValue "tags" renderTagLinks'
6687 where
6788 renderTagLinks' = intercalate ", "
6889 . map ((\t -> link t $ urlFunction t) . trim)
src/Text/Hakyll/Util.hs+3−3
@@ -6,20 +6,20 @@ module Text.Hakyll.Util
66
77import Data.Char (isSpace)
88
9−-- | Trim a string (drop spaces and tabs at both sides).
9+-- | Trim a string (drop spaces, tabs and newlines at both sides).
1010trim :: String -> String
1111trim = reverse . trim' . reverse . trim'
1212 where
1313 trim' = dropWhile isSpace
1414
15−-- | Strip html tags.
15+-- | Strip html tags from the given string.
1616stripHTML :: String -> String
1717stripHTML [] = []
1818stripHTML str = let (beforeTag, rest) = break (== '<') str
1919 (_, afterTag) = break (== '>') rest
2020 in beforeTag ++ stripHTML (tail' afterTag)
21− -- We need a failsafe tail function.
2221 where
22+ -- We need a failsafe tail function.
2323 tail' [] = []
2424 tail' xs = tail xs
2525