@graphy/content.trig.write
Advanced tools
Comparing version 3.2.2 to 4.0.0
374
main.js
@@ -5,4 +5,5 @@ | ||
const factory = require('@graphy/core.data.factory'); | ||
const writable = require('@graphy/core.class.writable'); | ||
const Writable = require('@graphy/core.class.writable'); | ||
// eslint-disable-next-line no-misleading-character-class | ||
const RT_PREFIXED_NAME_NAMESPACE_VALID = /^([A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\u{02ff}\u{0370}-\u{037d}\u{037f}-\u{1fff}\u{200c}-\u{200d}\u{2070}-\u{218f}\u{2c00}-\u{2fef}\u{3001}-\u{d7ff}\u{f900}-\u{fdcf}\u{fdf0}-\u{fffd}\u{10000}-\u{effff}]([A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\u{02ff}\u{0370}-\u{037d}\u{037f}-\u{1fff}\u{200c}-\u{200d}\u{2070}-\u{218f}\u{2c00}-\u{2fef}\u{3001}-\u{d7ff}\u{f900}-\u{fdcf}\u{fdf0}-\u{fffd}\u{10000}-\u{effff}_\-0-9\xb7\u{0300}-\u{036f}\u{203f}-\u{2040}.]*[A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\u{02ff}\u{0370}-\u{037d}\u{037f}-\u{1fff}\u{200c}-\u{200d}\u{2070}-\u{218f}\u{2c00}-\u{2fef}\u{3001}-\u{d7ff}\u{f900}-\u{fdcf}\u{fdf0}-\u{fffd}\u{10000}-\u{effff}_\-0-9\xb7\u{0300}-\u{036f}\u{203f}-\u{2040}])?)?$/u; | ||
@@ -13,3 +14,3 @@ const N_MAX_STRING_BUFFER = 1 << 12; | ||
class TriG_Writer extends writable { | ||
class TriG_Writer extends Writable { | ||
constructor(gc_writer={}) { | ||
@@ -20,54 +21,64 @@ super(gc_writer); | ||
prefixes: h_prefixes={}, | ||
indent: s_indent='\t', | ||
tokens: gc_tokens=null, | ||
collections: gc_collections=null, | ||
lists: gc_lists=null, | ||
debug: b_debug=false, | ||
style: gc_style=null, | ||
} = gc_writer; | ||
Object.assign(this, { | ||
indent: s_indent, | ||
debug: b_debug, | ||
_b_debug: b_debug, | ||
_s_indent: '\t', | ||
_b_simplify_default_graph: false, | ||
}); | ||
let s_graph_token = ''; | ||
let s_graph_keyword = ''; | ||
// token config | ||
if(gc_tokens) { | ||
// 'graph' token | ||
if(gc_tokens.graph) { | ||
let z_token = gc_tokens.graph; | ||
// boolean true | ||
if(true === z_token) { | ||
s_graph_token = 'GRAPH '; | ||
// style config | ||
if(gc_style) { | ||
// 'graph' keyword | ||
let z_graph_keyword = gc_style.graph_keyword || gc_style.graphKeyword || gc_style['graph-keyword']; | ||
if(z_graph_keyword) { | ||
// boolean true | ||
if(true === z_graph_keyword) { | ||
s_graph_keyword = 'GRAPH '; | ||
} | ||
// invalid type | ||
else if('string' !== typeof z_token) { | ||
throw new TypeError(`Invalid argument type given for 'graph' token: ${z_token}`); | ||
// invalid type | ||
else if('string' !== typeof z_graph_keyword) { | ||
throw new TypeError(`Invalid argument type given for 'graph' token: ${z_graph_keyword}`); | ||
} | ||
// invalid token string | ||
else if(!/^graph$/i.test(z_token)) { | ||
throw new Error(`Graph token must equal case-insensitive "GRAPH"; found: "${z_token}"`); | ||
// invalid token string | ||
else if(!/^graph$/i.test(z_graph_keyword)) { | ||
throw new Error(`Graph token must equal case-insensitive "GRAPH"; found: "${z_graph_keyword}"`); | ||
} | ||
// valid graph token; append space | ||
// valid graph token; append space | ||
else { | ||
s_graph_token = z_token+' '; | ||
s_graph_keyword = z_graph_keyword+' '; | ||
} | ||
} | ||
// default graph simplification | ||
let w_simplify_default_graph = gc_style.simplify_default_graph || gc_style.simplifyDefaultGraph || gc_style['simplify-default-graph']; | ||
if(w_simplify_default_graph) { | ||
this._b_simplify_default_graph = !!w_simplify_default_graph; | ||
} | ||
// indent | ||
if(gc_style.indent) { | ||
this._s_indent = gc_style.indent.replace(/[^\s]/g, ''); | ||
} | ||
} | ||
// set graph token | ||
this.graph_token = s_graph_token; | ||
// set graph token | ||
this._s_graph_keyword = s_graph_keyword; | ||
// custom collection keys | ||
if(gc_collections) { | ||
// serialize collection object | ||
this.collection_object = function(a_collection, n_nest_level) { | ||
// transcode collection object | ||
let hc2_transcoded = this.transcode_collection(a_collection); | ||
// custom list keys | ||
if(gc_lists) { | ||
// serialize list object | ||
this._serialize_list_object = function(a_list, n_nest_level) { | ||
// transcode list object | ||
let hc2_transcoded = this._transcode_list(a_list); | ||
// serialize object | ||
return this.objects(hc2_transcoded, n_nest_level); | ||
return this._encode_objects(hc2_transcoded, n_nest_level); | ||
}; | ||
@@ -102,9 +113,12 @@ } | ||
// serialize prefixes | ||
serialize_prefixes(h_prefixes) { | ||
_serialize_prefixes(h_prefixes) { | ||
// build prefixes string | ||
let s_prefixes = (2 === this.state)? '\n\n': ''; | ||
let s_prefixes = (2 === this._xc_state)? '\n\n': ''; | ||
// update state | ||
this.state = 0; | ||
this._xc_state = 0; | ||
// clone prefixes | ||
this._h_prefixes = {...this._h_prefixes}; | ||
// each user-defined prefix | ||
@@ -121,5 +135,8 @@ for(let s_prefix_id in h_prefixes) { | ||
// set prefix | ||
this.prefixes[s_prefix_id] = h_prefixes[s_prefix_id]; | ||
this._h_prefixes[s_prefix_id] = h_prefixes[s_prefix_id]; | ||
} | ||
// recache | ||
factory.cache_prefixes(this._h_prefixes); | ||
// return prefix string | ||
@@ -131,23 +148,31 @@ return s_prefixes; | ||
// serialize c3 hash | ||
serialize_c3(hc3_triples) { | ||
// serialize c3 hash | ||
_serialize_c3(hc3_triples) { | ||
let { | ||
prefixes: h_prefixes, | ||
indent: s_indent, | ||
_h_prefixes: h_prefixes, | ||
_s_indent: s_indent, | ||
} = this; | ||
// break line if non-data state | ||
let s_write = 2 !== this.state? '\n': ''; | ||
// update state | ||
this.state = 2; | ||
// break line if non-data state | ||
let s_write = 2 !== this._xc_state? '\n': ''; | ||
// update state | ||
this._xc_state = 2; | ||
// triple delimiter | ||
// triple delimiter | ||
let s_delim_triples = ''; | ||
// subject exit listener | ||
let f_exit_subject = null; | ||
// each subject | ||
for(let sc1_subject in hc3_triples) { | ||
// directive | ||
// directive | ||
if('`' === sc1_subject[0]) { | ||
s_write += s_delim_triples+this.directive(sc1_subject, hc3_triples[sc1_subject]); | ||
// do not break next line | ||
s_delim_triples = ''; | ||
let g_apply = this._apply_directive(sc1_subject, hc3_triples[sc1_subject]); | ||
// write data | ||
if(g_apply.write) { | ||
s_write += s_delim_triples+g_apply.write; | ||
// do not break next line | ||
s_delim_triples = ''; | ||
} | ||
// save exit listener | ||
if(g_apply.exit) f_exit_subject = g_apply.exit; | ||
continue; | ||
@@ -168,14 +193,21 @@ } | ||
let b_empty = true; | ||
// predicate exit listener | ||
let f_exit_predicate = null; | ||
// each predicate | ||
for(let sc1_predicate in hc2_pairs) { | ||
// directive | ||
// directive | ||
if('`' === sc1_predicate[0]) { | ||
// apply directive | ||
let g_apply = this._apply_directive(sc1_predicate, hc2_pairs[sc1_predicate]); | ||
// write data | ||
if(g_apply.write) { | ||
// break line | ||
s_write += (s_indent_pairs? s_term_pairs: '\n')+s_indent; | ||
// serialize directive | ||
s_write += this.directive(sc1_predicate, hc2_pairs[sc1_predicate]); | ||
// pair already terminated | ||
s_term_pairs = ''; | ||
// indent next pair | ||
s_indent_pairs = s_indent; | ||
s_write += (s_indent_pairs? s_term_pairs: '\n')+s_indent+g_apply.write; | ||
// pair already terminated | ||
s_term_pairs = ''; | ||
// indent next pair | ||
s_indent_pairs = s_indent; | ||
} | ||
// save exit listener | ||
if(g_apply.exit) f_exit_predicate = g_apply.exit; | ||
continue; | ||
@@ -186,3 +218,3 @@ } | ||
// serialize objects | ||
let st_objects = this.objects(z_objects); | ||
let st_objects = this._encode_objects(z_objects); | ||
// no objects; skip pair | ||
@@ -203,4 +235,4 @@ if(!st_objects) continue; | ||
// update state | ||
this.state = 2; | ||
// // string buffer became too large | ||
this._xc_state = 2; | ||
// // string buffer became too large | ||
// if(s_write.length >= N_MAX_STRING_BUFFER) { | ||
@@ -213,2 +245,4 @@ // debugger; | ||
s_indent_pairs = s_indent; | ||
// call exit predicate listener | ||
if(f_exit_predicate) f_exit_predicate(); | ||
} | ||
@@ -224,6 +258,7 @@ // empty triples; cut out | ||
s_write += `${s_term_pairs? ' ': s_indent_pairs}.\n`; // | ||
// call exit subject listener | ||
if(f_exit_subject) f_exit_subject(); | ||
} | ||
s_write += '\n'; | ||
return s_write; | ||
@@ -233,32 +268,50 @@ } | ||
// serialize c4 hash | ||
serialize_c4(hc4_quads) { | ||
// serialize c4 hash | ||
_serialize_c4(hc4_quads) { | ||
let { | ||
prefixes: h_prefixes, | ||
indent: s_indent, | ||
_h_prefixes: h_prefixes, | ||
_s_indent: s_indent, | ||
} = this; | ||
// break line if non-data state | ||
let s_write = 2 !== this.state? '\n': ''; | ||
// update state | ||
this.state = 2; | ||
// graph token | ||
let s_graph_token = this.graph_token; | ||
// break line if non-data state | ||
let s_write = 2 !== this._xc_state? '\n': ''; | ||
// update state | ||
this._xc_state = 2; | ||
// force default graph brace | ||
let b_simplify_default_graph = this._b_simplify_default_graph; | ||
// each graph | ||
// graph token | ||
let s_graph_keyword = this._s_graph_keyword; | ||
// graph exit listener | ||
let f_exit_graph = null; | ||
// each graph | ||
for(let sc1_graph in hc4_quads) { | ||
// directive | ||
// directive | ||
if('`' === sc1_graph[0]) { | ||
s_write += this.directive(sc1_graph, hc4_quads[sc1_graph]); | ||
let g_apply = this._apply_directive(sc1_graph, hc4_quads[sc1_graph]); | ||
// write data | ||
if(g_apply.write) s_write += g_apply.write; | ||
// save exit listener | ||
if(g_apply.exit) f_exit_graph = g_apply.exit; | ||
continue; | ||
} | ||
// serialize open graph | ||
// serialize open graph | ||
let st_graph = factory.c1_node(sc1_graph, h_prefixes).terse(h_prefixes); | ||
s_write += s_graph_token+(st_graph? st_graph+' ': '')+'{\n'; | ||
// update state | ||
this.state = 2; | ||
s_write += st_graph | ||
? s_graph_keyword+st_graph+' {\n' | ||
: (b_simplify_default_graph? '': s_graph_keyword+'{\n'); | ||
// ref triples | ||
// simplify default graph implies no indent | ||
let s_indent_root = (!st_graph && b_simplify_default_graph)? '': s_indent; | ||
// update state | ||
this._xc_state = 2; | ||
// ref triples | ||
let hc3_triples = hc4_quads[sc1_graph]; | ||
@@ -268,68 +321,85 @@ | ||
let s_delim_triples = ''; | ||
// each subject | ||
// subject exit listener | ||
let f_exit_subject = null; | ||
// each subject | ||
for(let sc1_subject in hc3_triples) { | ||
// directive | ||
if('`' === sc1_subject[0]) { | ||
s_write += s_delim_triples+s_indent+this.directive(sc1_subject, hc3_triples[sc1_subject]); | ||
// do not break next line | ||
s_delim_triples = ''; | ||
let g_apply = this._apply_directive(sc1_subject, hc3_triples[sc1_subject]); | ||
// write data | ||
if(g_apply.write) { | ||
s_write += s_delim_triples+s_indent_root+g_apply.write; | ||
// do not break next line | ||
s_delim_triples = ''; | ||
} | ||
// save exit listener | ||
if(g_apply.exit) f_exit_subject = g_apply.exit; | ||
continue; | ||
} | ||
// position before subject | ||
// position before subject | ||
let i_triples = s_write.length; | ||
// serialize subject | ||
s_write += s_delim_triples+s_indent+factory.c1_node(sc1_subject, h_prefixes).terse(h_prefixes)+' '; | ||
// pair indent & terminator | ||
// serialize subject | ||
s_write += s_delim_triples+s_indent_root+factory.c1_node(sc1_subject, h_prefixes).terse(h_prefixes)+' '; | ||
// pair indent & terminator | ||
let s_indent_pairs = ''; | ||
let s_term_pairs = ''; | ||
// ref pairs | ||
// ref pairs | ||
let hc2_pairs = hc3_triples[sc1_subject]; | ||
// position before pairs | ||
// position before pairs | ||
let i_pairs = s_write.length; | ||
// were objects written? | ||
// were objects written? | ||
let b_empty = true; | ||
// each predicate | ||
// predicate exit listener | ||
let f_exit_predicate = null; | ||
// each predicate | ||
for(let sc1_predicate in hc2_pairs) { | ||
// directive | ||
if('`' === sc1_predicate[0]) { | ||
// break line | ||
s_write += (s_indent_pairs? s_term_pairs: '\n')+s_indent+s_indent; | ||
// serialize directive | ||
s_write += this.directive(sc1_predicate, hc2_pairs[sc1_predicate]); | ||
// pair already terminated | ||
s_term_pairs = ''; | ||
// indent next pair | ||
s_indent_pairs = s_indent+s_indent; | ||
// apply directive | ||
let g_apply = this._apply_directive(sc1_predicate, hc2_pairs[sc1_predicate]); | ||
// write data | ||
if(g_apply.write) { | ||
// break line | ||
s_write += (s_indent_pairs? s_term_pairs: '\n')+s_indent+s_indent_root+g_apply.write; | ||
// pair already terminated | ||
s_term_pairs = ''; | ||
// indent next pair | ||
s_indent_pairs = s_indent+s_indent_root; | ||
} | ||
// save exit listener | ||
if(g_apply.exit) f_exit_predicate = g_apply.exit; | ||
continue; | ||
} | ||
// ref objects | ||
// ref objects | ||
let z_objects = hc2_pairs[sc1_predicate]; | ||
// serialize objects | ||
let st_objects = this.objects(z_objects); | ||
// no objects; skip pair | ||
// serialize objects | ||
let st_objects = this._encode_objects(z_objects); | ||
// no objects; skip pair | ||
if(!st_objects) continue; | ||
// not empty | ||
// not empty | ||
b_empty = false; | ||
// cannot use blank node in predicate position | ||
// cannot use blank node in predicate position | ||
if('_' === sc1_predicate[0] && ':' === sc1_predicate[1]) { | ||
throw new Error(`Cannot use blank node in predicate position of c4 hash; graph:'${sc1_graph}', subject:'${sc1_subject}', predicate:'${sc1_predicate}'`); | ||
} | ||
// create predicate | ||
// create predicate | ||
let kt_predicate = factory.c1_named_node(sc1_predicate, h_prefixes); | ||
// tersify rdf:type | ||
// tersify rdf:type | ||
let st_predicate = kt_predicate.isRdfTypeAlias? 'a': kt_predicate.terse(h_prefixes); | ||
// serialize predicate and object(s) | ||
// serialize predicate and object(s) | ||
s_write += s_term_pairs+s_indent_pairs+st_predicate+' '+st_objects; | ||
// update state | ||
this.state = 2; | ||
// // string buffer became too large | ||
// update state | ||
this._xc_state = 2; | ||
// // string buffer became too large | ||
// if(s_write.length >= N_MAX_STRING_BUFFER) { | ||
// debugger; | ||
// } | ||
// terminate next pair | ||
// terminate next pair | ||
s_term_pairs = ' ;\n'; | ||
// indent next pair | ||
s_indent_pairs = s_indent+s_indent; | ||
// indent next pair | ||
s_indent_pairs = s_indent+s_indent_root; | ||
// call exit predicate listener | ||
if(f_exit_predicate) f_exit_predicate(); | ||
} | ||
// empty triples; cut out | ||
// empty triples; cut out | ||
if(b_empty) { | ||
@@ -339,20 +409,24 @@ s_write = s_write.slice(0, i_triples)+s_write.slice(i_pairs); | ||
} | ||
// delimit triple(s) | ||
// delimit triple(s) | ||
s_delim_triples = '\n'; | ||
// close triple | ||
// close triple | ||
s_write += `${s_term_pairs? ' ': s_indent_pairs}.\n`; // \n | ||
// call exit subject listener | ||
if(f_exit_subject) f_exit_subject(); | ||
} | ||
// close graph | ||
s_write += '}\n\n'; | ||
// close graph | ||
s_write += ((st_graph || !b_simplify_default_graph)? '}\n': '')+'\n'; | ||
// call exit graph listener | ||
if(f_exit_graph) f_exit_graph(); | ||
} | ||
return s_write; | ||
} | ||
// write objects | ||
objects(z_objects, n_nest_level=1) { | ||
// write objects | ||
_encode_objects(z_objects, n_nest_level=1) { | ||
let { | ||
prefixes: h_prefixes, | ||
indent: s_indent, | ||
coercions: hm_coercions, | ||
_h_prefixes: h_prefixes, | ||
_s_indent: s_indent, | ||
_hm_coercions: hm_coercions, | ||
} = this; | ||
@@ -385,5 +459,5 @@ | ||
for(let z_item of z_objects) { | ||
// item is an array; write RDF collection | ||
// item is an array; serialize list | ||
if(Array.isArray(z_item)) { | ||
s_write += s_term_object + this.collection_object(z_item, n_nest_level); | ||
s_write += s_term_object + this._serialize_list_object(z_item, n_nest_level); | ||
} | ||
@@ -393,3 +467,3 @@ // non-array | ||
// recurse on item | ||
s_write += s_term_object + this.objects(z_item, n_nest_level); | ||
s_write += s_term_object + this._encode_objects(z_item, n_nest_level); | ||
} | ||
@@ -411,2 +485,5 @@ | ||
// object exit listener | ||
let f_exit_object = null; | ||
// each pair | ||
@@ -422,3 +499,9 @@ for(let sc1_predicate in z_objects) { | ||
if('`' === sc1_predicate[0]) { | ||
s_write += this.directive(sc1_predicate, z_objects[sc1_predicate]); | ||
let g_apply = this._apply_directive(sc1_predicate, z_objects[sc1_predicate]); | ||
// write data | ||
if(g_apply.write) s_write += g_apply.write; | ||
// save exit listener | ||
if(g_apply.exit) f_exit_object = g_apply.exit; | ||
continue; | ||
@@ -429,3 +512,3 @@ } | ||
s_write += factory.c1(sc1_predicate, h_prefixes).terse(h_prefixes) + ' ' | ||
+ this.objects(z_objects[sc1_predicate], n_nest_level+1) +' ;'; | ||
+ this._encode_objects(z_objects[sc1_predicate], n_nest_level+1) +' ;'; | ||
} | ||
@@ -436,2 +519,5 @@ | ||
// call exit object listener | ||
if(f_exit_object) f_exit_object(); | ||
// serialize current predicate to blank node | ||
@@ -466,4 +552,4 @@ return s_write; | ||
// serialize collection object | ||
collection_object(a_collection, n_nest_level) { | ||
let s_indent = this.indent; | ||
_serialize_collection_object(a_collection, n_nest_level) { | ||
let s_indent = this._s_indent; | ||
@@ -479,7 +565,7 @@ // open collection block | ||
if(Array.isArray(z_item)) { | ||
s_objects = this.collection_object(z_item, n_nest_level+1); | ||
s_objects = this._serialize_collection_object(z_item, n_nest_level+1); | ||
} | ||
// non-array item | ||
else { | ||
s_objects = this.objects(z_item, n_nest_level+1); | ||
s_objects = this._encode_objects(z_item, n_nest_level+1); | ||
} | ||
@@ -501,4 +587,4 @@ | ||
// rdfjs quad | ||
serialize_quad(g_quad) { | ||
let h_prefixes = this.prefixes; | ||
_serialize_quad(g_quad) { | ||
let h_prefixes = this._h_prefixes; | ||
let kq_quad = factory.from.quad(g_quad); | ||
@@ -509,4 +595,4 @@ | ||
// serialize quad | ||
let s_write = (2 !== this.state? '\n': '') | ||
+this.graph_token+(st_graph? st_graph+' ': '')+'{\n\t' | ||
this._s_push += (2 !== this._xc_state? '\n': '') | ||
+this._s_graph_keyword+(st_graph? st_graph+' ': '')+'{\n\t' | ||
+kq_quad.subject.terse(h_prefixes)+' ' | ||
@@ -519,5 +605,3 @@ +kq_quad.predicate.terse(h_prefixes)+' ' | ||
// update state | ||
this.state = 2; | ||
return s_write; | ||
this._xc_state = 2; | ||
} | ||
@@ -528,2 +612,6 @@ } | ||
anonymous_blank_nodes: true, | ||
_serialize_c3r: TriG_Writer.prototype._serialize_c3, | ||
_serialize_c4r: TriG_Writer.prototype._serialize_c4, | ||
_serialize_comment: Writable.prototype._serialize_hash_comment, | ||
_serialize_list_object: TriG_Writer.prototype._serialize_collection_object, | ||
}); | ||
@@ -530,0 +618,0 @@ |
{ | ||
"name": "@graphy/content.trig.write", | ||
"version": "3.2.2", | ||
"description": "RDF TriG content writer", | ||
"version": "4.0.0", | ||
"description": "RDF TriG content writer for dynamic and stylized output", | ||
"keywords": [ | ||
@@ -26,5 +26,5 @@ "linked-data", | ||
"dependencies": { | ||
"@graphy/core.class.writable": "^3.2.2", | ||
"@graphy/core.data.factory": "^3.2.2", | ||
"big-integer": "^1.6.41" | ||
"@graphy/core.class.writable": "^4.0.0", | ||
"@graphy/core.data.factory": "^4.0.0", | ||
"big-integer": "^1.6.48" | ||
}, | ||
@@ -34,2 +34,2 @@ "engines": { | ||
} | ||
} | ||
} |
18417
509
+ Added@graphy/core.class.scribable@4.3.7(transitive)
+ Added@graphy/core.class.writable@4.3.7(transitive)
+ Added@graphy/core.data.factory@4.3.7(transitive)
+ Added@graphy/core.iso.stream@4.3.7(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
- Removed@graphy/core.class.writable@3.2.2(transitive)
- Removed@graphy/core.data.factory@3.2.2(transitive)
- Removed@graphy/core.iso.stream@3.2.2(transitive)
Updatedbig-integer@^1.6.48