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