summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2010-02-01 16:43:26 +0100
committerJasper Van der Jeugt <jaspervdj@gmail.com>2010-02-01 16:43:26 +0100
commitc4b1fd5900bdff5c94891b7b1c9e764653d572a3 (patch)
tree578ed9966565a5642de4531d65786a58cd2ccac5 /src
parentfbd9ff95bdd9c9bd904d87f2b2f2d88470c68173 (diff)
downloadhakyll-c4b1fd5900bdff5c94891b7b1c9e764653d572a3.tar.gz
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.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Hakyll.hs1
-rw-r--r--src/Text/Hakyll/File.hs54
-rw-r--r--src/Text/Hakyll/Hakyll.hs2
-rw-r--r--src/Text/Hakyll/Internal/Render.hs3
-rw-r--r--src/Text/Hakyll/Page.hs6
-rw-r--r--src/Text/Hakyll/Render.hs5
-rw-r--r--src/Text/Hakyll/Renderable.hs2
-rw-r--r--src/Text/Hakyll/Renderables.hs6
8 files changed, 52 insertions, 27 deletions
diff --git a/src/Text/Hakyll.hs b/src/Text/Hakyll.hs
index dfe7b02..d818ebb 100644
--- a/src/Text/Hakyll.hs
+++ b/src/Text/Hakyll.hs
@@ -28,6 +28,7 @@ defaultHakyllConfiguration = HakyllConfiguration
{ additionalContext = M.empty
, siteDirectory = "_site"
, cacheDirectory = "_cache"
+ , enableIndexUrl = False
}
-- | Main function to run Hakyll with the default configuration.
diff --git a/src/Text/Hakyll/File.hs b/src/Text/Hakyll/File.hs
index 649f2a3..92f93da 100644
--- a/src/Text/Hakyll/File.hs
+++ b/src/Text/Hakyll/File.hs
@@ -36,8 +36,15 @@ removeLeadingSeparator path
-- | Convert a relative filepath to a filepath in the destination
-- (default: @_site@).
toDestination :: FilePath -> Hakyll FilePath
-toDestination path = do dir <- askHakyll siteDirectory
- return $ dir </> removeLeadingSeparator path
+toDestination url = do dir <- askHakyll siteDirectory
+ enableIndexUrl' <- askHakyll enableIndexUrl
+ let destination = if enableIndexUrl' && separatorEnd
+ then dir </> noSeparator </> "index.html"
+ else dir </> noSeparator
+ return destination
+ where
+ noSeparator = removeLeadingSeparator url
+ separatorEnd = not (null url) && last url == '/'
-- | Convert a relative filepath to a filepath in the cache
-- (default: @_cache@).
@@ -48,21 +55,34 @@ toCache path = do dir <- askHakyll cacheDirectory
-- | Get the url for a given page. For most extensions, this would be the path
-- itself. It's only for rendered extensions (@.markdown@, @.rst@, @.lhs@ this
-- function returns a path with a @.html@ extension instead.
-toUrl :: FilePath -> FilePath
-toUrl path = if takeExtension path `elem` [ ".markdown"
- , ".md"
- , ".mdn"
- , ".mdwn"
- , ".mkd"
- , ".mkdn"
- , ".mkdwn"
- , ".rst"
- , ".text"
- , ".tex"
- , ".lhs"
- ]
- then flip addExtension ".html" $ dropExtension path
- else path
+toUrl :: FilePath -> Hakyll FilePath
+toUrl path = do enableIndexUrl' <- askHakyll enableIndexUrl
+ -- If the file does not have a renderable extension, like for
+ -- example favicon.ico, we don't have to change it at all.
+ return $ if not hasRenderableExtension
+ then path
+ -- If index url's are enabled, we create pick it
+ -- unless the page is an index already.
+ else if enableIndexUrl' && not isIndex
+ then indexUrl
+ else withSimpleHtmlExtension
+ where
+ hasRenderableExtension = takeExtension path `elem` [ ".markdown"
+ , ".md"
+ , ".mdn"
+ , ".mdwn"
+ , ".mkd"
+ , ".mkdn"
+ , ".mkdwn"
+ , ".rst"
+ , ".text"
+ , ".tex"
+ , ".lhs"
+ ]
+ isIndex = (dropExtension $ takeFileName path) == "index"
+ withSimpleHtmlExtension = flip addExtension ".html" $ dropExtension path
+ indexUrl = (dropExtension path) ++ "/"
+
-- | Get the relative url to the site root, for a given (absolute) url
toRoot :: FilePath -> FilePath
diff --git a/src/Text/Hakyll/Hakyll.hs b/src/Text/Hakyll/Hakyll.hs
index b33bbda..08cb7ea 100644
--- a/src/Text/Hakyll/Hakyll.hs
+++ b/src/Text/Hakyll/Hakyll.hs
@@ -19,6 +19,8 @@ data HakyllConfiguration = HakyllConfiguration
siteDirectory :: FilePath
, -- | Directory for cache files.
cacheDirectory :: FilePath
+ , -- | Enable index links.
+ enableIndexUrl :: Bool
}
-- | Our custom monad stack.
diff --git a/src/Text/Hakyll/Internal/Render.hs b/src/Text/Hakyll/Internal/Render.hs
index 167c58a..49e6be2 100644
--- a/src/Text/Hakyll/Internal/Render.hs
+++ b/src/Text/Hakyll/Internal/Render.hs
@@ -58,11 +58,10 @@ pureRenderChainWith manipulation templates context =
writePage :: Page -> Hakyll ()
writePage page = do
additionalContext' <- askHakyll additionalContext
+ url <- getUrl page
destination <- toDestination url
let context = additionalContext' `M.union` M.singleton "root" (toRoot url)
makeDirectories destination
    -- Substitute $root here, just before writing.
liftIO $ writeFile destination $ finalSubstitute (fromString $ getBody page)
context
- where
- url = getUrl page
diff --git a/src/Text/Hakyll/Page.hs b/src/Text/Hakyll/Page.hs
index d557665..63c5a40 100644
--- a/src/Text/Hakyll/Page.hs
+++ b/src/Text/Hakyll/Page.hs
@@ -30,7 +30,7 @@ import Text.Hakyll.Regex (substituteRegex, matchesRegex)
-- | A Page is basically key-value mapping. Certain keys have special
-- meanings, like for example url, body and title.
data Page = Page Context
- deriving (Show, Read, Eq)
+ deriving (Ord, Eq, Show, Read)
-- | Create a Page from a key-value mapping.
fromContext :: Context -> Page
@@ -124,6 +124,7 @@ readPageFromFile path = do
-- Read file.
contents <- liftIO $ readFile path
+ url <- toUrl path
let sections = splitAtDelimiters $ lines contents
context = concat $ zipWith ($) sectionFunctions sections
page = fromContext $ M.fromList $
@@ -134,7 +135,6 @@ readPageFromFile path = do
return page
where
- url = toUrl path
category = let dirs = splitDirectories $ takeDirectory path
in [("category", last dirs) | not (null dirs)]
@@ -153,7 +153,7 @@ readPage path = do
-- Make pages renderable.
instance Renderable Page where
getDependencies = (:[]) . getPagePath
- getUrl = getPageUrl
+ getUrl = return . getPageUrl
toContext (Page page) = return page
-- Make pages serializable.
diff --git a/src/Text/Hakyll/Render.hs b/src/Text/Hakyll/Render.hs
index e11879d..df0f553 100644
--- a/src/Text/Hakyll/Render.hs
+++ b/src/Text/Hakyll/Render.hs
@@ -100,8 +100,9 @@ renderChain = renderChainWith id
-- @ContextManipulation@ which to apply on the context when it is read first.
renderChainWith :: Renderable a
=> ContextManipulation -> [FilePath] -> a -> Hakyll ()
-renderChainWith manipulation templatePaths renderable =
- depends (getUrl renderable) dependencies render'
+renderChainWith manipulation templatePaths renderable = do
+ url <- getUrl renderable
+ depends url dependencies render'
where
dependencies = getDependencies renderable ++ templatePaths
render' = do
diff --git a/src/Text/Hakyll/Renderable.hs b/src/Text/Hakyll/Renderable.hs
index 2fdeacd..60e75ee 100644
--- a/src/Text/Hakyll/Renderable.hs
+++ b/src/Text/Hakyll/Renderable.hs
@@ -15,4 +15,4 @@ class Renderable a where
getDependencies :: a -> [FilePath]
-- | Get the destination for the renderable.
- getUrl :: a -> FilePath
+ getUrl :: a -> Hakyll FilePath
diff --git a/src/Text/Hakyll/Renderables.hs b/src/Text/Hakyll/Renderables.hs
index 6f5bcfe..0ccef5f 100644
--- a/src/Text/Hakyll/Renderables.hs
+++ b/src/Text/Hakyll/Renderables.hs
@@ -85,7 +85,7 @@ createListingWith manipulation url template renderables additional =
instance Renderable CustomPage where
getDependencies = customPageDependencies
- getUrl = customPageUrl
+ getUrl = return . customPageUrl
toContext page = do
values <- mapM (either return id . snd) (customPageContext page)
let pairs = zip (map fst $ customPageContext page) values
@@ -94,6 +94,7 @@ instance Renderable CustomPage where
-- | PagePath is a class that wraps a FilePath. This is used to render Pages
-- without reading them first through use of caching.
data PagePath = PagePath FilePath
+ deriving (Ord, Eq, Read, Show)
-- | Create a PagePath from a FilePath.
createPagePath :: FilePath -> PagePath
@@ -113,6 +114,7 @@ instance Binary PagePath where
-- | A combination of two other renderables.
data CombinedRenderable a b = CombinedRenderable a b
| CombinedRenderableWithUrl FilePath a b
+ deriving (Ord, Eq, Read, Show)
-- | Combine two renderables. The url will always be taken from the first
-- @Renderable@. Also, if a `$key` is present in both renderables, the
@@ -144,7 +146,7 @@ instance (Renderable a, Renderable b)
-- Take the url from the first renderable, or the specified URL.
getUrl (CombinedRenderable a _) = getUrl a
- getUrl (CombinedRenderableWithUrl url _ _) = url
+ getUrl (CombinedRenderableWithUrl url _ _) = return url
-- Take a union of the contexts.
toContext (CombinedRenderable a b) = do