|
| 1 | +/* |
| 2 | + This script generates the en.json file with the data from the data.json. |
| 3 | + It's run by the Grunt task runner. |
| 4 | +*/ |
| 5 | +function getenJSON() { |
| 6 | + var fs = require('fs'); |
| 7 | + var data = fs.readFileSync('src/templates/pages/reference/data.json'); |
| 8 | + var dataJSON = JSON.parse(data); |
| 9 | + var staticStrings = fs.readFileSync( |
| 10 | + 'src/templates/pages/reference/staticStrings.json' |
| 11 | + ); |
| 12 | + var staticStringsJSON = JSON.parse(staticStrings); |
| 13 | + var enJSON = {}; |
| 14 | + |
| 15 | + // static strings |
| 16 | + for (var sstr in staticStringsJSON) { |
| 17 | + enJSON[sstr] = staticStringsJSON[sstr]; |
| 18 | + } |
| 19 | + |
| 20 | + // modules |
| 21 | + for (var p5Module in dataJSON.modules) { |
| 22 | + if (p5Module !== 'p5.sound') { |
| 23 | + enJSON[p5Module] = p5Module; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + // classes: builds the p5 classes objects |
| 28 | + for (var p5Class in dataJSON.classes) { |
| 29 | + var entry = dataJSON['classes'][p5Class]; |
| 30 | + var classObj = buildClassObj(entry); |
| 31 | + enJSON[entry.name] = classObj; |
| 32 | + } |
| 33 | + |
| 34 | + // classitems: adds methods and properties to their respective class object |
| 35 | + for (var p5ItemIdx in dataJSON.classitems) { |
| 36 | + var entry2 = dataJSON.classitems[p5ItemIdx]; |
| 37 | + // only consider the items that have a name |
| 38 | + if (entry2.name) { |
| 39 | + var itemObj = buildItemObj(entry2); |
| 40 | + enJSON[entry2.class][entry2.name] = itemObj; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + fs.writeFileSync( |
| 45 | + 'src/data/reference/en.json', |
| 46 | + JSON.stringify(enJSON, null, 2), |
| 47 | + err => { |
| 48 | + if (err) { |
| 49 | + console.error(err); |
| 50 | + return; |
| 51 | + } |
| 52 | + } |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +function buildClassObj(p5Class) { |
| 57 | + var classObj = {}; |
| 58 | + |
| 59 | + if (p5Class.description) { |
| 60 | + classObj['description'] = getParagraphs(p5Class.description); |
| 61 | + } |
| 62 | + |
| 63 | + if (p5Class.return) { |
| 64 | + classObj['returns'] = buildReturnObj(p5Class.return); |
| 65 | + } |
| 66 | + |
| 67 | + if (p5Class.params) { |
| 68 | + classObj['params'] = buildParamsObj(p5Class.params); |
| 69 | + } |
| 70 | + return classObj; |
| 71 | +} |
| 72 | + |
| 73 | +function buildItemObj(p5Item) { |
| 74 | + var itemObj = {}; |
| 75 | + |
| 76 | + if (p5Item.description) { |
| 77 | + itemObj['description'] = getParagraphs(p5Item.description); |
| 78 | + } |
| 79 | + if (p5Item.return) { |
| 80 | + itemObj['returns'] = buildReturnObj(p5Item.return); |
| 81 | + } |
| 82 | + |
| 83 | + if (p5Item.itemtype === 'method') { |
| 84 | + if (p5Item.params) { |
| 85 | + itemObj['params'] = buildParamsObj(p5Item.params); |
| 86 | + } |
| 87 | + if (p5Item.overloads) { |
| 88 | + itemObj = getOverloads(p5Item, itemObj); |
| 89 | + } |
| 90 | + } |
| 91 | + return itemObj; |
| 92 | +} |
| 93 | + |
| 94 | +function buildReturnObj(returns) { |
| 95 | + return returns.type + ': ' + getText(returns.description); |
| 96 | +} |
| 97 | + |
| 98 | +function buildParamsObj(params) { |
| 99 | + var paramsObj = {}; |
| 100 | + |
| 101 | + params.forEach(p => { |
| 102 | + var descr = p.type; |
| 103 | + if (p.description) { |
| 104 | + descr += ': '; |
| 105 | + } |
| 106 | + if ('optional' in p && p['optional']) { |
| 107 | + descr += ' (Optional) '; |
| 108 | + } |
| 109 | + descr += p.description; |
| 110 | + paramsObj[p.name] = getText(descr); |
| 111 | + }); |
| 112 | + return paramsObj; |
| 113 | +} |
| 114 | + |
| 115 | +function getOverloads(p5Item, itemObj) { |
| 116 | + p5Item.overloads.forEach(o => { |
| 117 | + if (o.params) { |
| 118 | + var moreParams = buildParamsObj(o.params); |
| 119 | + if (itemObj.params) { |
| 120 | + for (var p in moreParams) { |
| 121 | + if (!(p in itemObj.params)) { |
| 122 | + itemObj['params'][p] = moreParams[p]; |
| 123 | + } |
| 124 | + } |
| 125 | + } else { |
| 126 | + itemObj['params'] = moreParams; |
| 127 | + } |
| 128 | + } |
| 129 | + }); |
| 130 | + return itemObj; |
| 131 | +} |
| 132 | + |
| 133 | +// returns the 'clean' version of the input text |
| 134 | +function getText(str) { |
| 135 | + return str |
| 136 | + .trim() |
| 137 | + .replace(/<p>|<\/p>|<br>/g, '') |
| 138 | + .replace(/\n|\s+/g, ' '); |
| 139 | +} |
| 140 | + |
| 141 | +// returns an array containing the 'clean' versions of the text in the <p> tags of the input text |
| 142 | +function getParagraphs(text) { |
| 143 | + return text |
| 144 | + .trim() |
| 145 | + .replace(/<\/p>|<br>/g, '') |
| 146 | + .replace(/\n|\s+/g, ' ') |
| 147 | + .split(/<p>/) |
| 148 | + .filter(x => x.length > 0); |
| 149 | +} |
| 150 | + |
| 151 | +module.exports = getenJSON; |
0 commit comments