logo

Grimgrains

Unnamed repository; edit this file 'description' to name the repository.
commit: e84bcba409b13e89f9c46bafd9d77a8475c65fd5
parent 8d11ac582eb5a93e3aee329d43dd85192e9ebca9
Author: Devine Lu Linvega <aliceffekt@gmail.com>
Date:   Thu,  8 Mar 2018 10:02:26 +1300

First working copy of Indental()

Diffstat:

Mscripts/database/recipes.js8++++++++
Mscripts/graph.js2+-
Mscripts/nodes/indental.js81+++++++++++++++++++++++++++++++++++++++++++------------------------------------
3 files changed, 53 insertions(+), 38 deletions(-)

diff --git a/scripts/database/recipes.js b/scripts/database/recipes.js @@ -1,4 +1,12 @@ DATABASE.recipes = ` + +ROOT + LEAF + LEAF + SOMETHING + LEAF + + SPINACH PAJEON DATE : 2014-08-19 TAGS : korean dinner sidedish diff --git a/scripts/graph.js b/scripts/graph.js @@ -30,7 +30,7 @@ function graph() ]) Ø("router").syphon("database") - Ø("database").syphon(["recipes","ingredients","pages"]) + Ø("database").syphon(["recipes"]) Ø("template").syphon("recipe") diff --git a/scripts/nodes/indental.js b/scripts/nodes/indental.js @@ -15,57 +15,64 @@ function IndentalNode(id,rect) function Parser(data) { - this.result = build(0,refs_tree(data)) + this.result = build(data.split("\n").map(liner)) - function build(target,tree) + function build(lines) { - var pack = null - var leaves = [] - var name = tree.lines[target] - for(id in tree.lines){ - if(tree.refs[id]-1 != target){ continue; } - if(!pack){ pack = {}; } - if(tree.lines[id].indexOf(" : ") > -1){ - var parts = tree.lines[id].split(" : "); - pack[parts[0]] = parts[1] - } - else{ - pack[tree.lines[id]] = build(id,tree) - } + var stack = {} + var target = lines[0] + for(id in lines){ + var line = lines[id] + if(line.skip){ continue; } + target = stack[line.indent-2]; + if(target){ target.children.push(line) } + stack[line.indent] = line } - return pack ? pack : nip(target,tree,leaves) + return nip(lines); } - function nip(target,tree,leaves) + function nip(lines) { - var tips = {} - tips[tree.lines[target]] = leaves - return Object.values(tips)[0].length == 0 ? Object.keys(tips)[0] : tips + var h = {} + + for(id in lines){ + var line = lines[id]; + if(line.skip || line.indent > 0){ continue; } + h[line.content] = format(line) + } + + return h } - function refs_tree(d) + function format(line) { - var l = d.split("\n"); - var stash = {}; - var prev = {indent:-1}; - var refs = [] - var lines = []; - var i = 0 - for(id in l){ - var line = liner(l[id]); - if(line.skip){ continue; } - stash[line.indent] = line.indent > prev.indent ? i : stash[line.indent]; - refs.push(stash[line.indent]); - lines.push(line.content) - prev = line - i += 1 + var a = []; + var h = {}; + for(id in line.children){ + var child = line.children[id]; + if(child.key){ + h[child.key] = child.value + } + else if(child.children.length == 0){ + a.push(child.content) + } + else{ + h[child.content] = format(child) + } } - return {refs:refs,lines:lines}; + return a.length > 0 ? a : h } function liner(line) { - return {indent:line.search(/\S|$/),content:line.trim(),skip:line == "" || line.substr(0,1) == "~"} + return { + indent:line.search(/\S|$/), + content:line.trim(), + skip:line == "" || line.substr(0,1) == "~", + key:line.indexOf(" : ") > -1 ? line.split(" : ")[0].trim() : null, + value:line.indexOf(" : ") > -1 ? line.split(" : ")[1].trim() : null, + children:[] + } } } } \ No newline at end of file