site.hs 4.3 KB

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