96 lines
3.1 KiB
JavaScript
96 lines
3.1 KiB
JavaScript
const csv = require('fast-csv')
|
|
const fs = require('fs')
|
|
|
|
let records = []
|
|
|
|
/**
|
|
* Parse CSV file and call processor functions on every line.
|
|
*/
|
|
function start (dataFile) {
|
|
csv
|
|
.parseFile(dataFile, {
|
|
delimiter: ';'
|
|
})
|
|
.on("data", function (data) {
|
|
if (data[0] !== 'Bereich') processInput(...data)
|
|
})
|
|
.on("end", createHTML)
|
|
}
|
|
|
|
/**
|
|
* Generate one object for every line in the CSV file and push
|
|
* it to the records array.
|
|
*/
|
|
function processInput (section, chair, category, audience, abbreviation,
|
|
topic, description, deadline, link) {
|
|
records.push({
|
|
section, chair, category, audience, abbreviation, topic, description, deadline, link
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Generate HTML from the records array.
|
|
* The resulting markup is written to ./result.txt.
|
|
*/
|
|
function createHTML() {
|
|
let resultString = ''
|
|
const recordsBySection = groupBy(records, x => x.section)
|
|
|
|
Array.from(['Informatik', 'Angewandte Informatik', 'Wirtschaftsinformatik'])
|
|
.forEach(function (section) {
|
|
if (recordsBySection.has(section)) {
|
|
resultString += `<h1>${section}</h1>\n`
|
|
|
|
let recordsByChair = groupBy(recordsBySection.get(section), x => x.chair)
|
|
let chairs = [...recordsByChair.keys()].sort()
|
|
chairs.forEach(function (chair) {
|
|
resultString += `<h3>${chair}</h3>\n`
|
|
|
|
let recordsByCategory = groupBy(recordsByChair.get(chair), x => x.category)
|
|
let categories = [...recordsByCategory.keys()].sort()
|
|
categories.forEach(function (category) {
|
|
|
|
recordsByCategory.get(category).forEach(function (cur) {
|
|
resultString += `<div style="border: 1px solid lightgray; box-shadow: 0px 2px 4px rgba(0,0,0,0.3); padding: 1rem; margin-bottom: 1rem;"><h5>${category}`
|
|
resultString += cur.abbreviation ? ` ${cur.abbreviation}` : ''
|
|
resultString += `: ${cur.topic} (${cur.audience})</h5>\n`
|
|
resultString += `<p><strong>Kurzbeschreibung:</strong> ${cur.description}</p>\n`
|
|
resultString += `<p><strong>Anmeldefrist</strong>: ${cur.deadline}</p>\n`
|
|
resultString += `<p><strong>Weitere Informationen und Anmeldung:</strong> `
|
|
resultString += cur.link.startsWith('http') ? `<br/><a href="${cur.link}">${cur.link}</a>` : cur.link
|
|
resultString += `</p></div>\n`
|
|
})
|
|
})
|
|
})
|
|
}
|
|
})
|
|
|
|
fs.writeFileSync('result.txt', resultString)
|
|
console.log(`Successfully parsed ${process.argv[2]}.`)
|
|
}
|
|
|
|
if (process.argv[2]) {
|
|
start(process.argv[2])
|
|
} else {
|
|
console.error("Missing argument. You have to pass the name of the data file.")
|
|
}
|
|
|
|
/**
|
|
* Group an array of records by a given key.
|
|
* Taken from the answer to this question:
|
|
* https: //stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects#38327540
|
|
*/
|
|
function groupBy(list, keyGetter) {
|
|
const map = new Map();
|
|
list.forEach((item) => {
|
|
const key = keyGetter(item);
|
|
const collection = map.get(key);
|
|
if (!collection) {
|
|
map.set(key, [item]);
|
|
} else {
|
|
collection.push(item);
|
|
}
|
|
});
|
|
return map;
|
|
}
|