render-template.js
5.6 KB
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
'use strict'
var align = require('wide-align')
var validate = require('aproba')
var objectAssign = require('object-assign')
var wideTruncate = require('./wide-truncate')
var error = require('./error')
var TemplateItem = require('./template-item')
function renderValueWithValues (values) {
return function (item) {
return renderValue(item, values)
}
}
var renderTemplate = module.exports = function (width, template, values) {
var items = prepareItems(width, template, values)
var rendered = items.map(renderValueWithValues(values)).join('')
return align.left(wideTruncate(rendered, width), width)
}
function preType (item) {
var cappedTypeName = item.type[0].toUpperCase() + item.type.slice(1)
return 'pre' + cappedTypeName
}
function postType (item) {
var cappedTypeName = item.type[0].toUpperCase() + item.type.slice(1)
return 'post' + cappedTypeName
}
function hasPreOrPost (item, values) {
if (!item.type) return
return values[preType(item)] || values[postType(item)]
}
function generatePreAndPost (baseItem, parentValues) {
var item = objectAssign({}, baseItem)
var values = Object.create(parentValues)
var template = []
var pre = preType(item)
var post = postType(item)
if (values[pre]) {
template.push({value: values[pre]})
values[pre] = null
}
item.minLength = null
item.length = null
item.maxLength = null
template.push(item)
values[item.type] = values[item.type]
if (values[post]) {
template.push({value: values[post]})
values[post] = null
}
return function ($1, $2, length) {
return renderTemplate(length, template, values)
}
}
function prepareItems (width, template, values) {
function cloneAndObjectify (item, index, arr) {
var cloned = new TemplateItem(item, width)
var type = cloned.type
if (cloned.value == null) {
if (!(type in values)) {
if (cloned.default == null) {
throw new error.MissingTemplateValue(cloned, values)
} else {
cloned.value = cloned.default
}
} else {
cloned.value = values[type]
}
}
if (cloned.value == null || cloned.value === '') return null
cloned.index = index
cloned.first = index === 0
cloned.last = index === arr.length - 1
if (hasPreOrPost(cloned, values)) cloned.value = generatePreAndPost(cloned, values)
return cloned
}
var output = template.map(cloneAndObjectify).filter(function (item) { return item != null })
var outputLength = 0
var remainingSpace = width
var variableCount = output.length
function consumeSpace (length) {
if (length > remainingSpace) length = remainingSpace
outputLength += length
remainingSpace -= length
}
function finishSizing (item, length) {
if (item.finished) throw new error.Internal('Tried to finish template item that was already finished')
if (length === Infinity) throw new error.Internal('Length of template item cannot be infinity')
if (length != null) item.length = length
item.minLength = null
item.maxLength = null
--variableCount
item.finished = true
if (item.length == null) item.length = item.getBaseLength()
if (item.length == null) throw new error.Internal('Finished template items must have a length')
consumeSpace(item.getLength())
}
output.forEach(function (item) {
if (!item.kerning) return
var prevPadRight = item.first ? 0 : output[item.index - 1].padRight
if (!item.first && prevPadRight < item.kerning) item.padLeft = item.kerning - prevPadRight
if (!item.last) item.padRight = item.kerning
})
// Finish any that have a fixed (literal or intuited) length
output.forEach(function (item) {
if (item.getBaseLength() == null) return
finishSizing(item)
})
var resized = 0
var resizing
var hunkSize
do {
resizing = false
hunkSize = Math.round(remainingSpace / variableCount)
output.forEach(function (item) {
if (item.finished) return
if (!item.maxLength) return
if (item.getMaxLength() < hunkSize) {
finishSizing(item, item.maxLength)
resizing = true
}
})
} while (resizing && resized++ < output.length)
if (resizing) throw new error.Internal('Resize loop iterated too many times while determining maxLength')
resized = 0
do {
resizing = false
hunkSize = Math.round(remainingSpace / variableCount)
output.forEach(function (item) {
if (item.finished) return
if (!item.minLength) return
if (item.getMinLength() >= hunkSize) {
finishSizing(item, item.minLength)
resizing = true
}
})
} while (resizing && resized++ < output.length)
if (resizing) throw new error.Internal('Resize loop iterated too many times while determining minLength')
hunkSize = Math.round(remainingSpace / variableCount)
output.forEach(function (item) {
if (item.finished) return
finishSizing(item, hunkSize)
})
return output
}
function renderFunction (item, values, length) {
validate('OON', arguments)
if (item.type) {
return item.value(values, values[item.type + 'Theme'] || {}, length)
} else {
return item.value(values, {}, length)
}
}
function renderValue (item, values) {
var length = item.getBaseLength()
var value = typeof item.value === 'function' ? renderFunction(item, values, length) : item.value
if (value == null || value === '') return ''
var alignWith = align[item.align] || align.left
var leftPadding = item.padLeft ? align.left('', item.padLeft) : ''
var rightPadding = item.padRight ? align.right('', item.padRight) : ''
var truncated = wideTruncate(String(value), length)
var aligned = alignWith(truncated, length)
return leftPadding + aligned + rightPadding
}