1
0

Support string templating for key-level attributes

This commit is contained in:
Bán Dénes 2022-01-22 23:13:18 +01:00
parent 6225013828
commit 2b98b502d6
10 changed files with 459 additions and 47 deletions

View File

@ -72,10 +72,6 @@ const render_zone = exports._render_zone = (zone_name, zone, anchor, global_key,
'object'
)()
// propagating object key to name field
col.name = col_name
// combining row data from zone-wide defs and col-specific defs
const actual_rows = Object.keys(prep.extend(zone_wide_rows, col.rows))
@ -98,7 +94,9 @@ const render_zone = exports._render_zone = (zone_name, zone, anchor, global_key,
height: units.$default_height,
padding: units.$default_padding,
skip: false,
asym: 'both'
asym: 'both',
colrow: '{{col.name}}_{{row}}',
name: '{{zone.name}}_{{colrow}}'
}
for (const row of actual_rows) {
const key = prep.extend(
@ -110,10 +108,11 @@ const render_zone = exports._render_zone = (zone_name, zone, anchor, global_key,
col.rows[row] || {}
)
key.name = key.name || `${zone_name}_${col_name}_${row}`
key.zone = zone
key.zone.name = zone_name
key.col = col
key.col.name = col_name
key.row = row
key.colrow = `${col_name}_${row}`
key.stagger = a.sane(key.stagger, `${key.name}.shift`, 'number')(units)
key.spread = a.sane(key.spread, `${key.name}.spread`, 'number')(units)
@ -128,6 +127,13 @@ const render_zone = exports._render_zone = (zone_name, zone, anchor, global_key,
key.skip = a.sane(key.skip, `${key.name}.skip`, 'boolean')()
key.asym = a.in(key.asym, `${key.name}.asym`, ['left', 'right', 'both'])
// templating support
for (const [k, v] of Object.entries(key)) {
if (a.type(v)(units) == 'string') {
key[k] = u.template(v, key)
}
}
keys.push(key)
}

View File

@ -18,6 +18,20 @@ const deep = exports.deep = (obj, key, val) => {
return obj
}
exports.template = (str, vals={}) => {
const regex = /\{\{([^}]*)\}\}/g
let res = str
let shift = 0
for (const match of str.matchAll(regex)) {
const replacement = deep(vals, match[1]) || ''
res = res.substring(0, match.index + shift)
+ replacement
+ res.substring(match.index + match[0].length)
shift += replacement.length - match[0].length
}
return res
}
const eq = exports.eq = (a=[], b=[]) => {
return a[0] === b[0] && a[1] === b[1]
}

View File

@ -19,10 +19,12 @@ matrix:
padding: 19
skip: false
asym: both
colrow: default_default
name: matrix
zone:
name: matrix
col:
rows: {}
key: {}
name: default
row: default
colrow: default_default

View File

@ -19,10 +19,16 @@ matrix_col_row:
padding: 19
skip: false
asym: both
name: matrix_col_row
col:
rows: {}
key: {}
name: col
row: row
colrow: col_row
name: matrix_col_row
zone:
columns:
col: &ref_0
rows: {}
key: {}
name: col
rows:
row: {}
name: matrix
col: *ref_0
row: row

View File

@ -124,7 +124,7 @@ for (let w of cli_what) {
const command = read(t, 'command')
const output_path = exists(t, 'path') ? read(t, 'path') : 'output'
fs.removeSync(output_path)
const version_regex = /\bv\d+\.\d+\.\d+\b/
const version_regex = /\bv\d+\.\d+\.\d+(\-develop)?\b/
// correct execution
if (exists(t, 'log')) {
const ref_log = read(t, 'log').replace(version_regex, '<version>')

View File

@ -22,14 +22,46 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "left_bottom",
"name": "matrix_left_bottom",
"zone": {
"columns": {
"left": null,
"right": {
"key": {
"stagger": 5,
"spread": 25,
"splay": 5,
"origin": [
-9,
-9
]
},
"rows": {
"top": {
"orient": -90,
"shift": [
0,
10
],
"rotate": 90
}
},
"name": "right"
}
},
"rows": {
"bottom": {},
"top": {}
},
"name": "matrix"
},
"col": {
"rows": {},
"key": {},
"name": "left"
},
"row": "bottom",
"colrow": "left_bottom"
"row": "bottom"
}
},
"matrix_left_top": {
@ -55,14 +87,46 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "left_top",
"name": "matrix_left_top",
"zone": {
"columns": {
"left": null,
"right": {
"key": {
"stagger": 5,
"spread": 25,
"splay": 5,
"origin": [
-9,
-9
]
},
"rows": {
"top": {
"orient": -90,
"shift": [
0,
10
],
"rotate": 90
}
},
"name": "right"
}
},
"rows": {
"bottom": {},
"top": {}
},
"name": "matrix"
},
"col": {
"rows": {},
"key": {},
"name": "left"
},
"row": "top",
"colrow": "left_top"
"row": "top"
}
},
"matrix_right_bottom": {
@ -88,7 +152,40 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "right_bottom",
"name": "matrix_right_bottom",
"zone": {
"columns": {
"left": null,
"right": {
"key": {
"stagger": 5,
"spread": 25,
"splay": 5,
"origin": [
-9,
-9
]
},
"rows": {
"top": {
"orient": -90,
"shift": [
0,
10
],
"rotate": 90
}
},
"name": "right"
}
},
"rows": {
"bottom": {},
"top": {}
},
"name": "matrix"
},
"col": {
"key": {
"stagger": 5,
@ -111,8 +208,7 @@
},
"name": "right"
},
"row": "bottom",
"colrow": "right_bottom"
"row": "bottom"
}
},
"matrix_right_top": {
@ -138,7 +234,40 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "right_top",
"name": "matrix_right_top",
"zone": {
"columns": {
"left": null,
"right": {
"key": {
"stagger": 5,
"spread": 25,
"splay": 5,
"origin": [
-9,
-9
]
},
"rows": {
"top": {
"orient": -90,
"shift": [
0,
10
],
"rotate": 90
}
},
"name": "right"
}
},
"rows": {
"bottom": {},
"top": {}
},
"name": "matrix"
},
"col": {
"key": {
"stagger": 5,
@ -161,8 +290,7 @@
},
"name": "right"
},
"row": "top",
"colrow": "right_top"
"row": "top"
}
}
}

View File

@ -22,14 +22,25 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "left_bottom",
"name": "matrix_left_bottom",
"zone": {
"columns": {
"left": null,
"right": null
},
"rows": {
"bottom": {},
"top": {}
},
"name": "matrix"
},
"col": {
"rows": {},
"key": {},
"name": "left"
},
"row": "bottom",
"colrow": "left_bottom"
"row": "bottom"
}
},
"matrix_left_top": {
@ -55,14 +66,25 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "left_top",
"name": "matrix_left_top",
"zone": {
"columns": {
"left": null,
"right": null
},
"rows": {
"bottom": {},
"top": {}
},
"name": "matrix"
},
"col": {
"rows": {},
"key": {},
"name": "left"
},
"row": "top",
"colrow": "left_top"
"row": "top"
}
},
"matrix_right_bottom": {
@ -88,14 +110,25 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "right_bottom",
"name": "matrix_right_bottom",
"zone": {
"columns": {
"left": null,
"right": null
},
"rows": {
"bottom": {},
"top": {}
},
"name": "matrix"
},
"col": {
"rows": {},
"key": {},
"name": "right"
},
"row": "bottom",
"colrow": "right_bottom"
"row": "bottom"
}
},
"matrix_right_top": {
@ -121,14 +154,25 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "right_top",
"name": "matrix_right_top",
"zone": {
"columns": {
"left": null,
"right": null
},
"rows": {
"bottom": {},
"top": {}
},
"name": "matrix"
},
"col": {
"rows": {},
"key": {},
"name": "right"
},
"row": "top",
"colrow": "right_top"
"row": "top"
}
}
}

View File

@ -22,14 +22,17 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "default_default",
"name": "matrix",
"zone": {
"name": "matrix"
},
"col": {
"rows": {},
"key": {},
"name": "default"
},
"row": "default",
"colrow": "default_default"
"row": "default"
}
}
}

View File

@ -22,14 +22,42 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "left_bottom",
"name": "matrix_left_bottom",
"zone": {
"columns": {
"left": null,
"middle": {
"rows": {
"top": {}
},
"key": {},
"name": "middle"
},
"right": {
"key": {
"stagger": "u"
},
"rows": {
"bottom": "$unset",
"home": {},
"top": {}
},
"name": "right"
}
},
"rows": {
"bottom": {},
"home": {}
},
"name": "matrix"
},
"col": {
"rows": {},
"key": {},
"name": "left"
},
"row": "bottom",
"colrow": "left_bottom"
"row": "bottom"
}
},
"matrix_left_home": {
@ -55,14 +83,42 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "left_home",
"name": "matrix_left_home",
"zone": {
"columns": {
"left": null,
"middle": {
"rows": {
"top": {}
},
"key": {},
"name": "middle"
},
"right": {
"key": {
"stagger": "u"
},
"rows": {
"bottom": "$unset",
"home": {},
"top": {}
},
"name": "right"
}
},
"rows": {
"bottom": {},
"home": {}
},
"name": "matrix"
},
"col": {
"rows": {},
"key": {},
"name": "left"
},
"row": "home",
"colrow": "left_home"
"row": "home"
}
},
"matrix_middle_bottom": {
@ -88,7 +144,36 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "middle_bottom",
"name": "matrix_middle_bottom",
"zone": {
"columns": {
"left": null,
"middle": {
"rows": {
"top": {}
},
"key": {},
"name": "middle"
},
"right": {
"key": {
"stagger": "u"
},
"rows": {
"bottom": "$unset",
"home": {},
"top": {}
},
"name": "right"
}
},
"rows": {
"bottom": {},
"home": {}
},
"name": "matrix"
},
"col": {
"rows": {
"top": {}
@ -96,8 +181,7 @@
"key": {},
"name": "middle"
},
"row": "bottom",
"colrow": "middle_bottom"
"row": "bottom"
}
},
"matrix_middle_home": {
@ -123,7 +207,36 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "middle_home",
"name": "matrix_middle_home",
"zone": {
"columns": {
"left": null,
"middle": {
"rows": {
"top": {}
},
"key": {},
"name": "middle"
},
"right": {
"key": {
"stagger": "u"
},
"rows": {
"bottom": "$unset",
"home": {},
"top": {}
},
"name": "right"
}
},
"rows": {
"bottom": {},
"home": {}
},
"name": "matrix"
},
"col": {
"rows": {
"top": {}
@ -131,8 +244,7 @@
"key": {},
"name": "middle"
},
"row": "home",
"colrow": "middle_home"
"row": "home"
}
},
"matrix_middle_top": {
@ -158,7 +270,36 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "middle_top",
"name": "matrix_middle_top",
"zone": {
"columns": {
"left": null,
"middle": {
"rows": {
"top": {}
},
"key": {},
"name": "middle"
},
"right": {
"key": {
"stagger": "u"
},
"rows": {
"bottom": "$unset",
"home": {},
"top": {}
},
"name": "right"
}
},
"rows": {
"bottom": {},
"home": {}
},
"name": "matrix"
},
"col": {
"rows": {
"top": {}
@ -166,8 +307,7 @@
"key": {},
"name": "middle"
},
"row": "top",
"colrow": "middle_top"
"row": "top"
}
},
"matrix_right_home": {
@ -193,7 +333,36 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "right_home",
"name": "matrix_right_home",
"zone": {
"columns": {
"left": null,
"middle": {
"rows": {
"top": {}
},
"key": {},
"name": "middle"
},
"right": {
"key": {
"stagger": "u"
},
"rows": {
"bottom": "$unset",
"home": {},
"top": {}
},
"name": "right"
}
},
"rows": {
"bottom": {},
"home": {}
},
"name": "matrix"
},
"col": {
"key": {
"stagger": "u"
@ -205,8 +374,7 @@
},
"name": "right"
},
"row": "home",
"colrow": "right_home"
"row": "home"
}
},
"matrix_right_top": {
@ -232,7 +400,36 @@
"padding": 19,
"skip": false,
"asym": "both",
"colrow": "right_top",
"name": "matrix_right_top",
"zone": {
"columns": {
"left": null,
"middle": {
"rows": {
"top": {}
},
"key": {},
"name": "middle"
},
"right": {
"key": {
"stagger": "u"
},
"rows": {
"bottom": "$unset",
"home": {},
"top": {}
},
"name": "right"
}
},
"rows": {
"bottom": {},
"home": {}
},
"name": "matrix"
},
"col": {
"key": {
"stagger": "u"
@ -244,8 +441,7 @@
},
"name": "right"
},
"row": "top",
"colrow": "right_top"
"row": "top"
}
}
}

View File

@ -22,6 +22,19 @@ describe('Utils', function() {
should.equal(u.deep(obj, 'non.existent.key'), undefined)
})
it('template', function() {
u.template('arst').should.equal('arst')
u.template('{arst}}').should.equal('{arst}}')
u.template('{{arst}}').should.equal('')
u.template('{{arst}}', {arst: 'neio'}).should.equal('neio')
u.template('{{a}}_{{b}}', {a: 'c', b: 'd'}).should.equal('c_d')
u.template(
'{{longlonglong}}_{{short}}',
{longlonglong: 'long', short: 'shortshortshort'}
).should.equal('long_shortshortshort')
u.template('{{a.b.c}}', {a: {b: {c: 'deep'}}}).should.equal('deep')
})
it('eq', function() {
// basic point usage
u.eq([1, 2], [1, 2]).should.equal(true)