Skip to content

Commit

Permalink
Replace codemeta-local.jsonld with concatenated contexts (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjonin authored May 8, 2024
1 parent 28b7936 commit ae70f9a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 108 deletions.
92 changes: 0 additions & 92 deletions data/contexts/codemeta-local.jsonld

This file was deleted.

34 changes: 25 additions & 9 deletions js/codemeta_generation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

"use strict";

const LOCAL_CONTEXT_PATH = "./data/contexts/codemeta-local.jsonld";
const LOCAL_CONTEXT_URL = "local";
const CODEMETA_CONTEXTS = {
"2.0": {
path: "./data/contexts/codemeta-2.0.jsonld",
Expand All @@ -23,14 +21,12 @@ const CODEMETA_CONTEXTS = {
const SPDX_PREFIX = 'https://spdx.org/licenses/';

const loadContextData = async () => {
const [contextLocal, contextV2, contextV3] =
const [contextV2, contextV3] =
await Promise.all([
fetch(LOCAL_CONTEXT_PATH).then(response => response.json()),
fetch(CODEMETA_CONTEXTS["2.0"].path).then(response => response.json()),
fetch(CODEMETA_CONTEXTS["3.0"].path).then(response => response.json())
]);
return {
[LOCAL_CONTEXT_URL]: contextLocal,
[CODEMETA_CONTEXTS["2.0"].url]: contextV2,
[CODEMETA_CONTEXTS["3.0"].url]: contextV3
}
Expand All @@ -54,6 +50,10 @@ const initJsonldLoader = contexts => {
jsonld.documentLoader = getJsonldCustomLoader(contexts);
};

const getAllCodemetaContextUrls= () => {
return Object.values(CODEMETA_CONTEXTS).map(context => context.url);
}

function emptyToUndefined(v) {
if (v == null || v == "")
return undefined;
Expand Down Expand Up @@ -217,9 +217,9 @@ function generateReview() {
return doc;
}

async function buildExpandedJson() {
async function buildExpandedDocWithAllContexts() {
var doc = {
"@context": LOCAL_CONTEXT_URL,
"@context": getAllCodemetaContextUrls(),
"@type": "SoftwareSourceCode",
};

Expand Down Expand Up @@ -274,7 +274,9 @@ async function generateCodemeta(codemetaVersion = "2.0") {
var codemetaText, errorHTML;

if (inputForm.checkValidity()) {
const expanded = await buildExpandedJson();
// Expand document with all contexts before compacting
// to allow generating property from any context
const expanded = await buildExpandedDocWithAllContexts();
const compacted = await jsonld.compact(expanded, CODEMETA_CONTEXTS[codemetaVersion].url);
codemetaText = JSON.stringify(compacted, null, 4);
errorHTML = "";
Expand Down Expand Up @@ -380,9 +382,23 @@ function importPersons(prefix, legend, docs) {
});
}

async function recompactDocWithAllContexts(doc) {
const allContexts = getAllCodemetaContextUrls();
const newDoc = structuredClone(doc);
newDoc["@context"] = allContexts;
const expanded = await jsonld.expand(newDoc);
const compacted = await jsonld.compact(expanded, allContexts);
return compacted;
}

async function importCodemeta() {
var inputForm = document.querySelector('#inputForm');
var doc = await parseAndValidateCodemeta(false);
var doc = parseAndValidateCodemeta(false);

// Re-compact document with all contexts
// to allow importing property from any context
doc = await recompactDocWithAllContexts(doc);

resetForm();

if (doc['license'] !== undefined) {
Expand Down
11 changes: 4 additions & 7 deletions js/validation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ function validateDocument(doc) {
}


async function parseAndValidateCodemeta(showPopup) {
function parseAndValidateCodemeta(showPopup) {
var codemetaText = document.querySelector('#codemetaText').innerText;
let parsed, doc;
var doc;

try {
parsed = JSON.parse(codemetaText);
doc = JSON.parse(codemetaText);
}
catch (e) {
setError(`Could not read codemeta document because it is not valid JSON (${e}). Check for missing or extra quote, colon, or bracket characters.`);
Expand All @@ -79,7 +79,7 @@ async function parseAndValidateCodemeta(showPopup) {

setError("");

var isValid = validateDocument(parsed);
var isValid = validateDocument(doc);
if (showPopup) {
if (isValid) {
alert('Document is valid!')
Expand All @@ -89,8 +89,5 @@ async function parseAndValidateCodemeta(showPopup) {
}
}

parsed["@context"] = LOCAL_CONTEXT_URL;
const expanded = await jsonld.expand(parsed);
doc = await jsonld.compact(expanded, LOCAL_CONTEXT_URL);
return doc;
}

0 comments on commit ae70f9a

Please sign in to comment.