0b401421fd6e0137d564a394090609015cc5b928 — Thomas Landin 4 months ago bc6869b
Add proper template lookup

It will now look for the template filename.mustache for any filename.md
it processes, and will default to post.mustache for anything recognized
as a blog post per the findBlogPosts procedure.

It defaults to page.mustache for any non-blog page without you having to
do anything about it. So if the default template is OK for a new page
you don't need to make a new template for it, but if you want a specific
template for a specific page (i.e. the index page might hold a list of
blog posts) you just copy the page.mustache template and edit that.
1 files changed, 18 insertions(+), 14 deletions(-)

M src/dtt.nim
M src/dtt.nim => src/dtt.nim +18 -14
@@ 106,15 106,24 @@ proc loadMetaData(path: string, mcontext: var Context) =
  mcontext["author"] = cfgdict.getSectionValue("Author", "name")

proc renderTemplate(context: Context, file: string,
                    tmpl: string = "page.mustache",
                    tmpl: string = "page",
                    tmpldir: string = "templates"): string =
    tpl = readFile(tmpldir / tmpl)
    tpl = readFile(tmpldir / tmpl & ".mustache")
    (_,_,ext) = splitFile(file)
  if ext == ".md":
    context["content"] = markdown(readFile(file))
    result = tpl.render(context)

proc findTemplate(filename: string, tmpldir: string, isBlog: bool = false): string =
  let tmpl = case isBlog:
    of true:
    of false:
      if existsFile(tmpldir / filename & ".mustache"): filename else: "page"

  result = tmpl

proc getSlug(filename: string, hasYMD: bool): string =
  result = filename
  if hasYMD:

@@ 144,7 153,7 @@ proc dateSort(x, y: Table[string, string]): int =

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

@@ 153,9 162,11 @@ proc findBlogPosts(directory: string, tmpldir: string, tmpl: string = "post.must
      discard scanf(name, "$i-$i-$i", year, month, day)
        basedir = lastPathPart(dir)
        blog = if basedir in blog_dirs or year > 0: true else: false
      if blog:
        let date = getBlogDate(f, (year, month, day))
        isBlog = if basedir in blog_dirs or year > 0: true else: false
      if isBlog:
          date = getBlogDate(f, (year, month, day))
          tmpl = findTemplate(name, tmpldir, isBlog)
        var blogContext = newContext(searchDirs = @[tmpldir])
        blogContext["slug"] = getSlug(name, year > 0)
        blogContext["date"] = date

@@ 190,14 201,7 @@ proc buildCmd() =
          (dir, fname, ext) = splitFile(f)
          savepath = joinPath(outdir, dir)
        if ext == ".md":
          # TODO: Build in some logic for selecting predetermined templates
          # based on filename. e.g. index -> index.mustache
          # Either hardcode some options or let it look for <name>.mustache
          # It's probably better to just make it look for <name>.mustache.
          var tmpl = "page.mustache"
          if fname == "index":
            tmpl = "index.mustache"
          let tmpl = findTemplate(fname, tmpldir)
          if blogs.len > 0:
            # TODO: Implement proper blog display here
            # Right now it just makes everything into a page and does