Commit | Line | Data |
---|---|---|
ccadafdc | 1 | ;;; GNU Guix --- Functional package management for GNU |
f9e0488c | 2 | ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
ccadafdc LC |
3 | ;;; |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
6 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
7 | ;;; under the terms of the GNU General Public License as published by | |
8 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
9 | ;;; your option) any later version. | |
10 | ;;; | |
11 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
12 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ;;; GNU General Public License for more details. | |
15 | ;;; | |
16 | ;;; You should have received a copy of the GNU General Public License | |
17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
18 | ||
19 | ||
20 | ;; This file contains machinery to build HTML and PDF copies of the manual | |
21 | ;; that can be readily published on the web site. To do that, run: | |
22 | ;; | |
23 | ;; guix build -f build.scm | |
24 | ;; | |
25 | ;; The result is a directory hierarchy that can be used as the manual/ | |
26 | ;; sub-directory of the web site. | |
27 | ||
28 | (use-modules (guix) | |
29 | (guix gexp) | |
30 | (guix git) | |
31 | (guix git-download) | |
7854bbeb | 32 | (guix utils) |
ccadafdc LC |
33 | (git) |
34 | (gnu packages base) | |
35 | (gnu packages gawk) | |
36 | (gnu packages gettext) | |
37 | (gnu packages guile) | |
f8c143a7 | 38 | (gnu packages guile-xyz) |
e591541d | 39 | (gnu packages iso-codes) |
ccadafdc LC |
40 | (gnu packages texinfo) |
41 | (gnu packages tex) | |
42 | (srfi srfi-19) | |
43 | (srfi srfi-71)) | |
44 | ||
45 | (define file-append* | |
46 | (@@ (guix self) file-append*)) | |
47 | ||
48 | (define translated-texi-manuals | |
49 | (@@ (guix self) translate-texi-manuals)) | |
50 | ||
51 | (define info-manual | |
52 | (@@ (guix self) info-manual)) | |
53 | ||
cacb5576 LC |
54 | (define %manual |
55 | ;; The manual to build--i.e., the base name of a .texi file, such as "guix" | |
56 | ;; or "guix-cookbook". | |
57 | (or (getenv "GUIX_MANUAL") | |
58 | "guix")) | |
59 | ||
ccadafdc LC |
60 | (define %languages |
61 | '("de" "en" "es" "fr" "ru" "zh_CN")) | |
62 | ||
63 | (define (texinfo-manual-images source) | |
64 | "Return a directory containing all the images used by the user manual, taken | |
65 | from SOURCE, the root of the source tree." | |
66 | (define graphviz | |
67 | (module-ref (resolve-interface '(gnu packages graphviz)) | |
68 | 'graphviz)) | |
69 | ||
70 | (define images | |
71 | (file-append* source "doc/images")) | |
72 | ||
73 | (define build | |
74 | (with-imported-modules '((guix build utils)) | |
75 | #~(begin | |
76 | (use-modules (guix build utils) | |
77 | (srfi srfi-26)) | |
78 | ||
79 | (define (dot->image dot-file format) | |
80 | (invoke #+(file-append graphviz "/bin/dot") | |
81 | "-T" format "-Gratio=.9" "-Gnodesep=.005" | |
82 | "-Granksep=.00005" "-Nfontsize=9" | |
83 | "-Nheight=.1" "-Nwidth=.1" | |
84 | "-o" (string-append #$output "/" | |
85 | (basename dot-file ".dot") | |
86 | "." format) | |
87 | dot-file)) | |
88 | ||
89 | ;; Build graphs. | |
90 | (mkdir-p #$output) | |
91 | (for-each (lambda (dot-file) | |
92 | (for-each (cut dot->image dot-file <>) | |
93 | '("png" "pdf"))) | |
94 | (find-files #$images "\\.dot$")) | |
95 | ||
96 | ;; Copy other PNGs. | |
97 | (for-each (lambda (png-file) | |
98 | (install-file png-file #$output)) | |
99 | (find-files #$images "\\.png$"))))) | |
100 | ||
101 | (computed-file "texinfo-manual-images" build)) | |
102 | ||
103 | (define* (texinfo-manual-source source #:key | |
104 | (version "0.0") | |
105 | (languages %languages) | |
106 | (date 1)) | |
107 | "Gather all the source files of the Texinfo manuals from SOURCE--.texi file | |
108 | as well as images, OS examples, and translations." | |
109 | (define documentation | |
110 | (file-append* source "doc")) | |
111 | ||
112 | (define examples | |
113 | (file-append* source "gnu/system/examples")) | |
114 | ||
115 | (define build | |
116 | (with-imported-modules '((guix build utils)) | |
117 | #~(begin | |
118 | (use-modules (guix build utils) | |
119 | (srfi srfi-19)) | |
120 | ||
121 | (define (make-version-texi language) | |
122 | ;; Create the 'version.texi' file for LANGUAGE. | |
123 | (let ((file (if (string=? language "en") | |
124 | "version.texi" | |
125 | (string-append "version-" language ".texi")))) | |
126 | (call-with-output-file (string-append #$output "/" file) | |
127 | (lambda (port) | |
128 | (let* ((version #$version) | |
129 | (time (make-time time-utc 0 #$date)) | |
130 | (date (time-utc->date time))) | |
131 | (format port " | |
132 | @set UPDATED ~a | |
133 | @set UPDATED-MONTH ~a | |
134 | @set EDITION ~a | |
135 | @set VERSION ~a\n" | |
136 | (date->string date "~e ~B ~Y") | |
137 | (date->string date "~B ~Y") | |
138 | version version)))))) | |
139 | ||
140 | (install-file #$(file-append* documentation "/htmlxref.cnf") | |
141 | #$output) | |
142 | ||
143 | (for-each (lambda (texi) | |
144 | (install-file texi #$output)) | |
145 | (append (find-files #$documentation "\\.(texi|scm)$") | |
146 | (find-files #$(translated-texi-manuals source) | |
147 | "\\.texi$"))) | |
148 | ||
149 | ;; Create 'version.texi'. | |
150 | (for-each make-version-texi '#$languages) | |
151 | ||
152 | ;; Copy configuration templates that the manual includes. | |
153 | (for-each (lambda (template) | |
154 | (copy-file template | |
155 | (string-append | |
156 | #$output "/os-config-" | |
157 | (basename template ".tmpl") | |
158 | ".texi"))) | |
159 | (find-files #$examples "\\.tmpl$")) | |
160 | ||
161 | (symlink #$(texinfo-manual-images source) | |
162 | (string-append #$output "/images"))))) | |
163 | ||
164 | (computed-file "texinfo-manual-source" build)) | |
165 | ||
166 | (define %web-site-url | |
167 | ;; URL of the web site home page. | |
168 | (or (getenv "GUIX_WEB_SITE_URL") | |
169 | "/software/guix/")) | |
170 | ||
171 | (define %makeinfo-html-options | |
172 | ;; Options passed to 'makeinfo --html'. | |
2f000f2e JL |
173 | '("--css-ref=https://www.gnu.org/software/gnulib/manual.css" |
174 | "-c" "EXTRA_HEAD=<meta name=\"viewport\" \ | |
175 | content=\"width=device-width, initial-scale=1\" />")) | |
ccadafdc | 176 | |
7854bbeb LC |
177 | (define guile-lib/htmlprag-fixed |
178 | ;; Guile-Lib with a hotfix for (htmlprag). | |
179 | (package | |
180 | (inherit guile-lib) | |
181 | (source (origin | |
182 | (inherit (package-source guile-lib)) | |
183 | (modules '(( guix build utils))) | |
184 | (snippet | |
185 | '(begin | |
186 | ;; When parsing | |
187 | ;; "<body><blockquote><p>foo</p>\n</blockquote></body>", | |
188 | ;; 'html->shtml' would mistakenly close 'blockquote' right | |
189 | ;; before <p>. This patch removes 'p' from the | |
190 | ;; 'parent-constraints' alist to fix that. | |
191 | (substitute* "src/htmlprag.scm" | |
192 | (("^[[:blank:]]*\\(p[[:blank:]]+\\. \\(body td th\\)\\).*") | |
193 | "")) | |
194 | #t)))) | |
195 | (arguments | |
196 | (substitute-keyword-arguments (package-arguments guile-lib) | |
197 | ((#:phases phases '%standard-phases) | |
198 | `(modify-phases ,phases | |
199 | (add-before 'check 'skip-known-failure | |
200 | (lambda _ | |
201 | ;; XXX: The above change causes one test failure among | |
202 | ;; the htmlprag tests. | |
203 | (setenv "XFAIL_TESTS" "htmlprag.scm") | |
204 | #t)))))))) | |
205 | ||
f8c143a7 LC |
206 | (define* (syntax-highlighted-html input |
207 | #:key | |
208 | (name "highlighted-syntax") | |
209 | (syntax-css-url | |
210 | "/static/base/css/code.css")) | |
211 | "Return a derivation called NAME that processes all the HTML files in INPUT | |
212 | to (1) add them a link to SYNTAX-CSS-URL, and (2) highlight the syntax of all | |
213 | its <pre class=\"lisp\"> blocks (as produced by 'makeinfo --html')." | |
214 | (define build | |
7854bbeb | 215 | (with-extensions (list guile-lib/htmlprag-fixed guile-syntax-highlight) |
f8c143a7 LC |
216 | (with-imported-modules '((guix build utils)) |
217 | #~(begin | |
218 | (use-modules (htmlprag) | |
219 | (syntax-highlight) | |
220 | (syntax-highlight scheme) | |
221 | (syntax-highlight lexers) | |
222 | (guix build utils) | |
223 | (ice-9 match) | |
224 | (ice-9 threads)) | |
225 | ||
012c93e9 LC |
226 | (define (pair-open/close lst) |
227 | ;; Pair 'open' and 'close' tags produced by 'highlights' and | |
228 | ;; produce nested 'paren' tags instead. | |
229 | (let loop ((lst lst) | |
230 | (level 0) | |
231 | (result '())) | |
232 | (match lst | |
233 | ((('open open) rest ...) | |
234 | (call-with-values | |
235 | (lambda () | |
236 | (loop rest (+ 1 level) '())) | |
237 | (lambda (inner close rest) | |
238 | (loop rest level | |
239 | (cons `(paren ,level ,open ,inner ,close) | |
240 | result))))) | |
241 | ((('close str) rest ...) | |
242 | (if (> level 0) | |
243 | (values (reverse result) str rest) | |
244 | (begin | |
245 | (format (current-error-port) | |
246 | "warning: extra closing paren; context:~% ~y~%" | |
247 | (reverse result)) | |
248 | (loop rest 0 (cons `(close ,str) result))))) | |
249 | ((item rest ...) | |
250 | (loop rest level (cons item result))) | |
251 | (() | |
252 | (when (> level 0) | |
253 | (format (current-error-port) | |
254 | "warning: missing ~a closing parens; context:~% ~y%" | |
255 | level (reverse result))) | |
256 | (values (reverse result) "" '()))))) | |
257 | ||
258 | (define (highlights->sxml* highlights) | |
259 | ;; Like 'highlights->sxml', but handle nested 'paren tags. This | |
260 | ;; allows for paren matching highlights via appropriate CSS | |
261 | ;; "hover" properties. | |
262 | (define (tag->class tag) | |
263 | (string-append "syntax-" (symbol->string tag))) | |
264 | ||
265 | (map (match-lambda | |
266 | ((? string? str) str) | |
267 | (('paren level open (body ...) close) | |
268 | `(span (@ (class ,(string-append "syntax-paren" | |
269 | (number->string level)))) | |
270 | ,open | |
271 | (span (@ (class "syntax-symbol")) | |
272 | ,@(highlights->sxml* body)) | |
273 | ,close)) | |
274 | ((tag text) | |
275 | `(span (@ (class ,(tag->class tag))) ,text))) | |
276 | highlights)) | |
277 | ||
f8c143a7 LC |
278 | (define entity->string |
279 | (match-lambda | |
280 | ("rArr" "⇒") | |
295c6a7e | 281 | ("rarr" "→") |
f8c143a7 LC |
282 | ("hellip" "…") |
283 | ("rsquo" "’") | |
284 | (e (pk 'unknown-entity e) (primitive-exit 2)))) | |
285 | ||
286 | (define (concatenate-snippets pieces) | |
287 | ;; Concatenate PIECES, which contains strings and entities, | |
288 | ;; replacing entities with their corresponding string. | |
289 | (let loop ((pieces pieces) | |
290 | (strings '())) | |
291 | (match pieces | |
292 | (() | |
293 | (string-concatenate-reverse strings)) | |
294 | (((? string? str) . rest) | |
295 | (loop rest (cons str strings))) | |
296 | ((('*ENTITY* "additional" entity) . rest) | |
297 | (loop rest (cons (entity->string entity) strings))) | |
298 | ((('span _ lst ...) . rest) ;for <span class="roman"> | |
299 | (loop (append lst rest) strings)) | |
300 | (something | |
301 | (pk 'unsupported-code-snippet something) | |
302 | (primitive-exit 1))))) | |
303 | ||
304 | (define (syntax-highlight sxml) | |
305 | ;; Recurse over SXML and syntax-highlight code snippets. | |
306 | (match sxml | |
307 | (('*TOP* decl body ...) | |
308 | `(*TOP* ,decl ,@(map syntax-highlight body))) | |
309 | (('head things ...) | |
310 | `(head ,@things | |
311 | (link (@ (rel "stylesheet") | |
312 | (type "text/css") | |
313 | (href #$syntax-css-url))))) | |
314 | (('pre ('@ ('class "lisp")) code-snippet ...) | |
315 | `(pre (@ (class "lisp")) | |
012c93e9 LC |
316 | ,@(highlights->sxml* |
317 | (pair-open/close | |
318 | (highlight lex-scheme | |
319 | (concatenate-snippets code-snippet)))))) | |
f8c143a7 LC |
320 | ((tag ('@ attributes ...) body ...) |
321 | `(,tag (@ ,@attributes) ,@(map syntax-highlight body))) | |
322 | ((tag body ...) | |
323 | `(,tag ,@(map syntax-highlight body))) | |
324 | ((? string? str) | |
325 | str))) | |
326 | ||
327 | (define (process-html file) | |
328 | ;; Parse FILE and perform syntax highlighting for its Scheme | |
329 | ;; snippets. Install the result to #$output. | |
330 | (format (current-error-port) "processing ~a...~%" file) | |
331 | (let* ((shtml (call-with-input-file file html->shtml)) | |
332 | (highlighted (syntax-highlight shtml)) | |
333 | (base (string-drop file (string-length #$input))) | |
334 | (target (string-append #$output base))) | |
335 | (mkdir-p (dirname target)) | |
336 | (call-with-output-file target | |
337 | (lambda (port) | |
338 | (write-shtml-as-html highlighted port))))) | |
339 | ||
340 | (define (copy-as-is file) | |
341 | ;; Copy FILE as is to #$output. | |
342 | (let* ((base (string-drop file (string-length #$input))) | |
343 | (target (string-append #$output base))) | |
344 | (mkdir-p (dirname target)) | |
345 | (catch 'system-error | |
346 | (lambda () | |
347 | (if (eq? 'symlink (stat:type (lstat file))) | |
348 | (symlink (readlink file) target) | |
349 | (link file target))) | |
350 | (lambda args | |
351 | (let ((errno (system-error-errno args))) | |
352 | (pk 'error-link file target (strerror errno)) | |
353 | (primitive-exit 3)))))) | |
354 | ||
355 | ;; Install a UTF-8 locale so we can process UTF-8 files. | |
356 | (setenv "GUIX_LOCPATH" | |
357 | #+(file-append glibc-utf8-locales "/lib/locale")) | |
358 | (setlocale LC_ALL "en_US.utf8") | |
359 | ||
360 | (n-par-for-each (parallel-job-count) | |
361 | (lambda (file) | |
362 | (if (string-suffix? ".html" file) | |
363 | (process-html file) | |
364 | (copy-as-is file))) | |
365 | (find-files #$input)))))) | |
366 | ||
367 | (computed-file name build)) | |
368 | ||
ccadafdc LC |
369 | (define* (html-manual source #:key (languages %languages) |
370 | (version "0.0") | |
cacb5576 | 371 | (manual %manual) |
ccadafdc LC |
372 | (date 1) |
373 | (options %makeinfo-html-options)) | |
374 | "Return the HTML manuals built from SOURCE for all LANGUAGES, with the given | |
375 | makeinfo OPTIONS." | |
376 | (define manual-source | |
377 | (texinfo-manual-source source | |
378 | #:version version | |
379 | #:languages languages | |
380 | #:date date)) | |
381 | ||
e3e9c191 LC |
382 | (define images |
383 | (texinfo-manual-images source)) | |
384 | ||
ccadafdc LC |
385 | (define build |
386 | (with-imported-modules '((guix build utils)) | |
387 | #~(begin | |
388 | (use-modules (guix build utils) | |
389 | (ice-9 match)) | |
390 | ||
391 | (define (normalize language) | |
e591541d | 392 | ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn". |
ccadafdc LC |
393 | (string-map (match-lambda |
394 | (#\_ #\-) | |
395 | (chr chr)) | |
396 | (string-downcase language))) | |
397 | ||
cacb5576 LC |
398 | (define (language->texi-file-name language) |
399 | (if (string=? language "en") | |
400 | (string-append #$manual-source "/" | |
401 | #$manual ".texi") | |
402 | (string-append #$manual-source "/" | |
403 | #$manual "." language ".texi"))) | |
404 | ||
ccadafdc LC |
405 | ;; Install a UTF-8 locale so that 'makeinfo' is at ease. |
406 | (setenv "GUIX_LOCPATH" | |
407 | #+(file-append glibc-utf8-locales "/lib/locale")) | |
408 | (setenv "LC_ALL" "en_US.utf8") | |
409 | ||
410 | (setvbuf (current-output-port) 'line) | |
411 | (setvbuf (current-error-port) 'line) | |
412 | ||
f9e0488c LC |
413 | ;; 'makeinfo' looks for "htmlxref.cnf" in the current directory, so |
414 | ;; copy it right here. | |
415 | (copy-file (string-append #$manual-source "/htmlxref.cnf") | |
416 | "htmlxref.cnf") | |
417 | ||
ccadafdc | 418 | (for-each (lambda (language) |
cacb5576 LC |
419 | (let* ((texi (language->texi-file-name language)) |
420 | (opts `("--html" | |
421 | "-c" ,(string-append "TOP_NODE_UP_URL=/manual/" | |
ccadafdc | 422 | language) |
cacb5576 LC |
423 | #$@options |
424 | ,texi))) | |
ccadafdc LC |
425 | (format #t "building HTML manual for language '~a'...~%" |
426 | language) | |
427 | (mkdir-p (string-append #$output "/" | |
428 | (normalize language))) | |
429 | (setenv "LANGUAGE" language) | |
430 | (apply invoke #$(file-append texinfo "/bin/makeinfo") | |
431 | "-o" (string-append #$output "/" | |
432 | (normalize language) | |
433 | "/html_node") | |
434 | opts) | |
435 | (apply invoke #$(file-append texinfo "/bin/makeinfo") | |
436 | "--no-split" | |
437 | "-o" | |
438 | (string-append #$output "/" | |
439 | (normalize language) | |
440 | "/" #$manual | |
441 | (if (string=? language "en") | |
442 | "" | |
443 | (string-append "." language)) | |
444 | ".html") | |
e3e9c191 LC |
445 | opts) |
446 | ||
447 | ;; Make sure images are available. | |
448 | (symlink #$images | |
449 | (string-append #$output "/" (normalize language) | |
450 | "/images")) | |
451 | (symlink #$images | |
452 | (string-append #$output "/" (normalize language) | |
453 | "/html_node/images")))) | |
cacb5576 LC |
454 | (filter (compose file-exists? language->texi-file-name) |
455 | '#$languages))))) | |
ccadafdc | 456 | |
f8c143a7 LC |
457 | (let* ((name (string-append manual "-html-manual")) |
458 | (manual (computed-file name build))) | |
459 | (syntax-highlighted-html manual | |
460 | #:name (string-append name "-highlighted")))) | |
ccadafdc LC |
461 | |
462 | (define* (pdf-manual source #:key (languages %languages) | |
463 | (version "0.0") | |
cacb5576 | 464 | (manual %manual) |
ccadafdc LC |
465 | (date 1) |
466 | (options '())) | |
467 | "Return the HTML manuals built from SOURCE for all LANGUAGES, with the given | |
468 | makeinfo OPTIONS." | |
469 | (define manual-source | |
470 | (texinfo-manual-source source | |
471 | #:version version | |
472 | #:languages languages | |
473 | #:date date)) | |
474 | ||
475 | ;; FIXME: This union works, except for the table of contents of non-English | |
476 | ;; manuals, which contains escape sequences like "^^ca^^fe" instead of | |
477 | ;; accented letters. | |
478 | ;; | |
479 | ;; (define texlive | |
480 | ;; (texlive-union (list texlive-tex-texinfo | |
481 | ;; texlive-generic-epsf | |
482 | ;; texlive-fonts-ec))) | |
483 | ||
484 | (define build | |
485 | (with-imported-modules '((guix build utils)) | |
486 | #~(begin | |
487 | (use-modules (guix build utils) | |
488 | (srfi srfi-34) | |
489 | (ice-9 match)) | |
490 | ||
491 | (define (normalize language) ;XXX: deduplicate | |
492 | ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn". | |
493 | (string-map (match-lambda | |
494 | (#\_ #\-) | |
495 | (chr chr)) | |
496 | (string-downcase language))) | |
497 | ||
498 | ;; Install a UTF-8 locale so that 'makeinfo' is at ease. | |
499 | (setenv "GUIX_LOCPATH" | |
500 | #+(file-append glibc-utf8-locales "/lib/locale")) | |
501 | (setenv "LC_ALL" "en_US.utf8") | |
502 | (setenv "PATH" | |
503 | (string-append #+(file-append texlive "/bin") ":" | |
504 | #+(file-append texinfo "/bin") ":" | |
505 | ||
506 | ;; Below are command-line tools needed by | |
507 | ;; 'texi2dvi' and friends. | |
508 | #+(file-append sed "/bin") ":" | |
509 | #+(file-append grep "/bin") ":" | |
510 | #+(file-append coreutils "/bin") ":" | |
511 | #+(file-append gawk "/bin") ":" | |
512 | #+(file-append tar "/bin") ":" | |
513 | #+(file-append diffutils "/bin"))) | |
514 | ||
515 | (setvbuf (current-output-port) 'line) | |
516 | (setvbuf (current-error-port) 'line) | |
517 | ||
518 | (setenv "HOME" (getcwd)) ;for kpathsea/mktextfm | |
519 | ||
520 | ;; 'SOURCE_DATE_EPOCH' is honored by pdftex. | |
521 | (setenv "SOURCE_DATE_EPOCH" "1") | |
522 | ||
523 | (for-each (lambda (language) | |
524 | (let ((opts `("--pdf" | |
525 | "-I" "." | |
526 | #$@options | |
527 | ,(if (string=? language "en") | |
528 | (string-append #$manual-source "/" | |
529 | #$manual ".texi") | |
530 | (string-append #$manual-source "/" | |
531 | #$manual "." language ".texi"))))) | |
532 | (format #t "building PDF manual for language '~a'...~%" | |
533 | language) | |
534 | (mkdir-p (string-append #$output "/" | |
535 | (normalize language))) | |
536 | (setenv "LANGUAGE" language) | |
537 | ||
538 | ||
539 | ;; FIXME: Unfortunately building PDFs for non-Latin | |
540 | ;; alphabets doesn't work: | |
541 | ;; <https://lists.gnu.org/archive/html/help-texinfo/2012-01/msg00014.html>. | |
542 | (guard (c ((invoke-error? c) | |
543 | (format (current-error-port) | |
544 | "~%~%Failed to produce \ | |
545 | PDF for language '~a'!~%~%" | |
546 | language))) | |
547 | (apply invoke #$(file-append texinfo "/bin/makeinfo") | |
548 | "--pdf" "-o" | |
549 | (string-append #$output "/" | |
550 | (normalize language) | |
551 | "/" #$manual | |
552 | (if (string=? language "en") | |
553 | "" | |
554 | (string-append "." | |
555 | language)) | |
556 | ".pdf") | |
557 | opts)))) | |
558 | '#$languages)))) | |
559 | ||
560 | (computed-file (string-append manual "-pdf-manual") build)) | |
561 | ||
562 | (define (guix-manual-text-domain source languages) | |
563 | "Return the PO files for LANGUAGES of the 'guix-manual' text domain taken | |
564 | from SOURCE." | |
565 | (define po-directory | |
566 | (file-append* source "/po/doc")) | |
567 | ||
568 | (define build | |
569 | (with-imported-modules '((guix build utils)) | |
570 | #~(begin | |
571 | (use-modules (guix build utils)) | |
572 | ||
573 | (mkdir-p #$output) | |
574 | (for-each (lambda (language) | |
575 | (define directory | |
576 | (string-append #$output "/" language | |
577 | "/LC_MESSAGES")) | |
578 | ||
579 | (mkdir-p directory) | |
580 | (invoke #+(file-append gnu-gettext "/bin/msgfmt") | |
581 | "-c" "-o" | |
582 | (string-append directory "/guix-manual.mo") | |
583 | (string-append #$po-directory "/guix-manual." | |
584 | language ".po"))) | |
585 | '#$(delete "en" languages))))) | |
586 | ||
587 | (computed-file "guix-manual-po" build)) | |
588 | ||
589 | (define* (html-manual-indexes source | |
590 | #:key (languages %languages) | |
591 | (version "0.0") | |
cacb5576 | 592 | (manual %manual) |
208cc522 LC |
593 | (title (if (string=? "guix" manual) |
594 | "GNU Guix Reference Manual" | |
595 | "GNU Guix Cookbook")) | |
ccadafdc LC |
596 | (date 1)) |
597 | (define build | |
e591541d LC |
598 | (with-extensions (list guile-json-3) |
599 | (with-imported-modules '((guix build utils)) | |
600 | #~(begin | |
601 | (use-modules (guix build utils) | |
602 | (json) | |
603 | (ice-9 match) | |
604 | (ice-9 popen) | |
605 | (sxml simple) | |
606 | (srfi srfi-1) | |
607 | (srfi srfi-19)) | |
608 | ||
609 | (define (normalize language) ;XXX: deduplicate | |
610 | ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn". | |
611 | (string-map (match-lambda | |
612 | (#\_ #\-) | |
613 | (chr chr)) | |
614 | (string-downcase language))) | |
615 | ||
616 | (define-syntax-rule (with-language language exp ...) | |
617 | (let ((lang (getenv "LANGUAGE"))) | |
618 | (dynamic-wind | |
619 | (lambda () | |
620 | (setenv "LANGUAGE" language) | |
621 | (setlocale LC_MESSAGES)) | |
622 | (lambda () exp ...) | |
623 | (lambda () | |
624 | (if lang | |
625 | (setenv "LANGUAGE" lang) | |
626 | (unsetenv "LANGUAGE")) | |
627 | (setlocale LC_MESSAGES))))) | |
628 | ||
629 | ;; (put 'with-language 'scheme-indent-function 1) | |
630 | (define* (translate str language | |
631 | #:key (domain "guix-manual")) | |
632 | (define exp | |
633 | `(begin | |
634 | (bindtextdomain "guix-manual" | |
635 | #+(guix-manual-text-domain | |
636 | source | |
637 | languages)) | |
638 | (bindtextdomain "iso_639-3" ;language names | |
639 | #+(file-append iso-codes | |
640 | "/share/locale")) | |
641 | (write (gettext ,str ,domain)))) | |
642 | ||
643 | (with-language language | |
644 | ;; Since the 'gettext' function caches msgid translations, | |
645 | ;; regardless of $LANGUAGE, we have to spawn a new process each | |
646 | ;; time we want to translate to a different language. Bah! | |
647 | (let* ((pipe (open-pipe* OPEN_READ | |
648 | #+(file-append guile-2.2 | |
649 | "/bin/guile") | |
650 | "-c" (object->string exp))) | |
651 | (str (read pipe))) | |
652 | (close-pipe pipe) | |
653 | str))) | |
654 | ||
655 | (define (seconds->string seconds language) | |
656 | (let* ((time (make-time time-utc 0 seconds)) | |
657 | (date (time-utc->date time))) | |
658 | (with-language language (date->string date "~e ~B ~Y")))) | |
659 | ||
660 | (define (guix-url path) | |
661 | (string-append #$%web-site-url path)) | |
662 | ||
663 | (define (sxml-index language title body) | |
664 | ;; FIXME: Avoid duplicating styling info from guix-artwork.git. | |
665 | `(html (@ (lang ,language)) | |
666 | (head | |
667 | (title ,(string-append title " — GNU Guix")) | |
668 | (meta (@ (charset "UTF-8"))) | |
669 | (meta (@ (name "viewport") (content "width=device-width, initial-scale=1.0"))) | |
670 | ;; Menu prefetch. | |
671 | (link (@ (rel "prefetch") (href ,(guix-url "menu/index.html")))) | |
672 | ;; Base CSS. | |
673 | (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/elements.css")))) | |
674 | (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/common.css")))) | |
675 | (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/messages.css")))) | |
676 | (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/navbar.css")))) | |
677 | (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/breadcrumbs.css")))) | |
678 | (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/buttons.css")))) | |
679 | (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/footer.css")))) | |
680 | ||
681 | (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/page.css")))) | |
682 | (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/post.css"))))) | |
683 | (body | |
684 | (header (@ (class "navbar")) | |
685 | (h1 (a (@ (class "branding") | |
686 | (href #$%web-site-url))) | |
687 | (span (@ (class "a11y-offset")) | |
688 | "Guix")) | |
689 | (nav (@ (class "menu")))) | |
690 | (nav (@ (class "breadcrumbs")) | |
691 | (a (@ (class "crumb") | |
692 | (href #$%web-site-url)) | |
693 | "Home")) | |
694 | ,body | |
695 | (footer)))) | |
696 | ||
697 | (define (language-index language) | |
698 | (define title | |
208cc522 | 699 | (translate #$title language)) |
e591541d LC |
700 | |
701 | (sxml-index | |
702 | language title | |
703 | `(main | |
704 | (article | |
705 | (@ (class "page centered-block limit-width")) | |
706 | (h2 ,title) | |
707 | (p (@ (class "post-metadata centered-text")) | |
708 | #$version " — " | |
709 | ,(seconds->string #$date language)) | |
710 | ||
711 | (div | |
712 | (ul | |
713 | (li (a (@ (href "html_node")) | |
714 | "HTML, with one page per node")) | |
715 | (li (a (@ (href | |
716 | ,(string-append | |
717 | #$manual | |
718 | (if (string=? language | |
719 | "en") | |
720 | "" | |
721 | (string-append "." | |
722 | language)) | |
723 | ".html"))) | |
724 | "HTML, entirely on one page")) | |
725 | ,@(if (member language '("ru" "zh_CN")) | |
726 | '() | |
727 | `((li (a (@ (href ,(string-append | |
728 | #$manual | |
729 | (if (string=? language "en") | |
730 | "" | |
731 | (string-append "." | |
732 | language)) | |
733 | ".pdf")))) | |
734 | "PDF"))))))))) | |
735 | ||
736 | (define %iso639-languages | |
737 | (vector->list | |
738 | (assoc-ref (call-with-input-file | |
739 | #+(file-append iso-codes | |
740 | "/share/iso-codes/json/iso_639-3.json") | |
741 | json->scm) | |
742 | "639-3"))) | |
743 | ||
744 | (define (language-code->name code) | |
745 | "Return the full name of a language from its ISO-639-3 code." | |
746 | (let ((code (match (string-index code #\_) | |
747 | (#f code) | |
748 | (index (string-take code index))))) | |
749 | (any (lambda (language) | |
750 | (and (string=? (or (assoc-ref language "alpha_2") | |
751 | (assoc-ref language "alpha_3")) | |
752 | code) | |
753 | (assoc-ref language "name"))) | |
754 | %iso639-languages))) | |
755 | ||
756 | (define (top-level-index languages) | |
208cc522 | 757 | (define title #$title) |
e591541d LC |
758 | (sxml-index |
759 | "en" title | |
760 | `(main | |
761 | (article | |
762 | (@ (class "page centered-block limit-width")) | |
763 | (h2 ,title) | |
764 | (div | |
208cc522 | 765 | "This document is available in the following |
e591541d LC |
766 | languages:\n" |
767 | (ul | |
768 | ,@(map (lambda (language) | |
769 | `(li (a (@ (href ,(normalize language))) | |
770 | ,(translate | |
771 | (language-code->name language) | |
772 | language | |
773 | #:domain "iso_639-3")))) | |
774 | languages))))))) | |
775 | ||
776 | (define (write-html file sxml) | |
777 | (call-with-output-file file | |
778 | (lambda (port) | |
779 | (display "<!DOCTYPE html>\n" port) | |
780 | (sxml->xml sxml port)))) | |
781 | ||
782 | (setenv "GUIX_LOCPATH" | |
783 | #+(file-append glibc-utf8-locales "/lib/locale")) | |
784 | (setenv "LC_ALL" "en_US.utf8") | |
785 | (setlocale LC_ALL "en_US.utf8") | |
786 | ||
787 | (for-each (lambda (language) | |
788 | (define directory | |
789 | (string-append #$output "/" | |
790 | (normalize language))) | |
791 | ||
792 | (mkdir-p directory) | |
793 | (write-html (string-append directory "/index.html") | |
794 | (language-index language))) | |
795 | '#$languages) | |
796 | ||
797 | (write-html (string-append #$output "/index.html") | |
798 | (top-level-index '#$languages)))))) | |
ccadafdc LC |
799 | |
800 | (computed-file "html-indexes" build)) | |
801 | ||
802 | (define* (pdf+html-manual source | |
803 | #:key (languages %languages) | |
804 | (version "0.0") | |
805 | (date (time-second (current-time time-utc))) | |
cacb5576 | 806 | (manual %manual)) |
ccadafdc LC |
807 | "Return the union of the HTML and PDF manuals, as well as the indexes." |
808 | (directory-union (string-append manual "-manual") | |
809 | (map (lambda (proc) | |
810 | (proc source | |
811 | #:date date | |
812 | #:languages languages | |
813 | #:version version | |
814 | #:manual manual)) | |
815 | (list html-manual-indexes | |
816 | html-manual pdf-manual)) | |
817 | #:copy? #t)) | |
818 | ||
819 | (define (latest-commit+date directory) | |
820 | "Return two values: the last commit ID (a hex string) for DIRECTORY, and its | |
821 | commit date (an integer)." | |
822 | (let* ((repository (repository-open directory)) | |
823 | (head (repository-head repository)) | |
824 | (oid (reference-target head)) | |
825 | (commit (commit-lookup repository oid))) | |
826 | ;; TODO: Use (git describe) when it's widely available. | |
827 | (values (oid->string oid) (commit-time commit)))) | |
828 | ||
829 | \f | |
830 | (let* ((root (canonicalize-path | |
831 | (string-append (current-source-directory) "/.."))) | |
832 | (commit date (latest-commit+date root))) | |
833 | (format (current-error-port) | |
834 | "building manual from work tree around commit ~a, ~a~%" | |
835 | commit | |
836 | (let* ((time (make-time time-utc 0 date)) | |
837 | (date (time-utc->date time))) | |
838 | (date->string date "~e ~B ~Y"))) | |
839 | (pdf+html-manual (local-file root "guix" #:recursive? #t | |
840 | #:select? (git-predicate root)) | |
841 | #:version (or (getenv "GUIX_MANUAL_VERSION") | |
842 | (string-take commit 7)) | |
843 | #:date date)) |