-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2o.js
109 lines (89 loc) · 3.13 KB
/
p2o.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
const fs = require("fs");
const path = require("path");
const peggy = require("peggy");
const generate = require("./src/generate");
let peggydata;
let input_src;
const default_out_dir = path.resolve(__dirname, "output");
const template_path = path.resolve(__dirname, 'templates');
let groups = [];
let args = process.argv.slice(2);
let input_file = args.shift();
console.warn();
console.warn(`=== Loading PlantUML File ===================================`);
try {
input_src = fs.readFileSync(path.resolve(input_file), {encoding: 'utf-8', flag:'r'});
console.warn(`Successfully loaded file ${input_file}`);
console.warn();
} catch (error) {
if (input_file) console.warn(`Error: "${input_file}" couln not be opened!`);
if (!input_file) console.warn(`Error: No input file provided!`);
console.warn();
console.warn(error);
process.exit();
}
const grammar_src = fs.readFileSync(path.resolve(__dirname, "src", "p2o.peggy"), {encoding: 'utf-8', flag:'r'});
const parser = peggy.generate(grammar_src);
console.warn();
console.warn(`=== Generating Ontology Data Structure ======================`);
try {
peggydata = parser.parse(input_src);
console.warn(`Success!`);
console.warn();
} catch (error) {
console.warn();
console.warn(error);
process.exit();
}
let activegroup, elem;
function emptygroup() {
return {
templates: [],
extension: "md",
json: false,
outdir: default_out_dir
}
}
if (!args.length) {
groups.push(emptygroup());
}
while (args.length) {
activegroup = emptygroup();
elem = args.shift();
while (elem && elem.substring(0,1) != "-") {
if (elem != "json") activegroup.templates.push(elem+".hbs");
if (elem == "json") activegroup.json = true;
elem = args.shift();
}
if (elem == "-e") {
elem = args.shift();
activegroup.extension = elem;
elem = args.shift();
}
if (elem == "-o") {
elem = args.shift();
activegroup.outdir = path.resolve(__dirname, elem);
elem = args.shift();
}
if (elem) args.unshift(elem);
groups.push(activegroup);
}
console.warn();
console.warn(`=== Generating Output For Templates =========================`);
groups.forEach((currentgroup) => {
if (currentgroup.templates.length == 0 && currentgroup.json == true) {
// generate only json-representation
generate.toJson(peggydata, currentgroup.outdir);
} else if (currentgroup.templates.length == 0 && currentgroup.json == false) {
// use all templates in template dir
generate.all(peggydata, template_path, currentgroup.extension, currentgroup.outdir);
} else {
// use all specified templates and maybe generate json-representation
currentgroup.templates.forEach((tmpl) => generate.byTemplate(peggydata, template_path, tmpl, currentgroup.extension, currentgroup.outdir));
if (currentgroup.json) generate.toJson(peggydata, currentgroup.outdir);
}
});
console.warn()
console.warn(`========================= Good Bye ==========================`);
process.exit();
// console.warn(ontology);