~kungtotte/dtt

c91518bdfdbada779bf107516372bfb647f87a88 — Thomas Landin 4 months ago dd657b4
Add nav generation

Automatically create nav links for any non-blog page you have on your
site. We don't want a giant long list of nav links at the top to each
individual blog post so we exclude those from the list of links.

I had to add the base_url thing to the config to make nav generation as
simple as possible, since now everything is relative to the base_url and
saves a lot of complicated logic in the link generation stage. It's not
a major headache though for anyone using the tool.
2 files changed, 19 insertions(+), 3 deletions(-)

M dtt.nimble
M src/dtt.nim
M dtt.nimble => dtt.nimble +1 -1
@@ 1,6 1,6 @@
# Package

version       = "0.1.1"
version       = "0.2.0"
author        = "Thomas Landin"
description   = "A straight-forward Markdown/Html static site generator."
license       = "GPL-3.0"

M src/dtt.nim => src/dtt.nim +18 -2
@@ 18,7 18,7 @@ const
  blog_dirs = ["blog", "blogs", "post", "posts", "articles", "journal", "journals"]

let
  version = "0.1.2"
  version = "0.2.0"

type
  DttError {.pure.} = enum


@@ 97,6 97,9 @@ proc loadMetaData(path: string, mcontext: var Context) =
  # Load meta-data from config file into the supplied
  # mustache context
  let cfgdict = loadConfig(path)
  var url = cfgdict.getSectionValue("Site", "base_url")
  url.removeSuffix('/') # Modifies string in-place, so we need to do it here
  mcontext["base_url"] = url
  mcontext["charset"] = cfgdict.getSectionValue("Site", "charset")
  mcontext["language"] = cfgdict.getSectionValue("Site","language")
  mcontext["page_title"] = cfgdict.getSectionValue("Site","title")


@@ 115,6 118,19 @@ proc renderTemplate(context: Context, file: string,
    context["content"] = markdown(readFile(file))
    result = tpl.render(context)

proc findLinks(content_dir: string, excludes: seq[Table[string,string]]): seq[Table[string, string]] =
  var blog_ids: seq[string]
  for b in excludes:
    blog_ids.add(b["id"])

  for f in walkDirRec(content_dir, relative = true):
    let (dir, name, _) = splitFile(f)
    let
      target = if dir != "": dir & "/" & name else: name
      title = name.title()
    if not (name in blog_ids):
      result.add({"target": target, "title": title}.toTable)

proc findTemplate(filename: string, tmpldir: string, isBlog: bool = false): string =
  let tmpl = case isBlog:
    of true:


@@ 189,8 205,8 @@ proc buildCmd() =
    createDir(outdir)

  loadMetaData(wd / "config.cfg", mcontext)

  let blogs = findBlogPosts(condir, tmpldir)
  mcontext["links"] = findLinks(condir, excludes=blogs)

  for f in walkDirRec(condir, {pcFile, pcDir}, relative = true):
    case getFileInfo(condir / f).kind: