Skip to content

Commit

Permalink
Merge pull request #523 from datagov-cz/198-escapovat-znak-paragrafu-…
Browse files Browse the repository at this point in the history
…v-iri

Escapování znaků při tvorbě IRI
  • Loading branch information
bindeali authored Feb 4, 2024
2 parents b2dcaa7 + c088578 commit af59343
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions src/function/FunctionCreateVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,41 @@ export function createValues(
return result;
}

export function createNewElemIRI(scheme: string, name: string): string {
// https://www.w3.org/TR/sparql11-query/#rPN_CHARS_U without [#x10000-#xEFFFF]
const PN_CHARS_U =
/[A-Za-z]|[\u00C0-\u00D6]|[\u00D8-\u00F6]|[\u00F8-\u02FF]|[\u0370-\u037D]|[\u037F-\u1FFF]|[\u200C-\u200D]|[\u2070-\u218F]|[\u2C00-\u2FEF]|[\u3001-\uD7FF]|[\uF900-\uFDCF]|[\uFDF0-\uFFFD]|_/gu;
const PERCENT = /%([0-9A-Fa-f])([0-9A-Fa-f])/gu;
const PN_LOCAL_ESC = /\\[_~.\-!$&'()*+,;=/?#@%]/g;
const PLX = new RegExp(`(${PERCENT.source})|(${PN_LOCAL_ESC.source})`, "gi");
const PN_CHARS = new RegExp(
`(${PN_CHARS_U.source})|[-0-9]|\u00B7|[\u0300-\u036F]|[\u203F-\u2040]`,
"gu"
);
const PN_LOCAL_1 = new RegExp(`(${PN_CHARS_U.source})|[:0-9]|(${PLX.source})`);
const PN_LOCAL_2 = new RegExp(`(${PN_CHARS.source})|[.:]|(${PLX.source})`);
const PN_LOCAL_3 = new RegExp(`(${PN_CHARS.source})|:|(${PLX.source})`);

export function createNewElemIRI(scheme: string, input: string): string {
// https://www.w3.org/TR/sparql11-query/#rPN_LOCAL
const PN_LOCAL = new RegExp(
`(${PN_LOCAL_1.source})((${PN_LOCAL_2.source})*(${PN_LOCAL_3.source}))?`,
"gu"
);
const name = input.toLowerCase().trim().normalize();
let result = "";
while (true) {
const arr = PN_LOCAL.exec(name);
if (arr === null) break;
result =
result
.padEnd(PN_LOCAL.lastIndex, "-")
.substring(0, PN_LOCAL.lastIndex - arr[0].length) + arr[0];
}
// Remove trailing dashes
result = result.replace(/-+$/g, "");
return (
(WorkspaceVocabularies[getVocabularyFromScheme(scheme)].namespace ||
`${scheme}/${Locale[Environment.language].term}/`) +
name
.toLowerCase()
.trim()
.normalize()
.replace(/[\s\\/]/g, "-")
.replace(/[(?&)"^<>]/g, "")
`${scheme}/${Locale[Environment.language].term}/`) + result
);
}

Expand Down

0 comments on commit af59343

Please sign in to comment.