site.hs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 ["index.html"] $ do
  63. route idRoute
  64. compile $ do
  65. post <- fmap head . recentFirst =<< loadAllSnapshots "posts/*.md" "post_content"
  66. loadAndApplyTemplate "templates/default.html" (postCtxWithTags tags) post
  67. >>= relativizeUrls
  68. >>= changeIdentifier "index.html"
  69. match "templates/*" $ compile templateBodyCompiler
  70. --------------------------------------------------------------------------------
  71. postCtx :: Context String
  72. postCtx =
  73. dateField "date" "%B %e, %Y" `mappend`
  74. defaultContext
  75. postCtxWithTags :: Tags -> Context String
  76. postCtxWithTags tags = tagsField "tags" tags `mappend` postCtx
  77. changeIdentifier :: Identifier -> Item a -> Compiler (Item a)
  78. changeIdentifier idt item = return (itemSetIdentifier idt item)
  79. itemSetIdentifier :: Identifier -> Item a -> Item a
  80. itemSetIdentifier x (Item _ i) = Item x i
  81. siteCompiler :: Compiler (Item String)
  82. siteCompiler =
  83. let addExtensions = [Ext_tex_math_dollars, Ext_tex_math_double_backslash,
  84. Ext_latex_macros, Ext_footnotes, Ext_inline_notes]
  85. wExtensions = foldr S.insert (writerExtensions defaultHakyllWriterOptions) addExtensions
  86. rExtensions = foldr S.insert (readerExtensions defaultHakyllReaderOptions) addExtensions
  87. siteWriterOptions = defaultHakyllWriterOptions {
  88. writerExtensions = wExtensions,
  89. writerHTMLMathMethod = MathJax ""
  90. }
  91. siteReaderOptions = defaultHakyllReaderOptions {
  92. readerExtensions = rExtensions
  93. }
  94. in pandocCompilerWith siteReaderOptions siteWriterOptions