mdld-parse 0.9.2 → 0.9.3

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.
Files changed (60) hide show
  1. package/dist/.nojekyll +0 -0
  2. package/dist/CNAME +1 -0
  3. package/dist/README.md +425 -0
  4. package/dist/docs/API.md +397 -0
  5. package/dist/docs/Architecture.md +190 -0
  6. package/dist/docs/Guide.md +322 -0
  7. package/dist/docs/LLM-MDLD-Inducer.md +550 -0
  8. package/dist/docs/Parser.md +654 -0
  9. package/dist/docs/Performance.md +238 -0
  10. package/dist/docs/Spec-render.md +1439 -0
  11. package/dist/docs/Subject.md +180 -0
  12. package/dist/docs/Syntax.md +463 -0
  13. package/dist/docs/TodoList.md +215 -0
  14. package/dist/docs/Use-Cases.md +354 -0
  15. package/dist/docs/index.html +108 -0
  16. package/dist/docs/index.md +56 -0
  17. package/dist/docs/origin.md +459 -0
  18. package/dist/docs/polarity.md +586 -0
  19. package/dist/docs/primary-subject.md +199 -0
  20. package/dist/docs/statements.md +226 -0
  21. package/dist/docs/tokenization-performance-report.md +164 -0
  22. package/dist/examples/Cookbook.md +32 -0
  23. package/dist/examples/Status-SHACL.md +33 -0
  24. package/dist/examples/demo/index.md +35 -0
  25. package/dist/examples/demo/journal.md +18 -0
  26. package/dist/examples/demo/minimal.md +11 -0
  27. package/dist/examples/demo/project.md +15 -0
  28. package/dist/examples/demo/recipe.md +31 -0
  29. package/dist/examples/demo/research.md +46 -0
  30. package/dist/examples/demo/space-mission.md +48 -0
  31. package/dist/examples/dogfood.md +288 -0
  32. package/dist/examples/few-shot.md +309 -0
  33. package/dist/examples/graph/rdf-graph.html +473 -0
  34. package/dist/examples/graph/rdf_force_graph_web_component.html +506 -0
  35. package/dist/examples/index.md +45 -0
  36. package/dist/examples/llm-time-workflow.md +173 -0
  37. package/dist/examples/prov-o.md +2367 -0
  38. package/dist/examples/rdf.md +827 -0
  39. package/dist/examples/shacl.md +3399 -0
  40. package/dist/examples/statements.md +43 -0
  41. package/dist/examples/workflow.md +150 -0
  42. package/dist/index.html +2827 -0
  43. package/dist/index.js +1548 -0
  44. package/dist/logo.svg +7 -0
  45. package/dist/spec/Spec-compact.md +363 -0
  46. package/dist/spec/Spec-ultra.md +221 -0
  47. package/dist/spec/Spec.md +1068 -0
  48. package/dist/spec/index.md +46 -0
  49. package/package.json +7 -8
  50. package/src/constants.js +0 -31
  51. package/src/generate.js +0 -245
  52. package/src/highlight.js +0 -362
  53. package/src/index.js +0 -13
  54. package/src/locate.js +0 -23
  55. package/src/merge.js +0 -157
  56. package/src/parse.js +0 -711
  57. package/src/render.js +0 -576
  58. package/src/shared.js +0 -577
  59. package/src/tokenizers.js +0 -594
  60. package/src/utils.js +0 -430
package/src/utils.js DELETED
@@ -1,430 +0,0 @@
1
- import { URL_REGEX, DEFAULT_CONTEXT } from './constants.js';
2
-
3
- const VALID_URI_SCHEMES = /^(https?|ftp|mailto|tag|nih|urn|uuid|did|web|ipfs|ipns|data|file):/;
4
-
5
- // Base Term class for RDF/JS compatibility
6
- export class Term {
7
- constructor(id) {
8
- this.id = id;
9
- }
10
-
11
- equals(other) {
12
- return !!other && this.termType === other.termType && this.value === other.value;
13
- }
14
- }
15
-
16
- // NamedNode implementation
17
- export class NamedNode extends Term {
18
- constructor(iri) {
19
- super(iri);
20
- this.termType = 'NamedNode';
21
- this.value = iri;
22
- }
23
- }
24
-
25
- // Literal implementation with language/direction support
26
- export class Literal extends Term {
27
- constructor(id) {
28
- super(id);
29
- this.termType = 'Literal';
30
- this.value = '';
31
- this.language = '';
32
- this.datatype = null;
33
-
34
- // Parse the literal ID - handle escaped quotes properly
35
- const dtMatch = id.match(/^"([^"\\]*(?:\\.[^"\\]*)*)"(\^\^([^"]+))?(@([^-]+)(--(.+))?)?$/);
36
- if (dtMatch) {
37
- // Unescape the value
38
- this.value = dtMatch[1].replace(/\\"/g, '"').replace(/\\\\/g, '\\');
39
- if (dtMatch[5]) {
40
- this.language = dtMatch[5];
41
- this.datatype = new NamedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString');
42
- } else if (dtMatch[3]) {
43
- this.datatype = new NamedNode(dtMatch[3]);
44
- } else {
45
- this.datatype = new NamedNode('http://www.w3.org/2001/XMLSchema#string');
46
- }
47
- } else {
48
- // Fallback for simple literals without complex parsing
49
- this.value = id.replace(/^"|"$/g, '');
50
- this.datatype = new NamedNode('http://www.w3.org/2001/XMLSchema#string');
51
- }
52
- }
53
-
54
- equals(other) {
55
- return !!other &&
56
- this.termType === other.termType &&
57
- this.value === other.value &&
58
- this.language === other.language &&
59
- this.datatype?.value === other.datatype?.value;
60
- }
61
- }
62
-
63
- // BlankNode implementation
64
- export class BlankNode extends Term {
65
- constructor(name) {
66
- super(name || `b${Math.random().toString(36).slice(2, 11)}`);
67
- this.termType = 'BlankNode';
68
- this.value = this.id;
69
- }
70
- }
71
-
72
- // Variable implementation
73
- export class Variable extends Term {
74
- constructor(name) {
75
- super(name);
76
- this.termType = 'Variable';
77
- this.value = name;
78
- }
79
- }
80
-
81
- // DefaultGraph implementation
82
- export class DefaultGraph extends Term {
83
- constructor() {
84
- super('');
85
- this.termType = 'DefaultGraph';
86
- this.value = '';
87
- }
88
-
89
- equals(other) {
90
- return !!other && this.termType === other.termType;
91
- }
92
- }
93
-
94
- // Default graph singleton
95
- const DEFAULTGRAPH = new DefaultGraph();
96
-
97
- // Quad implementation
98
- export class Quad extends Term {
99
- constructor(subject, predicate, object, graph = DEFAULTGRAPH) {
100
- super(`${subject.id}|${predicate.id}|${object.id}|${graph.id}`);
101
- this.termType = 'Quad';
102
- this.subject = subject;
103
- this.predicate = predicate;
104
- this.object = object;
105
- this.graph = graph;
106
- }
107
-
108
- equals(other) {
109
- return !!other &&
110
- this.termType === other.termType &&
111
- this.subject.equals(other.subject) &&
112
- this.predicate.equals(other.predicate) &&
113
- this.object.equals(other.object) &&
114
- this.graph.equals(other.graph);
115
- }
116
-
117
- toJSON() {
118
- return {
119
- termType: this.termType,
120
- subject: this.subject.toJSON ? this.subject.toJSON() : { termType: this.subject.termType, value: this.subject.value },
121
- predicate: this.predicate.toJSON ? this.predicate.toJSON() : { termType: this.predicate.termType, value: this.predicate.value },
122
- object: this.object.toJSON ? this.object.toJSON() : { termType: this.object.termType, value: this.object.value },
123
- graph: this.graph.toJSON ? this.graph.toJSON() : { termType: this.graph.termType, value: this.graph.value }
124
- };
125
- }
126
- }
127
-
128
- // XSD constants
129
- const xsd = {
130
- boolean: 'http://www.w3.org/2001/XMLSchema#boolean',
131
- integer: 'http://www.w3.org/2001/XMLSchema#integer',
132
- double: 'http://www.w3.org/2001/XMLSchema#double',
133
- string: 'http://www.w3.org/2001/XMLSchema#string'
134
- };
135
-
136
- // DataFactory singleton matching N3.js interface
137
- export const DataFactory = {
138
- namedNode: (iri) => new NamedNode(iri),
139
- blankNode: (name) => new BlankNode(name),
140
- literal: (value, languageOrDataType) => {
141
- // Convert non-string values to string for proper serialization
142
- const stringValue = String(value);
143
- // Escape quotes in the value for proper serialization
144
- const escapedValue = stringValue.replace(/"/g, '\\"');
145
-
146
- // Create a language-tagged string
147
- if (typeof languageOrDataType === 'string') {
148
- return new Literal(`"${escapedValue}"@${languageOrDataType.toLowerCase()}`);
149
- }
150
-
151
- // Create a language-tagged string with base direction
152
- if (languageOrDataType !== undefined && !('termType' in languageOrDataType)) {
153
- const direction = languageOrDataType.direction ? `--${languageOrDataType.direction.toLowerCase()}` : '';
154
- return new Literal(`"${escapedValue}"@${languageOrDataType.language.toLowerCase()}${direction}`);
155
- }
156
-
157
- // Automatically determine datatype for booleans and numbers
158
- let datatype = languageOrDataType ? languageOrDataType.value : '';
159
- if (datatype === '') {
160
- // Convert a boolean
161
- if (typeof value === 'boolean') {
162
- datatype = xsd.boolean;
163
- }
164
- // Convert an integer or double
165
- else if (typeof value === 'number') {
166
- if (Number.isFinite(value)) {
167
- datatype = Number.isInteger(value) ? xsd.integer : xsd.double;
168
- } else {
169
- datatype = xsd.double;
170
- if (!Number.isNaN(value)) {
171
- value = value > 0 ? 'INF' : '-INF';
172
- }
173
- }
174
- }
175
- }
176
-
177
- // Create a datatyped literal
178
- return (datatype === '' || datatype === xsd.string)
179
- ? new Literal(`"${escapedValue}"`)
180
- : new Literal(`"${escapedValue}"^^${datatype}`);
181
- },
182
- variable: (name) => new Variable(name),
183
- defaultGraph: () => DEFAULTGRAPH,
184
- quad: (subject, predicate, object, graph) => new Quad(subject, predicate, object, graph),
185
- triple: (subject, predicate, object, graph) => new Quad(subject, predicate, object, graph), // Alias for quad
186
- fromTerm: (term) => {
187
- if (term instanceof Term) return term;
188
-
189
- // Term instantiated with another library
190
- switch (term.termType) {
191
- case 'NamedNode':
192
- return new NamedNode(term.value);
193
- case 'BlankNode':
194
- return new BlankNode(term.value);
195
- case 'Variable':
196
- return new Variable(term.value);
197
- case 'DefaultGraph':
198
- return DEFAULTGRAPH;
199
- case 'Literal':
200
- // Escape quotes in value to ensure proper parsing
201
- const escapedValue = String(term.value).replace(/"/g, '\\"');
202
- if (term.language) {
203
- return new Literal(`"${escapedValue}"@${term.language}`);
204
- } else if (term.datatype) {
205
- return new Literal(`"${escapedValue}"^^${term.datatype.value || term.datatype}`);
206
- } else {
207
- return new Literal(`"${escapedValue}"`);
208
- }
209
- case 'Quad':
210
- return DataFactory.fromQuad(term);
211
- default:
212
- throw new Error(`Unexpected termType: ${term.termType}`);
213
- }
214
- },
215
- fromQuad: (inQuad) => {
216
- if (inQuad instanceof Quad) return inQuad;
217
- if (inQuad.termType !== 'Quad') {
218
- // Handle plain object quads by treating them as quads
219
- if (inQuad.subject && inQuad.predicate && inQuad.object) {
220
- return new Quad(
221
- DataFactory.fromTerm(inQuad.subject),
222
- DataFactory.fromTerm(inQuad.predicate),
223
- DataFactory.fromTerm(inQuad.object),
224
- DataFactory.fromTerm(inQuad.graph || DataFactory.defaultGraph())
225
- );
226
- }
227
- throw new Error(`Unexpected termType: ${inQuad.termType}`);
228
- }
229
- return new Quad(
230
- DataFactory.fromTerm(inQuad.subject),
231
- DataFactory.fromTerm(inQuad.predicate),
232
- DataFactory.fromTerm(inQuad.object),
233
- DataFactory.fromTerm(inQuad.graph)
234
- );
235
- }
236
- };
237
-
238
- export function hash(str) {
239
- let h = 5381;
240
- for (let i = 0; i < str.length; i++) h = ((h << 5) + h) + str.charCodeAt(i);
241
- return Math.abs(h).toString(16).slice(0, 12);
242
- }
243
-
244
- const iriCache = new Map();
245
-
246
- export function expandIRI(term, ctx) {
247
- if (term == null) return null;
248
-
249
- const cacheKey = `${term}|${ctx['@vocab'] || ''}|${Object.keys(ctx).filter(k => k !== '@vocab').sort().map(k => `${k}:${ctx[k]}`).join(',')}`;
250
- if (iriCache.has(cacheKey)) {
251
- return iriCache.get(cacheKey);
252
- }
253
-
254
- const raw = typeof term === 'string' ? term : (typeof term === 'object' && typeof term.value === 'string') ? term.value : String(term);
255
- const t = raw.trim();
256
- let result;
257
-
258
- if (t.match(URL_REGEX)) {
259
- result = t;
260
- } else if (t.includes(':')) {
261
- const [prefix, ref] = t.split(':', 2);
262
- if (prefix && !ctx[prefix] && prefix !== '@vocab') {
263
- console.warn(`Undefined prefix "${prefix}" in IRI "${t}" - treating as literal`);
264
- }
265
- result = ctx[prefix] ? ctx[prefix] + ref : t;
266
- } else {
267
- result = (ctx['@vocab'] || '') + t;
268
- }
269
-
270
- iriCache.set(cacheKey, result);
271
- return result;
272
- }
273
-
274
- export function shortenIRI(iri, ctx) {
275
- if (!iri || !VALID_URI_SCHEMES.test(iri)) return iri;
276
- if (ctx['@vocab'] && iri.startsWith(ctx['@vocab'])) return iri.substring(ctx['@vocab'].length);
277
-
278
- // Find the best matching prefix - more precise matching
279
- for (const [prefix, namespace] of Object.entries(ctx)) {
280
- if (prefix !== '@vocab' && iri.startsWith(namespace)) {
281
- // Check if this is the best match (longest namespace)
282
- const isBestMatch = Object.entries(ctx)
283
- .filter(([p, ns]) => p !== '@vocab' && iri.startsWith(ns))
284
- .every(([p, ns]) => namespace.length >= ns.length || (p === prefix && ns.length === namespace.length));
285
-
286
- if (isBestMatch) {
287
- return prefix + ':' + iri.substring(namespace.length);
288
- }
289
- }
290
- }
291
- return iri;
292
- }
293
-
294
- // Token pattern definitions for semantic block parsing
295
- const TOKEN_PATTERNS = {
296
- '=#': { kind: 'fragment', extract: t => t.substring(2).replace('}', '') },
297
- '+#': { kind: 'softFragment', extract: t => t.substring(2).replace('}', '') },
298
- '+': { kind: 'object', extract: t => t.substring(1) },
299
- '^^': { kind: 'datatype', extract: t => t.substring(2) },
300
- '@': { kind: 'language', extract: t => t.substring(1) },
301
- '.': { kind: 'type', extract: t => t.substring(1) },
302
- '!': { kind: 'property', form: '!', extract: t => t.substring(1) },
303
- '?': { kind: 'property', form: '?', extract: t => t.substring(1) }
304
- };
305
-
306
- export function parseSemanticBlock(raw) {
307
- try {
308
- const src = String(raw || '').trim();
309
- const cleaned = src.replace(/^\{|\}$/g, '').trim();
310
- if (!cleaned) return { subject: null, object: null, types: [], predicates: [], datatype: null, language: null, entries: [] };
311
-
312
- const result = { subject: null, object: null, types: [], predicates: [], datatype: null, language: null, entries: [] };
313
- const re = /\S+/g;
314
- let m;
315
-
316
- while ((m = re.exec(cleaned)) !== null) {
317
- let token = m[0];
318
- const relStart = 1 + m.index;
319
- const relEnd = relStart + token.length;
320
- const entryIndex = result.entries.length;
321
-
322
- // Handle remove polarity - strip leading - and set remove flag
323
- let remove = false;
324
- if (token.startsWith('-') && token.length > 1) {
325
- remove = true;
326
- token = token.slice(1);
327
- }
328
-
329
- // Handle special tokens
330
- if (token === '=') {
331
- if (remove) {
332
- console.warn('-= is not valid, subject declarations have no polarity');
333
- }
334
- result.subject = 'RESET';
335
- result.entries.push({ kind: 'subjectReset', relRange: { start: relStart, end: relEnd }, raw: token });
336
- continue;
337
- }
338
-
339
- if (token.startsWith('=') && !token.startsWith('=#')) {
340
- if (remove) {
341
- console.warn('-= is not valid, subject declarations have no polarity');
342
- }
343
- const iri = token.substring(1);
344
- result.subject = iri;
345
- result.entries.push({ kind: 'subject', iri, relRange: { start: relStart, end: relEnd }, raw: token });
346
- continue;
347
- }
348
-
349
- // Handle pattern-based tokens
350
- let processed = false;
351
- for (const [pattern, config] of Object.entries(TOKEN_PATTERNS)) {
352
- if (token.startsWith(pattern)) {
353
- const entry = { kind: config.kind, relRange: { start: relStart, end: relEnd }, raw: m[0] };
354
- const extracted = config.extract(token);
355
-
356
- if (config.kind === 'fragment') {
357
- result.subject = `=#${extracted}`;
358
- entry.fragment = extracted;
359
- } else if (config.kind === 'softFragment') {
360
- result.object = `#${extracted}`;
361
- entry.fragment = extracted;
362
- } else if (config.kind === 'object') {
363
- if (remove) {
364
- console.warn('-+ is not valid, object declarations have no polarity');
365
- remove = false;
366
- }
367
- result.object = extracted;
368
- entry.iri = extracted;
369
- } else if (config.kind === 'datatype') {
370
- if (remove) {
371
- console.warn('-^^ is not valid, datatype modifiers have no polarity');
372
- remove = false;
373
- }
374
- if (!result.language) result.datatype = extracted;
375
- entry.datatype = extracted;
376
- } else if (config.kind === 'language') {
377
- if (remove) {
378
- console.warn('-@ is not valid, language modifiers have no polarity');
379
- remove = false;
380
- }
381
- result.language = extracted;
382
- result.datatype = null;
383
- entry.language = extracted;
384
- } else if (config.kind === 'type') {
385
- result.types.push({ iri: extracted, entryIndex, remove });
386
- entry.iri = extracted;
387
- entry.remove = remove;
388
- } else if (config.kind === 'property') {
389
- result.predicates.push({ iri: extracted, form: config.form, entryIndex, remove });
390
- entry.iri = extracted;
391
- entry.form = config.form;
392
- entry.remove = remove;
393
- }
394
-
395
- result.entries.push(entry);
396
- processed = true;
397
- break;
398
- }
399
- }
400
-
401
- // Default case (no pattern match)
402
- if (!processed) {
403
- result.predicates.push({ iri: token, form: '', entryIndex, remove });
404
- result.entries.push({ kind: 'property', iri: token, form: '', relRange: { start: relStart, end: relEnd }, raw: m[0], remove });
405
- }
406
- }
407
-
408
- return result;
409
- } catch (error) {
410
- console.error(`Error parsing semantic block ${raw}:`, error);
411
- return { subject: null, object: null, types: [], predicates: [], datatype: null, language: null, entries: [] };
412
- }
413
- }
414
-
415
- export function quadIndexKey(subject, predicate, object) {
416
- const objKey = object.termType === 'Literal'
417
- ? JSON.stringify({ t: 'Literal', v: object.value, lang: object.language || '', dt: object.datatype?.value || '' })
418
- : JSON.stringify({ t: object.termType, v: object.value });
419
- return JSON.stringify([subject.value, predicate.value, objKey]);
420
- }
421
-
422
- export function quadToKeyForOrigin(q) {
423
- return q ? quadIndexKey(q.subject, q.predicate, q.object) : null;
424
- }
425
-
426
- export function createLiteral(value, datatype, language, context, dataFactory) {
427
- if (datatype) return dataFactory.literal(value, dataFactory.namedNode(expandIRI(datatype, context)));
428
- if (language) return dataFactory.literal(value, language);
429
- return dataFactory.literal(value);
430
- }