https://observablehq.com/d/6963272269291297
summary = `
/coe3-home
Home
CDP
AD User List
AD Group User List
AWS
Accounts
Toil
Misc
PunchCard
AD User Diff
`
function getlvl(line, left=0){
if (line[0] != ' ') return left
return getlvl(line.slice(1, line.length), left+1)
}
sss = summary.split("\n").reduce((g,c,i) => {
let line = c.trimEnd()
if (line == '') return g
let clvl = getlvl(line)
if (g.trimstart == -1){
g.trimstart = clvl
}
if (g.trimstart != -1){
line = line.slice(g.trimstart, line.length)
clvl -= g.trimstart
}
console.log('======================================')
console.log('line+++', line)
line = line.trim()
console.log('line>>>', line)
let attrs = [...line.matchAll(/\w+=\w+/g)].reduce((g,c,i) => {
line = line.replace(c[0], '')
let [left, right] = c[0].split('=')
g[left] = right
return g
}, {})
line = line.trim()
let lvl = g.lvl
let arr = g.arr
let db = g.db
let _ = 2
console.log(clvl, lvl, JSON.parse(JSON.stringify(arr)), JSON.parse(JSON.stringify(db)), _)
console.log('arr before', arr.map(d => d.name))
if (clvl > lvl){
if (clvl == 0){
db['name'] = line
db['children'] = []
arr.push(db)
}else{
let doc = {
'name': line,
'children': [],
...attrs,
}
arr[arr.length-1]['children'].push(doc)
arr.push(doc)
}
}else if (clvl == lvl){
let doc = {
'name': line,
'children': [],
...attrs,
}
arr = arr.slice(0, arr.length-1)
console.log('insert to', Math.floor(clvl/_)-1)
arr[Math.floor(clvl/_)-1]['children'].push(doc)
console.log(JSON.parse(JSON.stringify(db)))
arr.push(doc)
}else if (clvl < lvl){
arr = arr.slice(0, Math.ceil(clvl/_))
let doc = {
'name': line,
'children': [],
...attrs,
}
arr[arr.length-1]['children'].push(doc)
arr.push(doc)
}
g.lvl = clvl
g.arr = arr
console.log('arr after', arr.map(d => d.name))
return g
}, {lvl:-1, _:2, db:{}, arr: [], trimstart: -1})
{
"name": "/coe3-home",
"children": [
{
"name": "Home",
"children": []
},
{
"name": "CDP",
"children": [
{
"name": "AD User List",
"children": []
},
{
"name": "AD Group User List",
"children": []
}
]
},
{
"name": "AWS",
"children": [
{
"name": "Accounts",
"children": [],
"bg": "red"
}
]
},
{
"name": "Toil",
"children": []
},
{
"name": "Misc",
"children": [
{
"name": "PunchCard",
"children": []
},
{
"name": "AD User Diff",
"children": []
}
]
}
]
}
python
def dbprint(a):
return
def a2s(summary, _=2):
import re
db = {}
arr = []
row = []
lvl = -1
_ = 2
getlvl = lambda x,y=0: y if x[0] != ' ' else getlvl(x[1:], y+1)
for line in summary.split("\n"):
if line.strip() == "": continue
print("\n>>>>>>", line, "\n\n")
clvl = getlvl(line)
line2 = line.split(" ")
line = line.strip()
m = re.findall("((\w+)=([^ $]+))", line)
attr = {}
for [matchall,k,v] in m:
attr[k] = v
line = line.replace(matchall, "")
line = line.strip()
print("lvl", lvl, "clvl", clvl)
print([i['name'] for i in arr])
if clvl > lvl:
if clvl == 0:
db["name"] = line
db["children"] = []
db = {**attr,**db}
arr.append(db)
else:
doc = {
**attr,
"name": line,
"children": [],
}
# if clvl//_ == 2:
# row.append([arr[-1]['name'], line])
# if clvl//_ == 4:
# for user in 'ABCDE':
# print(">>>> add", doc)
# doc['children'].append({
# "name": "user"+user,
# "children": [],
# })
arr[-1]['children'].append(doc)
arr.append(doc)
elif clvl == lvl:
print("clvl", clvl, clvl//_)
#print(arr[(clvl//_)-1]['name'])
doc = {
**attr,
"name": line,
"children": [],
}
arr = arr[:-1]
arr[(clvl//_)-1]['children'].append(doc)
arr.append(doc)
# if clvl//_ == 2:
# row.append({
# "epic": [arr[-1]['name'], line])
# if clvl//_ == 4:
# for user in 'ABCDE':
# doc['children'].append({
# "name": "user"+user,
# "children": [],
# })
elif clvl < lvl:
arr = arr[:clvl//_]
doc = {
**attr,
"name": line,
"children": [],
}
arr[-1]['children'].append(doc)
arr.append(doc)
print("end", [i['name'] for i in arr])
lvl = clvl
return db