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.

134 lines
4.1KB

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