Skip to content

Commit

Permalink
add library code
Browse files Browse the repository at this point in the history
  • Loading branch information
robindemourat committed Jan 12, 2019
1 parent 27e099f commit 82740c2
Show file tree
Hide file tree
Showing 34 changed files with 6,529 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.DS_Store

# Runtime data
pids
Expand Down
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# peritext-utils

Collection of utilities for the peritext ecosystem.

Please refer to the [peritext repositories](https://github.com/peritext) to see related modules allowing to build an publishing system.

# API overview

Here is a brief overview of the utilities exposed by this package. Refer to the source code documentation to get a better documentation about each utility API.


`buildBibliography` : builds display-related data for rendering the bibliography of an edition

`buildCitations` : builds display-related data for rendering the bibliographic citations of an edition

`buildContextContent` : builds display-related data for rendering a resource's related contextualizations

`buildGlossary` : builds display-related data for rendering the glossary of an edition

`resourceToCslJSON` : converts a peritext resource into a CSL-JSON object

`generateOpenUrl` : generates an open URL out of csl-json data

`StructuredCOinS` : provides a span encoded with the Context Objects in Span / openURL specification

`getRelatedAssetsIds` : computes related ids for a given edition

`chooseAppropriateAsset` : defines the appropriate asset for rendering a contextualization based on a contextualizer's profile and a map of available assets

`chooseAppropriateSubAsset` : defines the appropriate asset for rendering a contextualization based on a contextualizer's profile and a map of available assets

`getContextualizationsFromEdition` : compute related contextualizations for an edition

`loadAssetsForEdition` : loads related assets for an edition

`buildHTMLMetadata` : builds HTML metadata for an edition

`abbrevString` : abbreviates a string if it exceeds a given maximum

`bibToSchema` : converts bibliographic data to microformat data

60 changes: 60 additions & 0 deletions dist/StructuredCOinS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;

var _react = _interopRequireDefault(require("react"));

var _propTypes = require("prop-types");

var _index = require("./index");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

// let styles = {};

/**
* dumb component for rendering the structured representation of a cited element in the format of openUrl/Context Object in Span
*/
class StructuredCOinS extends _react.default.Component {
constructor(...args) {
super(...args);

_defineProperty(this, "shouldComponentUpdate", () => true);
}

/**
* render
* @return {ReactElement} markup
*/
render() {
let openUrl;

if (this.props.resource) {
openUrl = (0, _index.generateOpenUrl)((0, _index.resourceToCslJSON)(this.props.resource));
} else if (this.props.cslRecord) {
openUrl = (0, _index.generateOpenUrl)([this.props.cslRecord]);
} else {
return null;
}

return _react.default.createElement("span", {
className: 'peritext-structured-context-object-in-span-container Z3988',
title: openUrl
});
}

}

exports.default = StructuredCOinS;

_defineProperty(StructuredCOinS, "propTypes", {
cslRecord: _propTypes.PropTypes.object,
resource: _propTypes.PropTypes.object
});

_defineProperty(StructuredCOinS, "defaultProps", {});
21 changes: 21 additions & 0 deletions dist/abbrevString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;

var _unicodeByteTruncate = _interopRequireDefault(require("unicode-byte-truncate"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const abbrevString = (str = '', maxLength = 30) => {
if (str.length > maxLength) {
return `${(0, _unicodeByteTruncate.default)(str, maxLength)}...`;
}

return str;
};

var _default = abbrevString;
exports.default = _default;
92 changes: 92 additions & 0 deletions dist/buildAuthorsIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = buildAuthorsIndex;

var _buildContextContent = _interopRequireDefault(require("./buildContextContent"));

var _resourceToCslJSON = _interopRequireDefault(require("./resourceToCslJSON"));

var _slugify = _interopRequireDefault(require("slugify"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
* Builds component-consumable data to represent
* the index of authors mentionned in the production
* @param {object} production - the production to process
* @return {array} authorsNameMentions - all the glossary entries properly formatted for rendering
*/
function buildAuthorsIndex(production) {
const {
contextualizations,
contextualizers,
resources
} = production;
let authorsNameMentions = Object.keys(contextualizations)
/*
* .filter(contextualizationId => {
* const contextualizerId = contextualizations[contextualizationId].contextualizerId;
* const contextualizer = contextualizers[contextualizerId];
* return contextualizer && contextualizer.type === 'bib';
* })
*/
.map(contextualizationId => _objectSpread({}, contextualizations[contextualizationId], {
contextualizer: contextualizers[contextualizations[contextualizationId].contextualizerId],
resource: resources[contextualizations[contextualizationId].resourceId],
contextContent: (0, _buildContextContent.default)(production, contextualizationId)
})).reduce((entries, contextualization) => {
const resource = contextualization.resource; // handling bib resources

if (resource
/*&&
resource.metadata.type === 'bib' &&
resource.data &&
Array.isArray(resource.data) &&
resource.data.length &&
resource.data[0] &&
resource.data[0].author*/
) {
const normalized = (0, _resourceToCslJSON.default)(resource) || [];
return normalized.filter(norm => norm.author !== undefined).reduce((authors, entr) => authors.concat(entr.author), []).reduce((finalEntries, author) => {
if (author.given === undefined) {
return finalEntries;
} // todo: what about homonyms ?


const id = (0, _slugify.default)(`${author.family} ${author.given}`.toLowerCase());

if (finalEntries[id]) {
return _objectSpread({}, finalEntries, {
[id]: _objectSpread({}, finalEntries[id], {
mentions: [...finalEntries[id].mentions, contextualization]
})
});
}

return _objectSpread({}, finalEntries, {
[id]: _objectSpread({}, author, {
id,
mentions: [contextualization]
})
});
}, entries);
}

return entries;
}, {});
authorsNameMentions = Object.keys(authorsNameMentions).map(id => authorsNameMentions[id]).sort((a, b) => {
if (a.given > b.given) {
return -1;
} else {
return 1;
}
});
return authorsNameMentions;
}
92 changes: 92 additions & 0 deletions dist/buildBibliography.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = buildBibliography;

var _buildContextContent = _interopRequireDefault(require("./buildContextContent"));

var _resourceToCslJSON = _interopRequireDefault(require("./resourceToCslJSON"));

var _reactCiteproc = require("react-citeproc");

var _peritextSchemas = require("peritext-schemas");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const {
draftEntitiesNames: {
INLINE_ASSET
}
} = _peritextSchemas.constants;
/**
* Builds component-consumable data to represent
* the citations of "bib" resources being mentionned in the production
* @param {object} production - the production to process
* @param {object} citations - the citation data
* @return {object} items - reference items ready to be visualized
*/

function buildBibliography(production, citations) {
const {
contextualizations,
contextualizers,
resources
} = production;
/*
* Assets preparation
*/

const assets = Object.keys(contextualizations).reduce((ass, id) => {
const contextualization = contextualizations[id];
const contextualizer = contextualizers[contextualization.contextualizerId];
return _objectSpread({}, ass, {
[id]: _objectSpread({}, contextualization, {
resource: resources[contextualization.resourceId],
contextualizer,
type: contextualizer ? contextualizer.type : INLINE_ASSET
})
});
}, {});
/*
* Citations preparation
*/
// isolate bib contextualizations

const bibContextualizations = Object.keys(assets).filter(assetKey => assets[assetKey].type !== 'glossary').map(assetKey => assets[assetKey]);
/*
* console.log('citations.citationItems', citations.citationItems);
* console.log('bib contextualizations', bibContextualizations);
*/

const items = Object.keys(citations.citationItems).map(citationKey => {
const mentions = bibContextualizations.filter(contextualization => {
if (contextualization.resource) {
// console.log('contextualization resource', contextualization.resource);
const cit = (0, _resourceToCslJSON.default)(contextualization.resource);
return cit && cit[0] && cit[0].id === citationKey;
}
});
const biblio = (0, _reactCiteproc.makeBibliography)(citations.citationItems, production.settings.citationStyle.data, production.settings.citationLocale.data, {
select: [{
field: 'id',
value: citationKey
}]
});
const title = biblio && biblio[1] && biblio[1][0];
return {
citationKey,
title,
item: citations.citationItems[citationKey],
mentions: mentions.map(mention => _objectSpread({}, mention, {
contextContent: (0, _buildContextContent.default)(production, mention.id)
}))
};
});
return items;
}
Loading

0 comments on commit 82740c2

Please sign in to comment.