-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
292 lines (281 loc) · 8.54 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import fs from 'fs'
import path from 'path'
import assert from 'assert'
import readline from 'readline'
import { fileURLToPath } from 'url';
// Create a readable stream
import rehypeParse from 'rehype-parse'
import ast from 'abstract-syntax-tree'
import rehypeConcatJavaScript from 'rehype-concat-javascript'
import rehypePresetMinify from 'rehype-preset-minify'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
const createName = (line) => {
const line_url = new URL('.', line);
const full_path = line_url.pathname;
const path_parts = full_path.slice(0,-1).split('/');
const { parents, name } = (() => {
if (line.endsWith('/index.html')) {
return {
parents: [...path_parts].slice(0, -1),
name: [...path_parts].pop()
}
}
return {
parents: [...path_parts],
name: line.split('/').pop().split('.').shift()
}
})();
return {
href: line_url.href,
collection: parents.filter((part) => {
if (part.includes('.')) {
return false;
}
return ![
'minerva', 'minerva-story', 'stories'
].includes(part.toLowerCase())
}).pop(),
name
}
}
const extract_called_exhibit = (line, script_nodes) => {
const assignments = script_nodes.filter(node => {
if (node.type != 'ExpressionStatement') {
return false;
}
return node.expression.type=='AssignmentExpression';
});
const call_node = assignments.map(node => {
return node.expression.right;
}).find(node => {
return node.type=='CallExpression'
});
return (node => {
if (node == undefined) {
return undefined;
}
const possible_sources = node.arguments[0].properties;
const properties = possible_sources.filter(property => {
return property.key.name == 'exhibit'
});
const property = properties[0];
if ( property.value.type == 'ObjectExpression' ) {
const exhibit = ast.serialize(property.value);
return { exhibit }
}
const url = new URL(property.value.value, line)
return { url }
})(call_node);
}
const extract_declared_exhibit = (script_nodes) => {
const declarations = script_nodes.filter(node => {
if (node.type!='VariableDeclaration') return false;
return node.declarations.length == 1;
}).map(node => {
const init = node.declarations[0].init;
return {
name: node.declarations[0].id.name,
value: init.value ? init.value : (
ast.serialize(init)
)
};
});
const image_path = declarations.find(declaration => {
return declaration.name == 'image_path';
})?.value;
const exhibit = declarations.find(declaration => {
return declaration.name == 'exhibit';
})?.value;
if (image_path?.length > 0) {
exhibit.Images.forEach(img => img.Path = image_path);
}
return exhibit
}
const parse_file = async (line, text_promise) => {
const text = await text_promise;
const file = await (
unified().use(rehypeParse).use(rehypePresetMinify).use(rehypeConcatJavaScript).parse(text)
)
return await new Promise((resolve, reject) => {
const { name, href, collection } = createName(line);
const html = file.children.find(el => el.tagName == 'html');
const body = html.children.find(el => el.tagName == 'body');
// Note: relies on coincidence that only Minerva Scripts have no properties
const script = body.children.filter(el => el.tagName == 'script').find(el => {
return Object.keys(el.properties).length == 0;
});
assert.ok(!!script, 'missing script tag');
const script_text = script.children.find(el => el.type == 'text');
const script_nodes = ast.parse(script_text.value).body;
const declared_exhibit = extract_declared_exhibit(
script_nodes
);
if (declared_exhibit) {
return resolve({
name, href, collection, exhibit: declared_exhibit
});
}
const called = extract_called_exhibit(
line, script_nodes
);
if (called.exhibit) {
return resolve({
name, href, collection, exhibit: called.exhibit
});
}
return fetch(called.url).then(res => {
return res.json();
}).then(exhibit => {
return resolve({
name, href, collection, exhibit
});
}).catch(e => {
reject(e);
});
});
}
const read_stories = async (urls_txt) => {
const lines = await new Promise((resolve) => {
fs.readFile(urls_txt, {encoding: 'utf-8'}, (err,data) => {
resolve(data.split('\n').filter(line => line));
});
});
const file_promises = lines.map((line) => {
return fetch(line).then(res => {
return { line, text: res.text() };
});
})
const file_texts = await Promise.all(file_promises);
const exhibit_promises = file_texts.map(({ line, text }) => {
return parse_file(line, text).catch(e => {
console.error(e);
});
})
return (await Promise.all(exhibit_promises)).map(parsed => {
const exhibit = proofread_exhibit(
parsed.exhibit, parsed.href
);
return {...parsed, exhibit};
});
}
const proofread_exhibit = (exhibit, href) => {
assert.ok(
!!exhibit.Images, 'missing images'
);
const images = [ ...exhibit.Images ];
const image = { ...exhibit.Images[0] };
const current_name = image.Description;
const misplaced_name = exhibit.Name;
if ( !current_name ) {
image.Description = misplaced_name || '';
}
image.Path = (
new URL(image.Path, href)
).href.replace(/\/+$/,'');
images[0] = image;
return {
...exhibit, Images: images
}
}
const group_collections = (stories) => {
const collections = stories.reduce((obj, story) => {
const collection = obj[story.collection] || {};
assert.ok(
!(story.name in collection),
`duplicate story name: ${story.name}`
);
collection[story.name] = {
exhibit: story.exhibit,
href: story.href
};
obj[story.collection] = collection;
return obj;
},{});
return collections;
}
async function createDir(path) {
fs.mkdirSync(path, { recursive: true });
}
const create_json_subdir = (
out_json_dir, minerva_nominal_version,
collection, story
) => {
if (minerva_nominal_version == 'minerva-1-0') {
const out_json_subdir = path.join(
out_json_dir, `config-${collection}`
);
createDir(out_json_subdir);
return path.join(
out_json_subdir, `${story}.json`
)
}
else if (minerva_nominal_version == 'minerva-1-5') {
const out_json_subdir = path.join(
out_json_dir, `config-${collection}`, story
);
createDir(out_json_subdir);
return path.join(
out_json_subdir, 'exhibit.json'
)
}
assert.ok(false, `unsupported: ${minerva_nominal_version}`)
}
read_stories('urls.txt').then((stories) => {
const domain = 'https://www.cycif.org/data';
const collections = group_collections(stories);
Object.entries(collections).forEach(([k, v]) => {
//console.log(`${k}: `, Object.keys(v).join(','), '\n');
});
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const out_json_dir = path.join(__dirname, '_data');
const out_yaml_dir = path.join(__dirname, 'data');
Object.entries(collections).forEach((item_i) => {
const [ collection, stories ] = item_i;
const out_yaml_subdir = path.join(
out_yaml_dir, `${collection}`
);
createDir(out_yaml_subdir);
Object.entries(stories).forEach((item_j) => {
const [ story, story_details ] = item_j;
const { exhibit, href } = story_details;
const minerva_nominal_version = 'minerva-' + (
('Channels' in exhibit) ? '1-5' : '1-0'
)
const out_json_path = create_json_subdir(
out_json_dir, minerva_nominal_version,
collection, story
)
const out_yaml_path = path.join(
out_yaml_subdir, `${story}.md`
);
assert.ok(
!!exhibit.Images, 'missing images'
);
const image = exhibit.Images[0];
const name = image.Description || story;
assert.ok(!!image?.Path, 'missing image path');
const image_path = image.Path ? `
image: ${image.Path}` : '';
const out_yaml = {
'minerva-1-0': `---
title: "${name}${image_path}"
layout: osd-exhibit
paper: config-${collection}
figure: ${story}
---`,
'minerva-1-5': `---
title: "${name}${image_path}"
layout: minerva-1-5
exhibit: config-${collection}/${story}
---`
}[minerva_nominal_version];
console.log(`${href},${domain}/${collection}/${story}`)
// console.log(out_yaml);
const out_json = JSON.stringify(exhibit);
fs.writeFileSync(out_json_path, out_json);
fs.writeFileSync(out_yaml_path, out_yaml);
});
});
});