@ -1,18 +1,27 @@
module Green.Site.Blog where
-- import qualified Data.HashMap.Strict as HashMap
import Control.Monad ( forM_ )
import Green.Common
import Green.Compiler ( loadExistingSnapshots )
import Green.Route
import Green.Site.Tags
import Green.Template
import Green.Template.Custom
import qualified Hakyll as H
blog :: Context String -> Rules ()
blog c ontext = do
blog siteC ontext = do
categories <- buildCategories " _posts/** " makeCategoryId
tags <- buildTags " _posts/** " makeTagId
let context =
tagCloudField " categoryCloud " categories
<> tagCloudField " tagCloud " tags
<> postContext categories tags
<> siteContext
blogHome categories tags context
blogHome context
posts context
archives context
@ -22,23 +31,16 @@ blog context = do
draftsIndex context
drafts context
blogHome :: Tags -> Tags -> Context String -> Rules ()
blogHome categories tags c ontext =
blogHome :: Context String -> Rules ()
blogHome context =
match " blog.html " do
route indexRoute
compile do
categoryCloud <- renderTagCloud categories
tagCloud <- renderTagCloud tags
recentPosts <- recentPostsContext
let blogContext =
constField " categoryCloud " categoryCloud
<> constField " tagCloud " tagCloud
<> recentPosts
<> postContext
<> context
let context' = recentPosts <> context
getResourceBody
>>= contentCompiler blogContext
>>= layoutCompiler blogContext
>>= contentCompiler context'
>>= layoutCompiler context'
>>= relativizeUrls
archives :: Context String -> Rules ()
@ -47,13 +49,12 @@ archives context = do
route indexRoute
compile do
publishedPosts <- H . recentFirst =<< loadPublishedPosts
let ar chivesC ontext =
let context' =
constField " posts " ( itemListValue context publishedPosts )
<> postContext
<> context
getResourceBody
>>= contentCompiler ar chivesC ontext
>>= layoutCompiler ar chivesC ontext
>>= contentCompiler context'
>>= layoutCompiler context'
>>= relativizeUrls
draftsIndex :: Context String -> Rules ()
@ -64,12 +65,13 @@ draftsIndex context = do
draftPosts <- H . recentFirst =<< loadDraftPosts
let draftsContext =
constField " posts " ( itemListValue context draftPosts )
<> postContext
<> context
getResourceBody
>>= contentCompiler draftsContext
>>= layoutCompiler draftsContext
>>= relativizeUrls
where
loadDraftPosts = loadExistingSnapshots " _drafts/** " " _draftPosts "
posts :: Context String -> Rules ()
posts context = do
@ -77,12 +79,10 @@ posts context = do
route postsRoute
compile $
getResourceBody
>>= contentCompiler postsC ontext
>>= contentCompiler c ontext
>>= snapshotCompiler [ publishedPostsSnapshot ]
>>= layoutCompiler postsC ontext
>>= layoutCompiler c ontext
>>= relativizeUrls
where
postsContext = postContext <> context
postsRoute :: Routes
postsRoute =
@ -97,12 +97,52 @@ drafts context = do
route draftsRoute
compile $
getResourceBody
>>= contentCompiler draftsC ontext
>>= contentCompiler c ontext
>>= snapshotCompiler [ draftPostsSnapshot ]
>>= layoutCompiler draftsC ontext
>>= layoutCompiler c ontext
>>= relativizeUrls
where
draftsContext = postContext <> context
categoriesPages :: TagCloud -> Context String -> Rules ()
categoriesPages categories context =
forM_ ( tagCloudTags categories ) \ category ->
rulesExtraDependencies [ tagCloudDependency categories ] do
create [ tagId category ] do
route indexRoute
compile do
categoryPosts <- H . recentFirst =<< H . loadAll ( fromList $ tagIds category )
let tagsContext =
constField " category " ( tagContext category )
<> constField " title " ( " Posted under " ++ tagName category )
<> constField " posts " ( itemListValue context categoryPosts )
<> constField " layout " ( " page " :: String )
<> context
template <- loadBody " _templates/posts-under-category.html "
makeItem " "
>>= applyTemplate template tagsContext
>>= pandocCompiler
>>= layoutCompiler tagsContext
>>= relativizeUrls
tagsPages :: TagCloud -> Context String -> Rules ()
tagsPages tags context =
forM_ ( tagCloudTags tags ) \ tag ->
rulesExtraDependencies [ tagCloudDependency tags ] do
create [ tagId tag ] do
route indexRoute
compile do
tagPosts <- H . recentFirst =<< H . loadAll ( fromList $ tagIds tag )
let tagsContext =
constField " tag " ( tagContext tag )
<> constField " title " ( " Posted under " ++ tagName tag )
<> constField " posts " ( itemListValue context tagPosts )
<> constField " layout " ( " page " :: String )
<> context
template <- loadBody " _templates/posts-under-tag.html "
makeItem " "
>>= applyTemplate template tagsContext
>>= pandocCompiler
>>= layoutCompiler tagsContext
>>= relativizeUrls
draftsRoute :: Routes
draftsRoute =
@ -111,52 +151,25 @@ draftsRoute =
` composeRoutes ` setExtension " html "
` composeRoutes ` indexRoute
categoriesPages :: Tags -> Context String -> Rules ()
categoriesPages categories context =
H . tagsRules categories \ category pat -> do
route indexRoute
compile do
categoryPosts <- H . recentFirst =<< H . loadAll pat
let categoryContext =
constField " category " category
<> constField " title " ( " Posts under \ " " ++ category ++ " \ " " )
<> constField " posts " ( itemListValue context categoryPosts )
<> constField " layout " ( " page " :: String )
<> postContext
<> context
dummy <- makeItem " "
template <- loadBody " _templates/posts-under-category.html "
applyTemplate template categoryContext dummy
>>= pandocCompiler
>>= layoutCompiler categoryContext
>>= relativizeUrls
tagsPages :: Tags -> Context String -> Rules ()
tagsPages tags context =
H . tagsRules tags \ tag pat -> do
route indexRoute
compile do
tagPosts <- H . recentFirst =<< H . loadAll pat
let tagsContext =
constField " tag " tag
<> constField " title " ( " Posts tagged \ " " ++ tag ++ " \ " " )
<> constField " posts " ( itemListValue context tagPosts )
<> constField " layout " ( " page " :: String )
<> postContext
<> context
dummy <- makeItem " "
template <- loadBody " _templates/posts-under-tag.html "
applyTemplate template tagsContext dummy
>>= pandocCompiler
>>= layoutCompiler tagsContext
>>= relativizeUrls
dateRoute :: Routes
dateRoute = gsubRoute datePattern ( H . replaceAll " - " ( const " / " ) )
where
datePattern = " [0-9]{4}-[0-9]{2}-[0-9]{2}- "
postContext :: Context String
postContext =
categoryLinksField " categoryLinks "
<> tagLink sField " tagLink s "
postContext :: TagCloud -> TagCloud -> Context String
postContext categories tags =
tagsField " categories " categories
<> tagsField " tags " tags
<> postHeaderField " postHeader "
postHeaderField :: String -> Context String
postHeaderField key = functionField key f
where
defaults = defaultKeys [ " headerLevel " , " latestPost " ]
f ( fields :: Context String ) = do
tplWithContext ( fields <> defaults ) do
itemBody <$> loadAndApplyTemplate' ( fromFilePath " _templates/post-header.html " )
recentPostsContext :: Compiler ( Context String )
recentPostsContext = do
recentPosts <- fmap ( take 5 ) . H . recentFirst =<< loadPublishedPosts
@ -172,24 +185,8 @@ teaserContext = teaserField "teaser" publishedPostsSnapshot
loadPublishedPosts :: Compiler [ Item String ]
loadPublishedPosts = loadExistingSnapshots " _posts/** " publishedPostsSnapshot
loadDraftPosts :: Compiler [ Item String ]
loadDraftPosts = loadExistingSnapshots " _drafts/** " draftPostsSnapshot
publishedPostsSnapshot :: String
publishedPostsSnapshot = " _publishedPosts "
draftPostsSnapshot :: String
draftPostsSnapshot = " _draftPosts "
dateRoute :: Routes
dateRoute = gsubRoute datePattern ( H . replaceAll " - " ( const " / " ) )
where
datePattern = " [0-9]{4}-[0-9]{2}-[0-9]{2}- "
postHeaderField :: String -> Context String
postHeaderField key = functionField key f
where
defaults = defaultKeys [ " headerLevel " , " latestPost " ]
f ( fields :: Context String ) = do
tplWithContext ( fields <> defaults ) do
itemBody <$> loadAndApplyTemplate' ( fromFilePath " _templates/post-header.html " )