Skip to content

Commit

Permalink
Add catalog creation (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
bherr2 committed Sep 29, 2023
1 parent de7869a commit 3ea8add
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/finalizing/create-catalogs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { listDeployed } from "../list.js";
import { readFileSync, writeFileSync } from 'fs';
import { Environment } from 'nunjucks';
import { resolve } from 'path';

export function createCatalogs(context) {
const catalog = {};
for (const dataset of listDeployed(context)) {
const [doType, doName, doVersion] = dataset.split('/');
const types = catalog[doType] = catalog[doType] || {};
const versions = types[doName] = types[doName] || [];
versions.push(doVersion);
}

const doTypes = Object.keys(catalog).sort();
createListing(context, '', doTypes, 'Catalog');

for (const doType of doTypes) {
const doNames = Object.keys(catalog[doType]);
createListing(context, doType, doNames, 'Catalog');

for (const doName of doNames) {
const versions = catalog[doType][doName];
createListing(context, `${doType}/${doName}`, versions, 'Dataset');
}
}
}

function createListing(context, path, items, itemType) {
const iri = `${context.lodIri}${path}`;
writeIndexHtml(context, path, { iri, items, itemType });
}

export function writeIndexHtml(context, path, data) {
const templateFile = resolve(context.processorHome, 'src/finalizing/templates/catalog-html.njk');
const htmlString = renderTemplate(templateFile, data);
const filePath = resolve(context.deploymentHome, path, 'index.html');
writeFileSync(filePath, htmlString);
}

export function renderTemplate(templateFile, data) {
const env = new Environment(undefined, { autoescape: false });
const template = readFileSync(templateFile).toString();
return env.renderString(template, data);
}
2 changes: 2 additions & 0 deletions src/finalizing/finalize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buildBlazegraphJournal } from './build-blazegraph.js';
import { createCatalogs } from './create-catalogs.js';
import { deriveLatest } from './derive-latest.js';

/**
Expand All @@ -10,6 +11,7 @@ import { deriveLatest } from './derive-latest.js';
*/
export function finalize(context) {
deriveLatest(context);
createCatalogs(context);
if (!context.skipDb) {
buildBlazegraphJournal(context);
}
Expand Down
50 changes: 50 additions & 0 deletions src/finalizing/templates/catalog-html.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!doctype html>
<html lang="en" prefix="dct: http://purl.org/dc/terms/
rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
dcat: http://www.w3.org/ns/dcat#
foaf: http://xmlns.com/foaf/0.1/">
<head>
<title>Catalog for {{ iri }}</title>
<script src="https://cdn.jsdelivr.net/gh/vanillawc/wc-markdown@1/index.js" type="module"></script>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/hubmapconsortium/ccf-releases/v1.3/docs/styles.css">
<link rel="icon" type="image/png" href="https://cdn.jsdelivr.net/gh/hubmapconsortium/ccf-releases/v1.3/docs/img/favicon.png">
<link rel="apple-touch-icon" href="https://cdn.jsdelivr.net/gh/hubmapconsortium/ccf-releases/v1.3/docs/img/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72" href="https://cdn.jsdelivr.net/gh/hubmapconsortium/ccf-releases/v1.3/docs/img/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="https://cdn.jsdelivr.net/gh/hubmapconsortium/ccf-releases/v1.3/docs/img/apple-touch-icon-114x114.png">
</head>
<body>
<header class="main-header">
<div class="main-logo">
<a href="https://humanatlas.io" title="Link to HuBMAP HRA Portal"><img src="https://cdn.jsdelivr.net/gh/hubmapconsortium/ccf-releases/v1.3/docs/img/logo-hubmap.svg" alt="HuBMAP logo" class="logo"></a>
</div>
</header>
<div class="main" typeof="dcat:Catalog" resource="{{ iri }}">
<h1 property="dct:title">Catalog for {{ iri }}</h1>

<ul>
{% for item in items -%}
{% if itemType == 'Dataset' %}
<li property="dcat:dataset" typeof="dcat:Dataset" resource="{{ iri }}/{{ item }}">
<a href="{{ iri }}/{{ item }}" property="dct:title">{{ item }}</a>
</li>
{% else if itemType == 'Catalog' %}
<li property="dcat:catalog" typeof="dcat:Catalog" resource="{{ iri }}/{{ item }}">
<a href="{{ iri }}/{{ item }}" property="dct:title">{{ item }}</a>
</li>
{% endif %}
{%- endfor %}
</ul>
</div>
<footer class="main-footer">
<p>
<small
<script>
new Date().getFullYear() > 2010 && document.write(new Date().getFullYear());
</script> All Rights
Reserved.</small>
</p>
</footer>
</body>
</html>
6 changes: 6 additions & 0 deletions src/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ export function list(context) {
console.log(digitalObjects.join('\n'));
}
}

export function listDeployed(context) {
const glob = '*/*/*/graph.ttl';
const digitalObjects = globSync(glob, { cwd: context.deploymnentHome }).map((p) => p.split('/').slice(0, -1).join('/'));
return digitalObjects;
}

0 comments on commit 3ea8add

Please sign in to comment.