1
+ const { ROCrate } = require ( 'ro-crate' ) ;
1
2
const rules = require ( './rules' ) ;
2
3
const { validateEntity} = require ( './validator' ) ;
3
4
const fs = require ( 'fs' ) ;
5
+ const path = require ( 'path' ) ;
4
6
5
7
6
8
/**
@@ -27,8 +29,36 @@ class LdacProfile {
27
29
}
28
30
ruleText [ key ] = text ;
29
31
}
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
+ }
30
60
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 ) ;
32
62
}
33
63
34
64
static generateDescribo ( ) {
0 commit comments