doc: Improve anchor collection.
[jackhill/guix/guix.git] / doc / build.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2019, 2020 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)
32 (guix utils)
33 (git)
34 (gnu packages base)
35 (gnu packages gawk)
36 (gnu packages gettext)
37 (gnu packages guile)
38 (gnu packages guile-xyz)
39 (gnu packages iso-codes)
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
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
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 "-c" "EXTRA_HEAD=<meta name=\"viewport\" \
175 content=\"width=device-width, initial-scale=1\" />"))
176
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
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
215 (with-extensions (list guile-lib/htmlprag-fixed guile-syntax-highlight)
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 (srfi srfi-1)
224 (srfi srfi-26)
225 (ice-9 match)
226 (ice-9 threads)
227 (ice-9 vlist))
228
229 (define (pair-open/close lst)
230 ;; Pair 'open' and 'close' tags produced by 'highlights' and
231 ;; produce nested 'paren' tags instead.
232 (let loop ((lst lst)
233 (level 0)
234 (result '()))
235 (match lst
236 ((('open open) rest ...)
237 (call-with-values
238 (lambda ()
239 (loop rest (+ 1 level) '()))
240 (lambda (inner close rest)
241 (loop rest level
242 (cons `(paren ,level ,open ,inner ,close)
243 result)))))
244 ((('close str) rest ...)
245 (if (> level 0)
246 (values (reverse result) str rest)
247 (begin
248 (format (current-error-port)
249 "warning: extra closing paren; context:~% ~y~%"
250 (reverse result))
251 (loop rest 0 (cons `(close ,str) result)))))
252 ((item rest ...)
253 (loop rest level (cons item result)))
254 (()
255 (when (> level 0)
256 (format (current-error-port)
257 "warning: missing ~a closing parens; context:~% ~y%"
258 level (reverse result)))
259 (values (reverse result) "" '())))))
260
261 (define (highlights->sxml* highlights anchors)
262 ;; Like 'highlights->sxml', but handle nested 'paren tags. This
263 ;; allows for paren matching highlights via appropriate CSS
264 ;; "hover" properties. When a symbol is encountered, look it up
265 ;; in ANCHORS, a vhash, and emit the corresponding href, if any.
266 (define (tag->class tag)
267 (string-append "syntax-" (symbol->string tag)))
268
269 (map (match-lambda
270 ((? string? str) str)
271 (('paren level open (body ...) close)
272 `(span (@ (class ,(string-append "syntax-paren"
273 (number->string level))))
274 ,open
275 (span (@ (class "syntax-symbol"))
276 ,@(highlights->sxml* body anchors))
277 ,close))
278 (('symbol text)
279 ;; Check whether we can emit a hyperlink for TEXT.
280 (match (vhash-assoc text anchors)
281 (#f
282 `(span (@ (class ,(tag->class 'symbol))) ,text))
283 ((_ . target)
284 `(a (@ (class ,(tag->class 'symbol)) (href ,target))
285 ,text))))
286 ((tag text)
287 `(span (@ (class ,(tag->class tag))) ,text)))
288 highlights))
289
290 (define entity->string
291 (match-lambda
292 ("rArr" "⇒")
293 ("rarr" "→")
294 ("hellip" "…")
295 ("rsquo" "’")
296 (e (pk 'unknown-entity e) (primitive-exit 2))))
297
298 (define (concatenate-snippets pieces)
299 ;; Concatenate PIECES, which contains strings and entities,
300 ;; replacing entities with their corresponding string.
301 (let loop ((pieces pieces)
302 (strings '()))
303 (match pieces
304 (()
305 (string-concatenate-reverse strings))
306 (((? string? str) . rest)
307 (loop rest (cons str strings)))
308 ((('*ENTITY* "additional" entity) . rest)
309 (loop rest (cons (entity->string entity) strings)))
310 ((('span _ lst ...) . rest) ;for <span class="roman">
311 (loop (append lst rest) strings))
312 (something
313 (pk 'unsupported-code-snippet something)
314 (primitive-exit 1)))))
315
316 (define (syntax-highlight sxml anchors)
317 ;; Recurse over SXML and syntax-highlight code snippets.
318 (let loop ((sxml sxml))
319 (match sxml
320 (('*TOP* decl body ...)
321 `(*TOP* ,decl ,@(map loop body)))
322 (('head things ...)
323 `(head ,@things
324 (link (@ (rel "stylesheet")
325 (type "text/css")
326 (href #$syntax-css-url)))))
327 (('pre ('@ ('class "lisp")) code-snippet ...)
328 `(pre (@ (class "lisp"))
329 ,@(highlights->sxml*
330 (pair-open/close
331 (highlight lex-scheme
332 (concatenate-snippets code-snippet)))
333 anchors)))
334 ((tag ('@ attributes ...) body ...)
335 `(,tag (@ ,@attributes) ,@(map loop body)))
336 ((tag body ...)
337 `(,tag ,@(map loop body)))
338 ((? string? str)
339 str))))
340
341 (define (underscore-decode str)
342 ;; Decode STR, an "underscore-encoded" string as produced by
343 ;; makeinfo for indexes, such as "_0025base_002dservices" for
344 ;; "%base-services".
345 (let loop ((str str)
346 (result '()))
347 (match (string-index str #\_)
348 (#f
349 (string-concatenate-reverse (cons str result)))
350 (index
351 (let ((char (string->number
352 (substring str (+ index 1) (+ index 5))
353 16)))
354 (loop (string-drop str (+ index 5))
355 (append (list (string (integer->char char))
356 (string-take str index))
357 result)))))))
358
359 (define (anchor-id->key id)
360 ;; Convert ID, an anchor ID such as
361 ;; "index-pam_002dlimits_002dservice" to the corresponding key,
362 ;; "pam-limits-service" in this example. Drop the suffix of
363 ;; duplicate anchor IDs like "operating_002dsystem-1".
364 (let ((id (if (any (cut string-suffix? <> id)
365 '("-1" "-2" "-3" "-4" "-5"))
366 (string-drop-right id 2)
367 id)))
368 (underscore-decode
369 (string-drop id (string-length "index-")))))
370
371 (define* (collect-anchors file #:optional (vhash vlist-null))
372 ;; Collect the anchors that appear in FILE, a makeinfo-generated
373 ;; file. Grab those from <dt> tags, which corresponds to
374 ;; Texinfo @deftp, @defvr, etc. Return VHASH augmented with
375 ;; more name/reference pairs.
376 (define string-or-entity?
377 (match-lambda
378 ((? string?) #t)
379 (('*ENTITY* _ ...) #t)
380 (_ #f)))
381
382 (define (worthy-entry? lst)
383 ;; Attempt to match:
384 ;; Scheme Variable: <strong>x</strong>
385 ;; but not:
386 ;; <code>cups-configuration</code> parameter: …
387 (let loop ((lst lst))
388 (match lst
389 (((? string-or-entity?) rest ...)
390 (loop rest))
391 ((('strong _ ...) _ ...)
392 #t)
393 (_ #f))))
394
395 (let ((shtml (call-with-input-file file html->shtml)))
396 (let loop ((shtml shtml)
397 (vhash vhash))
398 (match shtml
399 (('dt ('@ ('id id)) rest ...)
400 (if (and (string-prefix? "index-" id)
401 (worthy-entry? rest))
402 (vhash-cons (anchor-id->key id)
403 (string-append (basename file)
404 "#" id)
405 vhash)
406 vhash))
407 ((tag ('@ _ ...) body ...)
408 (fold loop vhash body))
409 ((tag body ...)
410 (fold loop vhash body))
411 (_ vhash)))))
412
413 (define (process-html file anchors)
414 ;; Parse FILE and perform syntax highlighting for its Scheme
415 ;; snippets. Install the result to #$output.
416 (format (current-error-port) "processing ~a...~%" file)
417 (let* ((shtml (call-with-input-file file html->shtml))
418 (highlighted (syntax-highlight shtml anchors))
419 (base (string-drop file (string-length #$input)))
420 (target (string-append #$output base)))
421 (mkdir-p (dirname target))
422 (call-with-output-file target
423 (lambda (port)
424 (write-shtml-as-html highlighted port)))))
425
426 (define (copy-as-is file)
427 ;; Copy FILE as is to #$output.
428 (let* ((base (string-drop file (string-length #$input)))
429 (target (string-append #$output base)))
430 (mkdir-p (dirname target))
431 (catch 'system-error
432 (lambda ()
433 (if (eq? 'symlink (stat:type (lstat file)))
434 (symlink (readlink file) target)
435 (link file target)))
436 (lambda args
437 (let ((errno (system-error-errno args)))
438 (pk 'error-link file target (strerror errno))
439 (primitive-exit 3))))))
440
441 (define (html? file stat)
442 (string-suffix? ".html" file))
443
444 ;; Install a UTF-8 locale so we can process UTF-8 files.
445 (setenv "GUIX_LOCPATH"
446 #+(file-append glibc-utf8-locales "/lib/locale"))
447 (setlocale LC_ALL "en_US.utf8")
448
449 ;; First process the mono-node 'guix.html' files.
450 (n-par-for-each (parallel-job-count)
451 (lambda (mono)
452 (let ((anchors (collect-anchors mono)))
453 (process-html mono anchors)))
454 (find-files #$input "^guix(\\.[a-zA-Z_-]+)?\\.html$"))
455
456 ;; Next process the multi-node HTML files in two phases: (1)
457 ;; collect the list of anchors, and (2) perform
458 ;; syntax-highlighting.
459 (let* ((multi (find-files #$input "^html_node$"
460 #:directories? #t))
461 (anchors (n-par-map (parallel-job-count)
462 (lambda (multi)
463 (cons multi
464 (fold collect-anchors vlist-null
465 (find-files multi html?))))
466 multi)))
467 (n-par-for-each (parallel-job-count)
468 (lambda (file)
469 (let ((anchors (assoc-ref anchors (dirname file))))
470 (process-html file anchors)))
471 (append-map (lambda (multi)
472 (find-files multi html?))
473 multi)))
474
475 ;; Last, copy non-HTML files as is.
476 (for-each copy-as-is
477 (find-files #$input (negate html?)))))))
478
479 (computed-file name build))
480
481 (define* (html-manual source #:key (languages %languages)
482 (version "0.0")
483 (manual %manual)
484 (date 1)
485 (options %makeinfo-html-options))
486 "Return the HTML manuals built from SOURCE for all LANGUAGES, with the given
487 makeinfo OPTIONS."
488 (define manual-source
489 (texinfo-manual-source source
490 #:version version
491 #:languages languages
492 #:date date))
493
494 (define images
495 (texinfo-manual-images source))
496
497 (define build
498 (with-imported-modules '((guix build utils))
499 #~(begin
500 (use-modules (guix build utils)
501 (ice-9 match))
502
503 (define (normalize language)
504 ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
505 (string-map (match-lambda
506 (#\_ #\-)
507 (chr chr))
508 (string-downcase language)))
509
510 (define (language->texi-file-name language)
511 (if (string=? language "en")
512 (string-append #$manual-source "/"
513 #$manual ".texi")
514 (string-append #$manual-source "/"
515 #$manual "." language ".texi")))
516
517 ;; Install a UTF-8 locale so that 'makeinfo' is at ease.
518 (setenv "GUIX_LOCPATH"
519 #+(file-append glibc-utf8-locales "/lib/locale"))
520 (setenv "LC_ALL" "en_US.utf8")
521
522 (setvbuf (current-output-port) 'line)
523 (setvbuf (current-error-port) 'line)
524
525 ;; 'makeinfo' looks for "htmlxref.cnf" in the current directory, so
526 ;; copy it right here.
527 (copy-file (string-append #$manual-source "/htmlxref.cnf")
528 "htmlxref.cnf")
529
530 (for-each (lambda (language)
531 (let* ((texi (language->texi-file-name language))
532 (opts `("--html"
533 "-c" ,(string-append "TOP_NODE_UP_URL=/manual/"
534 language)
535 #$@options
536 ,texi)))
537 (format #t "building HTML manual for language '~a'...~%"
538 language)
539 (mkdir-p (string-append #$output "/"
540 (normalize language)))
541 (setenv "LANGUAGE" language)
542 (apply invoke #$(file-append texinfo "/bin/makeinfo")
543 "-o" (string-append #$output "/"
544 (normalize language)
545 "/html_node")
546 opts)
547 (apply invoke #$(file-append texinfo "/bin/makeinfo")
548 "--no-split"
549 "-o"
550 (string-append #$output "/"
551 (normalize language)
552 "/" #$manual
553 (if (string=? language "en")
554 ""
555 (string-append "." language))
556 ".html")
557 opts)
558
559 ;; Make sure images are available.
560 (symlink #$images
561 (string-append #$output "/" (normalize language)
562 "/images"))
563 (symlink #$images
564 (string-append #$output "/" (normalize language)
565 "/html_node/images"))))
566 (filter (compose file-exists? language->texi-file-name)
567 '#$languages)))))
568
569 (let* ((name (string-append manual "-html-manual"))
570 (manual (computed-file name build)))
571 (syntax-highlighted-html manual
572 #:name (string-append name "-highlighted"))))
573
574 (define* (pdf-manual source #:key (languages %languages)
575 (version "0.0")
576 (manual %manual)
577 (date 1)
578 (options '()))
579 "Return the HTML manuals built from SOURCE for all LANGUAGES, with the given
580 makeinfo OPTIONS."
581 (define manual-source
582 (texinfo-manual-source source
583 #:version version
584 #:languages languages
585 #:date date))
586
587 ;; FIXME: This union works, except for the table of contents of non-English
588 ;; manuals, which contains escape sequences like "^^ca^^fe" instead of
589 ;; accented letters.
590 ;;
591 ;; (define texlive
592 ;; (texlive-union (list texlive-tex-texinfo
593 ;; texlive-generic-epsf
594 ;; texlive-fonts-ec)))
595
596 (define build
597 (with-imported-modules '((guix build utils))
598 #~(begin
599 (use-modules (guix build utils)
600 (srfi srfi-34)
601 (ice-9 match))
602
603 (define (normalize language) ;XXX: deduplicate
604 ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
605 (string-map (match-lambda
606 (#\_ #\-)
607 (chr chr))
608 (string-downcase language)))
609
610 ;; Install a UTF-8 locale so that 'makeinfo' is at ease.
611 (setenv "GUIX_LOCPATH"
612 #+(file-append glibc-utf8-locales "/lib/locale"))
613 (setenv "LC_ALL" "en_US.utf8")
614 (setenv "PATH"
615 (string-append #+(file-append texlive "/bin") ":"
616 #+(file-append texinfo "/bin") ":"
617
618 ;; Below are command-line tools needed by
619 ;; 'texi2dvi' and friends.
620 #+(file-append sed "/bin") ":"
621 #+(file-append grep "/bin") ":"
622 #+(file-append coreutils "/bin") ":"
623 #+(file-append gawk "/bin") ":"
624 #+(file-append tar "/bin") ":"
625 #+(file-append diffutils "/bin")))
626
627 (setvbuf (current-output-port) 'line)
628 (setvbuf (current-error-port) 'line)
629
630 (setenv "HOME" (getcwd)) ;for kpathsea/mktextfm
631
632 ;; 'SOURCE_DATE_EPOCH' is honored by pdftex.
633 (setenv "SOURCE_DATE_EPOCH" "1")
634
635 (for-each (lambda (language)
636 (let ((opts `("--pdf"
637 "-I" "."
638 #$@options
639 ,(if (string=? language "en")
640 (string-append #$manual-source "/"
641 #$manual ".texi")
642 (string-append #$manual-source "/"
643 #$manual "." language ".texi")))))
644 (format #t "building PDF manual for language '~a'...~%"
645 language)
646 (mkdir-p (string-append #$output "/"
647 (normalize language)))
648 (setenv "LANGUAGE" language)
649
650
651 ;; FIXME: Unfortunately building PDFs for non-Latin
652 ;; alphabets doesn't work:
653 ;; <https://lists.gnu.org/archive/html/help-texinfo/2012-01/msg00014.html>.
654 (guard (c ((invoke-error? c)
655 (format (current-error-port)
656 "~%~%Failed to produce \
657 PDF for language '~a'!~%~%"
658 language)))
659 (apply invoke #$(file-append texinfo "/bin/makeinfo")
660 "--pdf" "-o"
661 (string-append #$output "/"
662 (normalize language)
663 "/" #$manual
664 (if (string=? language "en")
665 ""
666 (string-append "."
667 language))
668 ".pdf")
669 opts))))
670 '#$languages))))
671
672 (computed-file (string-append manual "-pdf-manual") build))
673
674 (define (guix-manual-text-domain source languages)
675 "Return the PO files for LANGUAGES of the 'guix-manual' text domain taken
676 from SOURCE."
677 (define po-directory
678 (file-append* source "/po/doc"))
679
680 (define build
681 (with-imported-modules '((guix build utils))
682 #~(begin
683 (use-modules (guix build utils))
684
685 (mkdir-p #$output)
686 (for-each (lambda (language)
687 (define directory
688 (string-append #$output "/" language
689 "/LC_MESSAGES"))
690
691 (mkdir-p directory)
692 (invoke #+(file-append gnu-gettext "/bin/msgfmt")
693 "-c" "-o"
694 (string-append directory "/guix-manual.mo")
695 (string-append #$po-directory "/guix-manual."
696 language ".po")))
697 '#$(delete "en" languages)))))
698
699 (computed-file "guix-manual-po" build))
700
701 (define* (html-manual-indexes source
702 #:key (languages %languages)
703 (version "0.0")
704 (manual %manual)
705 (title (if (string=? "guix" manual)
706 "GNU Guix Reference Manual"
707 "GNU Guix Cookbook"))
708 (date 1))
709 (define build
710 (with-extensions (list guile-json-3)
711 (with-imported-modules '((guix build utils))
712 #~(begin
713 (use-modules (guix build utils)
714 (json)
715 (ice-9 match)
716 (ice-9 popen)
717 (sxml simple)
718 (srfi srfi-1)
719 (srfi srfi-19))
720
721 (define (normalize language) ;XXX: deduplicate
722 ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
723 (string-map (match-lambda
724 (#\_ #\-)
725 (chr chr))
726 (string-downcase language)))
727
728 (define-syntax-rule (with-language language exp ...)
729 (let ((lang (getenv "LANGUAGE")))
730 (dynamic-wind
731 (lambda ()
732 (setenv "LANGUAGE" language)
733 (setlocale LC_MESSAGES))
734 (lambda () exp ...)
735 (lambda ()
736 (if lang
737 (setenv "LANGUAGE" lang)
738 (unsetenv "LANGUAGE"))
739 (setlocale LC_MESSAGES)))))
740
741 ;; (put 'with-language 'scheme-indent-function 1)
742 (define* (translate str language
743 #:key (domain "guix-manual"))
744 (define exp
745 `(begin
746 (bindtextdomain "guix-manual"
747 #+(guix-manual-text-domain
748 source
749 languages))
750 (bindtextdomain "iso_639-3" ;language names
751 #+(file-append iso-codes
752 "/share/locale"))
753 (write (gettext ,str ,domain))))
754
755 (with-language language
756 ;; Since the 'gettext' function caches msgid translations,
757 ;; regardless of $LANGUAGE, we have to spawn a new process each
758 ;; time we want to translate to a different language. Bah!
759 (let* ((pipe (open-pipe* OPEN_READ
760 #+(file-append guile-2.2
761 "/bin/guile")
762 "-c" (object->string exp)))
763 (str (read pipe)))
764 (close-pipe pipe)
765 str)))
766
767 (define (seconds->string seconds language)
768 (let* ((time (make-time time-utc 0 seconds))
769 (date (time-utc->date time)))
770 (with-language language (date->string date "~e ~B ~Y"))))
771
772 (define (guix-url path)
773 (string-append #$%web-site-url path))
774
775 (define (sxml-index language title body)
776 ;; FIXME: Avoid duplicating styling info from guix-artwork.git.
777 `(html (@ (lang ,language))
778 (head
779 (title ,(string-append title " — GNU Guix"))
780 (meta (@ (charset "UTF-8")))
781 (meta (@ (name "viewport") (content "width=device-width, initial-scale=1.0")))
782 ;; Menu prefetch.
783 (link (@ (rel "prefetch") (href ,(guix-url "menu/index.html"))))
784 ;; Base CSS.
785 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/elements.css"))))
786 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/common.css"))))
787 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/messages.css"))))
788 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/navbar.css"))))
789 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/breadcrumbs.css"))))
790 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/buttons.css"))))
791 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/footer.css"))))
792
793 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/page.css"))))
794 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/post.css")))))
795 (body
796 (header (@ (class "navbar"))
797 (h1 (a (@ (class "branding")
798 (href #$%web-site-url)))
799 (span (@ (class "a11y-offset"))
800 "Guix"))
801 (nav (@ (class "menu"))))
802 (nav (@ (class "breadcrumbs"))
803 (a (@ (class "crumb")
804 (href #$%web-site-url))
805 "Home"))
806 ,body
807 (footer))))
808
809 (define (language-index language)
810 (define title
811 (translate #$title language))
812
813 (sxml-index
814 language title
815 `(main
816 (article
817 (@ (class "page centered-block limit-width"))
818 (h2 ,title)
819 (p (@ (class "post-metadata centered-text"))
820 #$version " — "
821 ,(seconds->string #$date language))
822
823 (div
824 (ul
825 (li (a (@ (href "html_node"))
826 "HTML, with one page per node"))
827 (li (a (@ (href
828 ,(string-append
829 #$manual
830 (if (string=? language
831 "en")
832 ""
833 (string-append "."
834 language))
835 ".html")))
836 "HTML, entirely on one page"))
837 ,@(if (member language '("ru" "zh_CN"))
838 '()
839 `((li (a (@ (href ,(string-append
840 #$manual
841 (if (string=? language "en")
842 ""
843 (string-append "."
844 language))
845 ".pdf"))))
846 "PDF")))))))))
847
848 (define %iso639-languages
849 (vector->list
850 (assoc-ref (call-with-input-file
851 #+(file-append iso-codes
852 "/share/iso-codes/json/iso_639-3.json")
853 json->scm)
854 "639-3")))
855
856 (define (language-code->name code)
857 "Return the full name of a language from its ISO-639-3 code."
858 (let ((code (match (string-index code #\_)
859 (#f code)
860 (index (string-take code index)))))
861 (any (lambda (language)
862 (and (string=? (or (assoc-ref language "alpha_2")
863 (assoc-ref language "alpha_3"))
864 code)
865 (assoc-ref language "name")))
866 %iso639-languages)))
867
868 (define (top-level-index languages)
869 (define title #$title)
870 (sxml-index
871 "en" title
872 `(main
873 (article
874 (@ (class "page centered-block limit-width"))
875 (h2 ,title)
876 (div
877 "This document is available in the following
878 languages:\n"
879 (ul
880 ,@(map (lambda (language)
881 `(li (a (@ (href ,(normalize language)))
882 ,(translate
883 (language-code->name language)
884 language
885 #:domain "iso_639-3"))))
886 languages)))))))
887
888 (define (write-html file sxml)
889 (call-with-output-file file
890 (lambda (port)
891 (display "<!DOCTYPE html>\n" port)
892 (sxml->xml sxml port))))
893
894 (setenv "GUIX_LOCPATH"
895 #+(file-append glibc-utf8-locales "/lib/locale"))
896 (setenv "LC_ALL" "en_US.utf8")
897 (setlocale LC_ALL "en_US.utf8")
898
899 (for-each (lambda (language)
900 (define directory
901 (string-append #$output "/"
902 (normalize language)))
903
904 (mkdir-p directory)
905 (write-html (string-append directory "/index.html")
906 (language-index language)))
907 '#$languages)
908
909 (write-html (string-append #$output "/index.html")
910 (top-level-index '#$languages))))))
911
912 (computed-file "html-indexes" build))
913
914 (define* (pdf+html-manual source
915 #:key (languages %languages)
916 (version "0.0")
917 (date (time-second (current-time time-utc)))
918 (manual %manual))
919 "Return the union of the HTML and PDF manuals, as well as the indexes."
920 (directory-union (string-append manual "-manual")
921 (map (lambda (proc)
922 (proc source
923 #:date date
924 #:languages languages
925 #:version version
926 #:manual manual))
927 (list html-manual-indexes
928 html-manual pdf-manual))
929 #:copy? #t))
930
931 (define (latest-commit+date directory)
932 "Return two values: the last commit ID (a hex string) for DIRECTORY, and its
933 commit date (an integer)."
934 (let* ((repository (repository-open directory))
935 (head (repository-head repository))
936 (oid (reference-target head))
937 (commit (commit-lookup repository oid)))
938 ;; TODO: Use (git describe) when it's widely available.
939 (values (oid->string oid) (commit-time commit))))
940
941 \f
942 (let* ((root (canonicalize-path
943 (string-append (current-source-directory) "/..")))
944 (commit date (latest-commit+date root)))
945 (format (current-error-port)
946 "building manual from work tree around commit ~a, ~a~%"
947 commit
948 (let* ((time (make-time time-utc 0 date))
949 (date (time-utc->date time)))
950 (date->string date "~e ~B ~Y")))
951 (pdf+html-manual (local-file root "guix" #:recursive? #t
952 #:select? (git-predicate root))
953 #:version (or (getenv "GUIX_MANUAL_VERSION")
954 (string-take commit 7))
955 #:date date))