hastysite.nim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
import json, strutils, yaml, pegs, os, securehash, sequtils, tables import minim, vendor/moustachu, hastyscribe import config type HastyDirs = object assets*: string contents*: string layouts*: string output*: string temp*: string tempContents: string HastyFiles = object rules*: string meta: string checksums: string modified: seq[string] HastySite* = object settings*: JsonNode dirs*: HastyDirs files*: HastyFiles metadata*: JsonNode NoMetadataException* = ref Exception DictionaryRequiredException* = ref Exception #### MiNiM Library proc hastysite_module*(i: In, hs: HastySite) = i.define("hastysite") .symbol("metadata") do (i: In): i.push i.fromJson(hs.metadata) .symbol("settings") do (i: In): i.push i.fromJson(hs.settings) .symbol("mustache") do (i: In): var t, c: MinValue i.reqQuotationAndString c, t if not c.isDictionary: raise DictionaryRequiredException(msg: "No dictionary provided as template context.") let ctx = newContext(%c) let tplname = t.getString & ".mustache" let tpl = readFile(hs.dirs.layouts/tplname) i.push tpl.render(ctx, hs.dirs.layouts).newval .symbol("markdown") do (i: In): var t, c: MinValue i.reqQuotationAndString c, t if not c.isDictionary: raise DictionaryRequiredException(msg: "No dictionary provided for markdown processor fields.") let options = HastyOptions(toc: false, output: nil, css: nil, watermark: nil, fragment: true) var fields = initTable[string, proc():string]() for items in c.qVal: fields[c.qVal[0].getString] = proc(): string = return $$c.qVal[1] var hastyscribe = newHastyScribe(options, fields) i.push hastyscribe.compileFragment(t.getString).newVal .finalize() #### Helper Functions proc preprocessFile(file, dir: string, obj: var JsonNode): string = let fileid = file.replace(dir, "") var f: File discard f.open(file) var s, yaml = "" result = "" var delimiter = 0 while f.readLine(s): if delimiter >= 2: result &= s else: if s.match(peg"'-' '-' '-' '-'*"): delimiter.inc else: yaml &= "\n" & s if yaml == "": raise NoMetadataException(msg: "No metadata found in file: " & file) if not obj.hasKey("contents"): obj["contents"] = newJObject() obj["contents"][fileid] = yaml.loadToJson()[0] f.close() proc checkFile(file, dir: string, obj: var JsonNode): bool = let fileid = file.replace(dir, "") if not obj.hasKey("contents"): obj["contents"] = newJObject() var oldChecksum = "" if obj["contents"].hasKey(fileid): oldChecksum = obj["contents"][fileid].getStr var newChecksum = $secureHashFile(file) obj["contents"][fileid] = %newChecksum return oldChecksum != newChecksum proc get(json: JsonNode, key, default: string): string = if json.hasKey(key): return json[key].getStr else: return default proc confirmClean(hs: HastySite): bool = stdout.write("Delete directory '$1' and all its contents? [Y/n] " % hs.dirs.temp) let confirm = stdin.readChar return confirm == 'Y' or confirm == 'y' proc quitIfNotExists(file: string) = if not file.fileExists: quit("Error: File '$1' not found." % file) #### Main Functions proc newHastySite*(file: string): HastySite = let json = file.parseFile() result.settings = json result.dirs.assets = json.get("assets", "assets") result.dirs.contents = json.get("contents", "contents") result.dirs.layouts = json.get("layouts", "layouts") result.dirs.output = json.get("output", "output") result.files.rules = json.get("rules", "rules.min") result.dirs.temp = json.get("temp", "temp") result.files.meta = result.dirs.temp / "metadata.json" result.files.checksums = result.dirs.temp / "checksums.json" result.dirs.tempContents = result.dirs.temp / result.dirs.contents proc preprocess*(hs: HastySite) = var meta = newJObject() for f in hs.dirs.contents.walkDirRec(): let content = f.preprocessFile(hs.dirs.contents & DirSep, meta) let dest = hs.dirs.temp/f dest.parentDir.createDir dest.writeFile(content) hs.files.meta.writeFile(meta.pretty) proc detectChanges*(hs: var HastySite) = hs.files.modified = newSeq[string](0) if not hs.files.checksums.fileExists: hs.files.checksums.writeFile("{}") var cs = hs.files.checksums.parseFile() let files = toSeq(hs.dirs.tempContents.walkDirRec()) let dir = hs.dirs.tempContents hs.files.modified = filter(files) do (f: string) -> bool: f.checkFile(dir & DirSep, cs) hs.files.checksums.writeFile(cs.pretty) proc init*(dir: string) = var json = newJObject() json["contents"] = %"contents" json["assets"] = %"assets" json["layouts"] = %"layouts" json["temp"] = %"temp" json["output"] = %"output" for key, value in json.pairs: createDir(dir/value.getStr) json["title"] = %"My Web Site" json["rules"] = %"rules.min" writeFile(dir/json["rules"].getStr, "") writeFile(dir/"config.json", json.pretty) proc clean*(hs: HastySite) = hs.dirs.temp.removeDir proc build*(hs: var HastySite) = echo "Preprocessing..." hs.preprocess() hs.detectChanges() # TODO echo hs.files.modified when isMainModule: import vendor/commandeer proc usage(): string = return """ $1 v$2 - a tiny static site generator (c) 2016 Fabio Cevasco Usage: hastysite command Commands: init Initializes a new site in the current directory. build Builds the site. clean Cleans temporary file. rebuild Rebuilds the site, after cleaning temporary files. Options: -h, --help Print this help -v, --version Print the program version""" % [appname, version] commandline: argument command, string exitoption "help", "h", usage() exitoption "version", "v", version errormsg usage() let pwd = getCurrentDir() let cfg = pwd/"config.json" case command: of "init": pwd.init() of "build": quitIfNotExists(cfg) var hs = newHastySite(cfg) hs.build() of "clean": quitIfNotExists(cfg) var hs = newHastySite(cfg) if hs.confirmClean(): hs.clean() else: quit("Aborted.") of "rebuild": quitIfNotExists(cfg) var hs = newHastySite(cfg) if hs.confirmClean(): hs.clean() hs.build() else: quit("Aborted.") else: quit("Error: Command '$1' is not supported" % command) |