-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·179 lines (145 loc) · 5.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* author: Pieter Heyvaert ([email protected])
* Ghent University - imec - IDLab
*/
const fs = require('fs-extra');
const path = require('path');
const downloadGithub = require('download-git-repo');
const parseTemplate = require('parse-author');
const replaceInFile = require('replace');
const yarrrml2rml = require('@rmlio/yarrrml-parser/lib/yarrrml2rml');
const N3 = require('n3');
const namespaces = require('prefix-ns').asMap();
namespaces.ql = 'http://semweb.mmlab.be/ns/ql#';
namespaces.fnml = 'http://semweb.mmlab.be/ns/fnml#';
namespaces.fno = 'http://w3id.org/function/ontology#';
namespaces.ql = 'http://semweb.mmlab.be/ns/ql#';
let csvPath;
function generate(answers, directory) {
const kgPath = path.resolve(directory, 'kg');
const websitePath = path.resolve(directory, 'website');
csvPath = path.resolve(kgPath, 'csv');
return new Promise( (resolve, reject) => {
console.log('\nDownloading required files...');
downloadGithub('kgb-workshop/sad-generator', directory, function (err) {
//console.log(err ? 'Error' : 'Success');
if (err) {
console.error(err);
reject(err);
} else {
console.log('Download complete.');
console.log('Updating CSV files...');
writeGeneralInfo(answers);
writeTopics(answers);
writeOrganizers(answers);
writeImportantDates(answers);
// create empty CSV files
fs.writeFileSync(path.resolve(csvPath, 'pc.csv'), 'id,name,organization');
fs.writeFileSync(path.resolve(csvPath, 'subtopics.csv'), 'id,subtopic');
// update YARRRML and RML rules
replaceBaseURLRules(path.resolve(kgPath, 'mapping.yml'), path.resolve(kgPath, 'mapping.rml.ttl'), answers.baseurl);
// remove files
fs.removeSync(path.resolve(kgPath, 'data.nt'));
fs.removeSync(path.resolve(websitePath, 'docs'));
console.log('Update complete.');
resolve();
}
});
});
}
function writeGeneralInfo(answers) {
let csv = 'id,title,duration,startDate,endDate,location,superEvent,twitter,email\n';
//id
csv += answers.name.replace(/ /g, '-').toLowerCase() + ',';
//rest
csv += `${answers.name},,${answers.startdate},${answers.enddate},${answers.location},${answers.superevent},${answers.twitter},${answers.email}`;
fs.writeFileSync(path.resolve(csvPath, 'general-info.csv'), csv);
}
function writeOrganizers(answers) {
const organizers = answers.organizers.split(',');
let csv = 'id,name,organization,email,photo,twitter,webpage,biography,role,linkedin\n';
organizers.forEach(organizer => {
if (organizer !== '') {
organizer = parseTemplate(organizer);
if (organizer.name) {
csv += organizer.name.replace(/ /g, '-').toLowerCase() + ',';
if (!organizer.email) {
organizer.email = ''
}
if (!organizer.url) {
organizer.url = ''
}
csv += `${organizer.name},,${organizer.email},,,${organizer.url},,,\n`;
} else {
console.error('An organizer should at least have a name.');
}
}
});
fs.writeFileSync(path.resolve(csvPath, 'organizers.csv'), csv);
}
function writeImportantDates(answers) {
const importantDates = answers.importantdates.split(',');
let csv = 'event,date,description\n';
importantDates.forEach(importantDate => {
if (importantDate !== '') {
importantDate = parseTemplate(importantDate);
if (importantDate.name && importantDate.email) {
const date = importantDate.name;
const label = importantDate.email;
let description = '';
if (importantDate.url) {
description = importantDate.url;
}
csv += `${label},${date},${description}\n`;
} else {
console.error('An important date should at least have a date and label.');
}
}
});
fs.writeFileSync(path.resolve(csvPath, 'important-dates.csv'), csv);
}
function writeTopics(answers) {
const topics = answers.topics.split(',');
let csv = 'id,name\n';
topics.forEach(topic => {
topic = topic.trim();
csv += topic.replace(/ /g, '-').toLowerCase() + ',' + topic + '\n';
});
fs.writeFileSync(path.resolve(csvPath, 'topics.csv'), csv);
}
function replaceBaseURLRules(yarrrmlFilePath, rmlFilePath, baseURL) {
return new Promise((resolve, reject) => {
replaceInFile({
regex: "http://example.com/resources/",
replacement: baseURL,
paths: [yarrrmlFilePath],
recursive: false,
silent: true,
});
const yaml = fs.readFileSync(yarrrmlFilePath, 'utf-8');
const y2r = new yarrrml2rml();
const quads = y2r.convert(yaml);
const writer = new N3.Writer({
prefixes: {
rr: namespaces.rr,
rdf: namespaces.rdf,
rdfs: namespaces.rdfs,
fnml: namespaces.fnml,
fno: namespaces.fno,
rml: namespaces.rml,
ql: namespaces.ql
}
});
writer.addQuads(quads);
writer.end((error, result) => {
if (error) {
console.error(error);
reject(error);
} else {
fs.writeFileSync(rmlFilePath, result);
resolve();
}
});
});
}
module.exports = generate;