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.

101 lines
3.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. >>= loadAndApplyTemplate "templates/default.html" postCtx
  46. >>= relativizeUrls
  47. >>= cleanIndexUrls
  48. >>= cleanIndexHtmls
  49. match "index.html" $ do
  50. route idRoute
  51. compile $ do
  52. posts <- recentFirst =<< loadAll "posts/*"
  53. let indexCtx =
  54. listField "posts" postCtx (return posts) <> ctx
  55. getResourceBody
  56. >>= applyAsTemplate indexCtx
  57. >>= loadAndApplyTemplate "templates/default.html" indexCtx
  58. >>= relativizeUrls
  59. >>= cleanIndexUrls
  60. >>= cleanIndexHtmls
  61. match "templates/*" $ compile templateBodyCompiler
  62. ctx :: Context String
  63. ctx = defaultContext <>
  64. faviconsField
  65. postCtx :: Context String
  66. postCtx =
  67. (dateField "date" "%B %e, %Y") <> ctx
  68. cleanRoute :: Routes
  69. cleanRoute = customRoute createIndexRoute
  70. where
  71. createIndexRoute ident =
  72. takeDirectory p </> takeBaseName p </> "index.html"
  73. where p = toFilePath ident
  74. cleanIndexUrls :: Item String -> Compiler (Item String)
  75. cleanIndexUrls = return . fmap (withUrls cleanIndex)
  76. cleanIndexHtmls :: Item String -> Compiler (Item String)
  77. cleanIndexHtmls = return . fmap (replaceAll pattern replacement)
  78. where
  79. pattern = "/index.html"
  80. replacement = const "/"
  81. cleanIndex :: String -> String
  82. cleanIndex url
  83. | idx `isSuffixOf` url = take (length url - length idx) url
  84. | otherwise = url
  85. where idx = "index.html"