tA's crappy blog
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

155 строки
4.7KB

  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 <- (return . (take 5))
  72. =<< recentFirst
  73. =<< loadAll "posts/*"
  74. let indexCtx =
  75. listField "posts" postCtx (return posts) <> ctx
  76. getResourceBody
  77. >>= applyAsTemplate indexCtx
  78. >>= loadAndApplyTemplate "templates/default.html" indexCtx
  79. >>= relativizeUrls
  80. >>= cleanIndexUrls
  81. >>= cleanIndexHtmls
  82. -- Agnememnon the Fuck-Upperer - Conquerer of Small Type Declarations
  83. compileFeed ::
  84. (FeedConfiguration
  85. -> Context String
  86. -> [Item String]
  87. -> Compiler (Item String)
  88. ) -> Rules ()
  89. -- For those left alive, this abstracts out creating
  90. -- Atom and RSS feeds
  91. compileFeed f = compile $ do
  92. let feedCtx = postCtx <>
  93. bodyField "description"
  94. posts <- fmap (take 10) . recentFirst
  95. =<< loadAllSnapshots "posts/*" "content"
  96. f feedConfig feedCtx posts
  97. -- The configuration for our Atom/RSS feeds
  98. feedConfig :: FeedConfiguration
  99. feedConfig = FeedConfiguration {
  100. feedTitle = "Regular Flolloping"
  101. , feedDescription = "tA's Blog"
  102. , feedAuthorName = "Shaun Kerr"
  103. , feedAuthorEmail = "s@p7.co.nz"
  104. , feedRoot = "https://regularflolloping.com"
  105. }
  106. -- Our default context for pages
  107. ctx :: Context String
  108. ctx = defaultContext <>
  109. faviconsField
  110. -- Default context for posts
  111. postCtx :: Context String
  112. postCtx =
  113. (dateField "date" "%B %e, %Y") <> ctx
  114. -- Functions to convert pages to /name/index.html
  115. cleanRoute :: Routes
  116. cleanRoute = customRoute createIndexRoute
  117. where
  118. createIndexRoute ident =
  119. takeDirectory p </> takeBaseName p </> "index.html"
  120. where p = toFilePath ident
  121. cleanIndexUrls :: Item String -> Compiler (Item String)
  122. cleanIndexUrls = return . fmap (withUrls cleanIndex)
  123. cleanIndexHtmls :: Item String -> Compiler (Item String)
  124. cleanIndexHtmls = return . fmap (replaceAll pattern replacement)
  125. where
  126. pattern = "/index.html"
  127. replacement = const "/"
  128. cleanIndex :: String -> String
  129. cleanIndex url
  130. | idx `isSuffixOf` url = take (length url - length idx) url
  131. | otherwise = url
  132. where idx = "index.html"