~aleteoryx/tpl

ref: c61c78153d0880d94486137deb2ce83a09009860 tpl/index.html -rw-r--r-- 6.3 KiB
c61c7815Aleteoryx allow multiple mountpoints 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!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>