From 5fee310c9668677b1dca0f01edbbc154c0be787c Mon Sep 17 00:00:00 2001 From: Ioannis Rousochatzakis Date: Sun, 8 Dec 2024 19:58:24 +0100 Subject: [PATCH 01/10] Improve SPARQL minification by removing comments --- index.html | 2 +- package-lock.json | 69 +++++++++++++++++++++++++++++++++++++++++------ package.json | 4 ++- src/js/script.js | 37 +++++++++++++------------ 4 files changed, 83 insertions(+), 29 deletions(-) diff --git a/index.html b/index.html index 169fa40..2e63079 100644 --- a/index.html +++ b/index.html @@ -323,7 +323,7 @@
Backend Components
- + diff --git a/package-lock.json b/package-lock.json index e54e7c6..63180a9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,25 @@ "cors": "^2.8.5", "express": "^4.17.1", "https-proxy-agent": "^7.0.5", - "node-fetch": "^2.6.1" + "node-fetch": "^2.6.1", + "sparqljs": "^3.7.3", + "ted-sparql-editor": "file:" + } + }, + "node_modules/@rdfjs/types": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@rdfjs/types/-/types-1.1.2.tgz", + "integrity": "sha512-wqpOJK1QCbmsGNtyzYnojPU8gRDPid2JO0Q0kMtb4j65xhCK880cnKAfEOwC+dX85VJcCByQx5zOwyyfCjDJsg==", + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/node": { + "version": "22.10.1", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.10.1.tgz", + "integrity": "sha512-qKgsUwfHZV2WCWLAnVP1JqnpE6Im6h3Y0+fYgMTasNQ7V++CBX5OT1as0g0f+OyubbFqhf6XVNIsmN4IIhEgGQ==", + "dependencies": { + "undici-types": "~6.20.0" } }, "node_modules/accepts": { @@ -243,9 +261,9 @@ } }, "node_modules/express": { - "version": "4.21.1", - "resolved": "https://registry.npmjs.org/express/-/express-4.21.1.tgz", - "integrity": "sha512-YSFlK1Ee0/GC8QaO91tHcDxJiE/X4FbpAyQWkxAvG6AXCuR65YzK8ua6D9hvi/TzUfZMpc+BwuM1IPw8fmQBiQ==", + "version": "4.21.2", + "resolved": "https://registry.npmjs.org/express/-/express-4.21.2.tgz", + "integrity": "sha512-28HqgMZAmih1Czt9ny7qr6ek2qddF4FclbMzwhCREB6OFfH+rXAnuNCwo1/wFvrtbgsQDb4kSbX9de9lFbrXnA==", "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", @@ -266,7 +284,7 @@ "methods": "~1.1.2", "on-finished": "2.4.1", "parseurl": "~1.3.3", - "path-to-regexp": "0.1.10", + "path-to-regexp": "0.1.12", "proxy-addr": "~2.0.7", "qs": "6.13.0", "range-parser": "~1.2.1", @@ -281,6 +299,10 @@ }, "engines": { "node": ">= 0.10.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" } }, "node_modules/finalhandler": { @@ -594,9 +616,9 @@ } }, "node_modules/path-to-regexp": { - "version": "0.1.10", - "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.10.tgz", - "integrity": "sha512-7lf7qcQidTku0Gu3YDPc8DJ1q7OOucfa/BSsIwjuh56VU7katFvuM8hULfkwB3Fns/rsVF7PwPKVw1sl5KQS9w==" + "version": "0.1.12", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.12.tgz", + "integrity": "sha512-RA1GjUVMnvYFxuqovrEqZoxxW5NUZqbwKtYz/Tt7nXerk0LbLblQmrsgdeOxV5SFHf0UDggjS/bSeOZwt1pmEQ==" }, "node_modules/proxy-addr": { "version": "2.0.7", @@ -646,6 +668,14 @@ "node": ">= 0.8" } }, + "node_modules/rdf-data-factory": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/rdf-data-factory/-/rdf-data-factory-1.1.2.tgz", + "integrity": "sha512-TfQD63Lokabd09ES1jAtKK8AA6rkr9rwyUBGo6olOt1CE0Um36CUQIqytyf0am2ouBPR0l7SaHxCiMcPGHkt1A==", + "dependencies": { + "@rdfjs/types": "*" + } + }, "node_modules/safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", @@ -758,6 +788,20 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/sparqljs": { + "version": "3.7.3", + "resolved": "https://registry.npmjs.org/sparqljs/-/sparqljs-3.7.3.tgz", + "integrity": "sha512-FQfHUhfwn5PD9WH6xPU7DhFfXMgqK/XoDrYDVxz/grhw66Il0OjRg3JBgwuEvwHnQt7oSTiKWEiCZCPNaUbqgg==", + "dependencies": { + "rdf-data-factory": "^1.1.2" + }, + "bin": { + "sparqljs": "bin/sparql-to-json" + }, + "engines": { + "node": "^12.22.0 || ^14.17.0 || >=16.0.0" + } + }, "node_modules/statuses": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", @@ -766,6 +810,10 @@ "node": ">= 0.8" } }, + "node_modules/ted-sparql-editor": { + "resolved": "", + "link": true + }, "node_modules/toidentifier": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", @@ -791,6 +839,11 @@ "node": ">= 0.6" } }, + "node_modules/undici-types": { + "version": "6.20.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.20.0.tgz", + "integrity": "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==" + }, "node_modules/unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", diff --git a/package.json b/package.json index ca9a9dd..541f0a1 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,8 @@ "cors": "^2.8.5", "express": "^4.17.1", "https-proxy-agent": "^7.0.5", - "node-fetch": "^2.6.1" + "node-fetch": "^2.6.1", + "sparqljs": "^3.7.3", + "ted-sparql-editor": "file:" } } diff --git a/src/js/script.js b/src/js/script.js index d6cf123..55e4840 100644 --- a/src/js/script.js +++ b/src/js/script.js @@ -11,6 +11,7 @@ * or implied. See the Licence for the specific language governing permissions and limitations under * the Lic */ +import SparqlJs from 'https://cdn.jsdelivr.net/npm/sparqljs@3.7.3/+esm'; // Production SPARQL endpoint const SPARQL_ENDPOINT = 'https://publications.europa.eu/webapi/rdf/sparql'; @@ -71,34 +72,31 @@ document.addEventListener('DOMContentLoaded', async function () { placeholder: "Enter your SPARQL query here..." }); + // Add this function to minify SPARQL queries using sparqljs + function minifySparqlQuery(query) { + const parser = new SparqlJs.Parser(); + const generator = new SparqlJs.Generator(); + const parsedQuery = parser.parse(query); + return generator.stringify(parsedQuery); + } + // Update button state on editor changes - editor.on("change", function() { + function onEditorChange() { runQueryButton.disabled = !editor.getValue().trim(); - }); - - // Add this function to minify SPARQL queries - function minifySparqlQuery(query) { - return query - .replace(/\s+/g, ' ') // Replace multiple spaces/newlines with single space - .replace(/\s*\{\s*/g, '{') // Remove spaces around braces - .replace(/\s*\}\s*/g, '}') - .replace(/\s*\(\s*/g, '(') // Remove spaces around parentheses - .replace(/\s*\)\s*/g, ')') - .replace(/\s*\.\s*/g, '.') // Remove spaces around dots - .replace(/\s*;\s*/g, ';') // Remove spaces around semicolons - .replace(/\s*,\s*/g, ',') // Remove spaces around commas - .trim(); } + editor.on("change", onEditorChange); + // Update copyUrlButton click handler copyUrlButton.addEventListener('click', function () { - const query = minifySparqlQuery(editor.getValue()); + const query = editor.getValue(); + const minifiedQuery = minifySparqlQuery(query); const format = document.getElementById("format").value || "application/sparql-results+json"; const defaultGraphUri = document.getElementById("default-graph-uri").value; const timeout = document.getElementById("timeout").value || 30000; // Use the original SPARQL endpoint URL - const url = `${SPARQL_ENDPOINT}?default-graph-uri=${encodeURIComponent(defaultGraphUri)}&query=${encodeURIComponent(query)}&format=${encodeURIComponent(format)}&timeout=${encodeURIComponent(timeout)}`; + const url = `${SPARQL_ENDPOINT}?default-graph-uri=${encodeURIComponent(defaultGraphUri)}&query=${encodeURIComponent(minifiedQuery)}&format=${encodeURIComponent(format)}&timeout=${encodeURIComponent(timeout)}`; console.log(`Generated URL: ${url}`); navigator.clipboard.writeText(url).then(() => { @@ -111,13 +109,14 @@ document.addEventListener('DOMContentLoaded', async function () { // Update openUrlButton click handler openUrlButton.addEventListener('click', function () { - const query = minifySparqlQuery(editor.getValue()); + const query = editor.getValue(); + const minifiedQuery = minifySparqlQuery(query); const format = document.getElementById("format").value || "application/sparql-results+json"; const defaultGraphUri = document.getElementById("default-graph-uri").value; const timeout = document.getElementById("timeout").value || 30000; // Use the original SPARQL endpoint URL - const url = `${SPARQL_ENDPOINT}?default-graph-uri=${encodeURIComponent(defaultGraphUri)}&query=${encodeURIComponent(query)}&format=${encodeURIComponent(format)}&timeout=${encodeURIComponent(timeout)}`; + const url = `${SPARQL_ENDPOINT}?default-graph-uri=${encodeURIComponent(defaultGraphUri)}&query=${encodeURIComponent(minifiedQuery)}&format=${encodeURIComponent(format)}&timeout=${encodeURIComponent(timeout)}`; window.open(url, '_blank'); }); From 9b1db35a0c34da7bf6e4759a1c8f23c69f2fdc65 Mon Sep 17 00:00:00 2001 From: Ioannis Rousochatzakis Date: Wed, 11 Dec 2024 12:21:21 +0100 Subject: [PATCH 02/10] Added error checking for SPARQL syntaxt --- index.html | 5 ++- src/assets/style.css | 18 +++++++++ src/js/script.js | 88 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 106 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index 2e63079..94249a0 100644 --- a/index.html +++ b/index.html @@ -34,7 +34,6 @@ -