blogng/site.hs

142 lines
4.5 KiB
Haskell
Raw Normal View History

2017-01-22 21:30:42 +00:00
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
2020-04-07 10:42:13 +00:00
import Data.Monoid (mappend)
import Hakyll
import Hakyll.Core.Identifier (toFilePath)
import Hakyll.Web.Feed
import System.FilePath
import Text.Regex (mkRegex, splitRegex)
2017-01-22 21:30:42 +00:00
--------------------------------------------------------------------------------
2017-02-02 14:42:52 +00:00
config :: Configuration
config = defaultConfiguration {
2019-01-08 20:46:39 +00:00
destinationDirectory = "public"
2017-02-02 14:42:52 +00:00
}
2019-01-13 00:43:27 +00:00
feedConfig :: FeedConfiguration
feedConfig = FeedConfiguration {
feedTitle = "Binary Strolls"
, feedDescription = "Blag"
, feedAuthorName = "Dhananjay Balan"
, feedRoot = "https://blog.dbalan.in"
, feedAuthorEmail = "blog@dbalan.in"
}
2017-01-22 21:30:42 +00:00
main :: IO ()
2017-02-02 14:42:52 +00:00
main = hakyllWith config $ do
2019-01-28 21:42:01 +00:00
tags <- buildTags "blog/*" (fromCapture "tags/*.html")
2017-02-01 15:38:12 +00:00
2017-01-22 21:30:42 +00:00
match "images/*" $ do
route idRoute
compile copyFileCompiler
match "css/*" $ do
route idRoute
compile compressCssCompiler
2019-08-03 22:00:30 +00:00
match "fonts/*/*" $ do
2017-01-31 21:42:20 +00:00
route $ idRoute
compile copyFileCompiler
2017-02-13 22:29:23 +00:00
match "notes/*" $ do
route $ idRoute
compile copyFileCompiler
2017-02-13 22:29:23 +00:00
match "blog/*" $ do
route $ octopressRoute
2017-01-22 21:30:42 +00:00
compile $ pandocCompiler
2019-01-28 21:42:01 +00:00
>>= loadAndApplyTemplate "templates/post.html" (postCtxWTags tags)
2019-01-13 00:43:27 +00:00
>>= saveSnapshot "content"
2019-01-28 21:42:01 +00:00
>>= loadAndApplyTemplate "templates/default.html" (postCtxWTags tags)
2017-01-22 21:30:42 +00:00
>>= relativizeUrls
2024-02-11 05:27:20 +00:00
match "pages/*" $ do
route pageRoute
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/pages.html" defaultContext
>>= relativizeUrls
2017-01-22 21:30:42 +00:00
match "index.html" $ do
route idRoute
compile $ do
posts <- fmap (take 4) . recentFirst =<< loadAllSnapshots "blog/*" "content"
2017-01-22 21:30:42 +00:00
let indexCtx =
listField "posts" postCtx (return $ take 8 posts) `mappend`
2017-01-22 21:30:42 +00:00
constField "title" "Home" `mappend`
defaultContext
getResourceBody
>>= applyAsTemplate indexCtx
>>= loadAndApplyTemplate "templates/default.html" indexCtx
>>= relativizeUrls
2019-01-13 00:43:27 +00:00
create ["archive.html"] $ do
route idRoute
compile $ do
posts <- recentFirst =<< loadAll "blog/*"
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
2019-01-28 21:42:01 +00:00
-- tags!
tagsRules tags $ \tag pattern -> do
let title = "Posts tagged \"" ++ tag ++ "\""
route idRoute
compile $ do
posts <- recentFirst =<< loadAll pattern
let ctx = constField "title" title
2020-04-07 10:42:13 +00:00
`mappend` listField "posts" (postCtxWTags tags) (return posts)
2019-01-28 21:42:01 +00:00
`mappend` defaultContext
makeItem ""
>>= loadAndApplyTemplate "templates/tag.html" ctx
>>= loadAndApplyTemplate "templates/default.html" ctx
>>= relativizeUrls
2019-01-13 00:43:27 +00:00
create ["atom.xml"] $ do
route idRoute
compile $ do
posts <- fmap (take 5) . recentFirst =<< loadAllSnapshots "blog/*" "content"
2019-01-13 00:57:16 +00:00
let feedCtx = postCtx `mappend` bodyField "description"
2019-01-13 00:43:27 +00:00
renderAtom feedConfig feedCtx posts
2017-01-22 21:30:42 +00:00
match "templates/*" $ compile templateBodyCompiler
--------------------------------------------------------------------------------
2017-02-01 15:38:12 +00:00
extractTags :: Rules Tags
extractTags = do
tags <- buildTags ("blogs/**" .&&. hasNoVersion) $ fromCapture "tags/*.html"
return $ sortTagsBy caseInsensitiveTags tags
2019-01-28 21:42:01 +00:00
postCtxWTags :: Tags -> Context String
postCtxWTags tags = tagsField "tags" tags <> postCtx
2017-01-22 21:30:42 +00:00
postCtx :: Context String
postCtx =
dateField "date" "%B %e, %Y" `mappend`
defaultContext
2024-02-11 05:27:20 +00:00
-- static page routes
pageRoute :: Routes
pageRoute = customRoute $ (\p -> (takeFileName (toFilePath p)) -<.> ".html")
-- octopress compatible routes
octopressRoute :: Routes
octopressRoute = customRoute $ octoMangaling
octoMangaling :: Identifier -> FilePath
octoMangaling pathId = base </> year </> month </> day </> post </> "index.html"
where
p = toFilePath pathId
base = takeDirectory p
seps = splitRegex (mkRegex "-") $ takeBaseName p
year:month:day:xs = seps
post = tail $ foldl (\a b -> a ++ "-" ++ b) "" xs