|
21 | 21 | * |
22 | 22 | */ |
23 | 23 | /*global require*/ |
| 24 | +/*jslint regexp:true */ |
24 | 25 |
|
25 | 26 | (function () { |
26 | 27 | "use strict"; |
27 | 28 |
|
28 | 29 | var fs = require("fs"), |
29 | 30 | https = require("https"), |
30 | | - instaview = require("instaview"), |
31 | | - unescape = require("lodash.unescape"); |
| 31 | + instaview = require("instaview"); // Wikitext > HTML |
32 | 32 |
|
33 | | - instaview.conf.paths.articles = "https://docs.webplatform.org/wiki/"; |
| 33 | + instaview.conf.paths.articles = "https://docs.webplatform.org/wiki/"; // base URL for every link |
| 34 | + instaview.conf.locale.image = "__Image__"; // disable <img> tags |
34 | 35 |
|
35 | 36 | var propertiesURL = "https://docs.webplatform.org/w/api.php?action=ask&format=json&query=%20%5B%5BPath%3A%3A~css%2Fproperties%2F*%5D%5D%7C%3FSummary%7Cprettyprint%3Dno%7Climit%3D100000", // #ask: [[Path::~css/properties/*]]|?Summary|prettyprint=no|limit=100000 |
36 | 37 | valuesURL = "https://docs.webplatform.org/w/api.php?action=ask&format=json&query=%5B%5BValue%20for%20property%3A%3A~css%2Fproperties%2F*%5D%5D%7C%3FProperty%20value%7C%3FProperty%20value%20description%7C%3FValue%20for%20property%7Cprettyprint%3Dno%7Climit%3D100000"; // #ask: [[Value for property::~css/properties/*]]|?Property value|?Property value description|?Value for property|prettyprint=no|limit=100000 |
|
39 | 40 | outputFile = "../css.json", |
40 | 41 | propertiesResponse = "", |
41 | 42 | valuesResponse = ""; |
| 43 | + |
| 44 | + function htmlEscape(str) { |
| 45 | + return str.replace(/<(\/?)([^>]*)>/g, function (match, slash, inner) { |
| 46 | + if (["code", "div", "tt"].indexOf(inner) === -1) { // escape all tags except <code>, <div>, <tt> |
| 47 | + return "<" + slash + inner + ">"; |
| 48 | + } |
| 49 | + return match; |
| 50 | + }); |
| 51 | + } |
42 | 52 |
|
43 | 53 | console.log("Getting properties"); |
44 | 54 | https.get(propertiesURL, function (res) { |
|
53 | 63 | var data = propertiesResponse[propertyName]; |
54 | 64 | var propertyData = {}; |
55 | 65 | if (data.printouts.Summary.length) { |
56 | | - propertyData.SUMMARY = instaview.convert(data.printouts.Summary[0]); |
| 66 | + propertyData.SUMMARY = instaview.convert(htmlEscape(data.printouts.Summary[0])); |
57 | 67 | propertyData.URL = data.fullurl; |
58 | 68 | propertyData.VALUES = []; |
59 | 69 |
|
|
67 | 77 | }); |
68 | 78 |
|
69 | 79 | res.on("end", function () { |
70 | | - function parseHTMLEntities(str) { |
71 | | - return str.replace(/&#([0-9]{1,4});/g, function (match, numStr) { |
72 | | - var num = parseInt(numStr, 10); |
73 | | - return String.fromCharCode(num); |
74 | | - }); |
75 | | - } |
76 | | - |
77 | 80 | console.log("Parsing values"); |
78 | 81 | valuesResponse = JSON.parse(valuesResponse).query.results; |
79 | 82 |
|
80 | 83 | Object.keys(valuesResponse).forEach(function (valueIdentifier) { |
81 | 84 | var data = valuesResponse[valueIdentifier].printouts; |
82 | 85 | var forProperty = data["Value for property"].length && data["Value for property"][0].fulltext; |
83 | 86 | var valueData = {}; |
| 87 | + var description; |
84 | 88 | if (data["Property value"].length && forProperty && result.hasOwnProperty(forProperty)) { |
85 | 89 | valueData.DESCRIPTION = ""; |
86 | 90 | if (data["Property value description"].length) { |
87 | | - valueData.DESCRIPTION = instaview.convert(data["Property value description"][0]); |
| 91 | + // Remove possible "alt=...;" (image Wikitext) |
| 92 | + description = htmlEscape(data["Property value description"][0].replace(/\|alt=([^;]*);/, "|$1")); |
| 93 | + valueData.DESCRIPTION = instaview.convert(description); |
88 | 94 | } |
89 | | - valueData.TITLE = parseHTMLEntities(unescape(data["Property value"][0])); |
| 95 | + valueData.TITLE = instaview.convert(htmlEscape(data["Property value"][0])).substr(3); // trim <p> tag |
90 | 96 |
|
| 97 | + // FUTURE: Currently, there's no deterministic order for the value listing |
91 | 98 | result[forProperty].VALUES.push(valueData); |
92 | 99 | } |
93 | 100 | }); |
|
0 commit comments