Compare commits

..

17 Commits

Author SHA1 Message Date
Shan Carter c77fbbd18f 0.0.15 2017-01-09 18:11:32 -08:00
Shan Carter adcfba4627 Updating data processing 2017-01-09 18:09:59 -08:00
Shan Carter 6ef3847bf6 0.0.14 2017-01-09 17:37:13 -08:00
Shan Carter 0b68c4ea1f fix 2017-01-09 17:37:12 -08:00
Shan Carter 60cf026b88 0.0.13 2017-01-09 17:32:05 -08:00
Shan Carter b30d4084ef fix 2017-01-09 17:32:04 -08:00
Shan Carter 8aa8ef2206 0.0.12 2017-01-09 17:27:58 -08:00
Shan Carter c0824c1827 bib 2017-01-09 17:27:55 -08:00
Shan Carter 7fe35c3b88 Merge branch 'master' of https://github.com/distillpub/distill-template 2017-01-09 17:24:19 -08:00
Christopher Olah b0b3265f72 Merge branch 'master' of https://github.com/distillpub/distill-template 2017-01-09 17:17:46 -08:00
Christopher Olah 971e90069e implement crossref generator 2017-01-09 17:17:30 -08:00
Shan Carter c8fa1f7eaa 0.0.11 2017-01-09 17:14:26 -08:00
Shan Carter 05179d887e Fix 2017-01-09 17:14:24 -08:00
Shan Carter c02748434c 0.0.10 2017-01-09 17:09:59 -08:00
Shan Carter bed9a09325 testing 2017-01-09 17:09:54 -08:00
Shan Carter 010daea5f5 0.0.9 2017-01-09 16:51:59 -08:00
Shan Carter 65ca734aa3 bug 2017-01-09 16:51:54 -08:00
10 changed files with 433 additions and 86 deletions
+13 -10
View File
@@ -2,17 +2,20 @@ import bibtexParse from "bibtex-parse-js";
export default function(dom, data) {
let el = dom.querySelector('script[type="text/bibliography"]');
let citations = [];
let bibliography = {};
//TODO If we don't have a local element, make a request for the document.
if (el) {
let rawBib = el.textContent;
let bibliography = {};
bibtexParse.toJSON(rawBib).forEach(e => {
bibliography[e.citationKey] = e.entryTags;
bibliography[e.citationKey].type = e.entryType;
});
let parsed = bibtexParse.toJSON(rawBib);
if(parsed) {
parsed.forEach(e => {
bibliography[e.citationKey] = e.entryTags;
bibliography[e.citationKey].type = e.entryType;
});
}
let citations = [];
var citeTags = [].slice.apply(dom.querySelectorAll("dt-cite"));
citeTags.forEach(el => {
let key = el.getAttribute("key");
@@ -21,14 +24,14 @@ export default function(dom, data) {
citationKeys.forEach(key => {
if (citations.indexOf(key) == -1){
citations.push(key);
if (! (key in bibliography)){
if (!(key in bibliography)){
console.warn("No bibliography entry found for: " + key);
}
}
});
}
});
data.bibliography = bibliography;
data.citations = citations;
}
data.bibliography = bibliography;
data.citations = citations;
}
+1
View File
@@ -10,6 +10,7 @@ export default function(dom, data) {
// Homepage
//data.homepage = !post.noHomepage;
data.journal = data.journal || {};
// Dates
// TODO: fix updated date
+25 -24
View File
@@ -1,33 +1,34 @@
import ymlParse from "js-yaml";
export default function(dom, data) {
let localData = {};
let el = dom.querySelector('script[type="text/front-matter"]');
//TODO If we don't have a local element, make a request for the document.
if (el) {
let text = el.textContent;
let localData = ymlParse.safeLoad(text);
data.title = localData.title;
data.description = localData.description;
data.published = new Date(localData.published);
data.updated = new Date(localData.published || localData.updated);
data.authors = localData.authors.map((author, i) =>{
let a = {};
let name = Object.keys(author)[0];
let names = name.split(" ");
a.firstName = names.slice(0, names.length - 1).join(" ");
a.lastName = names[names.length -1];
a.personalURL = author[name];
if(localData.affiliations[i]) {
let affiliation = Object.keys(localData.affiliations[i])[0];
a.affiliation = affiliation;
a.affiliationURL = localData.affiliations[i][affiliation];
}
return a;
});
localData = ymlParse.safeLoad(text);
}
data.title = localData.title ? localData.title : "Untitled";
data.description = localData.description ? localData.description : "No description.";
data.published = localData.published ? new Date(localData.published) : new Date("Invalid");
data.updated = localData.updated ? new Date(localData.updated) : new Date("Invalid");
data.authors = localData.authors ? localData.authors : [];
data.authors = data.authors.map((author, i) =>{
let a = {};
let name = Object.keys(author)[0];
let names = name.split(" ");
a.name = name;
a.firstName = names.slice(0, names.length - 1).join(" ");
a.lastName = names[names.length -1];
a.personalURL = author[name];
if(localData.affiliations[i]) {
let affiliation = Object.keys(localData.affiliations[i])[0];
a.affiliation = affiliation;
a.affiliationURL = localData.affiliations[i][affiliation];
}
return a;
});
}
+169 -1
View File
@@ -1,3 +1,171 @@
//import xml from "xml";
export default function(data) {
return "crossref";
if (data.published == undefined) {
//console.warn("Can't generate XML for post ", data.title, "with data", data);
//return "";
data.published = new Date("invalid");
}
var date = data.published;
var batch_timestamp = Math.floor(Date.now() / 1000);
console.log(data.authors)
var batch_id = data.authors.length ? data.authors[0].lastName.toLowerCase().slice(0,20) : "Anonymous";
batch_id += "_" + date.getFullYear();
batch_id += "_" + data.title.split(" ")[0].toLowerCase().slice(0,20) + "_" + batch_timestamp;
// generate XML
var crf_data =
{doi_batch : [
{ _attr: {
version: "4.3.7",
xmlns: "http://www.crossref.org/schema/4.3.7",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xsi:schemaLocation": "http://www.crossref.org/schema/4.3.7 http://www.crossref.org/schemas/crossref4.3.7.xsd",
}},
{ head: [
{doi_batch_id: batch_id},
{timestamp: batch_timestamp},
{depositor: [
{depositor_name: "Distill Admin"},
{email_address: "admin@distill.pub"},
]},
{registrant: "Distill"},
]},
{body: [
{journal: [
{journal_metadata: [
{full_title: data.journal.full_title || data.journal.title},
{abbrev_title: data.journal.abbrev_title || data.journal.title || data.journal.full_title},
{doi_data: [
{doi: data.journal.doi},
{resource: data.journal.url},
]},
]},
{journal_issue: [
{publication_date: [
{month: date.getMonth()+1},
{year: date.getFullYear()},
]},
{journal_volume: [
{volume: data.volume},
]},
{issue: data.issue},
]},
{journal_article: [
{titles: [
{title: data.title},
]},
{ contributors:
data.authors.map((author, ind) => (
{person_name: [
{ _attr: {
contributor_role: "author",
sequence: (ind == 0)? "first" : "additional"
}},
{given_name: author.firstName},
{surname: author.lastName},
{affiliation: author.affiliation}
// TODO: ORCID?
]}
))
},
{publication_date: [
{month: date.getMonth()+1},
{day: date.getDate()},
{year: date.getFullYear()}
]},
{ publisher_item: [
{item_number: data.doi}
]},
{doi_data: [
{doi: data.doi},
//{timestamp: ""},
{resource: data.url},
]},
{citation_list:
data.citations.map(key =>
citation_xml(key, data.bibliography[key]))
}
]},
]},
]},
]};
return xml(crf_data);
}
function citation_xml(key, ent){
if (ent == undefined) return {};
var info = [];
info.push({_attr: {key: key}});
if ("title" in ent)
info.push({article_title: ent.title});
if ("author" in ent)
info.push({author: ent.author.split(" and ")[0].split(",")[0].trim()});
if ("journal" in ent)
info.push({journal_title: ent.journal});
if ("booktitle" in ent)
info.push({volume_title: ent.booktitle});
if ("volume" in ent)
info.push({volume: ent.volume});
if ("issue" in ent)
info.push({issue: ent.issue});
if ("doi" in ent)
info.push({doi: ent.doi});
return {citation: info}
}
function xml(obj) {
//console.log(typeof(obj), obj)
if (typeof obj === 'string') return obj;
if (typeof obj === 'number') return ""+obj;
let keys = Object.keys(obj);
if (keys.length != 1) console.error("can't interpret ", obj, "as xml");
let name = keys[0];
var full_content = obj[name];
var attr = {};
if (Array.isArray(full_content)){
var content = [];
for (var i in full_content) {
var obj = full_content[i];
var obj_name = Object.keys(obj)[0];
if ("_attr" == obj_name) {
attr = obj["_attr"];
} else {
//console.log(Object.keys(obj)[0])
content.push(obj);
}
}
} else {
content = full_content;
}
if (content == undefined){
content = "undefined"
}
let attr_string = "";
for (var k in attr) {
attr_string += ` ${k}=\"${attr[k]}\"`
}
//console.log(typeof content, Array.isArray(content), content instanceof String, content)
if (Array.isArray(content)){
content = content.map(xml);
content = content.join("\n").split("\n");
content = content.map(s => " " + s).join("\n")
var result = `<${name}${attr_string}>\n${content}\n</${name}>`;
} else {
content = xml(content);
var result = `<${name}${attr_string}>${content}</${name}>`;
}
return result;
}
-1
View File
@@ -5,7 +5,6 @@ export default function(dom) {
let head = dom.querySelector("head");
if (!dom.querySelector("meta[charset]")) {
let meta = dom.createElement("meta");
meta.setAttribute("charset", "utf-8");
+7 -3
View File
@@ -80,13 +80,17 @@ export default function(dom, data) {
});
if (data.citations) {
data.citations.forEach(key =>
data.citations.forEach(key => {
let d = data.bibliography[key];
if(!d) {
console.warn("No bibliography data fround for " + key)
} else {
meta("citation_reference", citation_meta_content(data.bibliography[key]) )
);
};
});
}
}
function appendHtml(el, html) {
el.innerHTML += html;
}
+213 -42
View File
@@ -11,7 +11,6 @@ var html = function(dom) {
var head = dom.querySelector("head");
if (!dom.querySelector("meta[charset]")) {
var meta = dom.createElement("meta");
meta.setAttribute("charset", "utf-8");
@@ -283,6 +282,8 @@ function Type$2(tag, options) {
var type = Type$2;
/*eslint-disable max-len*/
var common$4 = common$1;
var YAMLException$3 = exception;
var Type$1 = type;
@@ -894,6 +895,8 @@ function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
/*eslint-disable no-bitwise*/
var NodeBuffer;
try {
@@ -1351,6 +1354,8 @@ var default_full = Schema$6.DEFAULT = new Schema$6({
]
});
/*eslint-disable max-len,no-use-before-define*/
var common = common$1;
var YAMLException$1 = exception;
var Mark = mark;
@@ -2942,6 +2947,8 @@ var loader$1 = {
safeLoad: safeLoad_1
};
/*eslint-disable no-use-before-define*/
var common$7 = common$1;
var YAMLException$5 = exception;
var DEFAULT_FULL_SCHEMA$2 = default_full;
@@ -3812,35 +3819,36 @@ var yaml = jsYaml;
var index = yaml;
var frontMatter = function(dom, data) {
var localData = {};
var el = dom.querySelector('script[type="text/front-matter"]');
//TODO If we don't have a local element, make a request for the document.
if (el) {
var text = el.textContent;
var localData = index.safeLoad(text);
data.title = localData.title;
data.description = localData.description;
data.published = new Date(localData.published);
data.updated = new Date(localData.published || localData.updated);
data.authors = localData.authors.map(function (author, i) {
var a = {};
var name = Object.keys(author)[0];
var names = name.split(" ");
a.firstName = names.slice(0, names.length - 1).join(" ");
a.lastName = names[names.length -1];
a.personalURL = author[name];
if(localData.affiliations[i]) {
var affiliation = Object.keys(localData.affiliations[i])[0];
a.affiliation = affiliation;
a.affiliationURL = localData.affiliations[i][affiliation];
}
return a;
});
localData = index.safeLoad(text);
}
data.title = localData.title ? localData.title : "Untitled";
data.description = localData.description ? localData.description : "No description.";
data.published = localData.published ? new Date(localData.published) : new Date("Invalid");
data.updated = localData.updated ? new Date(localData.updated) : new Date("Invalid");
data.authors = localData.authors ? localData.authors : [];
data.authors = data.authors.map(function (author, i) {
var a = {};
var name = Object.keys(author)[0];
var names = name.split(" ");
a.name = name;
a.firstName = names.slice(0, names.length - 1).join(" ");
a.lastName = names[names.length -1];
a.personalURL = author[name];
if(localData.affiliations[i]) {
var affiliation = Object.keys(localData.affiliations[i])[0];
a.affiliation = affiliation;
a.affiliationURL = localData.affiliations[i][affiliation];
}
return a;
});
};
var bibtexParse = createCommonjsModule(function (module, exports) {
@@ -4188,17 +4196,20 @@ var bibtexParse = createCommonjsModule(function (module, exports) {
var bibliography = function(dom, data) {
var el = dom.querySelector('script[type="text/bibliography"]');
var citations = [];
var bibliography = {};
//TODO If we don't have a local element, make a request for the document.
if (el) {
var rawBib = el.textContent;
var bibliography = {};
bibtexParse.toJSON(rawBib).forEach(function (e) {
bibliography[e.citationKey] = e.entryTags;
bibliography[e.citationKey].type = e.entryType;
});
var parsed = bibtexParse.toJSON(rawBib);
if(parsed) {
parsed.forEach(function (e) {
bibliography[e.citationKey] = e.entryTags;
bibliography[e.citationKey].type = e.entryType;
});
}
var citations = [];
var citeTags = [].slice.apply(dom.querySelectorAll("dt-cite"));
citeTags.forEach(function (el) {
var key = el.getAttribute("key");
@@ -4207,16 +4218,16 @@ var bibliography = function(dom, data) {
citationKeys.forEach(function (key) {
if (citations.indexOf(key) == -1){
citations.push(key);
if (! (key in bibliography)){
if (!(key in bibliography)){
console.warn("No bibliography entry found for: " + key);
}
}
});
}
});
data.bibliography = bibliography;
data.citations = citations;
}
data.bibliography = bibliography;
data.citations = citations;
};
var expandData = function(dom, data) {
@@ -4231,6 +4242,7 @@ var expandData = function(dom, data) {
// Homepage
//data.homepage = !post.noHomepage;
data.journal = data.journal || {};
// Dates
// TODO: fix updated date
@@ -4307,12 +4319,17 @@ var meta = function(dom, data) {
});
if (data.citations) {
data.citations.forEach(function (key) { return meta("citation_reference", citation_meta_content(data.bibliography[key]) ); }
);
data.citations.forEach(function (key) {
var d = data.bibliography[key];
if(!d) {
console.warn("No bibliography data fround for " + key);
} else {
meta("citation_reference", citation_meta_content(data.bibliography[key]) );
}
});
}
};
function appendHtml(el, html) {
el.innerHTML += html;
}
@@ -6631,10 +6648,164 @@ var code$1 = function(dom, data) {
});
};
//import xml from "xml";
var generateCrossref = function(data) {
return "crossref";
if (data.published == undefined) {
//console.warn("Can't generate XML for post ", data.title, "with data", data);
//return "";
data.published = new Date("invalid");
}
var date = data.published;
var batch_timestamp = Math.floor(Date.now() / 1000);
console.log(data.authors);
var batch_id = data.authors.length ? data.authors[0].lastName.toLowerCase().slice(0,20) : "Anonymous";
batch_id += "_" + date.getFullYear();
batch_id += "_" + data.title.split(" ")[0].toLowerCase().slice(0,20) + "_" + batch_timestamp;
// generate XML
var crf_data =
{doi_batch : [
{ _attr: {
version: "4.3.7",
xmlns: "http://www.crossref.org/schema/4.3.7",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xsi:schemaLocation": "http://www.crossref.org/schema/4.3.7 http://www.crossref.org/schemas/crossref4.3.7.xsd",
}},
{ head: [
{doi_batch_id: batch_id},
{timestamp: batch_timestamp},
{depositor: [
{depositor_name: "Distill Admin"},
{email_address: "admin@distill.pub"} ]},
{registrant: "Distill"} ]},
{body: [
{journal: [
{journal_metadata: [
{full_title: data.journal.full_title || data.journal.title},
{abbrev_title: data.journal.abbrev_title || data.journal.title || data.journal.full_title},
{doi_data: [
{doi: data.journal.doi},
{resource: data.journal.url} ]} ]},
{journal_issue: [
{publication_date: [
{month: date.getMonth()+1},
{year: date.getFullYear()} ]},
{journal_volume: [
{volume: data.volume} ]},
{issue: data.issue} ]},
{journal_article: [
{titles: [
{title: data.title} ]},
{ contributors:
data.authors.map(function (author, ind) { return (
{person_name: [
{ _attr: {
contributor_role: "author",
sequence: (ind == 0)? "first" : "additional"
}},
{given_name: author.firstName},
{surname: author.lastName},
{affiliation: author.affiliation}
// TODO: ORCID?
]}
); })
},
{publication_date: [
{month: date.getMonth()+1},
{day: date.getDate()},
{year: date.getFullYear()}
]},
{ publisher_item: [
{item_number: data.doi}
]},
{doi_data: [
{doi: data.doi},
//{timestamp: ""},
{resource: data.url} ]},
{citation_list:
data.citations.map(function (key) { return citation_xml(key, data.bibliography[key]); })
}
]} ]} ]} ]};
return xml(crf_data);
};
function citation_xml(key, ent){
if (ent == undefined) { return {}; }
var info = [];
info.push({_attr: {key: key}});
if ("title" in ent)
{ info.push({article_title: ent.title}); }
if ("author" in ent)
{ info.push({author: ent.author.split(" and ")[0].split(",")[0].trim()}); }
if ("journal" in ent)
{ info.push({journal_title: ent.journal}); }
if ("booktitle" in ent)
{ info.push({volume_title: ent.booktitle}); }
if ("volume" in ent)
{ info.push({volume: ent.volume}); }
if ("issue" in ent)
{ info.push({issue: ent.issue}); }
if ("doi" in ent)
{ info.push({doi: ent.doi}); }
return {citation: info}
}
function xml(obj) {
//console.log(typeof(obj), obj)
if (typeof obj === 'string') { return obj; }
if (typeof obj === 'number') { return ""+obj; }
var keys = Object.keys(obj);
if (keys.length != 1) { console.error("can't interpret ", obj, "as xml"); }
var name = keys[0];
var full_content = obj[name];
var attr = {};
if (Array.isArray(full_content)){
var content = [];
for (var i in full_content) {
var obj = full_content[i];
var obj_name = Object.keys(obj)[0];
if ("_attr" == obj_name) {
attr = obj["_attr"];
} else {
//console.log(Object.keys(obj)[0])
content.push(obj);
}
}
} else {
content = full_content;
}
if (content == undefined){
content = "undefined";
}
var attr_string = "";
for (var k in attr) {
attr_string += " " + k + "=\"" + (attr[k]) + "\"";
}
//console.log(typeof content, Array.isArray(content), content instanceof String, content)
if (Array.isArray(content)){
content = content.map(xml);
content = content.join("\n").split("\n");
content = content.map(function (s) { return " " + s; }).join("\n");
var result = "<" + name + attr_string + ">\n" + content + "\n</" + name + ">";
} else {
content = xml(content);
var result = "<" + name + attr_string + ">" + content + "</" + name + ">";
}
return result;
}
function renderImmediately(dom) {
html(dom);
styles(dom);
@@ -6651,16 +6822,16 @@ function renderOnLoad(dom, data) {
markdown(dom, data);
code$1(dom, data);
citation(dom, data);
// TODO remove script tag
}
// If we are in a browser, render automatically.
if(window && window.document) {
var data = data || {};
var data = {};
renderImmediately(window.document);
window.document.addEventListener("DOMContentLoaded", function (event) {
renderOnLoad(window.document, data);
console.log("final data:");
for (var k in data) {console.log(" ", k, ": ", data[k]);}
generateCrossref(data);
});
}
+1 -1
View File
File diff suppressed because one or more lines are too long
+3 -3
View File
@@ -28,16 +28,16 @@ function renderOnLoad(dom, data) {
markdown(dom, data);
code(dom, data);
citation(dom, data);
// TODO remove script tag
}
// If we are in a browser, render automatically.
if(window && window.document) {
let data = data || {};
let data = {};
renderImmediately(window.document);
window.document.addEventListener("DOMContentLoaded", (event) => {
renderOnLoad(window.document, data);
console.log("final data:");
for (var k in data) {console.log(" ", k, ": ", data[k])}
generateCrossref(data);
});
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "distill-template",
"version": "0.0.8",
"version": "0.0.15",
"description": "Template for creating Distill articles.",
"main": "dist/template.js",
"scripts": {