SkillAgentSearch skills...

Comunica

LiaScript template for interactive SPARQL-queries and playing around with the semantic web technologies

Install / Use

/learn @LiaTemplates/Comunica
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<!-- author: André Dietrich email: LiaScript@web.de version: 0.0.3 logo: https://semanticweb.cs.vu.nl/R/sparql_hollywood/sparql_hollywood_files/Screen-Shot-2012-09-28-at-20.27.12.png language: en narrator: US English Female comment: A macro library for trying and playing with SPARQL and RDF that is based on Comunica v5. script: dist/index.js @onload window.htmlTable = function (rows, opts = {}) { const { columns, caption, includeIndex = true, indexHeader = "i", linkify = true, urlRegex = /^(https?:\/\/|mailto:|ftp:\/\/)/i } = opts; const escapeHtml = (v) => String(v) .replace(/&/g, "&amp;") .replace(/</g, "&lt;") .replace(/>/g, "&gt;") .replace(/"/g, "&quot;") .replace(/'/g, "&#39;"); const allColumns = (() => { if (Array.isArray(columns) && columns.length) return columns.slice(); const set = new Set(); for (const r of rows || []) Object.keys(r || {}).forEach((k) => set.add(k)); return Array.from(set); })(); const formatCell = (value) => { if (value === null || value === undefined) return ""; const raw = typeof value === "string" ? value : JSON.stringify(value); if (linkify && typeof raw === "string" && urlRegex.test(raw)) { const href = escapeHtml(raw); return `<a href="${href}" target="_blank" rel="noopener noreferrer">${href}</a>`; } return escapeHtml(raw); }; const ths = [ includeIndex ? `<th>${escapeHtml(indexHeader)}</th>` : "", ...allColumns.map((c) => `<th>${escapeHtml(c)}</th>`) ].join(""); const trs = (rows || []).map((r, i) => { const tds = [ includeIndex ? `<td class="index">${i}</td>` : "", ...allColumns.map((c) => `<td>${formatCell(r?.[c])}</td>`) ].join(""); return `<tr>${tds}</tr>`; }).join(""); const cap = caption ? `<caption>${escapeHtml(caption)}</caption>` : ""; return ` <style> .html-console-table { width: 100%; border-collapse: collapse; font-family: Consolas, Monaco, monospace; font-size: 13px; background: #111; color: #eee; border: 1px solid #333; } /* Caption */ .html-console-table caption { caption-side: top; text-align: left; padding: 10px; font-weight: bold; color: #fff; } /* Header */ .html-console-table thead { background: #1b1b1b; } .html-console-table th { padding: 8px 10px; text-align: left; border: 1px solid #333; /* vertical + horizontal lines */ color: #9cdcfe; font-weight: 600; } /* Body cells */ .html-console-table td { padding: 8px 10px; border: 1px solid #2a2a2a; /* full grid */ vertical-align: top; word-break: break-all; } /* Zebra rows */ .html-console-table tbody tr:nth-child(even) { background: #161616; } /* Hover */ .html-console-table tbody tr:hover { background: #222; } /* Index column */ .html-console-table .index { color: #888; } /* Links */ .html-console-table a { color: #4fc1ff; text-decoration: none; word-break: break-all; } .html-console-table a:hover { text-decoration: underline; color: #82e0ff; } </style> <table class="html-console-table"> ${cap} <thead><tr>${ths}</tr></thead> <tbody>${trs}</tbody> </table> `.trim(); } @end @Comunica.SPARQL <script> function output(ok, message) { if (ok) { if (typeof message === 'string') send.lia(message) else { console.html(htmlTable(message)) send.lia("LIA: stop") } } else { let errMessage = [] let err = message.match(/Parse error on line (\d+)[^\n]\n((?:.|\n)+)/) if (err && err.length >= 3) { errMessage = [[{ row : parseInt(err[1]) - 1, column : 1, text : err[2], type : "error" }]] } send.lia(message, errMessage, false) } } window.RDF.query(`@input`, output, [] ) "LIA: wait" </script> @end @Comunica.RDF_SPARQL <script> function output(ok, message) { if (ok) { if (typeof message === 'string') send.lia(message) else { console.html(htmlTable(message)) send.lia("LIA: stop") } } else { let errMessage = [] let err = message.match(/Parse error on line (\d+)[^\n]\n((?:.|\n)+)/) if (err && err.length >= 3) { errMessage = [[], [{ row : parseInt(err[1]) - 1, column : 1, text : err[2], type : "error" }]] } send.lia(message, errMessage, false) } } window.RDF.parse.rdf(`@input`).then((e) => { window.RDF.query(`@input(1)`, output, [ e ] ) }).catch((error) => { output(false, error.message || error.toString()) }) "LIA: wait" </script> @end -->

Comunica - Template

                      --{{0}}--

This document defines some basic LiaScript-macros for dealing and experimenting with the semantic web-technologies, such as querying SPARQL and or dealing with RDF in the form of turtle. Others are about to come. This template is mainly based on Comunica an open-source knowledge graph querying framework. I hope in the future, there will be more functions available. At the moment it is possible to execute and edit code-blocks with SPARQL queries.

Try it on LiaScript:

https://liascript.github.io/course/?https://raw.githubusercontent.com/liaTemplates/Comunica/main/README.md

See the project on Github:

https://github.com/liaTemplates/Comunica

                     --{{1}}--

There are three ways to use this template. The easiest way is to use the import statement and the url of the raw text-file of the master branch or any other branch or version. But you can also copy the required functionionality directly into the header of your Markdown document, see therefor the last slide. And of course, you could also clone this project and change it, as you wish.

                       {{1}}
  1. Load the macros via

    import: https://raw.githubusercontent.com/LiaTemplates/Comunica/0.0.3/README.md

  2. Copy the definitions into your Project

  3. Clone this repository on GitHub

Minimal Requirements

                     --{{0}}--

What is actually needed to make your Markdown code-snippets with SPARQL executable and editable? Not much, simply add the following comment to the head of your Markdown document.

<!--
import: https://raw.githubusercontent.com/LiaTemplates/Comunica/0.0.3/README.md
-->

# Title

...
                     --{{1}}--

And finally attach the macros/functions, which are defined by this document, to the end of a code-block. You can define as much code-blocks as you want.

                       {{1}}
``` sparql
# source: https://fragments.dbpedia.org/2015/en

SELECT ?s ?p ?o WHERE {
  ?s ?p <http://dbpedia.org/resource/Ukraine>.
  ?s ?p ?o
} LIMIT 100
```
@Comunica.SPARQL

Macros

                      --{{0}}--

Currently you can use two macros, which can also be tweaked a bit. We will describe them both within this section.

  1. @Comunica.SPARQL
  2. @Comunica.RDF_SPARQL

@Comunica.SPARQL

                      --{{0}}--

Add the macro @Comunica.SPARQL to the end of your SPARQL query to make it executable and editable. You can add as much blocks as you want to.

``` sparql
# source: https://fragments.dbpedia.org/2015/en

SELECT ?s ?p ?o WHERE {
  ?s ?p <http://dbpedia.org/resource/Ukraine>.
  ?s ?p ?o
} LIMIT 10
```
@Comunica.SPARQL
                      --{{1}}--

The result will look as follows, you can execute it, change the code of the query and go back and forth between your version.

                        {{1}}
# source: https://fragments.dbpedia.org/2015/en

SELECT ?s ?p ?o WHERE {
  ?s ?p <http://dbpedia.org/resource/Ukraine>.
  ?s ?p ?o
} LIMIT 10

@Comunica.SPARQL

                      --{{2}}--

And to some extend also the errors are propagated to the user.

                        {{2}}
# source: https://fragments.dbpedia.org/2015/en

SELECT ?s ?p ?o WHERE {
  ?s ?p <http://dbpedia.org/resource/Ukraine>
  ?s ?p ?o
} LIMIT 10

@Comunica.SPARQL

Sources

                      --{{0}}--

As you have seen earlier, it is possible by you and by your user to define the sources for your query. Simply add one or multiple comments of the following form into your code-block. If you use a one-liner, your sources have to be separated by one space.

# source: https://fragments.dbpedia.org/2015/en
# source: https://www.rubensworks.net https://ruben.verborgh.org/profile/

SELECT ?s ?p ?o WHERE {
?s ?p <http://dbpedia.org/resource/Belgium>.
?s ?p ?o
} LIMIT 100

@Comunica.SPARQL

Formats

                      --{{0}}--

It is possible to define different output formats, simply by adding an comment of the form # format: _.

# format: table
# source: https://fragments.dbpedia.org/2015/en

SELECT ?s ?p ?o WHERE {
?s ?p <http://dbpedia.org/resource/Ukraine>.
?s ?p ?o
} LIMIT 5

@Comunica.SPARQL

                      --{{1}}--

The default format is currently table, however, you can overwrite this by one of the following media types. All possible formats are defined here. However, currently it only possible to use a subset of them, which are highlighted in the table.

                        {{1}}

| Media type | Supported | Description | | --------------------------------- |:---------:| -------------------------------------------------------------------------------- | | `application/

Related Skills

View on GitHub
GitHub Stars5
CategoryDevelopment
Updated1mo ago
Forks0

Languages

TypeScript

Security Score

90/100

Audited on Feb 12, 2026

No findings