forked from UTS-eResearch/ro-crate-html-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roc-site.js
executable file
·164 lines (124 loc) · 5.5 KB
/
roc-site.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
#!/usr/bin/env node
const process = require('process');
const program = require('commander');
const fs = require('fs/promises');
const path = require('path');
const ROCrate = require("ro-crate").ROCrate;
//const _ = require("lodash");
const {segmentPath, getLink} = require("./lib/rendering");
const {renderPage} = require('./lib/display');
const renderNew = require('./defaults/static_template.js');
const StaticUtils = require('./lib/static_utils');
const GeoJSON = require("geojson");
const Preview = require("./lib/ro-crate-preview");
const HtmlFile = require("./lib/ro-crate-preview-file");
const CratePruner = require('./lib/prune-crate');
program
.version(require('./lib/version').version)
.description(
"Extracts data from a spreadsheet to make an RO crate"
)
.arguments("<d>")
.option("-c, --config [conf]", "configuration file")
.option("-s, --cratescript [cratescript]", "URL of Crate-script")
.option("-r, --output-path [rep]", "Directory into which to write output", null)
.action((d) => {crateDir = d})
program.parse(process.argv);
const outPath = program.outputPath ? program.outputPath : crateDir;
async function makeRepo(outPath) {
await fs.mkdir(outPath, {recursive: true});
}
function indexByType(crate, config) {
const types = {}
for (let item of crate.getGraph()) {
if (!(item["@id"] === "./" || item["@id"].match(/^ro-crate-metadata.json$/))){
for (t of crate.utils.asArray(item["@type"])) {
if (config.collectionTypes.includes(t)) {
if (!types[t]) {
types[t] = [item];
} else {
types[t].push(item);
}
}
}
}
}
return types;
}
async function main(file) {
repo = await makeRepo(outPath);
const config = JSON.parse(await fs.readFile(program.config, 'utf8'));
if (program.cratescript) {
config.renderScript = program.cratescript;
}
config.utils = new StaticUtils(); // Functions for paths etc
// load the crate
const crate = new ROCrate(JSON.parse(await fs.readFile(path.join(crateDir, "ro-crate-metadata.json"), 'utf8')));
crate.index();
crate.addBackLinks();
repoRoot = crate.getRootDataset();
// Need to have context loaded
await crate.resolveContext();
const Pruner = new CratePruner(crate, config);
const repoCrate = Pruner.prune(repoRoot);
repoCrate.context = crate.context;
repoCrate.index();
repoRoot = repoCrate.getRootDataset();
repoRoot.hasPart = [];
repoCrate._relPath = "./";
const types = indexByType(crate, config);
for (let type of Object.keys(types)) {
const collection =
{"@id": `#type:${type}`,
"@type": "RepositoryCollection",
"name" : `${type} Collection`,
"hasMember": []
}
repoCrate.addItem(collection);
repoRoot.hasPart.push({"@id": collection["@id"]});
for (let item of types[type]) {
console.log("Processing", item.name)
var itemCrate
const Pruner1 = new CratePruner(crate, config);
itemCrate = Pruner1.prune(item);
itemCrate.context = crate.context;
const itemCrateRoot = itemCrate.getRootDataset();
//itemCrateRoot["@reverse"] = [];
itemCrateRoot.name = item.name;
itemCrate._relPath = segmentPath(item["@id"]);
itemCrate._dirPath = path.join(outPath, itemCrate._relPath)
itemCrate.addBackLinks();
// Paths and directory setup
await fs.mkdir(itemCrate._dirPath, {recursive: true});
itemCrate._htmlpath = path.join(itemCrate._dirPath, "ro-crate-preview.html");
itemCrate._relHtmlpath = path.join(itemCrate._relPath, "ro-crate-preview.html");
// TODO - remove this displayable item stuff and make the place finding stuff work with crates
var template;
if (config.types[type] && config.types[type].template){
template = require( path.join(process.cwd(), path.dirname(program.config), config.types[type].template));
} else {
template = renderPage;
}
if (config.types[type] && config.types[type].findPlaces){
findPlaces = require( path.join(process.cwd(), path.dirname(program.config), config.types[type].findPlaces));
places = findPlaces(item, itemCrate);
} else {
places = {};
}
const preview = new Preview(itemCrate, config);
// ATM the only thing we're relying on config for is config.types to tell it what types get their own pages
preview.places = places; // TODO make this work with GeoJSON
const html = await renderNew(item["@id"], preview);
await fs.writeFile(path.join(itemCrate._dirPath, "ro-crate-metadata.json"), JSON.stringify(itemCrate.getJson(), null, 2))
await fs.writeFile(itemCrate._htmlpath, html)
// Add item to relevant collection
collection.hasMember.push({"@id": item["@id"]});
repoCrate.addItem({"@id": item["@id"], "name": item.name, "@type": type});
}
}
const previewAll = new Preview(repoCrate, config);
const html = await renderNew("./", previewAll);
await fs.writeFile(path.join(outPath, "ro-crate-preview.html"), html);
}
main(crateDir);
//console.log(module);