tA's crappy blog
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.0KB

  1. {-# LANGUAGE OverloadedStrings #-}
  2. import Data.Monoid ((<>))
  3. import Data.List (sortBy,isSuffixOf)
  4. import GHC.IO.Encoding
  5. import Hakyll
  6. import Hakyll.Favicon (faviconsRules, faviconsField)
  7. import System.FilePath.Posix (takeBaseName,takeDirectory,(</>))
  8. main :: IO ()
  9. main = do
  10. setLocaleEncoding utf8
  11. hakyll $ do
  12. faviconsRules "icons/favicon.svg"
  13. match "humans.txt" $ do
  14. route idRoute
  15. compile copyFileCompiler
  16. match "css/*" $ do
  17. route idRoute
  18. compile compressCssCompiler
  19. match (fromList ["about.md", "contact.md"]) $ do
  20. route $ cleanRoute
  21. compile $ pandocCompiler
  22. >>= loadAndApplyTemplate "templates/default.html" ctx
  23. >>= relativizeUrls
  24. >>= cleanIndexUrls
  25. match "archive.md" $ do
  26. route $ cleanRoute
  27. compile $ pandocCompiler
  28. >>= loadAndApplyTemplate "templates/archive.html" ctx
  29. >>= relativizeUrls
  30. >>= cleanIndexUrls
  31. compile $ do
  32. posts <- recentFirst =<< loadAll "posts/*"
  33. let archiveCtx =
  34. listField "posts" postCtx (return posts) <>
  35. ctx
  36. pandocCompiler
  37. >>= loadAndApplyTemplate "templates/archive.html" archiveCtx
  38. >>= loadAndApplyTemplate "templates/default.html" archiveCtx
  39. >>= relativizeUrls
  40. >>= cleanIndexUrls
  41. match "posts/*" $ do
  42. route $ cleanRoute
  43. compile $ pandocCompiler
  44. >>= loadAndApplyTemplate "templates/post.html" postCtx
  45. >>= saveSnapshot "content"
  46. >>= loadAndApplyTemplate "templates/default.html" postCtx
  47. >>= relativizeUrls
  48. >>= cleanIndexUrls
  49. >>= cleanIndexHtmls
  50. match "index.html" $ do
  51. route idRoute
  52. compile $ do
  53. posts <- recentFirst =<< loadAll "posts/*"
  54. let indexCtx =
  55. listField "posts" postCtx (return posts) <> ctx
  56. getResourceBody
  57. >>= applyAsTemplate indexCtx
  58. >>= loadAndApplyTemplate "templates/default.html" indexCtx
  59. >>= relativizeUrls
  60. >>= cleanIndexUrls
  61. >>= cleanIndexHtmls
  62. match "templates/*" $ compile templateBodyCompiler
  63. create ["atom.xml"] $ do
  64. route idRoute
  65. compile $ do
  66. let feedCtx = postCtx <>
  67. bodyField "description"
  68. posts <- fmap (take 10) . recentFirst
  69. =<< loadAllSnapshots "posts/*" "content"
  70. renderAtom feedConfig feedCtx posts
  71. create ["rss.xml"] $ do
  72. route idRoute
  73. compile $ do
  74. let feedCtx = postCtx <>
  75. bodyField "description"
  76. posts <- fmap (take 10) . recentFirst
  77. =<< loadAllSnapshots "posts/*" "content"
  78. renderRss feedConfig feedCtx posts
  79. ctx :: Context String
  80. ctx = defaultContext <>
  81. faviconsField
  82. postCtx :: Context String
  83. postCtx =
  84. (dateField "date" "%B %e, %Y") <> ctx
  85. cleanRoute :: Routes
  86. cleanRoute = customRoute createIndexRoute
  87. where
  88. createIndexRoute ident =
  89. takeDirectory p </> takeBaseName p </> "index.html"
  90. where p = toFilePath ident
  91. cleanIndexUrls :: Item String -> Compiler (Item String)
  92. cleanIndexUrls = return . fmap (withUrls cleanIndex)
  93. cleanIndexHtmls :: Item String -> Compiler (Item String)
  94. cleanIndexHtmls = return . fmap (replaceAll pattern replacement)
  95. where
  96. pattern = "/index.html"
  97. replacement = const "/"
  98. cleanIndex :: String -> String
  99. cleanIndex url
  100. | idx `isSuffixOf` url = take (length url - length idx) url
  101. | otherwise = url
  102. where idx = "index.html"
  103. feedConfig :: FeedConfiguration
  104. feedConfig = FeedConfiguration {
  105. feedTitle = "Regular Flolloping"
  106. , feedDescription = "tA's Blog"
  107. , feedAuthorName = "Shaun Kerr"
  108. , feedAuthorEmail = "s@p7.co.nz"
  109. , feedRoot = "https://regularflolloping.com"
  110. }