-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3013 from w3c/update-jsep-mapper
Upgrade jsep-mapper code
- Loading branch information
Showing
3 changed files
with
75 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters