~kungtotte/dtt

bad3b7ffad4c0fd46ca67909df415c0498e22f29 — Thomas Landin 4 months ago a436136
Add sorting to blog entries

Now it sorts them automatically. For now it goes oldest first,
but that's an easy change.
1 files changed, 16 insertions(+), 3 deletions(-)

M src/dtt.nim
M src/dtt.nim => src/dtt.nim +16 -3
@@ 1,6 1,7 @@
import os, strutils, strformat
import parsecfg, strscans
import unicode, times
import algorithm

import docopt
import mustache


@@ 132,6 133,14 @@ proc getBlogDate(file: string, datestring: tuple[year, month, day: int]): string
    # using the yyyy-MM-dd-slug.md format.
    result = getCreationTime(file).format("yyyy-MM-dd")

proc dateSort(x, y: Table[string, string]): int =
  let xdate = x["date"].replace("-","").parseInt
  let ydate = y["date"].replace("-","").parseInt
  if xdate > ydate or xdate == ydate:
    1
  else:
    -1

proc findBlogPosts(directory: string, tmpldir: string, tmpl: string = "post.mustache"): seq[Table[string, string]] =
  for f in walkDirRec(directory):
    let (dir, name, ext) = splitFile(f)


@@ 143,11 152,15 @@ proc findBlogPosts(directory: string, tmpldir: string, tmpl: string = "post.must
        basedir = lastPathPart(dir)
        blog = if basedir in blog_dirs or year > 0: true else: false
      if blog:
        let date = getBlogDate(f, (year, month, day))
        var blogContext = newContext(searchDirs = @[tmpldir])
        blogContext["slug"] = getSlug(name, year > 0)
        blogContext["date"] = getBlogDate(f, (year, month, day))
        let rendered = blogContext.renderTemplate(f, tmpl)
        result.add({"post": rendered}.toTable)
        blogContext["date"] = date
        let
          rendered = blogContext.renderTemplate(f, tmpl)
        let tbl = {"id": name, "post": rendered, "date": date}.toTable
        result.add(tbl)
  sort(result, dateSort)

proc buildCmd() =
  let