Commit 17e1b7be authored by PTSEFTON's avatar PTSEFTON
Browse files

Tidied up table formatting, fixed some bugs

parent 70ffe728
...@@ -33,9 +33,18 @@ module.exports = function(){ ...@@ -33,9 +33,18 @@ module.exports = function(){
var item_json = {} var item_json = {}
for (var i=0; i < raw_collection_metadata.length ; i++) { for (var i=0; i < raw_collection_metadata.length ; i++) {
var name_value = raw_collection_metadata[i]; var name_value = raw_collection_metadata[i];
item_json[name_value['Name']] = name_value['Value']; if (item_json[name_value['Name']]) {
//console.log("VALUE", name_value['Name'] );
item_json[name_value['Name']].push(name_value['Value']);
}
else {
item_json[name_value['Name']] = [name_value['Value']];
} }
}
item_json["TYPE:"] = "Dataset"; item_json["TYPE:"] = "Dataset";
item_json["path"] = collection.rel_path; item_json["path"] = collection.rel_path;
if (!(collection.rel_path === "./")) { if (!(collection.rel_path === "./")) {
...@@ -180,7 +189,7 @@ module.exports = function(){ ...@@ -180,7 +189,7 @@ module.exports = function(){
fs.writeFileSync(path.join(collection.dir, "CATALOG.json"), JSON.stringify(flattenated, null, 2), fs.writeFileSync(path.join(collection.dir, "CATALOG.json"), JSON.stringify(flattenated, null, 2),
function(err) { function(err) {
if(err) { if(err) {
return console.log(err, "Error wirting in", collection.dir); return console.log(err, "Error writing in", collection.dir);
} }
log("The file was saved!" + path.join(collection.dir, "CATALOG.json")); log("The file was saved!" + path.join(collection.dir, "CATALOG.json"));
}); });
......
...@@ -61,36 +61,42 @@ module.exports = function(){ ...@@ -61,36 +61,42 @@ module.exports = function(){
if (!Array.isArray(creators)){ if (!Array.isArray(creators)){
creators = [creators] creators = [creators]
} }
var creators_el = xml.ele('creators'); var creators_el = xml.ele('creators');
//console.log(xml.end({ pretty: true })); //console.log(xml.end({ pretty: true }));
for (var i = 0; i < creators.length; i++) { for (var i = 0; i < creators.length; i++) {
var found_creator = false; var found_creator = false;
var creator_names = ""; var creator_names = "";
var creator = this.json_by_id[creators[i]["@id"]]; if (!this.json_by_id[creators[i]["@id"]]) {
var creator_el; creator_el = creators_el.ele("creator").ele("creatorName", creators[i]);
//console.log("Looking at creator", creators[i])
if (creator["familyName"] && creator["givenName"]) {
//console.log("Got names");
creator_el = creators_el.ele("creator");
creator_names = creator["familyName"] + ", " + creator["givenName"];
creator_el.ele("creatorName", creator_names) ;
creator_el.ele('givenName', creator["givenName"]);
creator_el.ele('familyName', creator["familyName"]);
found_creator = true;
creators_strings.push(creator_names);
} else if (creator["name"]) {
//console.log("Got a name");
creator_el = creators_el.ele("creator").ele("creatorName", creator["name"]);
creators_strings.push(creator["name"]);
creator_names = creator["name"];
found_creator = true; found_creator = true;
creators_strings.push(creators[i]);
} }
if (found_creator && creator["@id"] && creator["@id"].match("https?://orcid.org/")) { else {
var name_id_el = creator_el.ele("nameIdentifier", creator["@id"]); var creator = this.json_by_id[creators[i]["@id"]];
name_id_el.att("schemeURI","http://orcid.org"); var creator_el;
name_id_el.att("nameIdentifierScheme", "ORCID"); //console.log("Looking at creator", creators[i])
if (creator["familyName"] && creator["givenName"]) {
//console.log("Got names");
creator_el = creators_el.ele("creator");
creator_names = creator["familyName"] + ", " + creator["givenName"];
creator_el.ele("creatorName", creator_names) ;
creator_el.ele('givenName', creator["givenName"]);
creator_el.ele('familyName', creator["familyName"]);
found_creator = true;
creators_strings.push(creator_names);
} else if (creator["name"]) {
//console.log("Got a name");
creator_el = creators_el.ele("creator").ele("creatorName", creator["name"]);
creators_strings.push(creator["name"]);
creator_names = creator["name"];
found_creator = true;
}
if (found_creator && creator["@id"] && creator["@id"].match("https?://orcid.org/")) {
var name_id_el = creator_el.ele("nameIdentifier", creator["@id"]);
name_id_el.att("schemeURI","http://orcid.org");
name_id_el.att("nameIdentifierScheme", "ORCID");
}
} }
} }
...@@ -102,13 +108,15 @@ module.exports = function(){ ...@@ -102,13 +108,15 @@ module.exports = function(){
} }
if (root["@id"]){ if (root["@id"]){
var identifier = root["@id"]; var identifier = root["@id"];
if (identifier.startsWith('http://dx.doi.org/10.')) { console.log("ID", identifier)
if (identifier.match(/http:\/\/(dx\.)?doi.org\/10\./)) {
//<identifier identifierType="DOI">10.5072/example-full</identifier> //<identifier identifierType="DOI">10.5072/example-full</identifier>
id_el = xml.ele("identifier", identifier.replace("http://dx.doi.org/","")) id_el = xml.ele("identifier", identifier.replace("http://dx.doi.org/",""))
id_el. att("identifierType", "DOI"); id_el. att("identifierType", "DOI");
} }
else { else {
can_cite = false; can_cite = false;
report += "Collection needs to have a DOI as an ID";
} }
} }
else else
...@@ -168,7 +176,7 @@ module.exports = function(){ ...@@ -168,7 +176,7 @@ module.exports = function(){
//Creator (PublicationYear): Title. Version. Publisher. ResourceType. Identifier //Creator (PublicationYear): Title. Version. Publisher. ResourceType. Identifier
//console.log(creators_strings); //console.log(creators_strings);
this.citation += creators_strings.join(", "); this.citation += creators_strings.join("; ");
this.citation += ` (${published}) `; this.citation += ` (${published}) `;
this.citation += `${name}. `; this.citation += `${name}. `;
this.citation += `${publisher}. `; this.citation += `${publisher}. `;
......
{ {
"name": "schema:name", "name": "schema:name",
"givenName": "schema:givenName",
"familyName": "schema:familyName",
"description": "schema:description",
"file": "schema:mediaObject", "file": "schema:mediaObject",
"path": "schema:contentUrl", "path": "schema:contentUrl",
"contentSize": "schema:contentSize", "contentSize": "schema:contentSize",
...@@ -9,7 +12,6 @@ ...@@ -9,7 +12,6 @@
"issn": "schema:issn", "issn": "schema:issn",
"datePublished": "schema:datePublished", "datePublished": "schema:datePublished",
"identifier": "schema:identifier", "identifier": "schema:identifier",
"description": "schema:description",
"license": "schema:license", "license": "schema:license",
"creator": "schema:creator", "creator": "schema:creator",
"contributor": "schema:contributor", "contributor": "schema:contributor",
...@@ -36,6 +38,8 @@ ...@@ -36,6 +38,8 @@
"familyName": "schema:familyName", "familyName": "schema:familyName",
"Place": "schema:Place", "Place": "schema:Place",
"Organization": "schema:Organization", "Organization": "schema:Organization",
"memberOf": "schema:memberOf",
"member": "schema:hasMember",
"Project": "frapo:Project", "Project": "frapo:Project",
"affiliation": "schema:affiliation", "affiliation": "schema:affiliation",
"Funder": "schema:Funder", "Funder": "schema:Funder",
......
const builder = require('xmlbuilder'); const builder = require('xmlbuilder');
var fs = require('fs'); var fs = require('fs');
var ejs = require('ejs'); var ejs = require('ejs');
context = require('./defaults/context.json');
module.exports = function(){ module.exports = function(){
return( return(
...@@ -15,88 +16,100 @@ module.exports = function(){ ...@@ -15,88 +16,100 @@ module.exports = function(){
} }
}, },
sort_keys : function(keys) {
// Sort a set or array of keys to be in the same order as those in context.json
// Returns set
keys = new Set(keys);
return new Set(Object.keys(context).filter(function (k){return keys.has(k)}))
},
items_to_html : function items_to_html(node, html, toc) { items_to_html : function items_to_html(node, html, toc) {
var table_el = html.ele("table"); var table_el = html.ele("table").att("class", "table table-striped");
// Find all the keys // Find all the keys
keys = [] keys = [];
for (let [_, f] of Object.entries(node)) { for (let [_, f] of Object.entries(node)) {
keys = keys.concat(Object.keys(f)); keys = keys.concat(Object.keys(f));
} }
key_set = new Set(keys); key_set = this.sort_keys(keys);
key_set.delete('@type'); key_set.delete('@type');
key_set.delete('@id');
key_set.delete('identifier'); key_set.delete('identifier');
key_set.delete('fileFormat'); key_set.delete('fileFormat');
var header_row_el = table_el.ele("tr"); var thead_l = table_el.ele("thead").att("class","thead-inverse");
var header_row_el = thead_l.ele("tr");
for (let k of key_set) { for (let k of key_set) {
header_row_el.ele("th", k[0].toUpperCase() + k.substring(1)); header_row_el.ele("th", k[0].toUpperCase() + k.substring(1));
} }
for (let [_, f] of Object.entries(node)) { body_el = table_el.ele("tbody").att("class","table-striped");
var row_el = table_el.ele("tr").att("id", f['@id']); for (let [_, property] of Object.entries(node)) {
for (let k of key_set) { var row_el = table_el.ele("tr").att("id", property['@id']);
for (let key of key_set) {
td_ele = row_el.ele("td"); td_ele = row_el.ele("td");
var data = f[k];
if (!Array.isArray(data)) { this.format_cell(property, key, td_ele);
data = [data];
}
var i = 0;
for (let [_, part] of Object.entries(data)) {
//console.log("PART", part)
if (part === undefined) {
} else if ((k === 'name' || k === 'path') && f["@id"].match(/^https?:\/\//i)) {
td_ele.ele("a", part).att('href', f["@id"]);
} else if (k === 'encodingFormat' && f.fileFormat && f.fileFormat.match(/^https?:\/\//i)){
td_ele.ele("a", part).att('href', f.fileFormat);
} else if (
this.json_by_id[part['@id']] &&
!(this.json_by_id[part['@id']].name || this.json_by_id[part['@id']].description)) {
// Embed small bits of info that don't have a name or description
this.dataset_to_html(this.json_by_id[part['@id']], td_ele);
} else if (part['@id'] && this.json_by_id[part['@id']]) {
var target_name = this.json_by_id[part['@id']].name ? this.json_by_id[part['@id']].name : value[part['@id']];
td_ele.ele('a',target_name).att('href', '#' + part['@id']);
}
else {
td_ele.txt(part);
}
i++;
if (i < data.length) {
td_ele.txt(", ");
}
} }
} }
} },
format_cell : function(f, k, td_ele) {
var data = f[k];
if (!Array.isArray(data)) {
data = [data];
}
var i = 0;
for (let [_, part] of data.entries()) {
//console.log("PART", part)
if (part === undefined) {
} else if ((k === 'name' || k === 'path') && f["@id"].match(/^https?:\/\//i)) {
td_ele.ele("a", part).att('href', f["@id"]);
} else if (k === 'encodingFormat' && f.fileFormat && f.fileFormat.match(/^https?:\/\//i)){
td_ele.ele("a", part).att('href', f.fileFormat);
} else if (
this.json_by_id[part['@id']] &&
!(this.json_by_id[part['@id']].name || this.json_by_id[part['@id']].description)) {
// Embed small bits of info that don't have a name or description
this.dataset_to_html(this.json_by_id[part['@id']], td_ele);
} else if (part['@id'] && this.json_by_id[part['@id']]) {
var target_name = this.json_by_id[part['@id']].name ? this.json_by_id[part['@id']].name : value[part['@id']];
td_ele.ele('a',target_name).att('href', '#' + part['@id']);
}
else {
td_ele.txt(part);
}
i++;
if (i < data.length) {
td_ele.txt(", ");
}
}
}, },
dataset_to_html : function dataset_to_html(node, html, toc) { dataset_to_html : function dataset_to_html(node, html, toc) {
var table_el = html.ele("table") var table_el = html.ele("table").att("class", "table")
if (node["@id"]) { if (node["@id"]) {
table_el.att("id", node["@id"]); table_el.att("id", node["@id"]);
} }
var keys = Object.keys(node).filter(k => !(/HasPart/.test(k))); var keys = new Set(Object.keys(node).filter(k => !(/hasPart/.test(k))));
keys.delete("@id");
keys.delete("identifier");
keys.delete("@type");
key = this.sort_keys(keys);
for (let key of keys) { for (let key of keys) {
var value = node[key]; var value = node[key];
var row_el = table_el.ele("tr") var row_el = table_el.ele("tr")
row_el.ele("th", key[0].toUpperCase() + key.substring(1)); row_el.ele("th", key[0].toUpperCase() + key.substring(1));
if (value['@id']) { this.format_cell(node, key, row_el.ele("td"))
var target_name = this.json_by_id[value['@id']].name ? this.json_by_id[value['@id']].name : value['@id'];
row_el.ele("td").ele('a',target_name).att('href', '#' + value['@id']);
}
else {
row_el.ele("td", value); // TODO look for linking and embedding
}
} }
if (node.HasPart) { if (node.hasPart) {
if (!Array.isArray(node.HasPart)) { if (!Array.isArray(node.hasPart)) {
node.HasPart = [node.HasPart]; node.hasPart = [node.hasPart];
} }
var files = []; var files = [];
var datasets = []; var datasets = [];
for (let [key, value] of Object.entries(node.HasPart)) { for (let [key, value] of Object.entries(node.hasPart)) {
if (value["@id"] && this.json_by_id[value["@id"]]) { if (value["@id"] && this.json_by_id[value["@id"]]) {
var child = this.json_by_id[value["@id"]]; var child = this.json_by_id[value["@id"]];
if (child['@type']) { if (child['@type']) {
......
...@@ -15,14 +15,14 @@ module.exports = function(){ ...@@ -15,14 +15,14 @@ module.exports = function(){
this.nested_items = {}; this.nested_items = {};
function links_to_id(string, collection) { function links_to_id(string, collection) {
var links_to_id = undefined; var links_to = undefined;
if (collection.id_lookup[string]) { if (collection.id_lookup[string]) {
links_to_id = collection.id_lookup[string].id; links_to = collection.id_lookup[string].id;
} }
else if (collection.name_lookup[string]){ else if (collection.name_lookup[string]){
links_to_id = collection.name_lookup[string].id; links_to = collection.name_lookup[string].id;
} }
return links_to_id; return links_to;
} }
function make_link(string, collection, el) { function make_link(string, collection, el) {
...@@ -57,7 +57,8 @@ module.exports = function(){ ...@@ -57,7 +57,8 @@ module.exports = function(){
"to_json_ld_fragment" : function to_json_ld_fragment() { "to_json_ld_fragment" : function to_json_ld_fragment() {
//console.log("Keys at start of output", Object.keys(this.properties)); //console.log("Keys at start of output", Object.keys(this.properties));
var frag = {"@id" : this.id};
var frag = {"@id" : String(this.id)};
//console.log("Setting id", this.id, this.name, this.nested_items); //console.log("Setting id", this.id, this.name, this.nested_items);
//console.log(" NAMELOOKUPS", this.collection.name_lookup); //console.log(" NAMELOOKUPS", this.collection.name_lookup);
for (let [key, i] of Object.entries(this.nested_items)) { for (let [key, i] of Object.entries(this.nested_items)) {
...@@ -68,7 +69,7 @@ module.exports = function(){ ...@@ -68,7 +69,7 @@ module.exports = function(){
types = f.data; //Don't output now types = f.data; //Don't output now
} else if (f.name != "ID"){ } else if (f.name != "ID"){
frag[f.name] = []; frag[f.name] = [];
//console.log(f.name); if (!Array.isArray(f.data)) {f.data=[f.data]}
for (var k = 0; k < f.data.length ; k++){ for (var k = 0; k < f.data.length ; k++){
var link_id = f.links_to ? f.links_to: links_to_id(f.data[k], this.collection); var link_id = f.links_to ? f.links_to: links_to_id(f.data[k], this.collection);
//console.log("link_id", link_id, "relational", f.is_relational) //console.log("link_id", link_id, "relational", f.is_relational)
......
...@@ -63,7 +63,6 @@ module.exports = function(){ ...@@ -63,7 +63,6 @@ module.exports = function(){
if (this.name === "ID"){ if (this.name === "ID"){
this.name = 'identifier'; this.name = 'identifier';
console.log("GOT ID", this.name)
} }
else if (this.name === "TYPE:"){ else if (this.name === "TYPE:"){
this.is_type = true; this.is_type = true;
...@@ -134,7 +133,8 @@ module.exports = function(){ ...@@ -134,7 +133,8 @@ module.exports = function(){
if (this.is_repeating || this.is_type) { if (this.is_repeating || this.is_type) {
this.data = this.data.replace(/, +/g, ",").split(","); this.data = this.data.replace(/, +/g, ",").split(",");
//console.log("SPLIT", this.type, this.name, this.data) //console.log("SPLIT", this.type, this.name, this.data)
} else { } else if(!Array.isArray(this.data)) {
this.data = [this.data]; this.data = [this.data];
} }
......
...@@ -197,10 +197,7 @@ describe("Sample data details", function(){ ...@@ -197,10 +197,7 @@ describe("Sample data details", function(){
assert.equal(c.json_by_id["http://www.geonames.org/8152662/catalina-park.html"].name, 'Catalina Park'); assert.equal(c.json_by_id["http://www.geonames.org/8152662/catalina-park.html"].name, 'Catalina Park');
console.log("Place data", c.json_by_id["http://www.geonames.org/8152662/catalina-park.html"]) console.log("Place data", c.json_by_id["http://www.geonames.org/8152662/catalina-park.html"])
var catalina_location_id = c.json_by_id["http://www.geonames.org/8152662/catalina-park.html"].geo['@id']; var catalina_location_id = c.json_by_id["http://www.geonames.org/8152662/catalina-park.html"].geo['@id'];
console.log("CATALINA location", catalina_location_id);
var catalina_geo = c.json_by_id[catalina_location_id]; var catalina_geo = c.json_by_id[catalina_location_id];
console.log("CATALINA GEO XXXXXXXXXXXX", catalina_geo);
assert.equal(catalina_geo.latitude, '-33.7152'); assert.equal(catalina_geo.latitude, '-33.7152');
assert.equal(catalina_geo.longitude, '150.30119'); assert.equal(catalina_geo.longitude, '150.30119');
...@@ -246,7 +243,7 @@ describe("Datacite", function(){ ...@@ -246,7 +243,7 @@ describe("Datacite", function(){
function () { function () {
citer = new Datacite(); citer = new Datacite();
text_citation = citer.make_citation("./test_data/Glop_Pot/CATALOG.json", "./test_data/Glop_pot/datacite.xml") text_citation = citer.make_citation("./test_data/Glop_Pot/CATALOG.json", "./test_data/Glop_pot/datacite.xml")
assert.equal(text_citation, "Maynard, Phil (2017) Glop Pot data. University of Technology Sydney. Datacrate. http://dx.doi.org/10.1016/something"); assert.equal(text_citation, "Lake, Mike; Vaughan-Taylor, Keir; Klocker, Andreas; Maynard, Phil (2017) Glop Pot data. University of Technology Sydney. Datacrate. http://dx.doi.org/10.1016/something");
}, },
function(err) { function(err) {
......
...@@ -53,15 +53,7 @@ A machine-readable version of this page is available: ...@@ -53,15 +53,7 @@ A machine-readable version of this page is available:
<?xml version="1.0"?> <?xml version="1.0"?>
<div> <div>
<table id="http://dx.doi.org/10.5281/zenodo.1009240"> <table class="table" id="http://dx.doi.org/10.5281/zenodo.1009240">
<tr>
<th>@id</th>
<td>http://dx.doi.org/10.5281/zenodo.1009240</td>
</tr>
<tr>
<th>@type</th>
<td>Dataset</td>
</tr>
<tr> <tr>
<th>Contact</th> <th>Contact</th>
<td> <td>
...@@ -76,7 +68,9 @@ A machine-readable version of this page is available: ...@@ -76,7 +68,9 @@ A machine-readable version of this page is available:
</tr> </tr>
<tr> <tr>
<th>Path</th> <th>Path</th>
<td>./</td> <td>
<a href="http://dx.doi.org/10.5281/zenodo.1009240">./</a>
</td>
</tr> </tr>
<tr> <tr>
<th>Contributor</th> <th>Contributor</th>
...@@ -98,21 +92,15 @@ A machine-readable version of this page is available: ...@@ -98,21 +92,15 @@ A machine-readable version of this page is available:
<th>Description</th> <th>Description</th>
<td>This is a simple dataset for demonstration purposes it contains just one image and a directory full of useless text files.</td> <td>This is a simple dataset for demonstration purposes it contains just one image and a directory full of useless text files.</td>
</tr> </tr>
<tr>
<th>HasPart</th>
<td 0="[object Object]" 1="[object Object]"/>
</tr>
<tr>
<th>Identifier</th>
<td>http://dx.doi.org/10.5281/zenodo.1009240</td>
</tr>
<tr> <tr>
<th>Keywords</th> <th>Keywords</th>
<td>Dogs, Fences, The Gully</td> <td>Dogs, Fences, The Gully</td>
</tr> </tr>
<tr> <tr>
<th>Name</th> <th>Name</th>
<td>Sample dataset for DataCrate v0.2</td> <td>
<a href="http://dx.doi.org/10.5281/zenodo.1009240">Sample dataset for DataCrate v0.2</a>
</td>
</tr> </tr>
<tr> <tr>
<th>Publisher</th> <th>Publisher</th>
...@@ -125,118 +113,90 @@ A machine-readable version of this page is available: ...@@ -125,118 +113,90 @@ A machine-readable version of this page is available:
<td>2017</td> <td>2017</td>
</tr> </tr>
</table> </table>
<h1>Contextual info: GeoCoordinates</h1> <table class="table" id="lots_of_little_files">
<table>
<tr> <tr>
<th>@id</th> <th>Path</th>
<th>Latitude</th> <td>lots_of_little_files</td>
<th>Longitude</th>
</tr> </tr>
<tr id="263e4cc5-37e6-4bee-a8cd-893e42bec48b"> <tr>
<td>263e4cc5-37e6-4bee-a8cd-893e42bec48b</td> <th>Description</th>
<td>-33.7152</td> <td>This directory contains many small files, that we’re not going to describe in detail.</td>
<td>150.30119</td>
</tr>