1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.getLicenseSection = exports.getLicenseFile = void 0;
|
7 | const util_js_1 = require("./util.js");
|
8 | const spdx_expression_parse_1 = __importDefault(require("spdx-expression-parse"));
|
9 | const full_1 = __importDefault(require("spdx-license-list/full"));
|
10 | function 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 |
|
26 | .replace('\nCopyright (c) <year> <copyright holders>\n', '')
|
27 |
|
28 | .replace(/\s?Copyright.+holders>/gi, '')
|
29 |
|
30 | .replace(/^[\s\S]+<<endOptional>>\s*/m, '')
|
31 |
|
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 | }
|
46 | function getLicensesHTML(spdxString, output) {
|
47 | const sdpxObject = spdx_expression_parse_1.default(spdxString);
|
48 | return renderSpdxObject(sdpxObject, output);
|
49 | }
|
50 | function getLicenseIntroduction(data) {
|
51 |
|
52 | if (!data.license) {
|
53 | throw new Error('License file was requested, but no license was specified');
|
54 | }
|
55 |
|
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 |
|
66 | return result;
|
67 | }
|
68 | function getLicenseFile(data) {
|
69 |
|
70 | if (!data.license) {
|
71 | throw new Error('License file was requested, but no license was specified');
|
72 | }
|
73 |
|
74 | const result = [
|
75 | '<h1>License</h1>',
|
76 | '',
|
77 | getLicenseIntroduction(data),
|
78 | '',
|
79 | getLicensesHTML(data.license, 'body'),
|
80 | ].join('\n');
|
81 |
|
82 | return result;
|
83 | }
|
84 | exports.getLicenseFile = getLicenseFile;
|
85 | function getLicenseSection(data) {
|
86 |
|
87 | if (!data.license) {
|
88 | throw new Error('License file was requested, but no license was specified');
|
89 | }
|
90 |
|
91 | const result = [
|
92 | '<h2>License</h2>',
|
93 | '',
|
94 | getLicenseIntroduction(data),
|
95 | ].join('\n');
|
96 |
|
97 | return result;
|
98 | }
|
99 | exports.getLicenseSection = getLicenseSection;
|