1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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:
23 ;; guix build -f build.scm
25 ;; The result is a directory hierarchy that can be used as the manual/
26 ;; sub-directory of the web site.
35 (gnu packages gettext)
37 (gnu packages iso-codes)
38 (gnu packages texinfo)
44 (@@ (guix self) file-append*))
46 (define translated-texi-manuals
47 (@@ (guix self) translate-texi-manuals))
50 (@@ (guix self) info-manual))
53 '("de" "en" "es" "fr" "ru" "zh_CN"))
55 (define (texinfo-manual-images source)
56 "Return a directory containing all the images used by the user manual, taken
57 from SOURCE, the root of the source tree."
59 (module-ref (resolve-interface '(gnu packages graphviz))
63 (file-append* source "doc/images"))
66 (with-imported-modules '((guix build utils))
68 (use-modules (guix build utils)
71 (define (dot->image dot-file format)
72 (invoke #+(file-append graphviz "/bin/dot")
73 "-T" format "-Gratio=.9" "-Gnodesep=.005"
74 "-Granksep=.00005" "-Nfontsize=9"
75 "-Nheight=.1" "-Nwidth=.1"
76 "-o" (string-append #$output "/"
77 (basename dot-file ".dot")
83 (for-each (lambda (dot-file)
84 (for-each (cut dot->image dot-file <>)
86 (find-files #$images "\\.dot$"))
89 (for-each (lambda (png-file)
90 (install-file png-file #$output))
91 (find-files #$images "\\.png$")))))
93 (computed-file "texinfo-manual-images" build))
95 (define* (texinfo-manual-source source #:key
97 (languages %languages)
99 "Gather all the source files of the Texinfo manuals from SOURCE--.texi file
100 as well as images, OS examples, and translations."
101 (define documentation
102 (file-append* source "doc"))
105 (file-append* source "gnu/system/examples"))
108 (with-imported-modules '((guix build utils))
110 (use-modules (guix build utils)
113 (define (make-version-texi language)
114 ;; Create the 'version.texi' file for LANGUAGE.
115 (let ((file (if (string=? language "en")
117 (string-append "version-" language ".texi"))))
118 (call-with-output-file (string-append #$output "/" file)
120 (let* ((version #$version)
121 (time (make-time time-utc 0 #$date))
122 (date (time-utc->date time)))
125 @set UPDATED-MONTH ~a
128 (date->string date "~e ~B ~Y")
129 (date->string date "~B ~Y")
130 version version))))))
132 (install-file #$(file-append* documentation "/htmlxref.cnf")
135 (for-each (lambda (texi)
136 (install-file texi #$output))
137 (append (find-files #$documentation "\\.(texi|scm)$")
138 (find-files #$(translated-texi-manuals source)
141 ;; Create 'version.texi'.
142 (for-each make-version-texi '#$languages)
144 ;; Copy configuration templates that the manual includes.
145 (for-each (lambda (template)
148 #$output "/os-config-"
149 (basename template ".tmpl")
151 (find-files #$examples "\\.tmpl$"))
153 (symlink #$(texinfo-manual-images source)
154 (string-append #$output "/images")))))
156 (computed-file "texinfo-manual-source" build))
158 (define %web-site-url
159 ;; URL of the web site home page.
160 (or (getenv "GUIX_WEB_SITE_URL")
163 (define %makeinfo-html-options
164 ;; Options passed to 'makeinfo --html'.
165 '("--css-ref=https://www.gnu.org/software/gnulib/manual.css"))
167 (define* (html-manual source #:key (languages %languages)
171 (options %makeinfo-html-options))
172 "Return the HTML manuals built from SOURCE for all LANGUAGES, with the given
174 (define manual-source
175 (texinfo-manual-source source
177 #:languages languages
181 (texinfo-manual-images source))
184 (with-imported-modules '((guix build utils))
186 (use-modules (guix build utils)
189 (define (normalize language)
190 ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
191 (string-map (match-lambda
194 (string-downcase language)))
196 ;; Install a UTF-8 locale so that 'makeinfo' is at ease.
197 (setenv "GUIX_LOCPATH"
198 #+(file-append glibc-utf8-locales "/lib/locale"))
199 (setenv "LC_ALL" "en_US.utf8")
201 (setvbuf (current-output-port) 'line)
202 (setvbuf (current-error-port) 'line)
204 (for-each (lambda (language)
205 (let ((opts `("--html"
206 "-c" ,(string-append "TOP_NODE_UP_URL=/manual/"
209 ,(if (string=? language "en")
210 (string-append #$manual-source "/"
212 (string-append #$manual-source "/"
213 #$manual "." language ".texi")))))
214 (format #t "building HTML manual for language '~a'...~%"
216 (mkdir-p (string-append #$output "/"
217 (normalize language)))
218 (setenv "LANGUAGE" language)
219 (apply invoke #$(file-append texinfo "/bin/makeinfo")
220 "-o" (string-append #$output "/"
224 (apply invoke #$(file-append texinfo "/bin/makeinfo")
227 (string-append #$output "/"
230 (if (string=? language "en")
232 (string-append "." language))
236 ;; Make sure images are available.
238 (string-append #$output "/" (normalize language)
241 (string-append #$output "/" (normalize language)
242 "/html_node/images"))))
245 (computed-file (string-append manual "-html-manual") build))
247 (define* (pdf-manual source #:key (languages %languages)
252 "Return the HTML manuals built from SOURCE for all LANGUAGES, with the given
254 (define manual-source
255 (texinfo-manual-source source
257 #:languages languages
260 ;; FIXME: This union works, except for the table of contents of non-English
261 ;; manuals, which contains escape sequences like "^^ca^^fe" instead of
265 ;; (texlive-union (list texlive-tex-texinfo
266 ;; texlive-generic-epsf
267 ;; texlive-fonts-ec)))
270 (with-imported-modules '((guix build utils))
272 (use-modules (guix build utils)
276 (define (normalize language) ;XXX: deduplicate
277 ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
278 (string-map (match-lambda
281 (string-downcase language)))
283 ;; Install a UTF-8 locale so that 'makeinfo' is at ease.
284 (setenv "GUIX_LOCPATH"
285 #+(file-append glibc-utf8-locales "/lib/locale"))
286 (setenv "LC_ALL" "en_US.utf8")
288 (string-append #+(file-append texlive "/bin") ":"
289 #+(file-append texinfo "/bin") ":"
291 ;; Below are command-line tools needed by
292 ;; 'texi2dvi' and friends.
293 #+(file-append sed "/bin") ":"
294 #+(file-append grep "/bin") ":"
295 #+(file-append coreutils "/bin") ":"
296 #+(file-append gawk "/bin") ":"
297 #+(file-append tar "/bin") ":"
298 #+(file-append diffutils "/bin")))
300 (setvbuf (current-output-port) 'line)
301 (setvbuf (current-error-port) 'line)
303 (setenv "HOME" (getcwd)) ;for kpathsea/mktextfm
305 ;; 'SOURCE_DATE_EPOCH' is honored by pdftex.
306 (setenv "SOURCE_DATE_EPOCH" "1")
308 (for-each (lambda (language)
309 (let ((opts `("--pdf"
312 ,(if (string=? language "en")
313 (string-append #$manual-source "/"
315 (string-append #$manual-source "/"
316 #$manual "." language ".texi")))))
317 (format #t "building PDF manual for language '~a'...~%"
319 (mkdir-p (string-append #$output "/"
320 (normalize language)))
321 (setenv "LANGUAGE" language)
324 ;; FIXME: Unfortunately building PDFs for non-Latin
325 ;; alphabets doesn't work:
326 ;; <https://lists.gnu.org/archive/html/help-texinfo/2012-01/msg00014.html>.
327 (guard (c ((invoke-error? c)
328 (format (current-error-port)
329 "~%~%Failed to produce \
330 PDF for language '~a'!~%~%"
332 (apply invoke #$(file-append texinfo "/bin/makeinfo")
334 (string-append #$output "/"
337 (if (string=? language "en")
345 (computed-file (string-append manual "-pdf-manual") build))
347 (define (guix-manual-text-domain source languages)
348 "Return the PO files for LANGUAGES of the 'guix-manual' text domain taken
351 (file-append* source "/po/doc"))
354 (with-imported-modules '((guix build utils))
356 (use-modules (guix build utils))
359 (for-each (lambda (language)
361 (string-append #$output "/" language
365 (invoke #+(file-append gnu-gettext "/bin/msgfmt")
367 (string-append directory "/guix-manual.mo")
368 (string-append #$po-directory "/guix-manual."
370 '#$(delete "en" languages)))))
372 (computed-file "guix-manual-po" build))
374 (define* (html-manual-indexes source
375 #:key (languages %languages)
380 (with-extensions (list guile-json-3)
381 (with-imported-modules '((guix build utils))
383 (use-modules (guix build utils)
391 (define (normalize language) ;XXX: deduplicate
392 ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
393 (string-map (match-lambda
396 (string-downcase language)))
398 (define-syntax-rule (with-language language exp ...)
399 (let ((lang (getenv "LANGUAGE")))
402 (setenv "LANGUAGE" language)
403 (setlocale LC_MESSAGES))
407 (setenv "LANGUAGE" lang)
408 (unsetenv "LANGUAGE"))
409 (setlocale LC_MESSAGES)))))
411 ;; (put 'with-language 'scheme-indent-function 1)
412 (define* (translate str language
413 #:key (domain "guix-manual"))
416 (bindtextdomain "guix-manual"
417 #+(guix-manual-text-domain
420 (bindtextdomain "iso_639-3" ;language names
421 #+(file-append iso-codes
423 (write (gettext ,str ,domain))))
425 (with-language language
426 ;; Since the 'gettext' function caches msgid translations,
427 ;; regardless of $LANGUAGE, we have to spawn a new process each
428 ;; time we want to translate to a different language. Bah!
429 (let* ((pipe (open-pipe* OPEN_READ
430 #+(file-append guile-2.2
432 "-c" (object->string exp)))
437 (define (seconds->string seconds language)
438 (let* ((time (make-time time-utc 0 seconds))
439 (date (time-utc->date time)))
440 (with-language language (date->string date "~e ~B ~Y"))))
442 (define (guix-url path)
443 (string-append #$%web-site-url path))
445 (define (sxml-index language title body)
446 ;; FIXME: Avoid duplicating styling info from guix-artwork.git.
447 `(html (@ (lang ,language))
449 (title ,(string-append title " — GNU Guix"))
450 (meta (@ (charset "UTF-8")))
451 (meta (@ (name "viewport") (content "width=device-width, initial-scale=1.0")))
453 (link (@ (rel "prefetch") (href ,(guix-url "menu/index.html"))))
455 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/elements.css"))))
456 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/common.css"))))
457 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/messages.css"))))
458 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/navbar.css"))))
459 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/breadcrumbs.css"))))
460 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/buttons.css"))))
461 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/footer.css"))))
463 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/page.css"))))
464 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/post.css")))))
466 (header (@ (class "navbar"))
467 (h1 (a (@ (class "branding")
468 (href #$%web-site-url)))
469 (span (@ (class "a11y-offset"))
471 (nav (@ (class "menu"))))
472 (nav (@ (class "breadcrumbs"))
473 (a (@ (class "crumb")
474 (href #$%web-site-url))
479 (define (language-index language)
481 (translate "GNU Guix Reference Manual" language))
487 (@ (class "page centered-block limit-width"))
489 (p (@ (class "post-metadata centered-text"))
491 ,(seconds->string #$date language))
495 (li (a (@ (href "html_node"))
496 "HTML, with one page per node"))
500 (if (string=? language
506 "HTML, entirely on one page"))
507 ,@(if (member language '("ru" "zh_CN"))
509 `((li (a (@ (href ,(string-append
511 (if (string=? language "en")
518 (define %iso639-languages
520 (assoc-ref (call-with-input-file
521 #+(file-append iso-codes
522 "/share/iso-codes/json/iso_639-3.json")
526 (define (language-code->name code)
527 "Return the full name of a language from its ISO-639-3 code."
528 (let ((code (match (string-index code #\_)
530 (index (string-take code index)))))
531 (any (lambda (language)
532 (and (string=? (or (assoc-ref language "alpha_2")
533 (assoc-ref language "alpha_3"))
535 (assoc-ref language "name")))
538 (define (top-level-index languages)
540 "GNU Guix Reference Manual")
545 (@ (class "page centered-block limit-width"))
548 "The GNU Guix Reference Manual is available in the following
551 ,@(map (lambda (language)
552 `(li (a (@ (href ,(normalize language)))
554 (language-code->name language)
556 #:domain "iso_639-3"))))
559 (define (write-html file sxml)
560 (call-with-output-file file
562 (display "<!DOCTYPE html>\n" port)
563 (sxml->xml sxml port))))
565 (setenv "GUIX_LOCPATH"
566 #+(file-append glibc-utf8-locales "/lib/locale"))
567 (setenv "LC_ALL" "en_US.utf8")
568 (setlocale LC_ALL "en_US.utf8")
570 (for-each (lambda (language)
572 (string-append #$output "/"
573 (normalize language)))
576 (write-html (string-append directory "/index.html")
577 (language-index language)))
580 (write-html (string-append #$output "/index.html")
581 (top-level-index '#$languages))))))
583 (computed-file "html-indexes" build))
585 (define* (pdf+html-manual source
586 #:key (languages %languages)
588 (date (time-second (current-time time-utc)))
590 "Return the union of the HTML and PDF manuals, as well as the indexes."
591 (directory-union (string-append manual "-manual")
595 #:languages languages
598 (list html-manual-indexes
599 html-manual pdf-manual))
602 (define (latest-commit+date directory)
603 "Return two values: the last commit ID (a hex string) for DIRECTORY, and its
604 commit date (an integer)."
605 (let* ((repository (repository-open directory))
606 (head (repository-head repository))
607 (oid (reference-target head))
608 (commit (commit-lookup repository oid)))
609 ;; TODO: Use (git describe) when it's widely available.
610 (values (oid->string oid) (commit-time commit))))
613 (let* ((root (canonicalize-path
614 (string-append (current-source-directory) "/..")))
615 (commit date (latest-commit+date root)))
616 (format (current-error-port)
617 "building manual from work tree around commit ~a, ~a~%"
619 (let* ((time (make-time time-utc 0 date))
620 (date (time-utc->date time)))
621 (date->string date "~e ~B ~Y")))
622 (pdf+html-manual (local-file root "guix" #:recursive? #t
623 #:select? (git-predicate root))
624 #:version (or (getenv "GUIX_MANUAL_VERSION")
625 (string-take commit 7))