diff --git a/codemeta_generation.js b/codemeta_generation.js index 0be74ea..2c43880 100644 --- a/codemeta_generation.js +++ b/codemeta_generation.js @@ -1,181 +1,207 @@ /** * Copyright (C) 2019 The Software Heritage developers * See the AUTHORS file at the top-level directory of this distribution * License: GNU Affero General Public License version 3, or any later version * See top-level LICENSE file for more information */ "use strict"; const SPDX_PREFIX = 'https://spdx.org/licenses/'; function emptyToUndefined(v) { if (v == null || v == "") return undefined; else return v; } function getIfSet(query) { return emptyToUndefined(document.querySelector(query).value); } function setIfDefined(query, value) { if (value !== undefined) { document.querySelector(query).value = value; } } // Names of codemeta properties with a matching HTML field name const directCodemetaFields = [ 'codeRepository', 'contIntegration', 'dateCreated', 'datePublished', 'dateModified', 'issueTracker', 'name', 'version', 'identifier', 'description', 'applicationCategory', - //keywords TODO:keywords array 'releaseNotes', 'funding', 'runtimePlatform', - //softwareRequiremnts, 'operatingSystem', 'developmentStatus', - //relatedLink 'programmingLanguage', 'isPartOf', //'referencePublication' // "@type": "ScholarlyArticle", // "idendifier": "https://doi.org/xx.xxxx/xxxx.xxxx.xxxx", // "name": "title of publication" ]; +const splittedCodemetaFields = [ + ['keywords', ','], + ['softwareRequirements', '\n'], + ['relatedLink', '\n'], +] + // Names of codemeta properties with a matching HTML field name, // in a Person object const directPersonCodemetaFields = [ 'givenName', 'familyName', 'email', 'affiliation' ]; function generatePerson(idPrefix) { var doc = { "@type": "Person", "@id": getIfSet(`#${idPrefix}_id`), } directPersonCodemetaFields.forEach(function (item, index) { doc[item] = getIfSet(`#${idPrefix}_${item}`); }); return doc; } function generatePersons(prefix) { var persons = []; var nbPersons = getNbPersons(prefix); for (let personId = 1; personId <= nbPersons; personId++) { persons.push(generatePerson(`${prefix}_${personId}`)); } return persons; } function generateCodemeta() { var inputForm = document.querySelector('#inputForm'); var codemetaText, errorHTML; if (inputForm.checkValidity()) { var doc = { "@context": "https://doi.org/10.5063/schema/codemeta-2.0", "@type": "SoftwareSourceCode", "license": SPDX_PREFIX + getIfSet('#license'), }; // Generate most fields directCodemetaFields.forEach(function (item, index) { doc[item] = getIfSet('#' + item) }); + // Generate simple fields parsed simply by splitting + splittedCodemetaFields.forEach(function (item, index) { + const id = item[0]; + const separator = item[1]; + const value = getIfSet('#' + id); + if (value !== undefined) { + doc[id] = value.split(separator); + } + }); + // Generate dynamic fields doc = Object.assign(doc, { "author": generatePersons('author'), "contributor": generatePersons('contributor'), }); codemetaText = JSON.stringify(doc, null, 4); errorHTML = ""; } else { codemetaText = ""; errorHTML = "invalid input (see error above)"; inputForm.reportValidity(); } document.querySelector('#codemetaText').innerText = codemetaText; setError(errorHTML); if (codemetaText) { // For restoring the form state on page reload sessionStorage.setItem('codemetaText', codemetaText); } } function importPersons(prefix, legend, docs) { if (docs === 'undefined') { return; } docs.forEach(function (doc, index) { var personId = addPerson(prefix, legend); setIfDefined(`#${personId}_id`, doc['@id']); directPersonCodemetaFields.forEach(function (item, index) { setIfDefined(`#${prefix}_${personId}_${item}`, doc[item]); }); }) } function importCodemeta() { var inputForm = document.querySelector('#inputForm'); var codemetaText = document.querySelector('#codemetaText').innerText; var doc; try { doc = JSON.parse(codemetaText); } catch (e) { setError(`Could not read codemeta document because it is not valid JSON (${e})`); return; } resetForm(); if (doc['license'] !== undefined && doc['license'].indexOf(SPDX_PREFIX) == 0) { var license = doc['license'].substring(SPDX_PREFIX.length); document.querySelector('#license').value = license; } directCodemetaFields.forEach(function (item, index) { setIfDefined('#' + item, doc[item]); }); + // Import simple fields by joining on their separator + splittedCodemetaFields.forEach(function (item, index) { + const id = item[0]; + const separator = item[1]; + let value = doc[id]; + if (value !== undefined) { + if (Array.isArray(value)) { + value = value.join(separator); + } + setIfDefined('#' + id, value); + } + }); + importPersons('author', 'Author', doc['author']) importPersons('contributor', 'Contributor', doc['contributor']) setError(""); } function loadStateFromStorage() { codemetaText = sessionStorage.getItem('codemetaText') if (codemetaText) { document.querySelector('#codemetaText').innerText = codemetaText; importCodemeta(); } } diff --git a/index.html b/index.html index 72c76be..606477a 100644 --- a/index.html +++ b/index.html @@ -1,244 +1,262 @@ <!doctype html> <!-- Copyright (C) 2019 The Software Heritage developers See the AUTHORS file at the top-level directory of this distribution License: GNU Affero General Public License version 3, or any later version See top-level LICENSE file for more information --> <html lang="en"> <head> <meta charset="utf-8"> <title>CodeMeta generator</title> <script src="./utils.js"></script> <script src="./fields_data.js"></script> <script src="./dynamic_form.js"></script> <script src="./codemeta_generation.js"></script> <link rel="stylesheet" type="text/css" href="./style.css"> </head> <body> <h1>CodeMeta generator</h1> <p>Most fields are optional. Mandatory fields will be highlighted when generating Codemeta.</p> <form id="inputForm"> <fieldset id="fieldsetSoftwareItself"> <legend>The software itself</legend> <p title="The name of the software"> <label for="name">Name</label> <input type="text" name="name" id="name" aria-describedby="name_descr" placeholder="My Software" required="true" /> <div class="field-description" id="name_descr">the software title</div> </p> <p title="Unique identifier"> <label for="identifier">Unique identifier</label> <input type="text" name="identifier" id="identifier" placeholder="10.151.xxxxx" /> <div class="field-description">such as ISBNs, GTIN codes, UUIDs etc.. <a href="http://schema.org/identifier">http://schema.org/identifier</a> </div> </p> <!-- TODO:define better I looked at the schema.org definition of identifier (https://schema.org/identifier), it can be text, url or PropertyValue. Used as follows in data representation with microdata: <div property="identifier" typeof="PropertyValue"> <span property="propertyID">DOI</span>: <span property="value">10.151.xxxxx</span> </div> we can use that with identifier-type and identifier-value to have a clearer idea of what needs to be in the input. --> <p title="Type of the software application"> <label for="applicationCategory">Application category</label> <input type="text" name="applicationCategory" id="applicationCategory" placeholder="Astronomy" /> </p> <p title="a brief description of the software"> <label for="description">Description</label> <br /> <textarea rows="4" cols="50" name="description" id="description" placeholder="My Software computes ephemerides and orbit propagation. It has been developed from early ´80." ></textarea> </p> <p title="The date on which the software was created."> <label for="dateCreated">Creation date</label> <input type="text" name="dateCreated" id="dateCreated" placeholder="YYYY-MM-DD" pattern="\d{4}-\d{2}-\d{2}" /> </p> <p title="Date of first publication."> <label for="datePublished">First release date</label> <input type="text" name="datePublished" id="datePublished" placeholder="YYYY-MM-DD" pattern="\d{4}-\d{2}-\d{2}" /> </p> + <p title="Comma-separated list of keywords"> + <label for="keywords">Keywords</label> + <input type="text" name="keywords" id="keywords" + placeholder="ephemerides, orbit, astronomy" /> + </p> + <p> <label for="license">License</label> <input list="licenses" name="license" id="license" aria-describedby="licenses_descr"> <datalist id="licenses"> </datalist> <!-- TODO: insert placeholder --> <div class="field-description" id="licenses_descr">from <a href="https://spdx.org/license-list">SPDX licence list</a></div> </p> </fieldset> <fieldset id="fieldsetDevelopmentCommunity"> <legend>Development community / tools</legend> <p title="Link to the repository where the un-compiled, human readable code and related code is located (SVN, Git, GitHub, CodePlex, institutional GitLab instance, etc.)."> <label for="codeRepository">Code repository</label> <input type="URL" name="codeRepository" id="codeRepository" placeholder="git+https://github.com/You/RepoName.git" /> </p> <p title="Link to continuous integration service (Travis-CI, Gitlab CI, etc.)."> <label for="contIntegration">Continuous integration</label> <input type="URL" name="contIntegration" id="contIntegration" placeholder="https://travis-ci.org/You/RepoName" /> </p> <p title="Link to a place for users/developpers to report and manage bugs (JIRA, GitHub issues, etc.)."> <label for="issueTracker">Issue tracker</label> <input type="URL" name="issueTracker" id="issueTracker" placeholder="https://github.com/You/RepoName/issues" /> </p> + + <p title="Related document, software, tools"> + <label for="relatedLink">Related links</label> + <br /> + <textarea rows="4" cols="50" + name="relatedLink" id="relatedLink"></textarea> </fieldset> <fieldset id="fieldsetCurrentVersion"> <legend>Current version of the software</legend> <p title="Version number of the software"> <label for="version">Version number</label> <input type="text" name="version" id="version" placeholder="1.0.0" /> </p> <p title="The date on which the software was most recently modified."> <label for="dateModified">Release date</label> <input type="text" name="dateModified" id="dateModified" placeholder="YYYY-MM-DD" pattern="\d{4}-\d{2}-\d{2}" /> </p> <p title="a brief description of the software"> <label for="releaseNotes">Release notes</label> <br /> <textarea rows="4" cols="50" name="releaseNotes" id="releaseNotes" placeholder= "Change log: this and that; Bugfixes: that and this." ></textarea> </p> -<!--TODO: keywords as a string array --> -<!--TODO: software requirements as string array --> + <p title="Required software to run/use this one."> + <label for="softwareRequirements">Software requirements</label> + <br /> + <textarea rows="4" cols="50" + name="softwareRequirements" id="softwareRequirements" + placeholder= +"Python 3.4 +https://github.com/psf/requests"></textarea> <!--TODO: related links as URL array --> <!--TODO: referencePublication as ScholarlyArticle array --> </fieldset> <fieldset id="fieldsetAdditionalInfo"> <legend>Additional Info</legend> <p title="Funding"> <label for="funding">Funding</label> <input type="text" name="funding" id="funding" aria-describedby="funding_descr" placeholder="Università di Pisa (PRA_2018_73)"/> <div class="field-description" id="funding_descr">software funded by (e.g. specific grant)</div> </p> <p title="Runtime Platform"> <label for="runtimePlatform">Runtime Platform</label> <input type="text" name="runtimePlatform" id="runtimePlatform" placeholder="Python2.3" /> </p> <p title="Operating System"> <label for="operatingSystem">Operating System</label> <input type="text" name="operatingSystem" id="operatingSystem" placeholder="Android 1.6" /> </p> <p title="Development Status"> <label for="developmentStatus">Development Status</label> <datalist id="developmentStatuses"> <option value="Concept"> <option value="WIP"> <option value="Suspended"> <option value="Abandoned"> <option value="Active"> <option value="Inactive"> <option value="Unsupported"> <option value="Moved"> </datalist> <input list="developmentStatuses" id="developmentStatus" aria-describedby="developmentStatuses_descr" pattern="Concept|WIP|Suspended|Abandoned|Active|Inactive|Unsupported|Moved"> <div class="field-description" id="developmentStatuses_descr">see <a href="http://www.repostatus.org">www.repostatus.org</a> for details</div> </p> <p title="Programming Language"> <label for="programmingLanguage">Programming Language</label> <input type="text" name="programmingLanguage" id="programmingLanguage" placeholder="C#" /> </p> <!-- TODO: We should add support for multiple languages, eg. by splitting on commas when exporting. --> <p title="Part of"> <label for="isPartOf">I part of </label> <input type="URL" name="isPartOf" id="isPartOf" placeholder="http://The.Bigger.Framework.org" /> </p> </fieldset> <fieldset class="persons" id="author_container"> <legend>Authors</legend> <p>Order of authors does not matter.</p> <input type="hidden" id="author_nb" value="0" /> <div id="addRemoveAuthor"> <input type="button" id="author_add" value="Add one" onclick="addPerson('author', 'Author');" /> <input type="button" id="author_remove" value="Remove last" onclick="removePerson('author');" /> </div> </fieldset> <fieldset class="persons" id="contributor_container"> <legend>Contributors</legend> <p>Order of contributors does not matter.</p> <input type="hidden" id="contributor_nb" value="0" /> <div id="addRemoveContributor"> <input type="button" id="contributor_add" value="Add one" onclick="addPerson('contributor', 'Contributor');" /> <input type="button" id="contributor_remove" value="Remove last" onclick="removePerson('contributor');" /> </div> </fieldset> </form> <form> <input type="button" id="generateCodemeta" value="Generate Codemeta" /> <input type="button" id="resetForm" value="Reset form" /> <input type="button" id="importCodemeta" value="Import Codemeta" /> </form> <p id="errorMessage"> </p> <pre contentEditable="true" id="codemetaText"></pre> <script> initFieldsData(); initCallbacks(); loadStateFromStorage(); </script> </body> </html>