From e33ace65223d49fe8bd1b8d44f8fcc606711cbbf Mon Sep 17 00:00:00 2001 From: Aleteoryx Date: Tue, 27 Aug 2024 16:13:26 -0400 Subject: [PATCH] well that's the templater done --- index.html | 98 ++++++++++++++++++++++++++++++++++++++++- templates/PD/main.txt | 7 +-- templates/liability.txt | 6 +-- 3 files changed, 104 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 7e7f340..fc5f8e5 100644 --- a/index.html +++ b/index.html @@ -18,6 +18,27 @@

Generation Wizard


+
+
+

@@ -69,6 +90,7 @@ async function template_changed() { 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; @@ -96,8 +118,77 @@ async function params_changed() { continue; } license_text.value += "\n\n"; - license_text.value += part.contents.trim(); + 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) { @@ -107,8 +198,13 @@ for (const template in templates) { } 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(); })() + diff --git a/templates/PD/main.txt b/templates/PD/main.txt index 4864253..0734d12 100644 --- a/templates/PD/main.txt +++ b/templates/PD/main.txt @@ -1,3 +1,4 @@ -This $medium:lower is dedicated entirely to the public domain. The -$creator:lower waives all intellectual property rights to the work as -much as is possible in any relevant jurisdictions. +This $medium:lower$ is dedicated entirely to the public domain. The +$creator:lower$ $author_verb:waives|waive$ all intellectual property +rights to the work as much as is possible in any relevant +jurisdictions. diff --git a/templates/liability.txt b/templates/liability.txt index e46996d..56e8716 100644 --- a/templates/liability.txt +++ b/templates/liability.txt @@ -1,7 +1,7 @@ -THE $type:caps IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL -WARRANTIES WITH REGARD TO THIS $type:caps INCLUDING ALL IMPLIED WARRANTIES +THE $type:caps$ IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL +WARRANTIES WITH REGARD TO THIS $type:caps$ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS $type:caps. +OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS $type:caps$. -- 2.43.4