~kungtotte/dtt

281e9471ad2f5b54bde78a06a8b0a680ab05dd4f — Thomas Landin 2 months ago cd1252a
Added a modification time check for performance

Once you get a number of pages/posts together the performance starts to
suffer somewhat (~7s for 40 rendered pages on my machine), and the bulk
of that time is spent inside the markdown() function call which is in
one of the dependencies for this project.

So for the time being we get around this by checking modification times
of the markdown files versus the existing html files and only rebuild
them if the markdown was modified later than the html, or if the html
doesn't exist obviously.

That saves a whole bunch of time as it only takes about 0.2-0.3s for a
single markdown file to render.
3 files changed, 9 insertions(+), 11 deletions(-)

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

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

M src/dtt.nim => src/dtt.nim +3 -1
@@ 114,7 114,7 @@ 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 isBlog(dir, name):
      if needsUpdate(page.abs_path, page.out_path / page.filename) and isBlog(dir, name):
        let post = buildBlogPost(content_dir / f, tmpl_dir)
        posts.add(post)
      else:


@@ 124,6 124,8 @@ proc buildCmd(posts_per_page: int = 5) =
    num_blogs = min(posts.len - 1, posts_per_page)
  sort(posts, dateSort, Descending) # Sort blogs in reverse date order
  for page in pages:
    if not needsUpdate(page.abs_path, page.out_path / page.filename):
      continue
    var context = newContext(searchDirs = @[tmpl_dir])
    context["links"] = links
    context["blog_posts"] = postsToContext(posts[0..num_blogs])

M src/utils.nim => src/utils.nim +5 -9
@@ 27,6 27,11 @@ proc dateSort*(x, y: BlogPost): int =
  else:
    -1

proc needsUpdate*(source, dest: string): bool =
  if not existsFile(dest):
    return true
  return getLastModificationTime(dest) < getLastModificationTime(source)

proc renderTemplate*(context: Context, file: string,
                    tmpl: string = "page",
                    tmpldir: string = "templates"): string =


@@ 59,15 64,6 @@ proc findSlugAndDate(name: string): tuple[slug, date: string] =
    result.date = formatPaddedDate(y, m, d)
  result.slug = result.slug.title().multiReplace(("-", " "), ("_", " "))

proc renderBlog*(file: string, tmpl_dir: string): string =
  let
    (_, name, _) = splitFile(file)
    header = findSlugAndDate(name)
  var context = newContext(searchDirs = @[tmpl_dir])
  context["slug"] = header.slug
  context["date"] = header.date
  result = context.renderTemplate(file, "post", tmpl_dir)

proc isBlog*(path, name: string): bool =
  var
    y, m, d: int