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