on master help sign in
Worked on substitution.
6a2e699Jasper Van der Jeugt
src/Text/Hakyll/Context.hs−7
@@ -3,7 +3,6 @@ module Text.Hakyll.Context
3 ( ContextManipulation
3 ( ContextManipulation
4 , renderValue
4 , renderValue
5 , renderDate
5 , renderDate
6− , ignoreKeys
7 ) where
6 ) where
8
7
9import qualified Data.Map as M
8import qualified Data.Map as M
@@ -45,9 +44,3 @@ renderDate key format defaultValue context =
45 "%Y-%m-%d"
44 "%Y-%m-%d"
46 dateString :: Maybe UTCTime
45 dateString :: Maybe UTCTime
47 return $ formatTime defaultTimeLocale format time
46 return $ formatTime defaultTimeLocale format time
48−
49−-- | Ignore a number of keys during the render phase.
50−ignoreKeys :: [String] -> ContextManipulation
51−ignoreKeys keyList = M.union (M.fromList pairs)
52− where pairs = map pair keyList
53− pair key = (B.pack $ '$' : key, B.pack $ '$' : key)
src/Text/Hakyll/File.hs+3−1
@@ -38,9 +38,11 @@ toURL = flip addExtension ".html" . dropExtension
38
38
39-- | Get the relative url to the site root, for a given (absolute) url
39-- | Get the relative url to the site root, for a given (absolute) url
40toRoot :: FilePath -> FilePath
40toRoot :: FilePath -> FilePath
41−toRoot = joinPath . map parent . splitPath
41+toRoot = emptyException . joinPath . map parent . splitPath
42 . takeDirectory . removeLeadingSeparator
42 . takeDirectory . removeLeadingSeparator
43 where parent = const ".."
43 where parent = const ".."
44+ emptyException [] = "."
45+ emptyException x = x
44
46
45-- | Swaps spaces for '-'.
47-- | Swaps spaces for '-'.
46removeSpaces :: FilePath -> FilePath
48removeSpaces :: FilePath -> FilePath
src/Text/Hakyll/Page.hs−9
@@ -4,7 +4,6 @@ module Text.Hakyll.Page
4 , getValue
4 , getValue
5 , getBody
5 , getBody
6 , readPage
6 , readPage
7− , writePage
8 ) where
7 ) where
9
8
10import qualified Data.Map as M
9import qualified Data.Map as M
@@ -121,7 +120,6 @@ readPage pagePath = do
121 [ (B.pack "body", rendered)
120 [ (B.pack "body", rendered)
122 , packPair ("url", url)
121 , packPair ("url", url)
123 , packPair ("path", pagePath)
122 , packPair ("path", pagePath)
124− , packPair ("root", toRoot url)
125 ] ++ map packPair context
123 ] ++ map packPair context
126
124
127 -- Cache if needed
125 -- Cache if needed
@@ -130,13 +128,6 @@ readPage pagePath = do
130 where url = toURL pagePath
128 where url = toURL pagePath
131 cacheFile = toCache url
129 cacheFile = toCache url
132
130
133−-- | Write a page to the site destination.
134−writePage :: Page -> IO ()
135−writePage page = do
136− let destination = toDestination $ getURL page
137− makeDirectories destination
138− B.writeFile destination (getBody page)
139−
140-- Make pages renderable.
131-- Make pages renderable.
141instance Renderable Page where
132instance Renderable Page where
142 getDependencies = (:[]) . getPagePath
133 getDependencies = (:[]) . getPagePath
src/Text/Hakyll/Render.hs+15−1
@@ -52,7 +52,10 @@ renderWith manipulation templatePath renderable = do
52 templateString <- liftM B.pack $ hGetContents handle
52 templateString <- liftM B.pack $ hGetContents handle
53 seq templateString $ hClose handle
53 seq templateString $ hClose handle
54 context <- liftM manipulation $ toContext renderable
54 context <- liftM manipulation $ toContext renderable
55+ -- Ignore $root when substituting here. We will only replace that in the
56+ -- final render (just before writing).
57+ let contextIgnoringRoot = M.insert (B.pack "root") (B.pack "$root") context
55− let body = substitute templateString context
58+ body = substitute templateString contextIgnoringRoot
56 return $ fromContext (M.insert (B.pack "body") body context)
59 return $ fromContext (M.insert (B.pack "body") body context)
57
60
58-- | Render each renderable with the given template, then concatenate the
61-- | Render each renderable with the given template, then concatenate the
@@ -92,6 +95,17 @@ renderChainWith manipulation templates renderable =
92 result <- foldM (flip render) (fromContext initialPage) templates
95 result <- foldM (flip render) (fromContext initialPage) templates
93 writePage result
96 writePage result
94
97
98+-- | Write a page to the site destination.
99+writePage :: Page -> IO ()
100+writePage page = do
101+ let destination = toDestination url
102+ makeDirectories destination
103+ B.writeFile destination body
104+ where url = getURL page
105+ -- Substitute $root here, just before writing.
106+ body = substitute (getBody page)
107+ (M.singleton (B.pack "root") (B.pack $ toRoot url))
108+
95-- | Mark a certain file as static, so it will just be copied when the site is
109-- | Mark a certain file as static, so it will just be copied when the site is
96-- generated.
110-- generated.
97static :: FilePath -> IO ()
111static :: FilePath -> IO ()
src/Text/Hakyll/Renderables.hs+2−1
@@ -32,7 +32,8 @@ instance Renderable CustomPage where
32 toContext page = do
32 toContext page = do
33 values <- mapM (either (return . B.pack) (>>= return) . snd) (mapping page)
33 values <- mapM (either (return . B.pack) (>>= return) . snd) (mapping page)
34 let keys = map (B.pack . fst) (mapping page)
34 let keys = map (B.pack . fst) (mapping page)
35− return $ M.fromList $ (B.pack "url", B.pack $ url page) : zip keys values
35+ return $ M.fromList $ [ (B.pack "url", B.pack $ url page)
36+ ] ++ zip keys values
36
37
37-- | PagePath is a class that wraps a FilePath. This is used to render Pages
38-- | PagePath is a class that wraps a FilePath. This is used to render Pages
38-- without reading them first through use of caching.
39-- without reading them first through use of caching.
tests/Tests.hs+1−1
@@ -99,7 +99,7 @@ test_render_date2 = M.lookup (B.pack "date") rendered @?= Just (B.pack "Unknown
99-- toRoot test cases
99-- toRoot test cases
100test_to_root1 = toRoot "/posts/foo.html" @?= ".."
100test_to_root1 = toRoot "/posts/foo.html" @?= ".."
101test_to_root2 = toRoot "posts/foo.html" @?= ".."
101test_to_root2 = toRoot "posts/foo.html" @?= ".."
102−test_to_root3 = toRoot "foo.html" @?= ""
102+test_to_root3 = toRoot "foo.html" @?= "."
103
103
104-- Add an extension, and test that they have that extension
104-- Add an extension, and test that they have that extension
105prop_having_extension_count names extension =
105prop_having_extension_count names extension =