Public
Edited
Feb 12, 2023
Insert cell
# My ASCII to structure
Insert cell

summary2 = `
/coe3-home bg=orange a="\"dfadsf\" ad fas df"
asdf aa=2 b=3
Home
CDP
AD User List
AD Group User Lista
AWS
Accounts bg=red a="dfadf asdfasdf sadf, asdfasd "
Toil
Misc
PunchCard
AD User Diff
`
Insert cell
summary= `
/coe3-home bg=orange a="\"dfadsf\" ad fas df" c=23
asd332f aa=2 b=3
`
Insert cell
g = a2s(summary)
Insert cell
g.db.children[0]
Insert cell
function getlvl(line, left=0){
if (line[0] != ' ') return left
return getlvl(line.slice(1, line.length), left+1)
}
Insert cell
getlvl(' abcdef')
Insert cell
m = 'asdfs a="123 \\"4\\" 56" b=3 aa=324234 b2=324234adf'
Insert cell
breakQuotes(m)
Insert cell
breakQuotes = (sss) => {
let keys = []
let lifo = []
let attrs = {}
let lastc = ''
let key = ''
let isquote = false
for (let c of sss){
console.log('lifo', '|', lastc, '|', c, '|', lifo)
if (key.length > 0 && !isquote && c == ' '){
console.log('lifo case0')
attrs[key] = lifo.join('')
key = ''
lifo = []
isquote = false
lastc = c
continue
}
if (key.length > 0 && isquote && c == '"' && lastc != '\\'){
console.log('lifo case1')
attrs[key] = lifo.join('')
key = ''
lifo = []
isquote = false
lastc = ''
continue
}
if (lastc == '='){
console.log('lifo case2')
lastc = c
if (c == '"'){
console.log('lifo case2.1')
isquote = true
lifo = []
}else{
console.log('lifo case2.2')
lifo.push(c)
}
continue
}
if (c == '='){
console.log('lifo case3')
key = lifo.join('')
lifo = []
keys.push(key)
lastc = c
continue
}
if (c == ' '){
console.log('lifo case4')
if (key.length > 0 && isquote){
console.log('lifo case4.1')

}else{
console.log('lifo case4.2')
lifo = []
}
continue
}
if (c != '\\'){
lifo.push(c)
}
lastc = c
}
if (lifo.length > 0 && key.length > 0){
attrs[key] = lifo.join('')
}
return {
attrs,
keys,
}
}
Insert cell
summary.split("\n").map(d => console.log(`|${d.trimEnd()}|`))
Insert cell
function a2s(summary){
return 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)
line = line.replaceAll('\\"', ';;;;;')
let attrs2 = [...line.matchAll(/\w+="([^"]+)"/g)].reduce((g,c,i) => {
console.log('attrs2', c)
line = line.replace(c[0], '')
let [left, ...right] = c[0].split('=')
let val = right.join('=')
val = val.slice(1, val.length-1)
g[left] = val.replaceAll(';;;;;', '\\"')
return g
}, {})
let attrs = [...line.matchAll(/\w+=[^"][^ ]+/g)].reduce((g,c,i) => {
console.log('attrs', c)
line = line.replace(c[0], '')
let [left, ...right] = c[0].split('=')
g[left] = right.join('=')
return g
}, {})
for (let k in attrs2)
attrs[k] = attrs2[k]
line = line.trim()
console.log('line>>>2', line)
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'] = []
for (let k in attrs)
db[k] = attrs[k]
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})
}



Insert cell
JSON.stringify(sss.db, null, 4)
Insert cell
a = 'asdfasdf bg=asdf lala=fasdf '
Insert cell
[...a.matchAll(/\w+=\w+/g)]
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more