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