-
Notifications
You must be signed in to change notification settings - Fork 146
parse and serialize with dot in term name #718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -63,6 +63,36 @@ installed. | |||||
| npm install --save rdflib | ||||||
| ``` | ||||||
|
|
||||||
| ## Serializer flags | ||||||
|
|
||||||
| The Turtle/N3/JSON‑LD serializers accept an optional flags string to tweak output formatting and abbreviation behavior. | ||||||
|
|
||||||
| - Pass flags via the options argument to `serialize(...)`: | ||||||
|
|
||||||
| ```ts | ||||||
| import { graph, serialize, sym } from 'rdflib' | ||||||
|
|
||||||
| const kb = graph() | ||||||
| const doc = sym('http://example.com/doc') | ||||||
| // ... add some statements ... | ||||||
|
|
||||||
| // Example: prevent dotted local parts in prefixed names | ||||||
| const turtle = serialize(doc, kb, doc.value, 'text/turtle', undefined, { flags: 'o' }) | ||||||
| ``` | ||||||
|
|
||||||
| Common flags used internally (you can combine them, e.g. `'o k'`): | ||||||
|
|
||||||
| - `s` `i` – used by default for Turtle to suppress `=`, `=>` notations | ||||||
| - `d e i n p r s t u x` – used for N-Triples/N-Quads to simplify output | ||||||
| - `dr` – used with JSON‑LD conversion (no default, no relative prefix) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just looking for a confirmation that
Suggested change
|
||||||
| - `o` – new: do not abbreviate to a prefixed name when the local part contains a dot. This keeps IRIs like | ||||||
| `http://foo.test/ns/subject.example` in `<...>` form instead of `ns:subject.example`. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to match line 6 of
Suggested change
|
||||||
|
|
||||||
| Notes: | ||||||
|
|
||||||
| - For Turtle and JSON‑LD, user‑provided flags are merged with the defaults so your flags (like `o`) are honored. | ||||||
| - By contrast, passing `'p'` disables prefix abbreviations entirely (all terms are written as `<...>` IRIs). | ||||||
|
|
||||||
| ## Contribute | ||||||
|
|
||||||
| #### Subdirectories | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,15 @@ | ||||||
| const $rdf = require('./lib'); | ||||||
| const kb = $rdf.graph(); | ||||||
| const base = 'http://example.com/'; | ||||||
| const doc = $rdf.sym(base + 'doc'); | ||||||
| // A URI in a different namespace so it can abbreviate to a prefix | ||||||
| const other = 'http://foo.test/ns/subject.example'; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The different namespace should also be in the reserved TLDs.
Suggested change
|
||||||
| kb.add($rdf.sym(base + 's'), $rdf.sym(base + 'p'), $rdf.sym(other), doc); | ||||||
|
|
||||||
| function run(flags) { | ||||||
| const out = $rdf.serialize(doc, kb, doc.uri, 'text/turtle', undefined, { flags }); | ||||||
| console.log('FLAGS=' + flags + '\n' + out); | ||||||
| } | ||||||
|
|
||||||
| run(''); | ||||||
| run('o'); | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,15 +72,17 @@ export default function serialize ( | |
| return executeCallback(null, documentString) | ||
| case TurtleContentType: | ||
| case TurtleLegacyContentType: | ||
| sz.setFlags('si') // Suppress = for sameAs and => for implies | ||
| // Suppress = for sameAs and => for implies; preserve any user-specified flags (e.g., 'o') | ||
| sz.setFlags('si' + (opts.flags ? (' ' + opts.flags) : '')) | ||
| documentString = sz.statementsToN3(newSts) | ||
| return executeCallback(null, documentString) | ||
| case NTriplesContentType: | ||
| sz.setFlags('deinprstux') // Suppress nice parts of N3 to make ntriples | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be |
||
| documentString = sz.statementsToNTriples(newSts) | ||
| return executeCallback(null, documentString) | ||
| case JSONLDContentType: | ||
| sz.setFlags('si dr') // turtle + dr (means no default, no relative prefix) | ||
| // turtle + dr (means no default, no relative prefix); preserve user flags | ||
| sz.setFlags('si dr' + (opts.flags ? (' ' + opts.flags) : '')) | ||
| documentString = sz.statementsToJsonld(newSts) // convert via turtle | ||
| return executeCallback(null, documentString) | ||
| case NQuadsContentType: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,12 @@ export class Serializer { | |
| return this | ||
| } | ||
|
|
||
| /** | ||
| * Set serializer behavior flags. Letters can be combined with spaces. | ||
| * Examples: 'si', 'deinprstux', 'si dr', 'o'. | ||
| * Notable flags: | ||
| * - 'o': do not abbreviate to a prefixed name when the local part contains a dot | ||
| */ | ||
| setFlags(flags) { | ||
| this.flags = flags || ''; | ||
| return this | ||
|
|
@@ -255,6 +261,29 @@ export class Serializer { | |
| _notNameChars = | ||
| (this._notQNameChars + ':') | ||
|
|
||
| // Validate if a string is a valid PN_LOCAL per Turtle 1.1 spec | ||
| // Allows dots inside the local name but not as trailing character | ||
| // Also allows empty local names (for URIs ending in / or #) | ||
| isValidPNLocal(local) { | ||
| // Empty local name is valid (e.g., ex: for http://example.com/) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that resolution of this "empty" local name is determined server-side. It often defaults to |
||
| if (local.length === 0) return true | ||
|
|
||
| // Cannot end with a dot | ||
| if (local[local.length - 1] === '.') return false | ||
|
|
||
| // Check each character (allow dots mid-string) | ||
| for (var i = 0; i < local.length; i++) { | ||
| var ch = local[i] | ||
| // Dot is allowed unless it's the last character (checked above) | ||
| if (ch === '.') continue | ||
| // Other characters must not be in the blacklist | ||
| if (this._notNameChars.indexOf(ch) >= 0) { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| explicitURI(uri) { | ||
| if (this.flags.indexOf('r') < 0 && this.base) { | ||
| uri = Uri.refTo(this.base, uri) | ||
|
|
@@ -628,13 +657,17 @@ export class Serializer { | |
| if (j >= 0 && this.flags.indexOf('p') < 0 && | ||
| // Can split at namespace but only if http[s]: URI or file: or ws[s] (why not others?) | ||
| (uri.indexOf('http') === 0 || uri.indexOf('ws') === 0 || uri.indexOf('file') === 0)) { | ||
| var canSplit = true | ||
| for (var k = j + 1; k < uri.length; k++) { | ||
| if (this._notNameChars.indexOf(uri[k]) >= 0) { | ||
| canSplit = false | ||
| break | ||
| } | ||
| } | ||
| var localid = uri.slice(j + 1) | ||
| var namesp = uri.slice(0, j + 1) | ||
| // Don't split if namespace is just the protocol (e.g., https://) | ||
| // A valid namespace should have content after the protocol | ||
| var minNamespaceLength = uri.indexOf('://') + 4 // e.g., "http://x" minimum | ||
| // Also don't split if namespace is the base directory (would serialize as relative URI) | ||
| var baseDir = this.base ? this.base.slice(0, Math.max(this.base.lastIndexOf('/'), this.base.lastIndexOf('#')) + 1) : null | ||
| var namespaceIsBaseDir = baseDir && namesp === baseDir | ||
| // If flag 'o' is present, forbid dots in local part when abbreviating | ||
| var forbidDotLocal = this.flags.indexOf('o') >= 0 && localid.indexOf('.') >= 0 | ||
| var canSplit = !namespaceIsBaseDir && !forbidDotLocal && namesp.length > minNamespaceLength && this.isValidPNLocal(localid) | ||
| /* | ||
| if (uri.slice(0, j + 1) === this.base + '#') { // base-relative | ||
| if (canSplit) { | ||
|
|
@@ -645,8 +678,6 @@ export class Serializer { | |
| } | ||
| */ | ||
| if (canSplit) { | ||
| var localid = uri.slice(j + 1) | ||
| var namesp = uri.slice(0, j + 1) | ||
| if (this.defaultNamespace && this.defaultNamespace === namesp && | ||
| this.flags.indexOf('d') < 0) { // d -> suppress default | ||
| if (this.flags.indexOf('k') >= 0 && | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.