-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathBuilder.js
More file actions
167 lines (146 loc) · 5.04 KB
/
Builder.js
File metadata and controls
167 lines (146 loc) · 5.04 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
/* eslint-disable max-lines */
import fs from 'fs';
import path from 'path';
import IceCap from 'ice-cap';
import NPMUtil from 'esdoc/out/src/Util/NPMUtil.js';
/**
* Builder base class.
*/
export default class Builder {
/**
* Create builder base instance.
* @param {String} template - template absolute path
* @param {Taffy} data - doc object database.
* @param tags -
* @param builderOptions {object} - options/data specific to the builder.
* @param globalOptions {object} - options/data available to each builder.
*/
constructor(template, data, tags, builderOptions={}, globalOptions={}) {
this._template = template;
this._data = data;
this._tags = tags;
this._builderOptions = builderOptions;
this._globalOptions = globalOptions;
}
/* eslint-disable no-unused-vars */
/**
* execute building output.
* @abstract
* @param builderUtil Utility functions to build with.
* @param builderUtil.writeFile {function(html: string, filePath: string)} - to write files with.
* @param builderUtil.copyDir {function(src: string, dest: string)} - to copy directories with.
* @param builderUtil.readFile {function(filePath: string): string} - to read files with.
*/
exec({writeFile, copyDir, readFile}) {
}
/**
* read html template.
* @param {string} fileName - template file name.
* @return {string} html of template.
* @protected
*/
_readTemplate(fileName) {
const filePath = path.resolve(this._template, `./${fileName}`);
return fs.readFileSync(filePath, {encoding: 'utf-8'});
}
/**
* get output html page title.
* @param {DocObject} doc - target doc object.
* @returns {string} page title.
* @protected
*/
_getTitle(doc = '') {
const name = doc.name || doc.toString();
if (name) {
return `${name}`;
} else {
return '';
}
}
/**
* get base url html page. it is used html base tag.
* @param {string} fileName - output file path.
* @returns {string} base url.
* @protected
*/
_getBaseUrl(fileName) {
return '../'.repeat(fileName.split('/').length - 1);
}
/**
* build common layout output.
* @return {IceCap} layout output.
* @protected
*/
_buildLayoutDoc() {
const ice = new IceCap(this._readTemplate('layout.html'), {autoClose: false});
const packageObj = NPMUtil.findPackage();
if (packageObj) {
ice.text('esdocVersion', `(${packageObj.version})`);
} else {
ice.drop('esdocVersion');
}
ice.load('pageHeader', this._buildPageHeader());
ice.load('nav', this._buildNavDoc());
return ice;
}
/**
* build common page header output.
* @return {IceCap} layout output for page header.
* @protected
*/
_buildPageHeader() {
const ice = new IceCap(this._readTemplate('header.html'), {autoClose: false});
let headerLinks = this._globalOptions.headerLinks;
// If there is no headerLink configuration available, then use the old behaviour:
// insert default headerLinks based on available data.
if (!headerLinks) {
headerLinks = [];
headerLinks.push({
text: "Home",
href: "./"
});
const existManual = this._tags.find(tag => tag.kind.indexOf('manual') === 0);
const manualIndex = this._tags.find(tag => tag.kind === 'manualIndex');
if (!(!existManual || (manualIndex && manualIndex.globalIndex))) {
headerLinks.push({
text: "Manual",
href: "manual/index.html",
cssClass: 'header-manual-link'
});
}
headerLinks.push({
text: "Reference",
href: "identifiers.html",
cssClass: 'header-reference-link'
});
headerLinks.push({
text: "Source",
href: "source.html",
cssClass: 'header-source-link'
});
const existTest = this._tags.find(tag => tag.kind.indexOf('test') === 0);
if (existTest) headerLinks.push({
text: "Test",
href: "test.html",
cssClass: 'header-test-link'
});
}
// Insert all headerLinks into the template
ice.loop('headerLink', headerLinks, (i, link, ice)=>{
ice.text('headerLink', link.text);
ice.attr('headerLink', 'href', link.href);
if (link.cssClass) ice.attr('headerLink', 'class', link.cssClass);
});
return ice;
}
/**
* build common page side-nave output.
* @return {IceCap} layout output for side-nav.
* @protected
*/
_buildNavDoc() {
const html = this._readTemplate('nav.html');
return new IceCap(html);
// TODO: maybe fill nav with something by default?
}
}