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.

153 lines
4.6KB

  1. {-# LANGUAGE OverloadedStrings #-}
  2. import Data.Monoid ((<>))
  3. import Data.List (sortBy,isSuffixOf)
  4. import Data.Typeable
  5. import GHC.IO.Encoding
  6. import Hakyll
  7. import Hakyll.Favicon (faviconsRules, faviconsField)
  8. import System.FilePath.Posix (takeBaseName,takeDirectory,(</>))
  9. main :: IO ()
  10. main = do
  11. -- Set the encoding so w3c doesnt complain
  12. setLocaleEncoding utf8
  13. hakyll $ do
  14. -- Generate the favicons
  15. faviconsRules "icons/favicon.svg"
  16. -- Straight copying of files
  17. match (fromList ["humans.txt", "robots.txt", "fonts/*"]) $ do
  18. route idRoute
  19. compile copyFileCompiler
  20. -- CSS needs to be compiled and minified
  21. match "css/*" $ do
  22. route idRoute
  23. compile compressCssCompiler
  24. -- Load pages that need to be formatted
  25. match (fromList ["about.md", "contact.md"]) $ do
  26. route $ cleanRoute
  27. compile $ pandocCompiler
  28. >>= loadAndApplyTemplate "templates/default.html" ctx
  29. >>= relativizeUrls
  30. >>= cleanIndexUrls
  31. -- Render Atom + Rss feeds
  32. create ["atom.xml"] $ do
  33. route idRoute
  34. (compileFeed renderAtom)
  35. create ["rss.xml"] $ do
  36. route idRoute
  37. (compileFeed renderRss)
  38. -- Compile the templates
  39. match "templates/*" $ compile templateBodyCompiler
  40. -- Compile the archive page and post list
  41. match "archive.md" $ do
  42. route $ cleanRoute
  43. compile $ pandocCompiler
  44. >>= loadAndApplyTemplate "templates/archive.html" ctx
  45. >>= relativizeUrls
  46. >>= cleanIndexUrls
  47. compile $ do
  48. posts <- recentFirst =<< loadAll "posts/*"
  49. let archiveCtx =
  50. listField "posts" postCtx (return posts) <>
  51. ctx
  52. pandocCompiler
  53. >>= loadAndApplyTemplate "templates/archive.html" archiveCtx
  54. >>= loadAndApplyTemplate "templates/default.html" archiveCtx
  55. >>= relativizeUrls
  56. >>= cleanIndexUrls
  57. -- Compile posts + save snapshots for the web feeds
  58. match "posts/*" $ do
  59. route $ cleanRoute
  60. compile $ pandocCompiler
  61. >>= loadAndApplyTemplate "templates/post.html" postCtx
  62. >>= saveSnapshot "content"
  63. >>= loadAndApplyTemplate "templates/default.html" postCtx
  64. >>= relativizeUrls
  65. >>= cleanIndexUrls
  66. >>= cleanIndexHtmls
  67. -- Compile and load posts
  68. match "index.html" $ do
  69. route idRoute
  70. compile $ do
  71. posts <- recentFirst =<< loadAll "posts/*"
  72. let indexCtx =
  73. listField "posts" postCtx (return posts) <> ctx
  74. getResourceBody
  75. >>= applyAsTemplate indexCtx
  76. >>= loadAndApplyTemplate "templates/default.html" indexCtx
  77. >>= relativizeUrls
  78. >>= cleanIndexUrls
  79. >>= cleanIndexHtmls
  80. -- Agnememnon the Fuck-Upperer - Conquerer of Small Type Declarations
  81. compileFeed ::
  82. (FeedConfiguration
  83. -> Context String
  84. -> [Item String]
  85. -> Compiler (Item String)
  86. ) -> Rules ()
  87. -- For those left alive, this abstracts out creating
  88. -- Atom and RSS feeds
  89. compileFeed f = compile $ do
  90. let feedCtx = postCtx <>
  91. bodyField "description"
  92. posts <- fmap (take 10) . recentFirst
  93. =<< loadAllSnapshots "posts/*" "content"
  94. f feedConfig feedCtx posts
  95. -- The configuration for our Atom/RSS feeds
  96. feedConfig :: FeedConfiguration
  97. feedConfig = FeedConfiguration {
  98. feedTitle = "Regular Flolloping"
  99. , feedDescription = "tA's Blog"
  100. , feedAuthorName = "Shaun Kerr"
  101. , feedAuthorEmail = "s@p7.co.nz"
  102. , feedRoot = "https://regularflolloping.com"
  103. }
  104. -- Our default context for pages
  105. ctx :: Context String
  106. ctx = defaultContext <>
  107. faviconsField
  108. -- Default context for posts
  109. postCtx :: Context String
  110. postCtx =
  111. (dateField "date" "%B %e, %Y") <> ctx
  112. -- Functions to convert pages to /name/index.html
  113. cleanRoute :: Routes
  114. cleanRoute = customRoute createIndexRoute
  115. where
  116. createIndexRoute ident =
  117. takeDirectory p </> takeBaseName p </> "index.html"
  118. where p = toFilePath ident
  119. cleanIndexUrls :: Item String -> Compiler (Item String)
  120. cleanIndexUrls = return . fmap (withUrls cleanIndex)
  121. cleanIndexHtmls :: Item String -> Compiler (Item String)
  122. cleanIndexHtmls = return . fmap (replaceAll pattern replacement)
  123. where
  124. pattern = "/index.html"
  125. replacement = const "/"
  126. cleanIndex :: String -> String
  127. cleanIndex url
  128. | idx `isSuffixOf` url = take (length url - length idx) url
  129. | otherwise = url
  130. where idx = "index.html"