UNPKG

@thi.ng/transclude

Version:

Extensible functional template engine for text document generation, incl. various high-level Markdown features

167 lines (166 loc) 5.8 kB
import { isString } from "@thi.ng/checks/is-string"; import { link, list } from "./markdown.js"; const LICENSES = { "Apache-2.0": { name: "Apache License 2.0", url: "https://spdx.org/licenses/Apache-2.0.html" }, "BSD-2-Clause": { name: 'BSD 2-Clause "Simplified" License', url: "https://spdx.org/licenses/BSD-2-Clause.html" }, "BSD-3-Clause": { name: 'BSD 3-Clause "New" or "Revised" License', url: "https://spdx.org/licenses/BSD-3-Clause.html" }, "CC-BY-4.0": { name: "Creative Commons Attribution 4.0 International", url: "https://spdx.org/licenses/CC-BY-4.0.html" }, "CC-BY-NC-4.0": { name: "Creative Commons Attribution Non Commercial 4.0 International", url: "https://spdx.org/licenses/CC-BY-NC-4.0.html" }, "CC-BY-NC-ND-4.0": { name: "Creative Commons Attribution Non Commercial No Derivatives 4.0 International", url: "https://spdx.org/licenses/CC-BY-NC-ND-4.0.html" }, "CC-BY-NC-SA-4.0": { name: "Creative Commons Attribution Non Commercial Share Alike 4.0 International", url: "https://spdx.org/licenses/CC-BY-NC-SA-4.0.html" }, "CC-BY-ND-4.0": { name: "Creative Commons Attribution No Derivatives 4.0 International", url: "https://spdx.org/licenses/CC-BY-ND-4.0.html" }, "CC-BY-SA-4.0": { name: "Creative Commons Attribution Share Alike 4.0 International", url: "https://spdx.org/licenses/CC-BY-SA-4.0.html" }, "CPAL-1.0": { name: "Common Public Attribution License 1.0", url: "https://spdx.org/licenses/CPAL-1.0.html" }, "CPL-1.0": { name: "Common Public License 1.0", url: "https://spdx.org/licenses/CPL-1.0.html" }, "ECL-2.0": { name: "Educational Community License v2.0", url: "https://spdx.org/licenses/ECL-2.0.html" }, "EPL-2.0": { name: "Eclipse Public License 2.0", url: "https://spdx.org/licenses/EPL-2.0.html" }, EUDatagrid: { name: "EU DataGrid Software License", url: "https://spdx.org/licenses/EUDatagrid.html" }, "EUPL-1.2": { name: "European Union Public License 1.2", url: "https://spdx.org/licenses/EUPL-1.2.html" }, "GFDL-1.3-or-later": { name: "GNU Free Documentation License v1.3 or later", url: "https://spdx.org/licenses/GFDL-1.3-or-later.html" }, "GPL-3.0-or-later": { name: "GNU General Public License v3.0 or later", url: "https://spdx.org/licenses/GPL-3.0-or-later.html" }, ISC: { name: "ISC License", url: "https://spdx.org/licenses/ISC.html" }, "LGPL-3.0-or-later": { name: "GNU Lesser General Public License v3.0 or later", url: "https://spdx.org/licenses/LGPL-3.0-or-later.html" }, MIT: { name: "MIT License", url: "https://spdx.org/licenses/MIT.html" }, "MPL-2.0": { name: "Mozilla Public License 2.0", url: "https://spdx.org/licenses/MPL-2.0.html" }, "OFL-1.1": { name: "SIL Open Font License 1.1", url: "https://spdx.org/licenses/OFL-1.1.html" }, Unlicense: { name: "The Unlicense", url: "https://spdx.org/licenses/Unlicense.html" }, "UPL-1.0": { name: "Universal Permissive License v1.0", url: "https://spdx.org/licenses/UPL-1.0.html" }, Zlib: { name: "zlib License", url: "https://spdx.org/licenses/Zlib.html" } }; const shortName = (name) => { const idx = name.indexOf("/"); return idx > 0 ? name.substring(idx + 1) : name; }; const author = (author2) => isString(author2) ? author2.split(/\s*[<(]/)[0] : author2.name; const authorLink = (author2) => { if (isString(author2)) { const [name, a, b] = author2.split(/\s*[<(]/); const href = b ? b[b.length - 1] == ")" ? b : a : a ? a : ""; return href.length && href[href.length - 1] === ")" ? link(name, href.substring(0, href.length - 1)) : name; } return author2.url ? link(author2.name, author2.url) : author2.name; }; const contributors = (people, eol = "\n") => list(people.map(author), eol); const contributorLinks = (people, eol = "\n") => list(people.map(authorLink), eol); const license = (spdxID) => LICENSES[spdxID].name || `${spdxID} license`; const licenseLink = (spdxID) => { const license2 = LICENSES[spdxID]; return license2 ? link(license2.name, license2.url) : `${spdxID} license`; }; const packageTemplates = (pkg, opts) => { const $opts = { hdContributors: "### Contributors\n\n", ...opts }; const tpls = { "pkg.name": ({ user }) => pkg(user).name, "pkg.shortName": ({ user }) => shortName(pkg(user).name), "pkg.version": ({ user }) => "v" + pkg(user).version, "pkg.description": ({ user }) => pkg(user).description, "pkg.link": ({ user }) => link(pkg(user).name, pkg(user).homepage), "pkg.author": ({ user }) => author(pkg(user).author), "pkg.authorLink": ({ user }) => authorLink(pkg(user).author), "pkg.allAuthors": __allAuthors(pkg, author), "pkg.allAuthorLinks": __allAuthors(pkg, authorLink), "pkg.contributors": __allContribs(pkg, $opts, contributors), "pkg.contributorLinks": __allContribs(pkg, $opts, contributorLinks), "pkg.license": ({ user }) => license(pkg(user).license), "pkg.licenseLink": ({ user }) => licenseLink(pkg(user).license) }; return tpls; }; const __allAuthors = (pkg, itemFn) => ({ user, eol }) => { const $pkg = pkg(user); const $author = itemFn($pkg.author); const res = []; if ($pkg.contributors) { res.push( $author + " (Main author)", ...$pkg.contributors.map(itemFn) ); } else { res.push($author); } return list(res, eol); }; const __allContribs = (pkg, opts, itemFn) => ({ user, eol }) => { const people = pkg(user).contributors; return people ? opts.hdContributors + itemFn(people, eol) : ""; }; export { LICENSES, author, authorLink, contributorLinks, contributors, license, licenseLink, packageTemplates, shortName };