UNPKG

3.67 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.getLicenseSection = exports.getLicenseFile = void 0;
7const util_js_1 = require("./util.js");
8const spdx_expression_parse_1 = __importDefault(require("spdx-expression-parse"));
9const full_1 = __importDefault(require("spdx-license-list/full"));
10function renderSpdxObject(spdxObject, output, depth = 0) {
11 if (spdxObject.license) {
12 const code = spdxObject.license;
13 const details = full_1.default[code];
14 if (!details) {
15 throw new Error(`Could not find the details for the license ${code}`);
16 }
17 const name = details.name;
18 const body = details.licenseText;
19 const url = `http://spdx.org/licenses/${code}.html`;
20 return output === 'description'
21 ? depth === 0
22 ? `<ul><li><a href="${url}">${name}</a></li></ul>`
23 : `<a href="${url}">${name}</a>`
24 : body
25 // Remove useless copyright headers - (spdx-license-list@5.x)
26 .replace('\nCopyright (c) <year> <copyright holders>\n', '')
27 // Remove useless copyright headers - (spdx-license-list@6.x)
28 .replace(/\s?Copyright.+holders>/gi, '')
29 // Remove license introductions
30 .replace(/^[\s\S]+<<endOptional>>\s*/m, '')
31 // Convert the license into HTML
32 .replace(/^(.+?)\n\s*([\s\S]+)\s*$/, '<h2>$1</h2>\n\n<pre>\n$2\n</pre>');
33 }
34 else if (spdxObject.conjunction) {
35 const left = renderSpdxObject(spdxObject.left, output, depth + 1);
36 const middle = spdxObject.conjunction;
37 const right = renderSpdxObject(spdxObject.right, output, depth + 1);
38 return output === 'description'
39 ? `<ul><li>${left}</li>\n<li>${middle}</li>\n<li>${right}</li></ul>`
40 : `${left}\n\n${right}\n\n`.trim();
41 }
42 else {
43 throw new Error(`Unknown spdx object value: ${JSON.stringify(spdxObject, null, ' ')}`);
44 }
45}
46function getLicensesHTML(spdxString, output) {
47 const sdpxObject = spdx_expression_parse_1.default(spdxString);
48 return renderSpdxObject(sdpxObject, output);
49}
50function getLicenseIntroduction(data) {
51 // Check
52 if (!data.license) {
53 throw new Error('License file was requested, but no license was specified');
54 }
55 // Prepare
56 const result = [
57 'Unless stated otherwise all works are:',
58 '',
59 util_js_1.getPeopleHTML(data.authors, { displayCopyright: true, displayYears: true }),
60 '',
61 'and licensed under:',
62 '',
63 getLicensesHTML(data.license, 'description'),
64 ].join('\n');
65 // Return
66 return result;
67}
68function getLicenseFile(data) {
69 // Check
70 if (!data.license) {
71 throw new Error('License file was requested, but no license was specified');
72 }
73 // Prepare
74 const result = [
75 '<h1>License</h1>',
76 '',
77 getLicenseIntroduction(data),
78 '',
79 getLicensesHTML(data.license, 'body'),
80 ].join('\n');
81 // Return
82 return result;
83}
84exports.getLicenseFile = getLicenseFile;
85function getLicenseSection(data) {
86 // Check
87 if (!data.license) {
88 throw new Error('License file was requested, but no license was specified');
89 }
90 // Prepare
91 const result = [
92 '<h2>License</h2>',
93 '',
94 getLicenseIntroduction(data),
95 ].join('\n');
96 // Return
97 return result;
98}
99exports.getLicenseSection = getLicenseSection;