on master help sign in
Added indexUrl option - experimental.

This option will render, for example, about.html to about/index.html. This way, url's will generally look more pretty.

commitc4b1fd5900bdff5c94891b7b1c9e764653d572a3 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateMon 1 Feb 2010, 16:43 (17 years ago) parentfbd9ff9
8 files changed+52−27
src/Text/Hakyll.hs+1 src/Text/Hakyll/File.hs+37−17 src/Text/Hakyll/Hakyll.hs+2 src/Text/Hakyll/Internal/Render.hs+1−2 src/Text/Hakyll/Page.hs+3−3 src/Text/Hakyll/Render.hs+3−2 src/Text/Hakyll/Renderable.hs+1−1 src/Text/Hakyll/Renderables.hs+4−2
src/Text/Hakyll.hs+1
@@ -28,6 +28,7 @@ defaultHakyllConfiguration = HakyllConfiguration
2828 { additionalContext = M.empty
2929 , siteDirectory = "_site"
3030 , cacheDirectory = "_cache"
31+ , enableIndexUrl = False
3132 }
3233
3334-- | Main function to run Hakyll with the default configuration.
src/Text/Hakyll/File.hs+37−17
@@ -36,8 +36,15 @@ removeLeadingSeparator path
3636-- | Convert a relative filepath to a filepath in the destination
3737-- (default: @_site@).
3838toDestination :: FilePath -> Hakyll FilePath
39−toDestination path = do dir <- askHakyll siteDirectory
40− return $ dir </> removeLeadingSeparator path
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
46+ noSeparator = removeLeadingSeparator url
47+ separatorEnd = not (null url) && last url == '/'
4148
4249-- | Convert a relative filepath to a filepath in the cache
4350-- (default: @_cache@).
@@ -48,21 +55,34 @@ toCache path = do dir <- askHakyll cacheDirectory
4855-- | Get the url for a given page. For most extensions, this would be the path
4956-- itself. It's only for rendered extensions (@.markdown@, @.rst@, @.lhs@ this
5057-- function returns a path with a @.html@ extension instead.
51−toUrl :: FilePath -> FilePath
52−toUrl path = if takeExtension path `elem` [ ".markdown"
53− , ".md"
54− , ".mdn"
55− , ".mdwn"
56− , ".mkd"
57− , ".mkdn"
58− , ".mkdwn"
59− , ".rst"
60− , ".text"
61− , ".tex"
62− , ".lhs"
63− ]
64− then flip addExtension ".html" $ dropExtension path
65− else path
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
70+ hasRenderableExtension = takeExtension path `elem` [ ".markdown"
71+ , ".md"
72+ , ".mdn"
73+ , ".mdwn"
74+ , ".mkd"
75+ , ".mkdn"
76+ , ".mkdwn"
77+ , ".rst"
78+ , ".text"
79+ , ".tex"
80+ , ".lhs"
81+ ]
82+ isIndex = (dropExtension $ takeFileName path) == "index"
83+ withSimpleHtmlExtension = flip addExtension ".html" $ dropExtension path
84+ indexUrl = (dropExtension path) ++ "/"
85+
6686
6787-- | Get the relative url to the site root, for a given (absolute) url
6888toRoot :: FilePath -> FilePath
src/Text/Hakyll/Hakyll.hs+2
@@ -19,6 +19,8 @@ data HakyllConfiguration = HakyllConfiguration
1919 siteDirectory :: FilePath
2020 , -- | Directory for cache files.
2121 cacheDirectory :: FilePath
22+ , -- | Enable index links.
23+ enableIndexUrl :: Bool
2224 }
2325
2426-- | Our custom monad stack.
src/Text/Hakyll/Internal/Render.hs+1−2
@@ -58,11 +58,10 @@ pureRenderChainWith manipulation templates context =
5858writePage :: Page -> Hakyll ()
5959writePage page = do
6060 additionalContext' <- askHakyll additionalContext
61+ url <- getUrl page
6162 destination <- toDestination url
6263 let context = additionalContext' `M.union` M.singleton "root" (toRoot url)
6364 makeDirectories destination
6465    -- Substitute $root here, just before writing.
6566 liftIO $ writeFile destination $ finalSubstitute (fromString $ getBody page)
6667 context
67− where
68− url = getUrl page
src/Text/Hakyll/Page.hs+3−3
@@ -30,7 +30,7 @@ import Text.Hakyll.Regex (substituteRegex, matchesRegex)
3030-- | A Page is basically key-value mapping. Certain keys have special
3131-- meanings, like for example url, body and title.
3232data Page = Page Context
33− deriving (Show, Read, Eq)
33+ deriving (Ord, Eq, Show, Read)
3434
3535-- | Create a Page from a key-value mapping.
3636fromContext :: Context -> Page
@@ -124,6 +124,7 @@ readPageFromFile path = do
124124
125125 -- Read file.
126126 contents <- liftIO $ readFile path
127+ url <- toUrl path
127128 let sections = splitAtDelimiters $ lines contents
128129 context = concat $ zipWith ($) sectionFunctions sections
129130 page = fromContext $ M.fromList $
@@ -134,7 +135,6 @@ readPageFromFile path = do
134135
135136 return page
136137 where
137− url = toUrl path
138138 category = let dirs = splitDirectories $ takeDirectory path
139139 in [("category", last dirs) | not (null dirs)]
140140
@@ -153,7 +153,7 @@ readPage path = do
153153-- Make pages renderable.
154154instance Renderable Page where
155155 getDependencies = (:[]) . getPagePath
156− getUrl = getPageUrl
156+ getUrl = return . getPageUrl
157157 toContext (Page page) = return page
158158
159159-- Make pages serializable.
src/Text/Hakyll/Render.hs+3−2
@@ -100,8 +100,9 @@ renderChain = renderChainWith id
100100-- @ContextManipulation@ which to apply on the context when it is read first.
101101renderChainWith :: Renderable a
102102 => ContextManipulation -> [FilePath] -> a -> Hakyll ()
103−renderChainWith manipulation templatePaths renderable =
104− depends (getUrl renderable) dependencies render'
103+renderChainWith manipulation templatePaths renderable = do
104+ url <- getUrl renderable
105+ depends url dependencies render'
105106 where
106107 dependencies = getDependencies renderable ++ templatePaths
107108 render' = do
src/Text/Hakyll/Renderable.hs+1−1
@@ -15,4 +15,4 @@ class Renderable a where
1515 getDependencies :: a -> [FilePath]
1616
1717 -- | 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 =
8585
8686instance Renderable CustomPage where
8787 getDependencies = customPageDependencies
88− getUrl = customPageUrl
88+ getUrl = return . customPageUrl
8989 toContext page = do
9090 values <- mapM (either return id . snd) (customPageContext page)
9191 let pairs = zip (map fst $ customPageContext page) values
@@ -94,6 +94,7 @@ instance Renderable CustomPage where
9494-- | PagePath is a class that wraps a FilePath. This is used to render Pages
9595-- without reading them first through use of caching.
9696data PagePath = PagePath FilePath
97+ deriving (Ord, Eq, Read, Show)
9798
9899-- | Create a PagePath from a FilePath.
99100createPagePath :: FilePath -> PagePath
@@ -113,6 +114,7 @@ instance Binary PagePath where
113114-- | A combination of two other renderables.
114115data CombinedRenderable a b = CombinedRenderable a b
115116 | CombinedRenderableWithUrl FilePath a b
117+ deriving (Ord, Eq, Read, Show)
116118
117119-- | Combine two renderables. The url will always be taken from the first
118120-- @Renderable@. Also, if a `$key` is present in both renderables, the
@@ -144,7 +146,7 @@ instance (Renderable a, Renderable b)
144146
145147 -- Take the url from the first renderable, or the specified URL.
146148 getUrl (CombinedRenderable a _) = getUrl a
147− getUrl (CombinedRenderableWithUrl url _ _) = url
149+ getUrl (CombinedRenderableWithUrl url _ _) = return url
148150
149151 -- Take a union of the contexts.
150152 toContext (CombinedRenderable a b) = do