~kungtotte/dtt

38e66e6242c30acfdbce018a74fa97134cafd8e0 — Thomas Landin 2 months ago 6df7e90
Remove cleanCmd and config.cfg code

I realised I don't really need/use a clean command, my OS has tools for
taking care of that so I don't need to build it into dtt.

Also cleaning out remnants of config.cfg code since I don't use it
anymore.
3 files changed, 3 insertions(+), 46 deletions(-)

M src/dtt.nim
M src/tmpl.nim
M src/utils.nim
M src/dtt.nim => src/dtt.nim +0 -24
@@ 68,7 68,6 @@ proc initCmd(dir: string, force: bool = false) =
    stderr.writeLine(fmt"{bin}: Exception: {e} with message: {msg}")
    quit(ord(DttError.CreateDirectoryFailed))

  writeFile(wd / "config.cfg", tmpl.cfg)
  writeFile(wd / template_dir / "page.mustache", tmpl.page)
  writeFile(wd / template_dir / "post.mustache", tmpl.post)
  writeFile(wd / template_dir / "header.mustache", tmpl.header)


@@ 76,24 75,6 @@ proc initCmd(dir: string, force: bool = false) =
  writeFile(wd / output_dir / css_dir / "style.css", tmpl.style)
  setFilePermissions(wd / output_dir / css_dir / "style.css", outFilePerms)

proc cleanCmd(all: bool = false, force: bool = false) =
  let wd = if all: os.getCurrentDir() else: os.getCurrentDir() / output_dir
  if not force and not existsFile(os.getCurrentDir() / "config.cfg"):
    stderr.writeLine(fmt"""{bin}: config.cfg not found, this doesn't look like a {bin} directory. are you sure you wish to clean it? Confirm action with -f/--force.""")
    quit(ord(DttError.NotADttDirectory))
  if os.existsDir(wd):
    try:
      os.removeDir(wd)
    except OSError:
      let
        e = repr(getCurrentException())
        msg = getCurrentExceptionMsg()
      stderr.writeLine(fmt"{bin}: Exception {e} with message: {msg}")
      quit(ord(DttError.RemoveDirectoryFailed))
  else:
    stderr.writeLine(fmt"{bin}: {wd} does not exist.")
    quit(ord(DttError.DirectoryNotFound))

proc buildCmd(posts_per_page: int = 5) =
  let working_dir = try:
    findBaseDir(os.getCurrentDir())


@@ 152,7 133,6 @@ proc buildCmd(posts_per_page: int = 5) =
      output_file = page.out_path / page.filename
    writeFile(output_file, rendered)


let doc = """

dtt (Do The Thing)


@@ 165,11 145,9 @@ content from any host.
usage:
  dtt init [options] [DIR]
  dtt build [options] [POSTS]
  dtt clean [options]

Options:
  -f --force  Force init to overwrite existing files (if any)
  -a --all    Force clean to delete every file created by dtt, not just output directory
  -h --help   Show this screen.
  --version   Show version.
"""


@@ 178,8 156,6 @@ when isMainModule:

  if args["init"]:
    initCmd($args["DIR"], args["--force"])
  elif args["clean"]:
    cleanCmd(args["--all"], args["--force"])
  elif args["build"]:
    let posts = if args["POSTS"].kind == vkNone: 5 else: parseInt($args["POSTS"])
    buildCmd(posts)

M src/tmpl.nim => src/tmpl.nim +0 -12
@@ 1,16 1,4 @@
let
  cfg* = """
[Site]
charset = "UTF-8"
language = "en"
title = "My Own Webpage"
blogs_per_page = "4"
license = "CC-BY-SA"
license_url = "https://creativecommons.org/licenses/by-sa/2.0/"
[Author]
name = "Your Name"
email = "your@email.tld"
"""
  page* = """
<!DOCTYPE html>
<html lang="{{language}}">

M src/utils.nim => src/utils.nim +3 -10
@@ 1,5 1,5 @@
import os, strutils, strformat
import parsecfg, strscans
import strscans
import unicode, tables, times

import markdown


@@ 99,17 99,10 @@ proc buildBlogPost*(full_path: string, tmpl_dir: string): BlogPost =
  result.content = rendered

proc findBaseDir*(starting_dir: string): string =
  if existsDir(starting_dir / content_dir):
  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):
    if existsDir(p / content_dir) and existsDir(p / template_dir):
      return p
  raise new IOError

proc findConfigFile*(full_path: string): tuple[cfg: Config, wd: string] =
  let (path, name) = splitPath(full_path)
  for p in parentDirs(path):
      if existsFile(p / name):
        return (loadConfig(p / name), p)
  raise new IOError