<!doctype html>
<head>
<meta charset='utf-8' />
<title>The TPL License</title>
<link rel=preload href='/templates/list.txt' />
<!-- TODO: stylesheet -->
</head>
<body>
<main>
<h1>The <u>T</u>erse <u>P</u>ublic <u>L</u>icense Family</h1>
<p>A family of minimal licenses for artists and developers, without
the sheer size of the more thorough licenses. If you forsee
yourself going to court, maybe pick something more robust. If you
just want something simple for a small project, this may well be
for you.</p>
<p>Feel free to <a href="https://amehut.dev/~aleteoryx/tpl">suggest
improvements</a>.</p>
<h2>Generation Wizard</h2>
<section id=wizard>
<label for=license_sel>License:</label> <select id=license_sel></select><br/>
<label for=creator_sel>Creator Type:</label> <select id=creator_sel>
<option>creator</option>
<option>creators</option>
<option selected>creator(s)</option>
<option>artist</option>
<option>artists</option>
<option>artist(s)</option>
<option>author</option>
<option>authors</option>
<option>author(s)</option>
</select><br/>
<label for=worktype_sel>Work Type:</label> <select id=worktype_sel>
<option>software</option>
<option>content</option>
<option>work</option>
</select><br/>
<label for=medium_sel>Project Medium:</label> <select id=medium_sel>
<option>repository</option>
<option>archive</option>
<option>website</option>
</select><br/>
<input type=checkbox id=include_header checked /> <label for=include_header>Include header?</label><br/>
<div id=license_opts></div>
<textarea readonly id=license_text cols=74 rows=10></textarea>
</section>
</main>
<script>(async function() {
async function parse_and_fetch_parts(metadata) {
let format = [];
for (const part of metadata.format) {
if (!part.includes(":")) {
format.push({file: part});
continue;
}
let [name, file] = part.split(":", 2);
if (name[0] == "+") {
format.push({file, optional: name.slice(1), default: true});
continue;
} else {
format.push({file, optional: name, default: false})
}
}
format = await Promise.all(
format.map(x =>
fetch(`${location}/templates/${x.file}`).then(r => r.text())
.then(c => ({...x, contents: c}))));
metadata.format = format;
return metadata;
}
const metafiles = await fetch(`${location}/templates/list.txt`)
.then(r => r.text())
.then(x =>
x.split("\n").filter(x => x != "")
.map(x => `${location}/templates/${x.trim()}/meta.json`));
const metadata = await Promise.all(
metafiles.map(x => fetch(x).then(r => r.json())));
const templates = Object.fromEntries(
(await Promise.all(
metadata.map(parse_and_fetch_parts)))
.map(x => [x.name, x]));
async function template_changed() {
license_opts.innerText = "";
for (const part of templates[license_sel.value].format) {
if (part.optional) {
const box = document.createElement("input");
box.addEventListener("change", params_changed);
box.type = "checkbox";
box.id = part.optional.replace(" ", "-");
box.checked = part.default;
const label = document.createElement("label");
label.for = box.id;
label.innerText = part.optional;
const br = document.createElement("br");
license_opts.append(box, " ", label, br);
}
}
params_changed();
}
async function params_changed() {
template = templates[license_sel.value];
license_text.value = "";
if (include_header.checked) {
license_text.value +=
`-- TPL ${template.name} v${template.version}\n`;
license_text.value += "-- <https://amehut.dev/~aleteoryx/tpl>\n";
license_text.value += '-'.repeat(72);
}
for (const part of template.format) {
if (part.optional && !(document.getElementById(part.optional.replace(" ", "-")).checked)) {
continue;
}
license_text.value += "\n\n";
license_text.value += word_wrap(
do_substitution(
part.contents.trim()));
}
license_text.value = license_text.value.trim();
}
function do_substitution(text) {
// console.log(text);
let newtext = "";
let last_idx = -1;
let idx = -1;
while ((idx = text.indexOf("$", idx+1)) != -1) {
newtext += text.slice(last_idx + 1, idx);
// console.log(newtext);
let start = idx+1;
idx = text.indexOf("$", idx+1);
if (idx == -1) idx = text.length;
let end = idx;
let [key, filter] = text.slice(start, end).split(":", 2);
let subst = key;
switch (key) {
case "type":
subst = worktype_sel.value;
break;
case "medium":
subst = medium_sel.value;
break;
case "creator":
subst = creator_sel.value;
break;
case "author_verb":
let [single, plural] = filter.split("|", 2);
filter = "";
if (/s$|\(s\)$/.test(creator_sel.value))
subst = plural;
else subst = single;
}
if (filter?.includes("caps")) subst = subst.toUpperCase();
if (filter?.includes("lower")) subst = subst.toLowerCase();
newtext += subst;
last_idx = idx;
}
return newtext + text.slice(last_idx+1);
}
function word_wrap(text, cols = 72) {
// console.log(text);
let [firstword, ...words] = text.split(/[ \n\r\t]+/g)
.filter(x => x)
.map(s =>
s.match(RegExp(`.{1,${cols}}`, 'g'))).flat();
let newtext = firstword;
let count = firstword.length+1;
// console.log(words, firstword);
for (const word of words) {
count += word.length + 1;
if (count >= cols) {
newtext += "\n" + word;
count = word.length + 1;
} else {
newtext += ' ' + word;
}
}
return newtext;
}
for (const template in templates) {
const opt = document.createElement("option");
opt.innerText = template;
license_sel.append(opt);
}
license_sel.addEventListener("change", template_changed);
include_header.addEventListener("change", params_changed);
creator_sel.addEventListener("change", params_changed);
worktype_sel.addEventListener("change", params_changed);
medium_sel.addEventListener("change", params_changed);
template_changed();
})()</script>
<script>
</script>
</body>