Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add license/copyright field to narratives #1061

Merged
merged 1 commit into from
Apr 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 38 additions & 32 deletions cli/server/parseNarrative.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const makeFrontMatterBlock = (frontMatter) => {
const markdown = [];
markdown.push(`## ${frontMatter.title}`);
if (frontMatter.authors) {
const authors = parseContributors(frontMatter, "authors", "authorLinks");
const authors = parseAttributions(frontMatter, "authors", "authorLinks");
if (authors) {
markdown.push(`### Author: ${authors}`);
if (frontMatter.affiliations && typeof frontMatter.affiliations === "string") {
Expand All @@ -40,7 +40,7 @@ const makeFrontMatterBlock = (frontMatter) => {
}
}
if (frontMatter.translators) {
const translators = parseContributors(frontMatter, "translators", "translatorLinks");
const translators = parseAttributions(frontMatter, "translators", "translatorLinks");
if (translators) {
markdown.push(`### Translators: ${translators}`);
}
Expand All @@ -54,60 +54,66 @@ const makeFrontMatterBlock = (frontMatter) => {
if (frontMatter.updated && typeof frontMatter.updated === "string") {
markdown.push(`#### Updated: ${frontMatter.updated}`);
}

if (frontMatter.license) {
const license = parseAttributions(frontMatter, "license", "licenseLink");
if (license) {
markdown.push(`#### License: ${license}`);
}
}

const block = new Proxy({}, blockProxyHandler);
block.url = frontMatter.dataset;
block.contents = markdown.join("\n");
return block;
};

function parseContributors(frontMatter, contributorsKey, contributorLinksKey) {
const contributors = frontMatter[contributorsKey];
const contributorLinks = frontMatter[contributorLinksKey];
function parseAttributions(frontMatter, attributionsKey, attributionLinksKey) {
const attributions = frontMatter[attributionsKey];
const attributionLinks = frontMatter[attributionLinksKey];

if (Array.isArray(contributors)) {
return parseContributorsArray(contributors, contributorLinks, contributorsKey, contributorLinksKey);
} else if (typeof contributors === 'string') {
return parseContributorsString(contributors, contributorLinks, contributorsKey, contributorLinksKey);
if (Array.isArray(attributions)) {
return parseAttributionsArray(attributions, attributionLinks, attributionsKey, attributionLinksKey);
} else if (typeof attributions === 'string') {
return parseAttributionsString(attributions, attributionLinks, attributionsKey, attributionLinksKey);
}
return undefined;
}

function parseContributorsArray(contributors, contributorLinks, contributorsKey, contributorLinksKey) {
function parseAttributionsArray(attributions, attributionLinks, attributionsKey, attributionLinksKey) {
// validate links
if (contributorLinks) {
if (!Array.isArray(contributorLinks)) {
utils.warn(`Narrative parsing - if ${contributorsKey} is an array, then ${contributorLinksKey} must also be an array. Skipping links.`);
contributorLinks = undefined;
} else if (contributorLinks.length !== contributors.length) {
utils.warn(`Narrative parsing - the length of ${contributorsKey} and ${contributorLinksKey} did not match. Skipping links.`);
contributorLinks = undefined;
if (attributionLinks) {
if (!Array.isArray(attributionLinks)) {
utils.warn(`Narrative parsing - if ${attributionsKey} is an array, then ${attributionLinksKey} must also be an array. Skipping links.`);
attributionLinks = undefined;
} else if (attributionLinks.length !== attributions.length) {
utils.warn(`Narrative parsing - the length of ${attributionsKey} and ${attributionLinksKey} did not match. Skipping links.`);
attributionLinks = undefined;
}
}
if (contributorLinks) {
contributors = contributors.map((contributor, idx) => {
return contributorLink(contributor, contributorLinks[idx]);
if (attributionLinks) {
attributions = attributions.map((attribution, idx) => {
return attributionLink(attribution, attributionLinks[idx]);
});
}
return contributors.join(", ");
return attributions.join(", ");
}

function parseContributorsString(contributors, contributorLinks, contributorsKey, contributorLinksKey) {
function parseAttributionsString(attributions, attributionLinks, attributionsKey, attributionLinksKey) {
// validate links
if (contributorLinks) {
if (typeof contributorLinks !== "string") {
utils.warn(`Narrative parsing - if ${contributorsKey} is a string, then ${contributorLinksKey} must also be a string. Skipping links.`);
contributorLinks = undefined;
if (attributionLinks) {
if (typeof attributionLinks !== "string") {
utils.warn(`Narrative parsing - if ${attributionsKey} is a string, then ${attributionLinksKey} must also be a string. Skipping links.`);
attributionLinks = undefined;
}
}
return contributorLink(contributors, contributorLinks);
return attributionLink(attributions, attributionLinks);
}

function contributorLink(contributor, contributorLinkValue) {
if (contributorLink) {
return `[${contributor}](${contributorLinkValue})`;
function attributionLink(attribution, attributionLinkValue) {
if (attributionLink) {
return `[${attribution}](${attributionLinkValue})`;
}
return contributor;
return attribution;
}

/**
Expand Down