site.hs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. --------------------------------------------------------------------------------
  2. {-# LANGUAGE OverloadedStrings #-}
  3. import Data.Monoid (mappend)
  4. import Hakyll
  5. import qualified Data.Set as S
  6. import Text.Pandoc
  7. --------------------------------------------------------------------------------
  8. main :: IO ()
  9. main = hakyll $ do
  10. match "images/*" $ do
  11. route idRoute
  12. compile copyFileCompiler
  13. match "images/*/*" $ do
  14. route idRoute
  15. compile copyFileCompiler
  16. match "assets/*" $ do
  17. route idRoute
  18. compile copyFileCompiler
  19. match "assets/*/*" $ do
  20. route idRoute
  21. compile copyFileCompiler
  22. match "assets/*/*/*" $ do
  23. route idRoute
  24. compile copyFileCompiler
  25. match "assets/*/*/*/*" $ do
  26. route idRoute
  27. compile copyFileCompiler
  28. match "scripts/*" $ do
  29. route idRoute
  30. compile copyFileCompiler
  31. match "css/*" $ do
  32. route idRoute
  33. compile compressCssCompiler
  34. match "resume_jmelesky.pdf" $ do
  35. route idRoute
  36. compile copyFileCompiler
  37. match "stuff/*.md" $ do
  38. route $ setExtension "html"
  39. compile $ siteCompiler
  40. >>= loadAndApplyTemplate "templates/default.html" defaultContext
  41. >>= relativizeUrls
  42. match "*.md" $ do
  43. route $ setExtension "html"
  44. compile $ siteCompiler
  45. >>= loadAndApplyTemplate "templates/default.html" defaultContext
  46. >>= relativizeUrls
  47. tags <- buildTags "posts/*.md" (fromCapture "tags/*.html")
  48. tagsRules tags $ \tag pattern -> do
  49. let title = "Posts tagged \"" ++ tag ++ "\""
  50. route idRoute
  51. compile $ do
  52. posts <- recentFirst =<< loadAll pattern
  53. let ctx = constField "title" title
  54. `mappend` listField "posts" postCtx (return posts)
  55. `mappend` defaultContext
  56. makeItem ""
  57. >>= loadAndApplyTemplate "templates/tag.html" ctx
  58. >>= loadAndApplyTemplate "templates/default.html" ctx
  59. >>= relativizeUrls
  60. match "posts/*.md" $ do
  61. route $ setExtension "html"
  62. compile $ siteCompiler
  63. >>= loadAndApplyTemplate "templates/post.html" (postCtxWithTags tags)
  64. >>= saveSnapshot "post_content"
  65. >>= loadAndApplyTemplate "templates/default.html" (postCtxWithTags tags)
  66. >>= relativizeUrls
  67. create ["archive.html"] $ do
  68. route idRoute
  69. compile $ do
  70. posts <- recentFirst =<< loadAll "posts/*.md"
  71. let archiveCtx =
  72. listField "posts" postCtx (return posts) `mappend`
  73. constField "title" "Archives" `mappend`
  74. defaultContext
  75. makeItem ""
  76. >>= loadAndApplyTemplate "templates/archive.html" archiveCtx
  77. >>= loadAndApplyTemplate "templates/default.html" archiveCtx
  78. >>= relativizeUrls
  79. create ["index.html"] $ do
  80. route idRoute
  81. compile $ do
  82. post <- fmap head . recentFirst =<< loadAllSnapshots "posts/*.md" "post_content"
  83. loadAndApplyTemplate "templates/default.html" (postCtxWithTags tags) post
  84. >>= relativizeUrls
  85. >>= changeIdentifier "index.html"
  86. match "templates/*" $ compile templateBodyCompiler
  87. --------------------------------------------------------------------------------
  88. postCtx :: Context String
  89. postCtx =
  90. dateField "date" "%B %e, %Y" `mappend`
  91. defaultContext
  92. postCtxWithTags :: Tags -> Context String
  93. postCtxWithTags tags = tagsField "tags" tags `mappend` postCtx
  94. changeIdentifier :: Identifier -> Item a -> Compiler (Item a)
  95. changeIdentifier idt item = return (itemSetIdentifier idt item)
  96. itemSetIdentifier :: Identifier -> Item a -> Item a
  97. itemSetIdentifier x (Item _ i) = Item x i
  98. siteCompiler :: Compiler (Item String)
  99. siteCompiler =
  100. let addExtensions = [Ext_tex_math_dollars, Ext_tex_math_double_backslash,
  101. Ext_latex_macros, Ext_footnotes, Ext_inline_notes]
  102. wExtensions = foldr S.insert (writerExtensions defaultHakyllWriterOptions) addExtensions
  103. rExtensions = foldr S.insert (readerExtensions defaultHakyllReaderOptions) addExtensions
  104. siteWriterOptions = defaultHakyllWriterOptions {
  105. writerExtensions = wExtensions,
  106. writerHTMLMathMethod = MathJax ""
  107. }
  108. siteReaderOptions = defaultHakyllReaderOptions {
  109. readerExtensions = rExtensions
  110. }
  111. in pandocCompilerWith siteReaderOptions siteWriterOptions