-
Notifications
You must be signed in to change notification settings - Fork 95
/
updateKeywords.js
242 lines (207 loc) · 5.83 KB
/
updateKeywords.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
const fs = require('fs');
const path = require('path');
// This script is inspired by the old perl script here:
// https://github.com/processing/processing-docs/blob/master/generate/keywords_create.cgi
// TODO: Go through base and check if there are some things we can remove
// and just rely on generated code instead:
// - String
// - All Data > Primitive
const folder = path.join(
__dirname,
'..',
'content',
'references',
'translations',
'en',
'processing'
);
/**
A list of names to ignore, based on the old perl script.
An entry such a "height" will ignore both `height` and `PImage.height`
**/
const ignore = ['width', 'height', 'x', 'y', 'z', 'Object'];
/**
A list of global functions that need to have special handling
**/
const globalFuncs = [
'keyPressed()',
'keyReleased()',
'keyTyped()',
'mouseClicked()',
'mouseDragged()',
'mouseMoved()',
'mousePressed()',
'mouseReleased()',
'mouseWheel()',
'settings()',
'setup()',
'draw()'
];
/**
This script creates the keywords.txt file that is used to do syntax highligting
in the Processing IDE.
**/
const updateKeywords = () => {
if (!processingRepoExists()) {
console.error(
'To run this script, you must have the processing4 repo next to the processing-website repo on your computer.'
);
return;
}
// Load the base keywords
const [baseKeywords, baseContents] = parseKeywordsFile('keywords_base.txt');
// Load all json files
const entries = fs.readdirSync(folder).map((file) => {
let entry = require(path.join(folder, file));
entry = Object.assign(
{
filename: file,
basename: path.basename(file, '.json')
},
entry
);
entry.leftName = leftName(entry);
entry.rightName = rightName(entry);
entry.token = getToken(entry);
return entry;
});
// Sort by listing in original keywords.txt
entries.sort(sortByClassAndGlobal);
// Run through and add if not already in base
const keywords = [];
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
if (shouldInclude(baseKeywords, entry)) {
keywords.push([entry.leftName, entry.token, entry.rightName]);
}
}
const pad = (str, size) => (str ? str.padEnd(size, ' ') : str);
// Save to keywords.txt file
const combined = baseContents + '\n\n' + generateKeywordsFile(keywords);
fs.writeFileSync(
path.join(__dirname, '..', '..', 'processing4', 'java', 'keywords.txt'),
combined
);
console.log('keywords.txt file written to the processing4 repo!');
};
/**
Sorts the entries based on the basename
**/
const sortByClassAndGlobal = (a, b) => {
const strippedA = a.basename.replace(/_$/, '').toLowerCase();
const strippedB = b.basename.replace(/_$/, '').toLowerCase();
if (strippedA < strippedB) {
return -1;
}
if (strippedA > strippedB) {
return 1;
}
return 0;
};
/**
Looks to see if the entry is in the base keywords.
**/
const shouldInclude = (keywords, entry) => {
// Don't include ignored names
if (ignore.indexOf(entry.name) > -1) {
return false;
}
// See if entry is in the keywords base
for (let i = 0; i < keywords.length; i++) {
if (keywords[i][0] == entry.leftName && keywords[i][2] == entry.basename) {
return false;
}
}
// Otherwise, include
return true;
};
/**
Parse a keywords file into an array of arrays
**/
const parseKeywordsFile = (filename) => {
const content = fs.readFileSync(path.join(__dirname, filename)).toString();
const lines = content.split('\n');
const keywords = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line[0] !== '#' && line.length > 0) {
keywords.push(line.split(/\s/));
}
}
return [keywords, content];
};
const generateKeywordsFile = (keywords) => {
let str = '';
for (let i = 0; i < keywords.length; i++) {
str += `${keywords[i][0]}\t${keywords[i][1]}\t${keywords[i][2]}\n`;
}
return str;
};
/**
Handle special cases
**/
const specialCases = {
'?: (conditional)': '?',
'/** */ (doc comment)': '/**',
'/* */ (multiline comment)': '/*'
};
/**
Makes the clean version of the name for the left-hand side of the file
**/
const operatorRegex = /\s\([a-zA-Z\s]+\)$/;
const leftName = (entry) => {
// Special handling
if (specialCases[entry.name]) {
return specialCases[entry.name];
}
// Operators are renamed: += (add assign) => +=
if (entry.name.match(operatorRegex)) {
return entry.name.replace(operatorRegex, '');
}
// Functions and class methods are renamed: float() => float
if (entry.type === 'function' || entry.type === 'method') {
return entry.name.replace(/\(\)$/, '');
}
// Variables are renamed: pixels[] => pixels
if (entry.type === 'field' || entry.type === 'other') {
return entry.name.replace(/\[\]$/, '');
}
// Just use name (Constants, classes, primitives, etc)
return entry.name;
};
/**
Make a name suitable for the right hand side
**/
const rightName = (entry) => {
// global functions appear as just the name in the right
if (globalFuncs.includes(entry.name)) {
return entry.name.replace(/\(\)$/, '');
}
// Everything else uses the basename (fill_, PVector_add_, etc)
return entry.basename;
};
/**
Gets the syntax category. Based on the old pearl script.
**/
const getToken = (entry) => {
if (globalFuncs.includes(entry.name)) {
return 'FUNCTION4';
} else if (entry.type === 'class') {
return 'KEYWORD5';
} else if (entry.type === 'method') {
return 'FUNCTION2';
} else if (entry.type === 'other') {
return 'KEYWORD4';
} else if (entry.type === 'field') {
return 'KEYWORD2';
}
return 'FUNCTION1';
};
/**
Checks whether the processing4 repo is next to this repo
**/
const processingRepoExists = (keywords) => {
const siblings = fs.readdirSync(path.join(__dirname, '..', '..'));
return siblings.includes('processing4');
};
updateKeywords();