This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
queries-to-json
executable file
·75 lines (69 loc) · 2.62 KB
/
queries-to-json
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
#!/usr/bin/env node
/*! @license MIT ©2014–2016 Ruben Verborgh, Ghent University – imec */
/* Merges the queries from the 'queries' folder into 'settings.json'. */
var fs = require('fs'),
path = require('path');
var queryDir = path.join(__dirname, 'queries'),
settingsFile = path.join(__dirname, 'settings.json'),
outputFile = path.join(__dirname, 'queries.json');
// Write the settings with queries to the output file
var settings = JSON.parse(fs.readFileSync(settingsFile, { encoding: 'utf8' }));
settings.queries = readQueryDirectory(queryDir)
// Sort by ascending priority and name
.sort(function (a, b) {
return (a.priority - b.priority) ||
(a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1);
})
// Remove priority numbers in output
.map(function (d) { return delete d.priority, d; });
fs.writeFileSync(outputFile, JSON.stringify(settings));
// Recursively reads queries in the given directory
function readQueryDirectory(directory) {
return [].concat.apply([], fs.readdirSync(directory).map(function (filename) {
filename = path.join(directory, filename);
if (fs.statSync(filename).isDirectory())
return readQueryDirectory(filename);
else if (/\.sparql$/.test(filename))
return readQuery(filename);
else
return [];
}));
}
// Creates a query from the file
function readQuery(queryFile) {
// Construct the initial query
var query = { sparql: fs.readFileSync(queryFile, { encoding: 'utf8' }),
name: queryFile.replace(/^.+\/|\.\w+$/g, ''),
priority: 1000, datasources: [] };
// Parse SPARQL comments for metadata
var inputLines = query.sparql.split('\n'), outputLines = [];
inputLines.forEach(function (line, index) {
// The line might be a key/value pair
var keyValue = line.match(/^#\s*(\w+)\s*:\s*(.*)\s*/);
if (keyValue) {
var key = keyValue[1].toLowerCase(), value = keyValue[2];
switch (key) {
case 'datasource':
case 'datasources':
value.split(/\s+/)
.forEach(function (d) { if (d.length) query.datasources.push(d); });
return;
}
}
// The first line might contain a name
if (index === 0 && line[0] === '#') {
query.name = line.replace(/#\s*/, '');
// A number preceding the name indicates the priority
if (/^\d/.test(query.name)) {
query.priority = parseInt(query.name, 10);
query.name = query.name.replace(/^\d+\W\s*/, '');
}
}
// Any other line is part of the query
else {
outputLines.push(line);
}
});
query.sparql = outputLines.join('\n').trim();
return query;
}