From bda874c31f5bc46790badf890f5a998db04bedcc Mon Sep 17 00:00:00 2001 From: "Bruce W. Herr II" Date: Fri, 27 Oct 2023 12:00:19 -0400 Subject: [PATCH] Make readme generation a little more forgiving and use jsonld cli for jsonld conversion (it works well) --- scripts/setup-environment.sh | 3 ++ src/enrichment/enrich-graph.js | 10 +++--- src/finalizing/utils/generate-readme-md.js | 8 ++--- src/utils/reify.js | 40 +++++++++++++++------- 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/scripts/setup-environment.sh b/scripts/setup-environment.sh index bf5fcf9..948a673 100755 --- a/scripts/setup-environment.sh +++ b/scripts/setup-environment.sh @@ -87,6 +87,9 @@ if [ -e "$ENV/bin/activate" ]; then # Install the RDF Formatter (for RDFa support) npm install -g rdf-formatter + # Install the JSON-LD CLI (a better JSON-LD converter) + npm install -g jsonld-cli + # Download ontologies ${ROOT_DIR}/scripts/download-ontologies.sh diff --git a/src/enrichment/enrich-graph.js b/src/enrichment/enrich-graph.js index a573b3c..6604166 100644 --- a/src/enrichment/enrich-graph.js +++ b/src/enrichment/enrich-graph.js @@ -2,13 +2,14 @@ import { existsSync, readFileSync } from 'fs'; import { load } from 'js-yaml'; import { resolve } from 'path'; import { error, info } from '../utils/logging.js'; -import { convert, merge } from '../utils/robot.js'; +import { RDF_EXTENSIONS, convert } from '../utils/reify.js'; +import { merge } from '../utils/robot.js'; import { cleanTemporaryFiles, convertNormalizedDataToOwl, convertNormalizedMetadataToRdf, + logOutput, runCompleteClosure, - logOutput } from './utils.js'; export function enrichGraphMetadata(context) { @@ -39,7 +40,7 @@ export function enrichGraphData(context) { const extension = inputRdfFile.split('.').slice(-1)[0]; if (extension === 'ttl') { toMerge.push(inputRdf); - } else if (extension === 'rdf' || extension === 'jsonld' || extension === 'owl'){ + } else if (RDF_EXTENSIONS.has(extension)) { const outputTtl = resolve(obj.path, 'enriched', inputRdfFile + '.ttl'); convert(inputRdf, outputTtl, 'ttl'); toMerge.push(outputTtl); @@ -54,11 +55,10 @@ export function enrichGraphData(context) { info(`Creating graph: ${enrichedPath}`); convert(enrichedMergePath, enrichedPath, 'ttl'); - info('Optimizing graph...') + info('Optimizing graph...'); const redundantPath = resolve(obj.path, 'enriched/redundant.ttl'); runCompleteClosure(enrichedPath, redundantPath); logOutput(redundantPath); - } catch (e) { error(e); } finally { diff --git a/src/finalizing/utils/generate-readme-md.js b/src/finalizing/utils/generate-readme-md.js index 166e236..75e4bbd 100644 --- a/src/finalizing/utils/generate-readme-md.js +++ b/src/finalizing/utils/generate-readme-md.js @@ -11,17 +11,17 @@ export function renderReadmeMd(templateFile, metadata) { const env = new Environment(undefined, { autoescape: false }); env.addFilter('authorList', (list) => { - return list.map(a => a.fullName).join('; '); + return list?.map(a => a.fullName).join('; ') ?? ''; }); env.addFilter('orcidList', (list) => { - return list.map(a => `[${a.orcid}](https://orcid.org/${a.orcid})`).join('; '); + return list?.map(a => `[${a.orcid}](https://orcid.org/${a.orcid})`).join('; ') ?? ''; }); env.addFilter('downloadLinks', (datatable) => { - return datatable.map((str) => { + return datatable?.map((str) => { const ext = str !== undefined ? str.slice(str.replace('.zip', '').lastIndexOf('.') + 1).replace(')', '') : ''; return `[${ext.toUpperCase()}](assets/${str})` }).join(' '); - }); + }) ?? ''; const template = readFileSync(templateFile).toString(); return env.renderString(template, { ...TYPE_MAPPINGS, ...metadata }); } diff --git a/src/utils/reify.js b/src/utils/reify.js index b98935b..b1b432e 100644 --- a/src/utils/reify.js +++ b/src/utils/reify.js @@ -9,6 +9,27 @@ const FORMATS = { nq: 'application/n-quads', }; +export const RDF_EXTENSIONS = new Set([ + 'json', + 'jsonld', + 'json-ld', + 'nt', + 'xml', + 'rdf', + 'nq', + 'n3', + '.owl', + '.trig', + 'turtle', +]); + +export const JSONLD_EXTENSIONS = new Set(['json', 'jsonld', 'json-ld']); + +export function isJsonLd(fileOrUrl) { + const extension = fileOrUrl.split('.').slice(-1)[0]; + return JSONLD_EXTENSIONS.has(extension); +} + export function reifyDoTurtle(context, inputPath) { const graphName = context.selectedDigitalObject.iri; reifyTurtle(inputPath, graphName); @@ -44,18 +65,13 @@ function reifyTurtle(inputPath, graphName) { } } -function convert(inputPath, outputPath, outputFormat, graphName) { +export function convert(inputPath, outputPath, outputFormat, graphName) { + let command = `rdfpipe --output-format ${outputFormat} ${inputPath}`; + if (isJsonLd(inputPath)) { + command = `cat ${inputPath} | jsonld expand | rdfpipe --input-format json-ld --output-format ${outputFormat}`; + } if (graphName && outputFormat === 'application/n-quads') { - throwOnError( - `rdfpipe --output-format ${outputFormat} ${inputPath} | \\ - perl -pe 's|\\Qfile://${inputPath}\\E|${graphName}|g' \\ - > ${outputPath}`, - `Failed to fix n-quads file: ${outputPath}` - ); - } else { - throwOnError( - `rdfpipe --output-format ${outputFormat} ${inputPath} > ${outputPath}`, - `Failed to convert to '${outputFormat}' format.` - ); + command += ` | perl -pe 's|\\Qfile://${inputPath}\\E|${graphName}|g'`; } + throwOnError(`${command} > ${outputPath}`, `Failed to convert to '${outputFormat}' format.`); }