add support for tags
authorLucian Mogosanu <lucian.mogosanu@gmail.com>
Sat, 20 Jul 2013 13:31:14 +0000 (16:31 +0300)
committerLucian Mogosanu <lucian.mogosanu@gmail.com>
Sat, 20 Jul 2013 13:31:14 +0000 (16:31 +0300)
css/default.css
posts/000-first-post.markdown
site.hs
templates/post.html

index d4fb30d..4b538dd 100644 (file)
@@ -29,7 +29,7 @@ a:hover {
        color: #555;
        font-size: 15px;
        font-style: italic;
-       margin-top: -12px;
+       margin-top: -15px;
 }
 
 .info sup {
@@ -37,6 +37,10 @@ a:hover {
        font-style: normal;
 }
 
+.tags {
+       color: #333;
+}
+
 #header {
        border-bottom: 1.5px dashed #aaa;
        margin-bottom: 30px;
index 55af945..66aef3f 100644 (file)
@@ -3,6 +3,7 @@ postid: 000
 title: First post
 author: Lucian MogoČ™anu
 date: July 17, 2013
+tags: asphalt
 ---
 
 First post.
diff --git a/site.hs b/site.hs
index a4543ea..c4f327d 100644 (file)
--- a/site.hs
+++ b/site.hs
@@ -2,14 +2,26 @@
 import           Data.Monoid (mappend)
 import           Hakyll
 
+-- wrapping it up
 main :: IO ()
 main = hakyll $ do
+  -- tags
+  tags <- buildTags "posts/*" $ fromCapture "tags/*.html"
+
+  -- content
   match "index.html" compileIndex
   match "css/*" compileCss
-  match "posts/*" compilePosts
+  match "posts/*" $ compilePosts tags
   match (fromList ["about.markdown"]) compilePages
   create ["archive.html"] compileArchive
+
+  -- tags rules
+  tagsRules tags $ compileTags tags
+
+  -- rss feed
   create ["rss.xml"] compileRss
+
+  -- compile templates
   match "templates/*" $ compile templateCompiler
 
 -- compilers go here
@@ -32,13 +44,14 @@ compileCss = do
   route idRoute
   compile compressCssCompiler
 
-compilePosts :: Rules ()
-compilePosts = do
+compilePosts :: Tags -> Rules ()
+compilePosts tags = do
   route $ setExtension "html"
+  let ctx = tagsCtx tags
   compile $ pandocCompiler
     >>= saveSnapshot "content"
-    >>= loadAndApplyTemplate "templates/post.html" postCtx
-    >>= loadAndApplyTemplate "templates/default.html" postCtx
+    >>= loadAndApplyTemplate "templates/post.html" ctx
+    >>= loadAndApplyTemplate "templates/default.html" ctx
     >>= relativizeUrls
 
 compilePages :: Rules ()
@@ -62,6 +75,22 @@ compileArchive = do
       >>= loadAndApplyTemplate "templates/default.html" archiveCtx
       >>= relativizeUrls
 
+compileTags :: Tags -> String -> Pattern -> Rules ()
+compileTags tags tag pattern = do
+  let title = "Posts tagged '" ++ tag ++ "'"
+  route idRoute
+  compile $ do
+    posts <- loadAll pattern >>= recentFirst
+    let tagCtx =
+          constField "title" title                 `mappend`
+          listField "posts" postCtx (return posts) `mappend`
+          tagsCtx tags
+    makeItem ""
+      >>= loadAndApplyTemplate "templates/post-list.html" tagCtx
+      >>= loadAndApplyTemplate "templates/default.html" tagCtx
+      >>= relativizeUrls
+
+
 compileRss :: Rules ()
 compileRss = do
   -- shamelessly stolen from
@@ -82,6 +111,9 @@ compileRss = do
 postCtx :: Context String
 postCtx = dateField "date" "%B %e, %Y" `mappend` defaultContext
 
+tagsCtx :: Tags -> Context String
+tagsCtx tags = tagsField "tags" tags `mappend` postCtx
+
 -- support for RSS feeds
 tarpitFeed :: FeedConfiguration
 tarpitFeed = FeedConfiguration
index 3166e43..54b9e84 100644 (file)
@@ -1,9 +1,7 @@
 <div class="info">
        <sup>$postid$</sup>
-       Posted on $date$
-       $if(author)$
-               by $author$
-       $endif$
+       $date$ --
+       <span class="tags">$tags$</span>
 </div>
 
 $body$