Skip to content

Commit

Permalink
Make readme generation a little more forgiving and use jsonld cli for…
Browse files Browse the repository at this point in the history
… jsonld conversion (it works well)
  • Loading branch information
bherr2 committed Oct 27, 2023
1 parent 7229296 commit bda874c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
3 changes: 3 additions & 0 deletions scripts/setup-environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions src/enrichment/enrich-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions src/finalizing/utils/generate-readme-md.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
40 changes: 28 additions & 12 deletions src/utils/reify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.`);
}

0 comments on commit bda874c

Please sign in to comment.