on master help sign in
Worked on substitution.
commit6a2e6998b125024e9260d26819c262e0e7d22c8d authorJasper Van der Jeugt <jaspervdj@gmail.com> dateSun 10 Jan 2010, 13:26 (17 years ago) parentbc71d54
6 files changed+21−20
src/Text/Hakyll/Context.hs−7 src/Text/Hakyll/File.hs+3−1 src/Text/Hakyll/Page.hs−9 src/Text/Hakyll/Render.hs+15−1 src/Text/Hakyll/Renderables.hs+2−1 tests/Tests.hs+1−1
src/Text/Hakyll/Context.hs−7
@@ -3,7 +3,6 @@ module Text.Hakyll.Context
33 ( ContextManipulation
44 , renderValue
55 , renderDate
6− , ignoreKeys
76 ) where
87
98import qualified Data.Map as M
@@ -45,9 +44,3 @@ renderDate key format defaultValue context =
4544 "%Y-%m-%d"
4645 dateString :: Maybe UTCTime
4746 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
3838
3939-- | Get the relative url to the site root, for a given (absolute) url
4040toRoot :: FilePath -> FilePath
41−toRoot = joinPath . map parent . splitPath
41+toRoot = emptyException . joinPath . map parent . splitPath
4242 . takeDirectory . removeLeadingSeparator
4343 where parent = const ".."
44+ emptyException [] = "."
45+ emptyException x = x
4446
4547-- | Swaps spaces for '-'.
4648removeSpaces :: FilePath -> FilePath
src/Text/Hakyll/Page.hs−9
@@ -4,7 +4,6 @@ module Text.Hakyll.Page
44 , getValue
55 , getBody
66 , readPage
7− , writePage
87 ) where
98
109import qualified Data.Map as M
@@ -121,7 +120,6 @@ readPage pagePath = do
121120 [ (B.pack "body", rendered)
122121 , packPair ("url", url)
123122 , packPair ("path", pagePath)
124− , packPair ("root", toRoot url)
125123 ] ++ map packPair context
126124
127125 -- Cache if needed
@@ -130,13 +128,6 @@ readPage pagePath = do
130128 where url = toURL pagePath
131129 cacheFile = toCache url
132130
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−
140131-- Make pages renderable.
141132instance Renderable Page where
142133 getDependencies = (:[]) . getPagePath
src/Text/Hakyll/Render.hs+15−1
@@ -52,7 +52,10 @@ renderWith manipulation templatePath renderable = do
5252 templateString <- liftM B.pack $ hGetContents handle
5353 seq templateString $ hClose handle
5454 context <- liftM manipulation $ toContext renderable
55− let body = substitute templateString context
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
58+ body = substitute templateString contextIgnoringRoot
5659 return $ fromContext (M.insert (B.pack "body") body context)
5760
5861-- | Render each renderable with the given template, then concatenate the
@@ -92,6 +95,17 @@ renderChainWith manipulation templates renderable =
9295 result <- foldM (flip render) (fromContext initialPage) templates
9396 writePage result
9497
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+
95109-- | Mark a certain file as static, so it will just be copied when the site is
96110-- generated.
97111static :: FilePath -> IO ()
src/Text/Hakyll/Renderables.hs+2−1
@@ -32,7 +32,8 @@ instance Renderable CustomPage where
3232 toContext page = do
3333 values <- mapM (either (return . B.pack) (>>= return) . snd) (mapping page)
3434 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
3637
3738-- | PagePath is a class that wraps a FilePath. This is used to render Pages
3839-- 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
9999-- toRoot test cases
100100test_to_root1 = toRoot "/posts/foo.html" @?= ".."
101101test_to_root2 = toRoot "posts/foo.html" @?= ".."
102−test_to_root3 = toRoot "foo.html" @?= ""
102+test_to_root3 = toRoot "foo.html" @?= "."
103103
104104-- Add an extension, and test that they have that extension
105105prop_having_extension_count names extension =