~dbalan/blogng

eb676545687f41825dda4bd4fa8212332c094dec — Dhananjay Balan 4 years ago f41c5e6
Implement Tags!
2 files changed, 27 insertions(+), 9 deletions(-)

M site.hs
M templates/post.html
M site.hs => site.hs +21 -3
@@ 34,7 34,7 @@ feedConfig = FeedConfiguration {

main :: IO ()
main = hakyllWith config $ do
    tags <- extractTags
    tags <- buildTags "blog/*" (fromCapture "tags/*.html")

    match "images/*" $ do
        route   idRoute


@@ 59,9 59,9 @@ main = hakyllWith config $ do
    match "blog/*" $ do
        route $  octopressRoute
        compile $ pandocCompiler
            >>= loadAndApplyTemplate "templates/post.html"    postCtx
            >>= loadAndApplyTemplate "templates/post.html"    (postCtxWTags tags)
            >>= saveSnapshot "content"
            >>= loadAndApplyTemplate "templates/default.html" postCtx
            >>= loadAndApplyTemplate "templates/default.html" (postCtxWTags tags)
            >>= relativizeUrls

    match "index.html" $ do


@@ 93,6 93,21 @@ main = hakyllWith config $ do
                >>= loadAndApplyTemplate "templates/default.html" archiveCtx
                >>= relativizeUrls

    -- tags!
    tagsRules tags $ \tag pattern -> do
        let title = "Posts tagged \"" ++ tag ++ "\""
        route idRoute
        compile $ do
            posts <- recentFirst =<< loadAll pattern
            let ctx = constField "title" title
                      `mappend` listField "posts" postCtx (return posts)
                      `mappend` defaultContext

            makeItem ""
                >>= loadAndApplyTemplate "templates/tag.html" ctx
                >>= loadAndApplyTemplate "templates/default.html" ctx
                >>= relativizeUrls

    create ["atom.xml"] $ do
      route idRoute
      compile $ do


@@ 107,6 122,9 @@ extractTags = do
  tags <- buildTags ("blogs/**" .&&. hasNoVersion) $ fromCapture "tags/*.html"
  return $ sortTagsBy caseInsensitiveTags tags

postCtxWTags :: Tags -> Context String
postCtxWTags tags = tagsField "tags" tags <> postCtx

postCtx :: Context String
postCtx =
    dateField "date" "%B %e, %Y" `mappend`

M templates/post.html => templates/post.html +6 -6
@@ 1,12 1,12 @@
<article class="group">
    <h1>$title$</h1>
    <p class="subtitle">$date$</p>
    <p class="subtitle">$date$
    $if(tags)$
    , posted in $tags$
    $endif$
    </p>

    <section>
        $body$
    </section>
    <section>
        $if(tags)$
        <br>Posted in <i>$tags$</i>
        $endif$
    </section>
</article>