~kungtotte/dtt

f8511157ac9caf628eb4a546575e9c985499fdd2 — Thomas Landin a month ago 0126653
Turn a number of procs into funcs

A func is short-hand for a proc with no side effects (tracked by the
compiler), and most of the code really is without side-effects so I
think it should be easy to see this at a glance as well as have the
compiler check it for us.

Ultimately this is a program designed for file IO so there will be
side-effects, but now it can be boiled down to a select few procs.
3 files changed, 19 insertions(+), 16 deletions(-)

M src/dtt.nim
M src/tmpl.nim
M src/utils.nim
M src/dtt.nim => src/dtt.nim +2 -1
@@ 132,7 132,8 @@ proc buildCmd(posts_per_page: int = 5) =
      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)
      tmpl = loadTemplate(page.tmpl, tmpl_dir)
      rendered = context.renderTemplate(readFile(page.abs_path), tmpl)
      output_file = page.out_path / page.filename
    writeFile(output_file, rendered)


M src/tmpl.nim => src/tmpl.nim +1 -0
@@ 137,6 137,7 @@ pre code {
  background: var(--bg-code);
  padding: 1ch;
  padding-left: 4ch;
  overflow-x: auto;
}
footer {
  max-width: 70ch;

M src/utils.nim => src/utils.nim +16 -15
@@ 19,7 19,7 @@ type
    content: string
    url: string

proc dateSort*(x, y: BlogPost): int =
func dateSort*(x, y: BlogPost): int =
  let xdate = x.date.replace("-","").parseInt
  let ydate = y.date.replace("-","").parseInt
  if xdate > ydate or xdate == ydate:


@@ 30,17 30,16 @@ proc dateSort*(x, y: BlogPost): int =
proc needsUpdate*(source, dest: string): bool =
  if not existsFile(dest):
    return true
  return getLastModificationTime(dest) < getLastModificationTime(source)
  return dest.fileNewer(source)

proc renderTemplate*(context: Context, file: string,
                    tmpl: string = "page",
                    tmpldir: string = "templates"): string =
  let
    tpl = readFile(tmpldir / tmpl & ".mustache")
  context["content"] = markdown(readFile(file))
  result = tpl.render(context)
proc loadTemplate*(name: string = "page", dir: string = "templates"): string =
  return readFile(dir / name & ".mustache")

func renderTemplate*(context: Context, md: string, tmpl: string): string =
  context["content"] = markdown(md)
  result = tmpl.render(context)

proc formatPaddedDate(year, month, day: int): string =
func formatPaddedDate(year, month, day: int): string =
  # Return a zero-padded date string in the format YYYY-MM-DD
  # fmt uses parseInt behind the scenes which will kill leading
  # zeroes so 2020-01-09 becomes 2020-1-9 when using fmt"" which


@@ 51,7 50,7 @@ proc formatPaddedDate(year, month, day: int): string =
    d = intToStr(day, 2)
  result = fmt"{y}-{m}-{d}"

proc findSlugAndDate(name: string): tuple[slug, date: string] =
func findSlugAndDate(name: string): tuple[slug, date: string] =
  # Extract the (optional) YYYY-MM-DD date portion of the filename
  # as well as the remainder of the string properly formatted as a title
  # with any separating characters replaced by spaces.


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

proc isBlog*(path, name: string): bool =
func isBlog*(path, name: string): bool =
  var
    y, m, d: int
  if path in blog_dirs or scanf(name, "$i-$i-$i", y, m, d):


@@ 72,7 71,7 @@ proc isBlog*(path, name: string): bool =
  else:
    result = false

proc findBaseDir*(starting_dir: string): string =
func 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)


@@ 81,7 80,7 @@ proc findBaseDir*(starting_dir: string): string =
      return p
  raise new IOError

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



@@ 97,7 96,9 @@ 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
    tmpl = loadTemplate("post", tmpl_dir)
    rendered = context.renderTemplate(readFile(full_path), tmpl)
  result.slug = slug
  result.date = date
  result.url = target