Skip to content

Commit e31fcce

Browse files
committed
Add exampleEntities method in markdown renderer
1 parent 6e152c0 commit e31fcce

File tree

6 files changed

+78
-451
lines changed

6 files changed

+78
-451
lines changed

lib/profile.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
const { ROCrate } = require('ro-crate');
12
const rules = require('./rules');
23
const {validateEntity} = require('./validator');
34
const fs = require('fs');
5+
const path = require('path');
46

57

68
/**
@@ -27,8 +29,36 @@ class LdacProfile {
2729
}
2830
ruleText[key] = text;
2931
}
32+
// load examples
33+
const examples = {};
34+
for (const dir of fs.readdirSync('examples')) {
35+
const cratePath = path.join('examples', dir, 'ro-crate-metadata.json');
36+
try {
37+
const json = JSON.parse(fs.readFileSync(cratePath, 'utf8'));
38+
examples[dir] = new ROCrate(json, {array: true, link: true});
39+
} catch (error) {
40+
}
41+
}
42+
function exampleEntities(exampleName, entityIds) {
43+
var result = '';
44+
if (exampleName && examples[exampleName]) {
45+
entityIds = [].concat(entityIds);
46+
let part = '';
47+
for (const id of entityIds) {
48+
const entity = examples[exampleName].getEntity(id);
49+
if (entity) {
50+
part += '```json\n' + JSON.stringify(entity.toJSON(), null, 2) + '\n```\n';
51+
}
52+
}
53+
if (part) {
54+
part += `[source](../example/${exampleName}/README.md)\n\n`;
55+
result += part;
56+
}
57+
}
58+
return result;
59+
}
3060
const template = fs.readFileSync(templatePath || 'templates/profile.md');
31-
return (new Function('rules', 'return `' + template + '`;'))(ruleText);
61+
return (new Function('rules', 'exampleEntities', 'return `' + template + '`;'))(ruleText, exampleEntities);
3262
}
3363

3464
static generateDescribo() {

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"ldac-check": "./ldac-check.js"
88
},
99
"scripts": {
10-
"test": "mocha"
10+
"test": "mocha",
11+
"build-spec": "node make-profile.js"
1112
},
1213
"repository": {
1314
"type": "git",

profile-text/profile.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ works) that may be included - \`PrimaryMaterial\` - which is a recording or
262262
original text, or a citation of or proxy for it, \`DerivedMaterial\` which
263263
has been generated or sampled from primary material by a process such as format
264264
conversion or digitization, and \`Annotation\`, which contains one or more types of
265-
analysis of the `\PrimaryMaterial`\ or `\DerivedMaterial`\.
265+
analysis of the \`PrimaryMaterial\` or \`DerivedMaterial\`.
266266

267267
### PrimaryMaterial
268268

0 commit comments

Comments
 (0)