~kungtotte/dtt

9c4de644d8913153da579acfe2eb549fe2439d95 — Thomas Landin a month ago 782aa0d
Add more vars for the templates

Now you can reference the latest post, a subset of post, and the entire
blog index (no pagination yet):

{{latest_post}}
{{blog_posts}} - A short list of posts (default 5)
{{blog_index}} - The whole list of posts

Each post has the following vars to use:

{{id}} - The slug/title
{{date}} - The creation date
{{{post}}} - The actual post content (triple {{{ for including html)
{{link}} - The relative URL to this blog post for linking to it.

Also fixed a bug with the stale file check where it would error if you
tried to reference blog posts when no blog posts were rebuilt. Now it
will correctly populate the post variables even if they remain
unchanged.
2 files changed, 24 insertions(+), 15 deletions(-)

M src/dtt.nim
M src/utils.nim
M src/dtt.nim => src/dtt.nim +8 -3
@@ 114,8 114,10 @@ proc buildCmd(posts_per_page: int = 5) =
      page.out_path = savepath
      page.tmpl = if existsFile(tmpl_dir / name & ".mustache"): name else: "page"
      pages.add(page)
      if needsUpdate(page.abs_path, page.out_path / page.filename) and isBlog(dir, name):
        let post = buildBlogPost(content_dir / f, tmpl_dir)
      if isBlog(dir, name):
        let
          updatePost = needsUpdate(page.abs_path, page.out_path / page.filename)
          post = buildBlogPost(content_dir / f, tmpl_dir, updatePost)
        posts.add(post)
      else:
        links.add({"target": page.rel_path, "title": page.title}.toTable)


@@ 128,7 130,10 @@ proc buildCmd(posts_per_page: int = 5) =
      continue
    var context = newContext(searchDirs = @[tmpl_dir])
    context["links"] = links
    context["blog_posts"] = postsToContext(posts[0..num_blogs])
    if posts.len > 0:
      context["latest_post"] = postsToContext(posts[0..<1])
      context["blog_posts"] = postsToContext(posts[0..num_blogs])
      context["blog_index"] = postsToContext(posts[0..posts.high])
    let
      rendered = context.renderTemplate(page.abs_path, page.tmpl, tmpl_dir)
      output_file = page.out_path / page.filename

M src/utils.nim => src/utils.nim +16 -12
@@ 72,11 72,20 @@ proc isBlog*(path, name: string): bool =
  else:
    result = false

proc findBaseDir*(starting_dir: string): string =
  if existsDir(starting_dir / content_dir) and existsDir(starting_dir / template_dir):
    return starting_dir
  let (path, _) = splitPath(starting_dir)
  for p in parentDirs(path):
    if existsDir(p / content_dir) and existsDir(p / template_dir):
      return p
  raise new IOError

proc postsToContext*(posts: seq[BlogPost]): seq[Table[string, string]] =
  for post in posts:
    result.add({"id": post.slug, "post": post.content, "date": post.date}.toTable)
    result.add({"id": post.slug, "post": post.content, "date": post.date, "link": post.url}.toTable)

proc buildBlogPost*(full_path: string, tmpl_dir: string): BlogPost =
proc buildBlogPost*(full_path: string, tmpl_dir: string, updatePost: bool = false): BlogPost =
  let
    (dir, name, _) = splitFile(full_path)
    target = if lastPathPart(dir) == "content": name & ".html" else: lastPathPart(dir) / name & ".html"


@@ 88,17 97,12 @@ proc buildBlogPost*(full_path: string, tmpl_dir: string): BlogPost =
  context["slug"] = slug
  context["date"] = date
  context["blog_link"] = target
  let rendered = context.renderTemplate(full_path, "post", tmpl_dir)
  let rendered = if updatePost:
    context.renderTemplate(full_path, "post", tmpl_dir)
  else:
    let cached_file = findBaseDir(getCurrentDir()) / "output" / target
    readFile(cached_file)
  result.slug = slug
  result.date = date
  result.url = target
  result.content = rendered

proc findBaseDir*(starting_dir: string): string =
  if existsDir(starting_dir / content_dir) and existsDir(starting_dir / template_dir):
    return starting_dir
  let (path, _) = splitPath(starting_dir)
  for p in parentDirs(path):
    if existsDir(p / content_dir) and existsDir(p / template_dir):
      return p
  raise new IOError