Skip to content

Commit

Permalink
compile only published posts
Browse files Browse the repository at this point in the history
  • Loading branch information
3v0k4 committed May 11, 2020
1 parent 28b2b35 commit c4f8aca
Showing 1 changed file with 54 additions and 48 deletions.
102 changes: 54 additions & 48 deletions blog/site.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import Data.Monoid (mappend)
import Hakyll
import System.Environment (getEnvironment)

--------------------------------------------------------------------------------
feedConfiguration :: FeedConfiguration
Expand All @@ -17,58 +18,63 @@ feedConfiguration =
}

main :: IO ()
main = hakyll $ do
match "images/*" $ do
route idRoute
compile copyFileCompiler
match "css/*" $ do
route idRoute
compile compressCssCompiler
matchMetadata "posts/*" isPublished $ do
route $ setExtension "html"
compile $
pandocCompiler
>>= loadAndApplyTemplate "templates/post.html" postCtx
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/default.html" postCtx
>>= relativizeUrls
create ["archive.html"] $ do
route idRoute
compile $ do
posts <- recentFirst =<< loadAll "posts/*"
let archiveCtx =
listField "posts" postCtx (return posts)
`mappend` constField "title" "Archives"
`mappend` defaultContext
makeItem ""
>>= loadAndApplyTemplate "templates/archive.html" archiveCtx
>>= loadAndApplyTemplate "templates/default.html" archiveCtx
>>= relativizeUrls
match "index.html" $ do
route idRoute
compile $ do
let indexCtx =
constField "title" "Home"
`mappend` defaultContext
getResourceBody
>>= applyAsTemplate indexCtx
>>= loadAndApplyTemplate "templates/default.html" indexCtx
>>= relativizeUrls
match "templates/*" $ compile templateBodyCompiler
create ["atom.xml"] $ do
route idRoute
compile $ do
let feedCtx = mconcat [bodyField "description", defaultContext]
posts <-
fmap (take 10) . recentFirst
=<< loadAllSnapshots "posts/*" "content"
renderAtom feedConfiguration feedCtx posts
main = do
env <- getEnvironment
hakyll $ do
match "images/*" $ do
route idRoute
compile copyFileCompiler
match "css/*" $ do
route idRoute
compile compressCssCompiler
matchMetadata "posts/*" (isDevelopmentOrPublished env) $ do
route $ setExtension "html"
compile $
pandocCompiler
>>= loadAndApplyTemplate "templates/post.html" postCtx
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/default.html" postCtx
>>= relativizeUrls
create ["archive.html"] $ do
route idRoute
compile $ do
posts <- recentFirst =<< loadAll "posts/*"
let archiveCtx =
listField "posts" postCtx (return posts)
`mappend` constField "title" "Archives"
`mappend` defaultContext
makeItem ""
>>= loadAndApplyTemplate "templates/archive.html" archiveCtx
>>= loadAndApplyTemplate "templates/default.html" archiveCtx
>>= relativizeUrls
match "index.html" $ do
route idRoute
compile $ do
let indexCtx =
constField "title" "Home"
`mappend` defaultContext
getResourceBody
>>= applyAsTemplate indexCtx
>>= loadAndApplyTemplate "templates/default.html" indexCtx
>>= relativizeUrls
match "templates/*" $ compile templateBodyCompiler
create ["atom.xml"] $ do
route idRoute
compile $ do
let feedCtx = mconcat [bodyField "description", defaultContext]
posts <-
fmap (take 10) . recentFirst
=<< loadAllSnapshots "posts/*" "content"
renderAtom feedConfiguration feedCtx posts

--------------------------------------------------------------------------------
postCtx :: Context String
postCtx =
dateField "date" "%B %e, %Y"
`mappend` defaultContext

isPublished :: Metadata -> Bool
isPublished = maybe True (== "true") . lookupString "published"
isDevelopmentOrPublished :: [(String, String)] -> Metadata -> Bool
isDevelopmentOrPublished env metadata = isDevelopmentEnv || isPublished
where
isDevelopmentEnv = lookup "HAKYLL_ENV" env == Just "development"
isPublished = maybe True (== "true") . lookupString "published" $ metadata

0 comments on commit c4f8aca

Please sign in to comment.