raz / site / Build.raz

806 lines · 31.8 KiB · raw · history · last change de634aad

1(* Build: the website, plain files in site/public/, made from
2 site/content/ and the repository.
3
4 Build [vX.Y.Z=COMMIT ...]
5
6 Run from the repository root, by make site, with ./raz and the
7 report's PDFs (make pdf) built, and pandoc and pdfinfo on PATH.
8 site/releases records website distributions whose tags are not
9 pushed yet. Each additional vX.Y.Z=COMMIT is another such
10 distribution: it is offered as if the tag were at COMMIT, its
11 tarball made by make dist with VERSION, the same tarball the tag
12 will give.
13
14 The site is plain files: open public/index.html, or put the folder
15 on any web server. Nothing runs in the reader's browser; the code on
16 the tour's pages is run here, by ./raz, and the output shown is what
17 it printed, so a lesson that stops working fails the build. The
18 home page offers every release's tarball, made by make dist from
19 its tag.
20
21 Lessons are site/content/<tour>/NN-name.txt, one folder to a tour
22 (see Tours below):
23
24 title: Hello, world (the first lines are "key: value")
25 part: Raz (the lessons are grouped in parts)
26 summary: a line for the contents page
27 ---
28 Paragraphs, separated by blank lines. `code`, **bold**,
29 [links](url), "- " lists and pipe tables are understood, and
30 nothing else.
31
32 ```raz Name run
33 ```
34
35 A fenced block "raz Name" is a module, written as Name.raz in a
36 folder of its own for the lesson. "run" has the build run it and
37 show what it printed; "fail" runs it and shows the failure it ends
38 in. A block with nothing in it is taken from doc/raz-tutorial.7,
39 whose programs make check already runs, by the module's name.
40
41 Two other blocks: "```text" is shown as written, and "```sh" is a
42 shell session: each line that starts with "$ " is run in the
43 lesson's folder, with raz on the path, and shown with what it
44 printed.
45
46 It depends on Site, Releases, Markup and Sha256, and leaves out
47 running anything in the reader's browser. *)
48module Build;
49import Sys, Files, Buf, Str, Heap, Fmt, Site, Releases, Markup, Sha256;
50import Names := Vec([]const U8);
51import Lessons := Vec(Lesson);
52import Rels := Vec(Releases.Release);
53import Tars := Vec(Tarball);
54
55const
56 SiteUrl = "https://raz.lucela.org"; (* where the pages live, for the preview image *)
57 Usage = "usage: Build [vX.Y.Z=COMMIT ...]";
58 (* the head of every page: the arguments are the title (twice), the
59 site's address, then the way up to the root (six times), the
60 navigation, the body and the way up again *)
61 Page = `
62 <!doctype html>
63 <html lang="en">
64 <head>
65 <meta charset="utf-8">
66 <meta name="viewport" content="width=device-width, initial-scale=1">
67 <title>{}</title>
68 <meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux.">
69 <meta name="theme-color" content="#E07A5F">
70 <meta property="og:title" content="{}">
71 <meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report.">
72 <meta property="og:image" content="{}/social.png">
73 <meta name="twitter:card" content="summary_large_image">
74 <link rel="icon" href="{}favicon.svg" type="image/svg+xml">
75 <link rel="icon" href="{}favicon-32.png" type="image/png" sizes="32x32">
76 <link rel="apple-touch-icon" href="{}apple-touch-icon.png">
77 <link rel="stylesheet" href="{}style.css">
78 </head>
79 <body>
80 <header><nav><a class="mark" href="{}index.html"><img src="{}logo.svg" width="26" height="26" alt="">raz</a>{}</nav></header>
81 <main>
82 {}
83 </main>
84 <footer>raz is under the ISC license. <a href="{}source/index.html">Source</a></footer>
85 </body>
86 </html>
87 `;
88
89type
90 (* A tour: a folder of lessons. *)
91 Tour = record
92 folder: []const U8; (* in site/content/ *)
93 outDir: []const U8; (* in site/public/ *)
94 name: []const U8; (* shown on its pages *)
95 intro: []const U8; (* the file in site/content/ with the sentence under the name *)
96 end;
97
98 (* A lesson, as its file says. *)
99 Lesson = record
100 title: []const U8;
101 part: []const U8; (* the group it is listed in *)
102 summary: []const U8; (* its line on the contents page *)
103 slug: []const U8; (* its file name without number or .txt *)
104 body: []const U8; (* what follows the --- line *)
105 end;
106
107 (* A release's tarball: its version, file name and SHA-256 as hex. *)
108 Tarball = record
109 version: []const U8;
110 name: []const U8;
111 digest: []const U8;
112 end;
113
114 (* A fenced block of a lesson, as places in its text: the line of its
115 opening fence, its info line, its code and the end of its closing
116 fence. *)
117 Fence = record
118 start: INT;
119 infoLo, infoHi: INT;
120 codeLo, codeHi: INT;
121 stop: INT;
122 end;
123
124 (* A program's output: what it wrote, and the status it ended with. *)
125 Run = record
126 out, err: Buf.Buf;
127 status: INT;
128 end;
129
130const
131 Tours: [2]Tour = [
132 {folder: "tour", outDir: "tour", name: "Raz, Set, Go", intro: "tour-intro.html"},
133 {folder: "advanced", outDir: "advanced", name: "More Raz", intro: "advanced-intro.html"}];
134
135var
136 root: []const U8; (* the repository, as an absolute path *)
137 outDir: []const U8; (* site/public *)
138 raz: []const U8; (* the compiler, ./raz *)
139
140(* ---- the tutorial's programs -------------------------------------- *)
141
142(* Adds line as roff wrote it with the escapes undone: a backslash is
143 written \e, a backquote \(ga and a dot at a line's start \&. *)
144proc unescape(var b: Buf.Buf; line: []const U8);
145var a, c, d: Buf.Buf;
146begin
147 Buf.init(a, Heap.heap);
148 Buf.init(c, Heap.heap);
149 Buf.init(d, Heap.heap);
150 Str.replace(a, line, "\\&", "");
151 Str.replace(c, Buf.bytes(a), "\\(ga", "`");
152 Str.replace(d, Buf.bytes(c), "\\e", "\\");
153 Buf.str(b, Buf.bytes(d));
154 Buf.done(a);
155 Buf.done(c);
156end;
157
158(* Whether the block text has a line that starts "module name" and goes
159 on with something that isn't part of a word. *)
160proc declares(block, name: []const U8): BOOL;
161var all: [][]const U8; line: []const U8; head: []const U8; i: INT;
162begin
163 head := Site.cat(["module ", name]);
164 all := Site.lines(block);
165 for i := 0 to LEN(all) - 1 do
166 line := all[i];
167 if Str.hasPrefix(line, head) then
168 line := line[LEN(head)..LEN(line)];
169 if (LEN(line) = 0) or not (Str.isLetter(line[0]) or Str.isDigit(line[0]) or (line[0] = '_')) then
170 return true;
171 end;
172 end;
173 end;
174 return false;
175end;
176
177(* The first literal block of the manual page that declares module
178 name, as raz source; the build stops if there is none. *)
179proc tutorialModule(name: []const U8): []const U8;
180var all: [][]const U8; block: Buf.Buf; inside: BOOL; i, n: INT;
181begin
182 all := Site.lines(Site.read(Site.cat([root, "/doc/raz-tutorial.7"])));
183 for i := 0 to LEN(all) - 1 do
184 if Str.hasPrefix(all[i], ".Bd -literal") then
185 inside := true;
186 n := 0;
187 Buf.init(block, Heap.heap);
188 elsif inside and (all[i] = ".Ed") then
189 inside := false;
190 Buf.add(block, '\n');
191 if declares(Buf.bytes(block), name) then return Buf.bytes(block); end;
192 elsif inside then
193 if n > 0 then Buf.add(block, '\n'); end;
194 INC(n);
195 unescape(block, all[i]);
196 end;
197 end;
198 Site.die("no program {} in the tutorial", [Fmt.str(name)]);
199 return "";
200end;
201
202(* ---- running ------------------------------------------------------ *)
203
204(* Compiles and runs the module name in dir with raz run -Werror, and
205 says what it wrote and how it ended. *)
206proc runModule(dir, name: []const U8): Run;
207var r: Run;
208begin
209 Buf.init(r.out, Heap.heap);
210 Buf.init(r.err, Heap.heap);
211 r.status := Site.capture([raz, "run", "-Werror", Site.cat([name, ".raz"])], dir, [], false, r.out, r.err);
212 if r.status < 0 then Site.die("can't run {}", [Fmt.str(raz)]); end;
213 return r;
214end;
215
216(* Adds a shell session: each "$ command" line of script run in dir,
217 with raz on the path, and its output. *)
218proc shellHtml(var b: Buf.Buf; dir, script: []const U8);
219var
220 bin, shortcut: []const U8; env: [2][]const U8; all: [][]const U8; out, err: Buf.Buf;
221 text: []const U8; i: INT;
222begin
223 bin := Site.cat([dir, "/.bin"]);
224 Site.makeDirs(bin);
225 shortcut := Site.cat([bin, "/raz"]);
226 if not Site.exists(shortcut) then _ := Sys.symlink(raz, shortcut); end; (* raz finds lib/ by its real path *)
227 env[0] := Site.cat(["PATH=", bin, ":", Sys.env("PATH")]);
228 env[1] := Site.cat(["RAZLIB=", root, "/lib"]);
229 Buf.str(b, "<pre class=\"output term\"><code>");
230 all := Site.lines(script);
231 (* a final empty line is not a line *)
232 i := LEN(all);
233 while (i > 0) and (LEN(all[i - 1]) = 0) do DEC(i); end;
234 all := all[0..i];
235 for i := 0 to LEN(all) - 1 do
236 if i > 0 then Buf.add(b, '\n'); end;
237 if not Str.hasPrefix(all[i], "$ ") then
238 Markup.escape(b, all[i], true);
239 else
240 Buf.str(b, "<span class=\"cmd\">");
241 Markup.escape(b, all[i], true);
242 Buf.str(b, "</span>");
243 Buf.init(out, Heap.heap);
244 Buf.init(err, Heap.heap);
245 _ := Site.capture(["/bin/sh", "-c", all[i][2..LEN(all[i])]], dir, env, true, out, err);
246 text := Buf.bytes(out);
247 while (LEN(text) > 0) and (text[LEN(text) - 1] = '\n') do text := text[0..LEN(text) - 1]; end;
248 if LEN(text) > 0 then
249 Buf.add(b, '\n');
250 Markup.escape(b, text, true);
251 end;
252 end;
253 end;
254 Buf.str(b, "</code></pre>");
255end;
256
257(* Adds what a program wrote: its output, and its errors in a span. *)
258proc outputHtml(var b: Buf.Buf; const r: Run);
259begin
260 Buf.str(b, "<pre class=\"output\"><code>");
261 Markup.escape(b, Buf.bytes(r.out), true);
262 if r.err.len > 0 then
263 Buf.str(b, "<span class=\"err\">");
264 Markup.escape(b, Buf.bytes(r.err), true);
265 Buf.str(b, "</span>");
266 end;
267 Buf.str(b, "</code></pre>");
268end;
269
270(* ---- lessons ------------------------------------------------------ *)
271
272(* s without the line feeds at its end. *)
273proc chop(s: []const U8): []const U8;
274begin
275 while (LEN(s) > 0) and (s[LEN(s) - 1] = '\n') do s := s[0..LEN(s) - 1]; end;
276 return s;
277end;
278
279(* The lesson in the file at path. Its head is lines of "key: value"
280 up to a line of ---; the rest is its body. *)
281proc readLesson(path: []const U8): Lesson;
282var head, name: []const U8; all: [][]const U8; l: Lesson; key, value: []const U8; found: BOOL; i: INT;
283begin
284 head, l.body, found := Str.cut(Site.read(path), "\n---\n");
285 all := Site.lines(Str.trim(head));
286 for i := 0 to LEN(all) - 1 do
287 key, value, found := Str.cut(all[i], ": ");
288 if not found then Site.die("{}: not \"key: value\": {}", [Fmt.str(path), Fmt.str(all[i])]); end;
289 if key = "title" then l.title := value;
290 elsif key = "part" then l.part := value;
291 elsif key = "summary" then l.summary := value;
292 end;
293 end;
294 name := path[Str.findLast(path, "/") + 1..LEN(path)];
295 name := name[0..LEN(name) - 4]; (* without .txt *)
296 i := 0;
297 while (i < LEN(name)) and Str.isDigit(name[i]) do INC(i); end;
298 if (i > 0) and (i < LEN(name)) and (name[i] = '-') then name := name[i + 1..LEN(name)]; end;
299 l.slug := name;
300 return l;
301end;
302
303(* Finds the first fenced block of s that starts on a line after from (from the very start, if from is 0): a line of "```" and an info
304 line ("raz Name flags", "text" or "sh"), the code, and a line of only
305 "```". False if there is none. *)
306proc nextFence(s: []const U8; from: INT; var f: Fence): BOOL;
307var p, eol, q, next: INT; info: []const U8;
308begin
309 p := from;
310 if from > 0 then
311 p := Str.findFrom(s, "\n", from);
312 if p < 0 then return false; end;
313 INC(p);
314 end;
315 while p < LEN(s) do
316 eol := Str.findFrom(s, "\n", p);
317 if eol < 0 then return false; end;
318 if (eol - p >= 3) and Str.hasPrefix(s[p..eol], "```") then
319 info := s[p + 3..eol];
320 q := eol + 1;
321 while ((info = "text") or (info = "sh") or Str.hasPrefix(info, "raz ")) and (q < LEN(s)) do
322 if Str.hasPrefix(s[q..LEN(s)], "```") and ((q + 3 = LEN(s)) or (s[q + 3] = '\n')) then
323 f := {start: p, infoLo: p + 3, infoHi: eol, codeLo: eol + 1, codeHi: q, stop: q + 3};
324 return true;
325 end;
326 next := Str.findFrom(s, "\n", q);
327 if next < 0 then break; end;
328 q := next + 1;
329 end;
330 end;
331 p := eol + 1;
332 end;
333 return false;
334end;
335
336(* Starts a new part of the lesson in b: after a line feed, unless it is
337 the first. *)
338proc newPart(var b: Buf.Buf);
339begin
340 if b.len > 0 then Buf.add(b, '\n'); end;
341end;
342
343(* Adds a "raz Name flags" block of a lesson: its code, taken from the
344 tutorial if it has none, written to dir as Name.raz, and, if flags
345 say run or fail, the output of running it. *)
346proc razBlock(var b: Buf.Buf; const l: Lesson; dir, info, written: []const U8);
347var words: [][]const U8; name, source: []const U8; r: Run; run, fail: BOOL; i: INT;
348begin
349 words := Site.words(info);
350 if LEN(words) < 2 then Site.die("{}: a raz block needs a name", [Fmt.str(l.slug)]); end;
351 name := words[1];
352 source := written;
353 if LEN(Str.trim(source)) = 0 then source := tutorialModule(name); end;
354 Site.write(Site.cat([dir, "/", name, ".raz"]), source);
355 newPart(b);
356 Buf.str(b, "<div class=\"file\"><span>"); Buf.str(b, name); Buf.str(b, ".raz</span></div>");
357 Buf.str(b, "<pre class=\"code\"><code>");
358 Markup.highlight(b, chop(source));
359 Buf.str(b, "</code></pre>");
360 for i := 2 to LEN(words) - 1 do
361 run := run or (words[i] = "run");
362 fail := fail or (words[i] = "fail");
363 end;
364 if run or fail then
365 r := runModule(dir, name);
366 if fail = (r.status = 0) then
367 Site.die("{}: {} ended with status {}\n{}{}", [Fmt.str(l.slug), Fmt.str(name), Fmt.int(r.status),
368 Fmt.str(Buf.bytes(r.out)), Fmt.str(Buf.bytes(r.err))]);
369 end;
370 newPart(b);
371 outputHtml(b, r);
372 end;
373end;
374
375(* The lesson's page content, one part to a line; it runs its programs
376 in a folder of its own. *)
377proc lessonBody(const l: Lesson): []const U8;
378var b: Buf.Buf; dir, info: []const U8; f: Fence; pos, textEnd: INT; found: BOOL;
379begin
380 Buf.init(b, Heap.heap);
381 dir := Site.tempDir();
382 pos := 0;
383 repeat
384 found := nextFence(l.body, pos, f);
385 textEnd := LEN(l.body);
386 if found then textEnd := f.start; end;
387 if LEN(Str.trim(l.body[pos..textEnd])) > 0 then
388 newPart(b);
389 Markup.prose(b, l.body[pos..textEnd]);
390 end;
391 if found then
392 info := l.body[f.infoLo..f.infoHi];
393 if info = "text" then
394 newPart(b);
395 Buf.str(b, "<pre class=\"code\"><code>");
396 Markup.escape(b, chop(l.body[f.codeLo..f.codeHi]), true);
397 Buf.str(b, "</code></pre>");
398 elsif info = "sh" then
399 newPart(b);
400 shellHtml(b, dir, l.body[f.codeLo..f.codeHi]);
401 else
402 razBlock(b, l, dir, info, l.body[f.codeLo..f.codeHi]);
403 end;
404 pos := f.stop;
405 end;
406 until not found;
407 _ := Files.removeTree(dir);
408 return Buf.bytes(b);
409end;
410
411(* ---- pages -------------------------------------------------------- *)
412
413(* The way up from a page depth folders down: "../" depth times. *)
414proc wayUp(depth: INT): []const U8;
415var b: Buf.Buf; i: INT;
416begin
417 Buf.init(b, Heap.heap);
418 for i := 1 to depth do Buf.str(b, "../"); end;
419 return Buf.bytes(b);
420end;
421
422(* Adds a link of the navigation bar, marked if it is the current one. *)
423proc navLink(var b: Buf.Buf; up, key, current, href, label: []const U8);
424begin
425 Buf.str(b, "<a href=\""); Buf.str(b, up); Buf.str(b, href); Buf.str(b, "\"");
426 if key = current then Buf.str(b, " aria-current=\"page\""); end;
427 Buf.add(b, '>'); Buf.str(b, label); Buf.str(b, "</a>");
428end;
429
430(* A whole page: title, body, how many folders down it is and which
431 navigation entry it belongs to. *)
432proc page(title, body: []const U8; depth: INT; current: []const U8): []const U8;
433var b, nav: Buf.Buf; up, t: []const U8;
434begin
435 up := wayUp(depth);
436 t := Markup.escaped(title, true);
437 Buf.init(nav, Heap.heap);
438 navLink(nav, up, "tour", current, "tour/index.html", "Raz, Set, Go");
439 navLink(nav, up, "advanced", current, "advanced/index.html", "More Raz");
440 navLink(nav, up, "spec", current, "spec.html", "Report");
441 navLink(nav, up, "source", current, "source/index.html", "Source");
442 navLink(nav, up, "get", current, "index.html#download", "Download");
443 Buf.init(b, Heap.heap);
444 Fmt.write(b, Page, [Fmt.str(t), Fmt.str(t), Fmt.str(SiteUrl), Fmt.str(up), Fmt.str(up), Fmt.str(up),
445 Fmt.str(up), Fmt.str(up), Fmt.str(up), Fmt.str(Buf.bytes(nav)), Fmt.str(body), Fmt.str(up)]);
446 Buf.done(nav);
447 return Buf.bytes(b);
448end;
449
450(* Writes text to path under site/public. *)
451proc put(path, text: []const U8);
452begin
453 Site.write(Site.cat([outDir, "/", path]), text);
454end;
455
456(* The parts the lessons are grouped in, in order of first appearance. *)
457proc partsOf(lessons: []Lesson): [][]const U8;
458var v: Names.Vec; i, k: INT; seen: BOOL;
459begin
460 Names.init(v, Heap.heap);
461 for i := 0 to LEN(lessons) - 1 do
462 seen := false;
463 for k := 0 to v.len - 1 do seen := seen or (Names.all(v)[k] = lessons[i].part); end;
464 if not seen then Names.push(v, lessons[i].part); end;
465 end;
466 return Names.all(v);
467end;
468
469(* The number of the first lesson of part. *)
470proc firstOf(lessons: []Lesson; part: []const U8): INT;
471var i: INT;
472begin
473 for i := 0 to LEN(lessons) - 1 do
474 if lessons[i].part = part then return i + 1; end;
475 end;
476 return 0;
477end;
478
479(* Adds the side list of the lesson n: every part and its lessons, the
480 one on show marked. *)
481proc contents(var b: Buf.Buf; lessons: []Lesson; parts: [][]const U8; n: INT);
482var p, i: INT;
483begin
484 for p := 0 to LEN(parts) - 1 do
485 if p > 0 then Buf.add(b, '\n'); end;
486 Fmt.write(b, "<h3>{}</h3><ol start='{}'>", [Fmt.str(parts[p]), Fmt.int(firstOf(lessons, parts[p]))]);
487 for i := 0 to LEN(lessons) - 1 do
488 if lessons[i].part = parts[p] then
489 Buf.add(b, '\n');
490 Fmt.write(b, "<li><a href=\"{}.html\"", [Fmt.str(lessons[i].slug)]);
491 if i = n then Buf.str(b, " aria-current=\"page\""); end;
492 Fmt.write(b, ">{}</a></li>", [Fmt.str(Markup.escaped(lessons[i].title, true))]);
493 end;
494 end;
495 Buf.str(b, "\n</ol>");
496 end;
497end;
498
499(* Adds the links under lesson n: the one before, the contents, the one
500 after, or, past the last, next, an (address, label) pair. *)
501proc pager(var b: Buf.Buf; lessons: []Lesson; n: INT; nextHref, nextLabel: []const U8);
502begin
503 if n > 0 then
504 Fmt.write(b, "<a href=\"{}.html\">&larr; {}</a>",
505 [Fmt.str(lessons[n - 1].slug), Fmt.str(Markup.escaped(lessons[n - 1].title, true))]);
506 end;
507 Buf.str(b, "<a href=\"index.html\">Contents</a>");
508 if n + 1 < LEN(lessons) then
509 Fmt.write(b, "<a class=\"next\" href=\"{}.html\">{} &rarr;</a>",
510 [Fmt.str(lessons[n + 1].slug), Fmt.str(Markup.escaped(lessons[n + 1].title, true))]);
511 else
512 Fmt.write(b, "<a class=\"next\" href=\"{}\">{} &rarr;</a>", [Fmt.str(nextHref), Fmt.str(nextLabel)]);
513 end;
514end;
515
516(* Writes the page of lesson n of a tour. *)
517proc lessonPage(const t: Tour; lessons: []Lesson; parts: [][]const U8; n: INT; nextHref, nextLabel: []const U8);
518var b, toc, pg: Buf.Buf; l: Lesson;
519begin
520 l := lessons[n];
521 Buf.init(toc, Heap.heap);
522 Buf.init(pg, Heap.heap);
523 Buf.init(b, Heap.heap);
524 contents(toc, lessons, parts, n);
525 pager(pg, lessons, n, nextHref, nextLabel);
526 Fmt.write(b, "<div class=\"tour\"><aside><h2>{}</h2>{}</aside><article><p class=\"kicker\">{} &middot; lesson {} of {}</p><h1>{}</h1>\n",
527 [Fmt.str(t.name), Fmt.str(Buf.bytes(toc)), Fmt.str(l.part), Fmt.int(n + 1), Fmt.int(LEN(lessons)),
528 Fmt.str(Markup.escaped(l.title, true))]);
529 Buf.str(b, lessonBody(l));
530 Buf.str(b, "<p class=\"tryit\">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p>");
531 Fmt.write(b, "<div class=\"pager\">{}</div></article></div>", [Fmt.str(Buf.bytes(pg))]);
532 put(Site.cat([t.outDir, "/", l.slug, ".html"]),
533 page(Site.cat([l.title, " - ", t.name]), Buf.bytes(b), 1, t.folder));
534 Buf.done(toc);
535 Buf.done(pg);
536end;
537
538(* Writes a tour's contents page. *)
539proc contentsPage(const t: Tour; lessons: []Lesson; parts: [][]const U8);
540var b: Buf.Buf; p, i: INT;
541begin
542 Buf.init(b, Heap.heap);
543 Buf.str(b, "<div class=\"contents\">");
544 Buf.str(b, Site.read(Site.cat([root, "/site/content/", t.intro])));
545 for p := 0 to LEN(parts) - 1 do
546 if p > 0 then Buf.add(b, '\n'); end;
547 Fmt.write(b, "<h2>{}</h2><ol start='{}'>", [Fmt.str(parts[p]), Fmt.int(firstOf(lessons, parts[p]))]);
548 for i := 0 to LEN(lessons) - 1 do
549 if lessons[i].part = parts[p] then
550 Fmt.write(b, "\n<li><a href=\"{}.html\">{}</a> <span>{}</span></li>",
551 [Fmt.str(lessons[i].slug), Fmt.str(Markup.escaped(lessons[i].title, true)),
552 Fmt.str(Markup.escaped(lessons[i].summary, true))]);
553 end;
554 end;
555 Buf.str(b, "\n</ol>");
556 end;
557 Buf.str(b, "</div>");
558 put(Site.cat([t.outDir, "/index.html"]), page(t.name, Buf.bytes(b), 1, t.folder));
559end;
560
561(* Writes a tour's pages: a page to each lesson and a contents page.
562 nextHref and nextLabel say what follows its last lesson. *)
563proc buildTour(const t: Tour; lessons: []Lesson; nextHref, nextLabel: []const U8);
564var parts: [][]const U8; n: INT;
565begin
566 parts := partsOf(lessons);
567 for n := 0 to LEN(lessons) - 1 do lessonPage(t, lessons, parts, n, nextHref, nextLabel); end;
568 contentsPage(t, lessons, parts);
569end;
570
571(* Whether name is a lesson's file: digits, a dash, anything, .txt *)
572proc isLesson(name: []const U8): BOOL;
573var i: INT;
574begin
575 while (i < LEN(name)) and Str.isDigit(name[i]) do INC(i); end;
576 return (i > 0) and (i < LEN(name)) and (name[i] = '-') and Str.hasSuffix(name, ".txt");
577end;
578
579(* The lessons of a tour's folder, in the order of their file names. *)
580proc readLessons(const t: Tour): []Lesson;
581var v: Lessons.Vec; names: [][]const U8; dir: []const U8; i: INT;
582begin
583 Lessons.init(v, Heap.heap);
584 dir := Site.cat([root, "/site/content/", t.folder]);
585 names := Site.dirNames(dir);
586 for i := 0 to LEN(names) - 1 do
587 if isLesson(names[i]) then Lessons.push(v, readLesson(Site.cat([dir, "/", names[i]]))); end;
588 end;
589 return Lessons.all(v);
590end;
591
592(* Writes the report's page, which pandoc makes of the two volumes. *)
593proc buildSpec;
594var out, body: Buf.Buf; status: INT;
595begin
596 Buf.init(out, Heap.heap);
597 (* --columns is wide enough that no row of a table is "long": pandoc
598 would otherwise give each column an equal share of the width *)
599 status := Site.captureOut(["pandoc", Site.cat([root, "/doc/raz.md"]), Site.cat([root, "/doc/raz-lib.md"]),
600 "-s", "--template", Site.cat([root, "/site/content/spec.tmpl"]), "--toc", "--toc-depth=1",
601 "--shift-heading-level-by=-1", "--no-highlight", "-t", "html", "--columns=10000"], "", [], out);
602 if status = Sys.errNoEntry then Site.die("pandoc makes the report's page: apt-get install pandoc", []); end;
603 if status # 0 then Site.die("pandoc failed", []); end;
604 Buf.init(body, Heap.heap);
605 Buf.str(body, "<div class=\"spec\">");
606 Buf.str(body, Buf.bytes(out));
607 Buf.str(body, "</div>");
608 put("spec.html", page("The raz report", Buf.bytes(body), 0, "spec"));
609end;
610
611(* ---- releases ----------------------------------------------------- *)
612
613(* The SHA-256 of the file at path, as hex. *)
614proc digestOf(path: []const U8): []const U8;
615var b: Buf.Buf;
616begin
617 Buf.init(b, Heap.heap);
618 Sha256.hex(Site.read(path), b);
619 return Buf.bytes(b);
620end;
621
622(* Makes the tarball of one release, if public/download/ lacks it, and
623 its checksum file: a release's tarball is made of its tag by make
624 dist, not of the tree at hand, and one already there is kept as it
625 is, never made again. Those before 0.14.0 were made before make dist
626 made the same tarball each time, so a new one would differ, and so
627 its SHA-256, which a program's CI may check. *)
628proc tarballOf(const r: Releases.Release): Tarball;
629var t: Tarball; made, checksum: []const U8; expected: [][]const U8; v: []const U8;
630begin
631 v := r.tag[1..LEN(r.tag)];
632 t.version := v;
633 t.name := Site.cat(["raz-", v, "-linux-x86_64.tar.gz"]);
634 made := Site.cat([outDir, "/download/", t.name]);
635 if not Site.exists(made) then
636 if Site.runQuiet(["make", "-s", "-C", root, "dist", Site.cat(["REV=", r.commit]), Site.cat(["VERSION=", v])]) # 0 then
637 Site.die("make dist failed for {}", [Fmt.str(r.tag)]);
638 end;
639 Site.copy(Site.cat([root, "/build/", t.name]), made);
640 end;
641 t.digest := digestOf(made);
642 checksum := Site.cat([made, ".sha256"]);
643 if Site.exists(checksum) then
644 expected := Site.words(Site.read(checksum));
645 if (LEN(expected) = 0) or (expected[0] # t.digest) then
646 Site.die("release archive does not match its published checksum: {}", [Fmt.str(made)]);
647 end;
648 end;
649 put(Site.cat(["download/", t.name, ".sha256"]), Site.cat([t.digest, " ", t.name, "\n"]));
650 return t;
651end;
652
653(* Every release's tarball from v0.7.1 on, in public/download/ with its
654 SHA-256, and /download/latest leading to the newest; oldest first. A
655 program's CI can fetch the version it was written for by its name,
656 so none is ever taken away. *)
657proc buildDownloads(extra: []const Releases.Release): []Tarball;
658var all: []Releases.Release; why: []const U8; v: Tars.Vec; i: INT;
659begin
660 all, why := Releases.refs(root, extra);
661 if LEN(why) > 0 then Site.die("{}", [Fmt.str(why)]); end;
662 Tars.init(v, Heap.heap);
663 for i := 0 to LEN(all) - 1 do Tars.push(v, tarballOf(all[i])); end;
664 (* Cloudflare reads _redirects from the root of what it serves *)
665 put("_redirects", Site.cat(["/download/latest /download/", Tars.all(v)[v.len - 1].name, " 302\n"]));
666 return Tars.all(v);
667end;
668
669(* The number of pages of the PDF at path, as pdfinfo says. *)
670proc pageCount(path: []const U8): []const U8;
671var out: Buf.Buf; rest, text: []const U8; at: INT;
672begin
673 Buf.init(out, Heap.heap);
674 if Site.captureOut(["pdfinfo", path], "", [], out) # 0 then Site.die("pdfinfo failed", []); end;
675 at := Str.find(Buf.bytes(out), "Pages:");
676 if at < 0 then Site.die("pdfinfo says no page count", []); end;
677 text := Buf.bytes(out);
678 rest := Str.trimLeft(text[at + 6..LEN(text)]);
679 at := 0;
680 while (at < LEN(rest)) and Str.isDigit(rest[at]) do INC(at); end;
681 return rest[0..at];
682end;
683
684(* Replaces each place in text, in b, where old stands by new. *)
685proc replaceIn(var text: Buf.Buf; old, new: []const U8);
686var b: Buf.Buf;
687begin
688 Buf.init(b, Heap.heap);
689 Str.replace(b, Buf.bytes(text), old, new);
690 Buf.done(text);
691 text := b;
692end;
693
694(* Writes the home page: home.html with the example's text and output,
695 the page count and the downloads filled in. *)
696proc buildHome(releases: []Tarball);
697var
698 dir: []const U8; r: Run; text, snippetHtml, earlier: Buf.Buf; snippet: []const U8; last: Tarball; i: INT;
699begin
700 snippet := Site.read(Site.cat([root, "/site/content/home.raz"]));
701 dir := Site.tempDir();
702 Site.write(Site.cat([dir, "/Taste.raz"]), snippet);
703 r := runModule(dir, "Taste");
704 if r.status # 0 then Site.die("Taste failed:\n{}", [Fmt.str(Buf.bytes(r.err))]); end;
705 _ := Files.removeTree(dir);
706 Buf.init(text, Heap.heap);
707 Buf.str(text, Site.read(Site.cat([root, "/site/content/home.html"])));
708 Buf.init(snippetHtml, Heap.heap);
709 Markup.highlight(snippetHtml, chop(snippet));
710 replaceIn(text, "@SNIPPET@", Buf.bytes(snippetHtml));
711 replaceIn(text, "@OUTPUT@", Markup.escaped(chop(Buf.bytes(r.out)), true));
712 last := releases[LEN(releases) - 1];
713 Buf.init(earlier, Heap.heap);
714 for i := LEN(releases) - 2 to 0 by -1 do
715 if earlier.len > 0 then Buf.str(earlier, ", "); end;
716 Fmt.write(earlier, "<a href=\"download/{}\">{}</a>", [Fmt.str(releases[i].name), Fmt.str(releases[i].version)]);
717 end;
718 replaceIn(text, "@EARLIER@", Buf.bytes(earlier));
719 replaceIn(text, "@PAGES@", pageCount(Site.cat([root, "/build/raz.pdf"])));
720 replaceIn(text, "@VERSION@", last.version);
721 replaceIn(text, "@TARBALL@", last.name);
722 replaceIn(text, "@DIR@", last.name[0..LEN(last.name) - LEN(".tar.gz")]);
723 replaceIn(text, "@SHA256@", last.digest);
724 put("index.html", page("raz: a small systems language", Buf.bytes(text), 0, "home"));
725end;
726
727(* ---- the whole site ------------------------------------------------ *)
728
729(* Removes everything in public/ but the tarballs in download/, which
730 are never made again. *)
731proc clean;
732var names: [][]const U8; path: []const U8; i: INT;
733begin
734 if not Site.isDir(outDir) then return; end;
735 names := Site.dirNames(outDir);
736 for i := 0 to LEN(names) - 1 do
737 path := Site.cat([outDir, "/", names[i]]);
738 if names[i] = "download" then
739 elsif Site.isDir(path) then _ := Files.removeTree(path);
740 else _ := Sys.remove(path);
741 end;
742 end;
743end;
744
745(* Copies the style sheet, the logo and the icons into public/. *)
746proc copyAssets;
747var names: [][]const U8; i: INT;
748begin
749 Site.copy(Site.cat([root, "/site/style.css"]), Site.cat([outDir, "/style.css"]));
750 names := Site.dirNames(Site.cat([root, "/site/assets"]));
751 for i := 0 to LEN(names) - 1 do
752 if (names[i] # "social.html") and (names[i] # "render.sh") then
753 Site.copy(Site.cat([root, "/site/assets/", names[i]]), Site.cat([outDir, "/", names[i]]));
754 end;
755 end;
756end;
757
758(* The vX.Y.Z=COMMIT arguments, as releases; the build stops at a
759 mistake. *)
760proc extraReleases(): []Releases.Release;
761var v: Rels.Vec; tag, commit: []const U8; found, ok: BOOL; i: INT;
762begin
763 Rels.init(v, Heap.heap);
764 for i := 1 to Files.argCount() - 1 do
765 tag, commit, found := Str.cut(Files.arg(i), "=");
766 _, _, _, ok := Releases.version(tag);
767 if not found or not ok then Site.die(Usage, []); end;
768 Rels.push(v, {tag: tag, commit: commit});
769 end;
770 return Rels.all(v);
771end;
772
773proc main*;
774var extra: []Releases.Release; why: []const U8; err: INT; i: INT; counts: Buf.Buf; lessons: []Lesson; downloads: []Tarball;
775begin
776 root, err := Sys.workDir();
777 if err < 0 then Site.die("no working directory", []); end;
778 root := Site.cat([root]);
779 outDir := Site.cat([root, "/site/public"]);
780 raz := Site.cat([root, "/raz"]);
781 if not Site.exists(raz) then Site.die("make raz first", []); end;
782 extra := extraReleases();
783 _, why := Releases.refs(root, extra); (* validate the release records before removing old pages *)
784 if LEN(why) > 0 then Site.die("{}", [Fmt.str(why)]); end;
785 clean;
786 Site.makeDirs(Site.cat([outDir, "/download"]));
787 copyAssets;
788 Buf.init(counts, Heap.heap);
789 for i := 0 to LEN(Tours) - 1 do
790 lessons := readLessons(Tours[i]);
791 if i + 1 < LEN(Tours) then
792 buildTour(Tours[i], lessons, Site.cat(["../", Tours[i + 1].outDir, "/index.html"]), Tours[i + 1].name);
793 else
794 buildTour(Tours[i], lessons, "../spec.html", "The report");
795 end;
796 if i > 0 then Buf.str(counts, " + "); end;
797 Fmt.write(counts, "{}", [Fmt.int(LEN(lessons))]);
798 end;
799 buildSpec;
800 Site.copy(Site.cat([root, "/build/raz.pdf"]), Site.cat([outDir, "/raz.pdf"])); (* made by make pdf *)
801 Site.copy(Site.cat([root, "/build/raz-lib.pdf"]), Site.cat([outDir, "/raz-lib.pdf"]));
802 downloads := buildDownloads(extra);
803 buildHome(downloads);
804 Fmt.print("site/public: {} lessons\n", [Fmt.str(Buf.bytes(counts))]);
805end;
806end Build.