Skip to content

Instantly share code, notes, and snippets.

@skyzh
Created June 16, 2017 12:11
Show Gist options
  • Save skyzh/87b601f5034ac236e742bcb0187bbb65 to your computer and use it in GitHub Desktop.
Save skyzh/87b601f5034ac236e742bcb0187bbb65 to your computer and use it in GitHub Desktop.
This script will parse Data under College English Disc to a html containing all answers.
const _ = require('lodash');
const Promise = require('bluebird');
const parser = Promise.promisifyAll(new require('xml2js').Parser());
const fs = Promise.promisifyAll(require('fs'));
let output = "";
fs.readdirAsync(`${__dirname}/data`)
.filter(file => /^1U0[6-7]p[A-Z0-9]{2}[A-F].xml$/.test(file))
.map(file => {
let [_file, unit, exercise] = /^1U([0-9]{2})p([A-Z0-9]{2}[A-F]).xml$/.exec(file);
return fs.readFileAsync(`${__dirname}/data/${file}`)
.then(content => parser.parseStringAsync(content.toString('utf-8')))
.then(content => {
for(k in content) {
return Promise.resolve(content[k]);
}
})
.then(data => {
if (data.Information[0].Info[0].$.Id == 'direction' && data.Answers) {
let __answers = _.chain(data.Answers[0].Blank).map(d => {
let __tips = '';
if (d.Tip) {
__tips = `<br><span class="small text-info">Tips: ${d.Tip}</span>`
}
return `
<li>
${d.$.Id}. ${d.Answer[0]._}
${__tips}
</li>`;
}).reduce((a, b) => a + b).value();
return Promise.resolve(`
<h2>Unit ${unit}, Exercise ${exercise}
<h4 class="text-muted">${data.Information[0].Info[0]._}</h4>
<ul>${__answers}</ul>`.replace(/\|/g, '')
)
} else {
return Promise.resolve('');
}
});
})
.then(data => _.reduce(data, (a, b) => a + b))
.then(result => fs.writeFileAsync('_index.html', `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha256-eZrrJcwDc/3uDhsdt61sL2oOBY362qM3lon1gyExkL0=" crossorigin="anonymous" />
<style>body { font-family: "Noto Serif", "Noto Serif CJK SC Light" }</style>
</head>
<body>
<h1 class="display-4">Keys for College English Book 1</h1>
${result}
</body>
</html>
`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment