-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
357 lines (338 loc) · 11.3 KB
/
index.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
const GitHubSlugger = require('github-slugger')
const escape = require('escape-html')
const group = require('commonform-group-series')
const has = require('has')
const hash = require('commonform-hash')
const numberToWords = require('number-to-words-en')
const predicate = require('commonform-predicate')
const smartify = require('commonform-smartify')
function renderParagraph (paragraph, offset, path, blanks, options) {
const html5 = options.html5
return (
'<p>' +
paragraph.content
.map((element, index) => {
if (predicate.text(element)) {
return escape(element)
} else if (predicate.use(element)) {
return renderUse(element.use)
} else if (predicate.definition(element)) {
return (
(html5 ? '<dfn>' : '<span class="definition">') +
escape(element.definition) +
(html5 ? '</dfn>' : '</span>')
)
} else if (predicate.link(element)) {
const { link } = element
return `<a href="${escape(link)}">${escape(link)}</a>`
} else if (predicate.blank(element)) {
const elementPath = path.concat('content', offset + index)
const value = matchingValue(elementPath, blanks)
if (value) {
return '<span class="blank">' + escape(value) + '</span>'
} else if (options.complete) {
throw new Error('missing value for blank')
} else {
return '<span class="blank">' + escape('[•]') + '</span>'
}
} else /* if (predicate.reference(element)) */{
return renderReference(element.reference, options)
}
})
.join('') +
'</p>'
)
}
function renderUse (term) {
return (
'<span class="term">' +
escape(term) +
'</span>'
)
}
function renderReference (heading, options) {
if (options.ids) {
options.referenceSlugger.reset()
const slug = options.referenceSlugger.slug(heading)
return `<a class="reference" href="#${slug}">${escape(heading)}</a>`
} else {
return `<span class="reference">${escape(heading)}</span>`
}
}
function matchingValue (path, blanks) {
for (const blank of blanks) {
if (equal(blank.blank, path)) {
return blank.value
}
}
}
function heading (depth, text, options) {
const id = options.ids
? ` id="${encodeURIComponent(options.headingSlugger.slug(text))}"`
: ''
if (depth <= 6) {
return `<h${depth}${id}>${escape(text)}</h${depth}>`
} else {
return `<span class="h${depth}"${id}>${escape(text)}</span>`
}
}
function renderSeries (depth, offset, path, series, blanks, options) {
const simple = options.lists && !series.content.some(containsAHeading)
const html5 = options.html5
if (simple) {
return (
'<ol>' +
series.content
.map(function (child, index) {
const childPath = path.concat('content', offset + index, 'form')
const classes = []
const component = predicate.component(child)
if (component) classes.push('component')
if (!component && child.form.conspicuous) {
classes.push('conspicuous')
}
return (
(
classes.length > 0
? `<li class="${classes.join(' ')}">`
: '<li>'
) +
(
component
? (
renderComponent(
depth,
childPath,
child,
blanks,
options
)
)
: renderChild(depth, childPath, child.form, blanks, options)
) +
'</li>'
)
})
.join('') +
'</ol>'
)
} else {
return series.content
.map((child, index) => {
const childPath = path.concat('content', offset + index, 'form')
const classes = []
const component = predicate.component(child)
if (component) classes.push('component')
if (!component && child.form.conspicuous) {
classes.push('conspicuous')
}
if (!html5) classes.push('section')
return (
(
html5
? classes.length > 0
? `<section class="${classes.join(' ')}">`
: '<section>'
: `<div class="${classes.join(' ')}">`
) +
('heading' in child ? heading(depth, child.heading, options) : '') +
(
component
? (
renderComponent(
depth,
childPath,
child,
blanks,
options
)
)
: renderChild(depth, childPath, child.form, blanks, options)
) +
(html5 ? '</section>' : '</div>')
)
})
.join('')
}
}
function renderChild (depth, path, form, blanks, options) {
return (
renderAnnotations(path, options.annotations, options) +
renderForm(depth, path, form, blanks, options)
)
}
function renderForm (depth, path, form, blanks, options) {
let offset = 0
return group(form)
.map(group => {
const returned = group.type === 'series'
? renderSeries(
depth + 1, offset, path, group, blanks, options
)
: renderParagraph(group, offset, path, blanks, options)
offset += group.content.length
return returned
})
.join('')
}
function renderComponent (depth, path, component, blanks, options) {
if (has(component, 'form')) {
return renderLoadedComponent(depth, path, component, blanks, options)
} else {
return renderComponentReference(depth, path, component, blanks, options)
}
}
function renderLoadedComponent (depth, path, component, blanks, options) {
const style = options.loadedComponentStyle
if (style === 'copy') {
return renderChild(depth, path, component.form, blanks, options)
} else if (style === 'reference') {
return renderLoadedComponentReference(depth, path, component, blanks, options)
} else if (style === 'both') {
return renderLoadedComponentBoth(depth, path, component, blanks, options)
} else {
throw new Error(`Unknown loaded component display style: ${style}`)
}
}
function renderLoadedComponentReference (depth, path, component, blanks, options) {
let returned = '<p>' + escape(options.incorporateComponentText)
returned += ' '
const url = component.reference.component + '/' + component.reference.version
returned += `<a href="${escape(url)}">`
const meta = component.component
returned += `${meta.publisher} ${meta.name} Version ${meta.version}`
returned += '</a>'
const substitutions = component.reference.substitutions
const hasSubstitutions = (
Object.keys(substitutions.terms).length > 0 ||
Object.keys(substitutions.headings).length > 0 ||
Object.keys(substitutions.blanks).length > 0
)
if (hasSubstitutions) {
returned += ' substituting:</p>'
returned += renderSubstitutions(component.reference.substitutions, options)
} else {
returned += '.</p>'
}
return returned
}
function renderLoadedComponentBoth (depth, path, component, blanks, options) {
let returned = renderLoadedComponentReference(depth, path, component, blanks, options)
returned += `<p>${escape(options.quoteComponentText)}</p>`
returned += renderAnnotations(path, options.annotations, options)
returned += '<blockquote>'
returned += renderForm(depth, path, component.form, blanks, options)
returned += '</blockquote>'
return returned
}
function renderComponentReference (depth, path, component, blanks, options) {
const url = component.component + '/' + component.version
const substitutions = component.substitutions
const hasSubstitutions = (
Object.keys(substitutions.terms).length > 0 ||
Object.keys(substitutions.headings).length > 0 ||
Object.keys(substitutions.blanks).length > 0
)
let returned = `<p>${escape(options.incorporateComponentText)} <a href="${escape(url)}">${escape(url)}</a>`
if (hasSubstitutions) {
returned += ' substituting:</p>'
returned += renderSubstitutions(substitutions, options)
} else {
returned += '.</p>'
}
return returned
}
function renderSubstitutions (substitutions, options) {
return '<ul>' +
Object.keys(substitutions.terms).sort().map(from => {
const to = substitutions.terms[from]
return `<li>the term ${renderUse(to)} for the term ${renderUse(from)}</li>`
}).join('') +
Object.keys(substitutions.headings).sort().map(from => {
const to = substitutions.headings[from]
return `<li>references to ${renderReference(to, options)} for references to ${renderReference(from, { ids: false })}</li>`
}).join('') +
Object.keys(substitutions.blanks)
.sort((a, b) => parseInt(a) - parseInt(b))
.map(number => {
const value = substitutions.blanks[number]
return `<li>${quote(value)} for the ${numberToWords.toWordsOrdinal(parseInt(number))} blank</li>`
}).join('') +
'</ul>'
function quote (string) {
return options.smartify ? `“${string}”` : `"${string}"`
}
}
function renderAnnotations (path, annotations, options) {
const tag = options.html5 ? 'aside' : 'div'
return annotations
.filter(annotation => {
return equal(annotation.path.slice(0, -2), path)
})
.map(annotation => {
const classNames = ['annotation', annotation.level]
const paragraph = `<p>${escape(annotation.message)}</p>`
return `<${tag} class="${classNames.sort().join(' ')}">${paragraph}</${tag}>`
})
.join('')
}
module.exports = function (form, blanks, options) {
blanks = blanks || []
options = options || {}
const html5 = options.html5
const title = options.title
const version = options.version
let depth = options.depth || 0
const classNames = options.classNames || []
if (!options.quoteComponentText) {
options.quoteComponentText = 'Quoting for convenience, with any conflicts resolved in favor of the standard:'
}
if (!options.incorporateComponentText) {
options.incorporateComponentText = 'Incorporate'
}
options.annotations = options.annotations || []
if (options.ids) {
options.headingSlugger = new GitHubSlugger()
options.referenceSlugger = new GitHubSlugger()
}
if (!html5) classNames.push('article')
if (form.conspicuous) classNames.push('conspicuous')
classNames.sort()
if (title) depth++
return (
(
html5
? classNames.length === 0
? '<article>'
: `<article class="${classNames.join(' ')}">`
: `<div class="${classNames.join(' ')}">`
) +
(title ? (`<h1>${escape(title)}</h1>`) : '') +
(version ? (`<p class="version">${escape(version)}</p>`) : '') +
(
options.hash
? (`<p class="hash"><code>${hash(form)}</code></p>`)
: ''
) +
renderAnnotations([], options.annotations, options) +
renderForm(depth, [], options.smartify ? smartify(form) : form, blanks, options) +
(html5 ? '</article>' : '</div>')
)
}
function equal (a, b) {
return (
Array.isArray(a) &&
Array.isArray(b) &&
a.length === b.length &&
a.every((_, index) => a[index] === b[index])
)
}
function containsAHeading (child) {
return (
has(child, 'heading') ||
(
has(child, 'form') &&
child.form.content.some(element => has(element, 'form') && containsAHeading(element))
)
)
}