@lde/docgen 0.6.14 → 0.6.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -4
- package/dist/cli.js +1 -4
- package/dist/frame.d.ts +2 -2
- package/dist/frame.d.ts.map +1 -1
- package/dist/frame.js +35 -2
- package/dist/index.d.ts +10 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -0
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +25 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,9 +32,9 @@ npx @lde/docgen@latest from-shacl <shacl-file> <template-file> [options]
|
|
|
32
32
|
|
|
33
33
|
### Options
|
|
34
34
|
|
|
35
|
-
| Option | Description
|
|
36
|
-
| -------------------- |
|
|
37
|
-
| `-f, --frame <file>` | Path to a JSON-LD Frame file | Built-in `frames/shacl.frame.jsonld` |
|
|
35
|
+
| Option | Description | Default |
|
|
36
|
+
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------ |
|
|
37
|
+
| `-f, --frame <file>` | Path to a JSON-LD Frame file. Deep-merged on top of the built-in default frame, so it only needs to contain your additions (e.g. extra `@context` entries). | Built-in `frames/shacl.frame.jsonld` |
|
|
38
38
|
|
|
39
39
|
### Example
|
|
40
40
|
|
|
@@ -108,8 +108,20 @@ Property shapes with the same `sh:path` are common in SHACL (e.g. one for cardin
|
|
|
108
108
|
|
|
109
109
|
## Custom frames
|
|
110
110
|
|
|
111
|
-
The default frame selects all `sh:NodeShape` resources
|
|
111
|
+
The default frame selects all `sh:NodeShape` resources and provides type coercions for common SHACL terms (`targetClass`, `path`, `severity`, etc.). To extend it, pass a partial [JSON-LD Frame](https://www.w3.org/TR/json-ld11-framing/) – it is **deep-merged** on top of the default, so you only need to specify your additions:
|
|
112
|
+
|
|
113
|
+
```json
|
|
114
|
+
{
|
|
115
|
+
"@context": {
|
|
116
|
+
"nde": "https://def.nde.nl#",
|
|
117
|
+
"nde:futureChange": {},
|
|
118
|
+
"nde:version": {}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
```
|
|
112
122
|
|
|
113
123
|
```sh
|
|
114
124
|
npx @lde/docgen@latest from-shacl shapes.ttl template.liquid -f my-frame.jsonld
|
|
115
125
|
```
|
|
126
|
+
|
|
127
|
+
Plain objects are merged key-by-key, with user values winning; arrays and primitives in your frame replace the default. To override a built-in coercion (e.g. change `severity` from `@vocab` to `@id`), redefine the same key in your `@context`.
|
package/dist/cli.js
CHANGED
|
@@ -2,9 +2,6 @@
|
|
|
2
2
|
import { Command } from 'commander';
|
|
3
3
|
import { generateDocumentation } from './index.js';
|
|
4
4
|
import packageJson from '../package.json' with { type: 'json' };
|
|
5
|
-
import { dirname } from 'path';
|
|
6
|
-
import { fileURLToPath } from 'url';
|
|
7
|
-
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
8
5
|
const program = new Command();
|
|
9
6
|
program
|
|
10
7
|
.name('docgen')
|
|
@@ -15,7 +12,7 @@ program
|
|
|
15
12
|
.description('Generate documentation from a SHACL shapes file')
|
|
16
13
|
.argument('<shacl-file>', 'Path to SHACL shapes file (in any RDF serialization format)')
|
|
17
14
|
.argument('<template-file>', 'Path to Liquid template file')
|
|
18
|
-
.option('-f --frame <json-ld-frame-file>', 'Path to a JSON-LD Frame file
|
|
15
|
+
.option('-f --frame <json-ld-frame-file>', 'Path to a JSON-LD Frame file. Deep-merged on top of the built-in default frame, so it only needs to contain your additions.')
|
|
19
16
|
.addHelpText('after', `
|
|
20
17
|
Example:
|
|
21
18
|
$ npx @lde/docgen@latest from-shacl shacl.ttl template.liquid
|
package/dist/frame.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { JsonLdArray } from 'jsonld/jsonld-spec.js';
|
|
2
|
-
import
|
|
3
|
-
export declare function frame(document: JsonLdArray,
|
|
2
|
+
import type { NodeObject } from 'jsonld';
|
|
3
|
+
export declare function frame(document: JsonLdArray, userFramePath?: string): Promise<NodeObject>;
|
|
4
4
|
//# sourceMappingURL=frame.d.ts.map
|
package/dist/frame.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"frame.d.ts","sourceRoot":"","sources":["../src/frame.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"frame.d.ts","sourceRoot":"","sources":["../src/frame.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAS,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAChE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAUzC,wBAAsB,KAAK,CACzB,QAAQ,EAAE,WAAW,EACrB,aAAa,CAAC,EAAE,MAAM,GACrB,OAAO,CAAC,UAAU,CAAC,CAUrB"}
|
package/dist/frame.js
CHANGED
|
@@ -1,8 +1,41 @@
|
|
|
1
1
|
import jsonld from 'jsonld';
|
|
2
2
|
import { readFile } from 'node:fs/promises';
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
const defaultFramePath = join(__dirname, '../frames/shacl.frame.jsonld');
|
|
7
|
+
export async function frame(document, userFramePath) {
|
|
8
|
+
const defaultFrame = await readFrame(defaultFramePath);
|
|
9
|
+
const mergedFrame = userFramePath
|
|
10
|
+
? deepMerge(defaultFrame, await readFrame(userFramePath))
|
|
11
|
+
: defaultFrame;
|
|
12
|
+
return await jsonld.frame(document, mergedFrame, {
|
|
5
13
|
omitGraph: false,
|
|
6
14
|
embed: '@always',
|
|
7
15
|
});
|
|
8
16
|
}
|
|
17
|
+
async function readFrame(path) {
|
|
18
|
+
return JSON.parse(await readFile(path, 'utf8'));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Recursively merges `source` into `target`, returning a new object. Plain
|
|
22
|
+
* objects are merged key-by-key; arrays and primitives in `source` replace
|
|
23
|
+
* those in `target`. Used to compose a user-supplied JSON-LD frame on top of
|
|
24
|
+
* docgen’s built-in default so consumers only need to specify their additions.
|
|
25
|
+
*/
|
|
26
|
+
function deepMerge(target, source) {
|
|
27
|
+
const result = { ...target };
|
|
28
|
+
for (const [key, sourceValue] of Object.entries(source)) {
|
|
29
|
+
const targetValue = result[key];
|
|
30
|
+
if (isPlainObject(targetValue) && isPlainObject(sourceValue)) {
|
|
31
|
+
result[key] = deepMerge(targetValue, sourceValue);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
result[key] = sourceValue;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
function isPlainObject(value) {
|
|
40
|
+
return (typeof value === 'object' && value !== null && !Array.isArray(value));
|
|
41
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generate documentation from a SHACL shapes file using a Liquid template.
|
|
3
|
+
*
|
|
4
|
+
* @param rdfPath Path to a SHACL shapes file in any RDF serialization.
|
|
5
|
+
* @param templatePath Path to a Liquid template.
|
|
6
|
+
* @param framePath Optional path to a JSON-LD frame. When provided, it is
|
|
7
|
+
* deep-merged on top of docgen’s built-in default frame, so consumers only
|
|
8
|
+
* need to specify their additions (e.g. extra `@context` entries).
|
|
9
|
+
*/
|
|
10
|
+
export declare function generateDocumentation(rdfPath: string, templatePath: string, framePath?: string): Promise<string>;
|
|
2
11
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,wBAAsB,qBAAqB,CACzC,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA;;;;;;;;GAQG;AACH,wBAAsB,qBAAqB,CACzC,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,MAAM,EACpB,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,MAAM,CAAC,CAKjB"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { parseRdfToJsonLd } from './parse.js';
|
|
2
2
|
import { frame } from './frame.js';
|
|
3
3
|
import { render } from './render.js';
|
|
4
|
+
/**
|
|
5
|
+
* Generate documentation from a SHACL shapes file using a Liquid template.
|
|
6
|
+
*
|
|
7
|
+
* @param rdfPath Path to a SHACL shapes file in any RDF serialization.
|
|
8
|
+
* @param templatePath Path to a Liquid template.
|
|
9
|
+
* @param framePath Optional path to a JSON-LD frame. When provided, it is
|
|
10
|
+
* deep-merged on top of docgen’s built-in default frame, so consumers only
|
|
11
|
+
* need to specify their additions (e.g. extra `@context` entries).
|
|
12
|
+
*/
|
|
4
13
|
export async function generateDocumentation(rdfPath, templatePath, framePath) {
|
|
5
14
|
const jsonld = await parseRdfToJsonLd(rdfPath);
|
|
6
15
|
const framed = await frame(jsonld, framePath);
|
package/dist/parse.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAOpD,wBAAsB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAqB7E"}
|
package/dist/parse.js
CHANGED
|
@@ -2,6 +2,7 @@ import { rdfDereferencer } from 'rdf-dereference';
|
|
|
2
2
|
import jsonld from 'jsonld';
|
|
3
3
|
import { rdfSerializer } from 'rdf-serialize';
|
|
4
4
|
import streamToString from 'stream-to-string';
|
|
5
|
+
const XSD_STRING = 'http://www.w3.org/2001/XMLSchema#string';
|
|
5
6
|
export async function parseRdfToJsonLd(filePath) {
|
|
6
7
|
const { data } = await rdfDereferencer.dereference(filePath, {
|
|
7
8
|
localFiles: true,
|
|
@@ -11,7 +12,30 @@ export async function parseRdfToJsonLd(filePath) {
|
|
|
11
12
|
contentType: 'application/n-quads',
|
|
12
13
|
});
|
|
13
14
|
const nqString = await streamToString(nq);
|
|
14
|
-
|
|
15
|
+
const expanded = await jsonld.fromRDF(nqString, {
|
|
15
16
|
useNativeTypes: true, // Convert xsd:integer to Number etc.
|
|
16
17
|
});
|
|
18
|
+
// jsonld v9 emits @type: xsd:string on every plain string literal; v8 omitted
|
|
19
|
+
// it because xsd:string is the JSON-LD default datatype. Without this strip,
|
|
20
|
+
// framing yields { @value, @type } objects that templates render as
|
|
21
|
+
// ‘[object Object]’. See https://github.com/ldelements/lde/issues/369.
|
|
22
|
+
return stripDefaultStringType(expanded);
|
|
23
|
+
}
|
|
24
|
+
function stripDefaultStringType(value) {
|
|
25
|
+
if (Array.isArray(value)) {
|
|
26
|
+
return value.map(stripDefaultStringType);
|
|
27
|
+
}
|
|
28
|
+
if (value && typeof value === 'object') {
|
|
29
|
+
const record = value;
|
|
30
|
+
if (record['@value'] !== undefined && record['@type'] === XSD_STRING) {
|
|
31
|
+
const { ['@type']: _, ...rest } = record;
|
|
32
|
+
return rest;
|
|
33
|
+
}
|
|
34
|
+
const result = {};
|
|
35
|
+
for (const key of Object.keys(record)) {
|
|
36
|
+
result[key] = stripDefaultStringType(record[key]);
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
return value;
|
|
17
41
|
}
|