-
Notifications
You must be signed in to change notification settings - Fork 13
/
generator.js
149 lines (133 loc) · 4.96 KB
/
generator.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
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const { MDNPage } = require('./mdnPage.js');
const { Finder } = require('@jpmedley/mdn-helper/finder.js');
const { SourcePage } = require('./sourcePage.js');
const { COMPAT_TABLE, HEADER_MACROS, SPEC_TABLE } = require('./utils.js');
const IN = `content/en-US/api/`;
class _Generator {
constructor(source) {
this._sourcePage = new SourcePage(`${IN}${source}`);
const finder = new Finder(['Finder', this._sourcePage.title, '-f', '-o']);
this._IDL = finder.findAndReturn();
this._mdnPages = [];
this._interfaceName = source.split('.md')[0];
}
generate() {
this._makeInterface();
this._makeConstructor();
this._makeEvents();
this._makeMethods();
this._makeProperties();
this._replaceVariable('[[shared:interface]]', this._interfaceName);
this._writeContent();
}
_replaceVariable(variable, value) {
for (let m of this._mdnPages) {
m.replaceString(variable, value);
}
}
_writeContent() {
for (let m of this._mdnPages) {
m.write();
}
const writePath = this._mdnPages[0].mdnDirPath;
const msg = `\nNew files written to:\n\t${writePath}`;
console.log(msg);
}
_makeInterface() {
let interfaceText = this._sourcePage.interfaceText;
interfaceText = this.stripReaderComments(interfaceText);
interfaceText = interfaceText.replace('---<', `---\n${HEADER_MACROS}\n\n<`);
const interfacePage = new MDNPage(this._interfaceName, 'interface');
interfacePage.replaceContent(interfaceText);
interfacePage.append(`\n${SPEC_TABLE}\n`);
interfacePage.append(`\n${COMPAT_TABLE}\n`);
this._mdnPages.push(interfacePage);
}
_makeConstructor() {
let constructorText = this._sourcePage.constructorText;
if (!constructorText) { return; }
const constructorPage = new MDNPage(this._interfaceName, 'constructor');
const description = this._sourcePage.description;
constructorPage.inject(description, { location: "[[description]]" });
this._mdnPages.push(constructorPage);
}
_makeEvents() {
let eventText = this._sourcePage.events;
if (!eventText) { return; }
let newMDNPage;
const memberList = this._splitList(eventText);
for (let m of memberList) {
newMDNPage = new MDNPage(m[0], 'eventhandler');
let callbackName = m[0].split('.')[1];
let eventName = callbackName.substring(2);
newMDNPage.inject(eventName, { location: "[[eventName]]" });
newMDNPage.inject(callbackName, { location: "[[EventHandler]]" });
newMDNPage.inject(m[1], { location: "[[eventOccurs]]", lcStart: true });
this._mdnPages.push(newMDNPage);
}
}
_makeMethods() {
let methodText = this._sourcePage.methods;
if (!methodText) { return; }
let newMDNPage;
const methodList = this._splitList(methodText);
for (let m of methodList) {
newMDNPage = new MDNPage(m[0], 'method');
newMDNPage.inject(m[1], { location: "[[description]]", lcStart: true });
newMDNPage.inject(m[0], { location: "[[method]]" });
this._mdnPages.push(newMDNPage);
}
}
_makeProperties() {
let propertyText = this._sourcePage.properties;
if (!propertyText) { return; }
let newMDNPage;
const memberList = this._splitList(propertyText);
for (let m of memberList) {
newMDNPage = new MDNPage(m[0], 'property');
const names = m[0].split('.');
let propertyName = names[1];
newMDNPage.inject(propertyName, { location: "[[property]]" });
let description = m[1];
if (description.includes("{{readonlyInline}}")) {
newMDNPage.inject(" read-only", { location: "[[readOnly]]"});
description = description.split("{{readonlyInline}}")[1].trim();
} else {
newMDNPage.inject("", { location: "[[readOnly]]" });
description = description.trim();
}
newMDNPage.inject(description, { location: "[[description]]", lcStart: true });
this._mdnPages.push(newMDNPage);
}
}
_splitList(sourceText) {
let newList = [];
for (let s in sourceText) {
if (sourceText[s].includes('`**')) {
let sourceLines = sourceText[s].split('`**');
sourceLines[1] = sourceLines[1].replace('\n', '').trim();
newList.push(sourceLines);
}
}
return newList;
}
stripReaderComments(content) {
const SHIPPING_NOTICE = /\*\*When this feature ships[^*]*\*\*/;
return content.replace(SHIPPING_NOTICE, '');
}
}
module.exports.Generator = _Generator;