on master help sign in
Added indexUrl option - experimental.
c4b1fd5Jasper Van der Jeugt
src/Text/Hakyll.hs+1
@@ -28,6 +28,7 @@ defaultHakyllConfiguration = HakyllConfiguration
28 { additionalContext = M.empty
28 { additionalContext = M.empty
29 , siteDirectory = "_site"
29 , siteDirectory = "_site"
30 , cacheDirectory = "_cache"
30 , cacheDirectory = "_cache"
31+ , enableIndexUrl = False
31 }
32 }
32
33
33-- | Main function to run Hakyll with the default configuration.
34-- | Main function to run Hakyll with the default configuration.
src/Text/Hakyll/File.hs+37−17
@@ -36,8 +36,15 @@ removeLeadingSeparator path
36-- | Convert a relative filepath to a filepath in the destination
36-- | Convert a relative filepath to a filepath in the destination
37-- (default: @_site@).
37-- (default: @_site@).
38toDestination :: FilePath -> Hakyll FilePath
38toDestination :: FilePath -> Hakyll FilePath
39−toDestination path = do dir <- askHakyll siteDirectory
39+toDestination url = do dir <- askHakyll siteDirectory
40+ enableIndexUrl' <- askHakyll enableIndexUrl
41+ let destination = if enableIndexUrl' && separatorEnd
42+ then dir </> noSeparator </> "index.html"
43+ else dir </> noSeparator
44+ return destination
45+ where
40− return $ dir </> removeLeadingSeparator path
46+ noSeparator = removeLeadingSeparator url
47+ separatorEnd = not (null url) && last url == '/'
41
48
42-- | Convert a relative filepath to a filepath in the cache
49-- | Convert a relative filepath to a filepath in the cache
43-- (default: @_cache@).
50-- (default: @_cache@).
@@ -48,21 +55,34 @@ toCache path = do dir <- askHakyll cacheDirectory
48-- | Get the url for a given page. For most extensions, this would be the path
55-- | Get the url for a given page. For most extensions, this would be the path
49-- itself. It's only for rendered extensions (@.markdown@, @.rst@, @.lhs@ this
56-- itself. It's only for rendered extensions (@.markdown@, @.rst@, @.lhs@ this
50-- function returns a path with a @.html@ extension instead.
57-- function returns a path with a @.html@ extension instead.
51−toUrl :: FilePath -> FilePath
58+toUrl :: FilePath -> Hakyll FilePath
59+toUrl path = do enableIndexUrl' <- askHakyll enableIndexUrl
60+ -- If the file does not have a renderable extension, like for
61+ -- example favicon.ico, we don't have to change it at all.
62+ return $ if not hasRenderableExtension
63+ then path
64+ -- If index url's are enabled, we create pick it
65+ -- unless the page is an index already.
66+ else if enableIndexUrl' && not isIndex
67+ then indexUrl
68+ else withSimpleHtmlExtension
69+ where
52−toUrl path = if takeExtension path `elem` [ ".markdown"
70+ hasRenderableExtension = takeExtension path `elem` [ ".markdown"
53− , ".md"
71+ , ".md"
54− , ".mdn"
72+ , ".mdn"
55− , ".mdwn"
73+ , ".mdwn"
56− , ".mkd"
74+ , ".mkd"
57− , ".mkdn"
75+ , ".mkdn"
58− , ".mkdwn"
76+ , ".mkdwn"
59− , ".rst"
77+ , ".rst"
60− , ".text"
78+ , ".text"
61− , ".tex"
79+ , ".tex"
62− , ".lhs"
80+ , ".lhs"
63− ]
81+ ]
82+ isIndex = (dropExtension $ takeFileName path) == "index"
64− then flip addExtension ".html" $ dropExtension path
83+ withSimpleHtmlExtension = flip addExtension ".html" $ dropExtension path
65− else path
84+ indexUrl = (dropExtension path) ++ "/"
85+
66
86
67-- | Get the relative url to the site root, for a given (absolute) url
87-- | Get the relative url to the site root, for a given (absolute) url
68toRoot :: FilePath -> FilePath
88toRoot :: FilePath -> FilePath
src/Text/Hakyll/Hakyll.hs+2
@@ -19,6 +19,8 @@ data HakyllConfiguration = HakyllConfiguration
19 siteDirectory :: FilePath
19 siteDirectory :: FilePath
20 , -- | Directory for cache files.
20 , -- | Directory for cache files.
21 cacheDirectory :: FilePath
21 cacheDirectory :: FilePath
22+ , -- | Enable index links.
23+ enableIndexUrl :: Bool
22 }
24 }
23
25
24-- | Our custom monad stack.
26-- | Our custom monad stack.
src/Text/Hakyll/Internal/Render.hs+1−2
@@ -58,11 +58,10 @@ pureRenderChainWith manipulation templates context =
58writePage :: Page -> Hakyll ()
58writePage :: Page -> Hakyll ()
59writePage page = do
59writePage page = do
60 additionalContext' <- askHakyll additionalContext
60 additionalContext' <- askHakyll additionalContext
61+ url <- getUrl page
61 destination <- toDestination url
62 destination <- toDestination url
62 let context = additionalContext' `M.union` M.singleton "root" (toRoot url)
63 let context = additionalContext' `M.union` M.singleton "root" (toRoot url)
63 makeDirectories destination
64 makeDirectories destination
64    -- Substitute $root here, just before writing.
65    -- Substitute $root here, just before writing.
65 liftIO $ writeFile destination $ finalSubstitute (fromString $ getBody page)
66 liftIO $ writeFile destination $ finalSubstitute (fromString $ getBody page)
66 context
67 context
67− where
68− url = getUrl page
src/Text/Hakyll/Page.hs+3−3
@@ -30,7 +30,7 @@ import Text.Hakyll.Regex (substituteRegex, matchesRegex)
30-- | A Page is basically key-value mapping. Certain keys have special
30-- | A Page is basically key-value mapping. Certain keys have special
31-- meanings, like for example url, body and title.
31-- meanings, like for example url, body and title.
32data Page = Page Context
32data Page = Page Context
33− deriving (Show, Read, Eq)
33+ deriving (Ord, Eq, Show, Read)
34
34
35-- | Create a Page from a key-value mapping.
35-- | Create a Page from a key-value mapping.
36fromContext :: Context -> Page
36fromContext :: Context -> Page
@@ -124,6 +124,7 @@ readPageFromFile path = do
124
124
125 -- Read file.
125 -- Read file.
126 contents <- liftIO $ readFile path
126 contents <- liftIO $ readFile path
127+ url <- toUrl path
127 let sections = splitAtDelimiters $ lines contents
128 let sections = splitAtDelimiters $ lines contents
128 context = concat $ zipWith ($) sectionFunctions sections
129 context = concat $ zipWith ($) sectionFunctions sections
129 page = fromContext $ M.fromList $
130 page = fromContext $ M.fromList $
@@ -134,7 +135,6 @@ readPageFromFile path = do
134
135
135 return page
136 return page
136 where
137 where
137− url = toUrl path
138 category = let dirs = splitDirectories $ takeDirectory path
138 category = let dirs = splitDirectories $ takeDirectory path
139 in [("category", last dirs) | not (null dirs)]
139 in [("category", last dirs) | not (null dirs)]
140
140
@@ -153,7 +153,7 @@ readPage path = do
153-- Make pages renderable.
153-- Make pages renderable.
154instance Renderable Page where
154instance Renderable Page where
155 getDependencies = (:[]) . getPagePath
155 getDependencies = (:[]) . getPagePath
156− getUrl = getPageUrl
156+ getUrl = return . getPageUrl
157 toContext (Page page) = return page
157 toContext (Page page) = return page
158
158
159-- Make pages serializable.
159-- Make pages serializable.
src/Text/Hakyll/Render.hs+3−2
@@ -100,8 +100,9 @@ renderChain = renderChainWith id
100-- @ContextManipulation@ which to apply on the context when it is read first.
100-- @ContextManipulation@ which to apply on the context when it is read first.
101renderChainWith :: Renderable a
101renderChainWith :: Renderable a
102 => ContextManipulation -> [FilePath] -> a -> Hakyll ()
102 => ContextManipulation -> [FilePath] -> a -> Hakyll ()
103−renderChainWith manipulation templatePaths renderable =
103+renderChainWith manipulation templatePaths renderable = do
104+ url <- getUrl renderable
104− depends (getUrl renderable) dependencies render'
105+ depends url dependencies render'
105 where
106 where
106 dependencies = getDependencies renderable ++ templatePaths
107 dependencies = getDependencies renderable ++ templatePaths
107 render' = do
108 render' = do
src/Text/Hakyll/Renderable.hs+1−1
@@ -15,4 +15,4 @@ class Renderable a where
15 getDependencies :: a -> [FilePath]
15 getDependencies :: a -> [FilePath]
16
16
17 -- | Get the destination for the renderable.
17 -- | Get the destination for the renderable.
18− getUrl :: a -> FilePath
18+ getUrl :: a -> Hakyll FilePath
src/Text/Hakyll/Renderables.hs+4−2
@@ -85,7 +85,7 @@ createListingWith manipulation url template renderables additional =
85
85
86instance Renderable CustomPage where
86instance Renderable CustomPage where
87 getDependencies = customPageDependencies
87 getDependencies = customPageDependencies
88− getUrl = customPageUrl
88+ getUrl = return . customPageUrl
89 toContext page = do
89 toContext page = do
90 values <- mapM (either return id . snd) (customPageContext page)
90 values <- mapM (either return id . snd) (customPageContext page)
91 let pairs = zip (map fst $ customPageContext page) values
91 let pairs = zip (map fst $ customPageContext page) values
@@ -94,6 +94,7 @@ instance Renderable CustomPage where
94-- | PagePath is a class that wraps a FilePath. This is used to render Pages
94-- | PagePath is a class that wraps a FilePath. This is used to render Pages
95-- without reading them first through use of caching.
95-- without reading them first through use of caching.
96data PagePath = PagePath FilePath
96data PagePath = PagePath FilePath
97+ deriving (Ord, Eq, Read, Show)
97
98
98-- | Create a PagePath from a FilePath.
99-- | Create a PagePath from a FilePath.
99createPagePath :: FilePath -> PagePath
100createPagePath :: FilePath -> PagePath
@@ -113,6 +114,7 @@ instance Binary PagePath where
113-- | A combination of two other renderables.
114-- | A combination of two other renderables.
114data CombinedRenderable a b = CombinedRenderable a b
115data CombinedRenderable a b = CombinedRenderable a b
115 | CombinedRenderableWithUrl FilePath a b
116 | CombinedRenderableWithUrl FilePath a b
117+ deriving (Ord, Eq, Read, Show)
116
118
117-- | Combine two renderables. The url will always be taken from the first
119-- | Combine two renderables. The url will always be taken from the first
118-- @Renderable@. Also, if a `$key` is present in both renderables, the
120-- @Renderable@. Also, if a `$key` is present in both renderables, the
@@ -144,7 +146,7 @@ instance (Renderable a, Renderable b)
144
146
145 -- Take the url from the first renderable, or the specified URL.
147 -- Take the url from the first renderable, or the specified URL.
146 getUrl (CombinedRenderable a _) = getUrl a
148 getUrl (CombinedRenderable a _) = getUrl a
147− getUrl (CombinedRenderableWithUrl url _ _) = url
149+ getUrl (CombinedRenderableWithUrl url _ _) = return url
148
150
149 -- Take a union of the contexts.
151 -- Take a union of the contexts.
150 toContext (CombinedRenderable a b) = do
152 toContext (CombinedRenderable a b) = do