Skip to content

Commit

Permalink
Merge pull request #3013 from w3c/update-jsep-mapper
Browse files Browse the repository at this point in the history
Upgrade jsep-mapper code
  • Loading branch information
dontcallmedom authored Oct 30, 2024
2 parents c013120 + b01cb55 commit 5b23fab
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 25 deletions.
54 changes: 31 additions & 23 deletions jsep-mapping/mapper.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
var parseXml = require("xml2js").parseString;
var request = require("request");
var fs = require("fs");
const {Parser} = require("xml2js");
const fs = require("fs").promises;

var url = process.argv[2];
const url = process.argv[2];
if (!url) {
console.log("Usage: " + process.argv[1] + " <jsep_url>");
process.exit(2);
}

function sectionMapper(level, map) {
return function (section, i) {
var number = level + (i + 1) + ".";
const number = level + (i + 1) + ".";
map.sections[section['$'].anchor] = number;
if (section.section) {
section.section.forEach(sectionMapper(number, map));
}
}
};
}

request({url: url, headers: { 'User-Agent': 'W3C JSEP Mapper'}},
function(err, response, body) {
if (err || response.statusCode != 200) {
console.error("Failed to fetch " + url + ": " + ( err || response.statusCode));
process.exit(2);
(async function(url) {
let body;
try {
const res = await fetch(url, { headers: {'User-Agent': 'W3C JSEP Mapper'} });
if (res.status !== 200) {
console.error("Fetching " + url + " resulted in HTTP error: " + res.status);
process.exit(2);

}
var data = parseXml(body, function (err, res) {
var map = {sections:{}, metadata: {}};
map.metadata.version = res.rfc['$'].docName.split('-').pop();
map.metadata.authors = res.rfc.front[0].author.map(function (a) { return a['$'].fullname;});
var rfcDate = res.rfc.front[0].date[0]['$'];
if (rfcDate) {
map.metadata.date = rfcDate['day'] + ' ' + rfcDate['month'] + ' ' + rfcDate['year'];
}
res.rfc.middle[0].section.forEach(sectionMapper("", map));
fs.writeFileSync("map.json", JSON.stringify(map, null, 2));
});
});
body = await res.text();
} catch (err) {
console.error("Failed to fetch " + url + ": " + err);
process.exit(2);
}
const parser = new Parser();
const data = await parser.parseStringPromise(body);
const map = {sections:{}, metadata: {}};
map.metadata.version = data.rfc['$'].docName.split('-').pop();
map.metadata.authors = data.rfc.front[0].author.map(function (a) { return a['$'].fullname;});
const rfcDate = data.rfc.front[0].date[0]['$'];
if (rfcDate) {
map.metadata.date = (rfcDate['day'] ?? '') + ' ' + rfcDate['month'] + ' ' + rfcDate['year'];
}
data.rfc.middle[0].section.forEach(sectionMapper("", map));
await fs.writeFile("map.json", JSON.stringify(map, null, 2));
})(url);

43 changes: 43 additions & 0 deletions jsep-mapping/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions jsep-mapping/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"private": true,
"license": "MIT",
"dependencies": {
"request": "^2.67.0",
"xml2js": "^0.4.15"
"xml2js": "^0.6.2"
}
}

0 comments on commit 5b23fab

Please sign in to comment.