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.
36 (gnu packages gettext)
38 (gnu packages guile-xyz)
39 (gnu packages iso-codes)
40 (gnu packages texinfo)
46 (@@ (guix self) file-append*))
48 (define translated-texi-manuals
49 (@@ (guix self) translate-texi-manuals))
52 (@@ (guix self) info-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")
61 '("de" "en" "es" "fr" "ru" "zh_CN"))
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."
67 (module-ref (resolve-interface '(gnu packages graphviz))
71 (file-append* source "doc/images"))
74 (with-imported-modules '((guix build utils))
76 (use-modules (guix build utils)
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")
91 (for-each (lambda (dot-file)
92 (for-each (cut dot->image dot-file <>)
94 (find-files #$images "\\.dot$"))
97 (for-each (lambda (png-file)
98 (install-file png-file #$output))
99 (find-files #$images "\\.png$")))))
101 (computed-file "texinfo-manual-images" build))
103 (define* (texinfo-manual-source source #:key
105 (languages %languages)
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"))
113 (file-append* source "gnu/system/examples"))
116 (with-imported-modules '((guix build utils))
118 (use-modules (guix build utils)
121 (define (make-version-texi language)
122 ;; Create the 'version.texi' file for LANGUAGE.
123 (let ((file (if (string=? language "en")
125 (string-append "version-" language ".texi"))))
126 (call-with-output-file (string-append #$output "/" file)
128 (let* ((version #$version)
129 (time (make-time time-utc 0 #$date))
130 (date (time-utc->date time)))
133 @set UPDATED-MONTH ~a
136 (date->string date "~e ~B ~Y")
137 (date->string date "~B ~Y")
138 version version))))))
140 (install-file #$(file-append* documentation "/htmlxref.cnf")
143 (for-each (lambda (texi)
144 (install-file texi #$output))
145 (append (find-files #$documentation "\\.(texi|scm)$")
146 (find-files #$(translated-texi-manuals source)
149 ;; Create 'version.texi'.
150 (for-each make-version-texi '#$languages)
152 ;; Copy configuration templates that the manual includes.
153 (for-each (lambda (template)
156 #$output "/os-config-"
157 (basename template ".tmpl")
159 (find-files #$examples "\\.tmpl$"))
161 (symlink #$(texinfo-manual-images source)
162 (string-append #$output "/images")))))
164 (computed-file "texinfo-manual-source" build))
166 (define %web-site-url
167 ;; URL of the web site home page.
168 (or (getenv "GUIX_WEB_SITE_URL")
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\" />"))
177 (define guile-lib/htmlprag-fixed
178 ;; Guile-Lib with a hotfix for (htmlprag).
182 (inherit (package-source guile-lib))
183 (modules '(( guix build utils)))
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\\)\\).*")
196 (substitute-keyword-arguments (package-arguments guile-lib)
197 ((#:phases phases '%standard-phases)
198 `(modify-phases ,phases
199 (add-before 'check 'skip-known-failure
201 ;; XXX: The above change causes one test failure among
202 ;; the htmlprag tests.
203 (setenv "XFAIL_TESTS" "htmlprag.scm")
206 (define* (syntax-highlighted-html input
208 (name "highlighted-syntax")
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')."
215 (with-extensions (list guile-lib/htmlprag-fixed guile-syntax-highlight)
216 (with-imported-modules '((guix build utils))
218 (use-modules (htmlprag)
220 (syntax-highlight scheme)
221 (syntax-highlight lexers)
226 (define (pair-open/close lst)
227 ;; Pair 'open' and 'close' tags produced by 'highlights' and
228 ;; produce nested 'paren' tags instead.
233 ((('open open) rest ...)
236 (loop rest (+ 1 level) '()))
237 (lambda (inner close rest)
239 (cons `(paren ,level ,open ,inner ,close)
241 ((('close str) rest ...)
243 (values (reverse result) str rest)
245 (format (current-error-port)
246 "warning: extra closing paren; context:~% ~y~%"
248 (loop rest 0 (cons `(close ,str) result)))))
250 (loop rest level (cons item result)))
253 (format (current-error-port)
254 "warning: missing ~a closing parens; context:~% ~y%"
255 level (reverse result)))
256 (values (reverse result) "" '())))))
258 (define (highlights->sxml* highlights)
259 ;; Like 'highlights->sxml', but handle nested 'paren tags. This
260 ;; allows for paren matching highlights via appropriate CSS
261 ;; "hover" properties.
262 (define (tag->class tag)
263 (string-append "syntax-" (symbol->string tag)))
266 ((? string? str) str)
267 (('paren level open (body ...) close)
268 `(span (@ (class ,(string-append "syntax-paren"
269 (number->string level))))
271 (span (@ (class "syntax-symbol"))
272 ,@(highlights->sxml* body))
275 `(span (@ (class ,(tag->class tag))) ,text)))
278 (define entity->string
284 (e (pk 'unknown-entity e) (primitive-exit 2))))
286 (define (concatenate-snippets pieces)
287 ;; Concatenate PIECES, which contains strings and entities,
288 ;; replacing entities with their corresponding string.
289 (let loop ((pieces pieces)
293 (string-concatenate-reverse strings))
294 (((? string? str) . rest)
295 (loop rest (cons str strings)))
296 ((('*ENTITY* "additional" entity) . rest)
297 (loop rest (cons (entity->string entity) strings)))
298 ((('span _ lst ...) . rest) ;for <span class="roman">
299 (loop (append lst rest) strings))
301 (pk 'unsupported-code-snippet something)
302 (primitive-exit 1)))))
304 (define (syntax-highlight sxml)
305 ;; Recurse over SXML and syntax-highlight code snippets.
307 (('*TOP* decl body ...)
308 `(*TOP* ,decl ,@(map syntax-highlight body)))
311 (link (@ (rel "stylesheet")
313 (href #$syntax-css-url)))))
314 (('pre ('@ ('class "lisp")) code-snippet ...)
315 `(pre (@ (class "lisp"))
318 (highlight lex-scheme
319 (concatenate-snippets code-snippet))))))
320 ((tag ('@ attributes ...) body ...)
321 `(,tag (@ ,@attributes) ,@(map syntax-highlight body)))
323 `(,tag ,@(map syntax-highlight body)))
327 (define (process-html file)
328 ;; Parse FILE and perform syntax highlighting for its Scheme
329 ;; snippets. Install the result to #$output.
330 (format (current-error-port) "processing ~a...~%" file)
331 (let* ((shtml (call-with-input-file file html->shtml))
332 (highlighted (syntax-highlight shtml))
333 (base (string-drop file (string-length #$input)))
334 (target (string-append #$output base)))
335 (mkdir-p (dirname target))
336 (call-with-output-file target
338 (write-shtml-as-html highlighted port)))))
340 (define (copy-as-is file)
341 ;; Copy FILE as is to #$output.
342 (let* ((base (string-drop file (string-length #$input)))
343 (target (string-append #$output base)))
344 (mkdir-p (dirname target))
347 (if (eq? 'symlink (stat:type (lstat file)))
348 (symlink (readlink file) target)
351 (let ((errno (system-error-errno args)))
352 (pk 'error-link file target (strerror errno))
353 (primitive-exit 3))))))
355 ;; Install a UTF-8 locale so we can process UTF-8 files.
356 (setenv "GUIX_LOCPATH"
357 #+(file-append glibc-utf8-locales "/lib/locale"))
358 (setlocale LC_ALL "en_US.utf8")
360 (n-par-for-each (parallel-job-count)
362 (if (string-suffix? ".html" file)
365 (find-files #$input))))))
367 (computed-file name build))
369 (define* (html-manual source #:key (languages %languages)
373 (options %makeinfo-html-options))
374 "Return the HTML manuals built from SOURCE for all LANGUAGES, with the given
376 (define manual-source
377 (texinfo-manual-source source
379 #:languages languages
383 (texinfo-manual-images source))
386 (with-imported-modules '((guix build utils))
388 (use-modules (guix build utils)
391 (define (normalize language)
392 ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
393 (string-map (match-lambda
396 (string-downcase language)))
398 (define (language->texi-file-name language)
399 (if (string=? language "en")
400 (string-append #$manual-source "/"
402 (string-append #$manual-source "/"
403 #$manual "." language ".texi")))
405 ;; Install a UTF-8 locale so that 'makeinfo' is at ease.
406 (setenv "GUIX_LOCPATH"
407 #+(file-append glibc-utf8-locales "/lib/locale"))
408 (setenv "LC_ALL" "en_US.utf8")
410 (setvbuf (current-output-port) 'line)
411 (setvbuf (current-error-port) 'line)
413 (for-each (lambda (language)
414 (let* ((texi (language->texi-file-name language))
416 "-c" ,(string-append "TOP_NODE_UP_URL=/manual/"
420 (format #t "building HTML manual for language '~a'...~%"
422 (mkdir-p (string-append #$output "/"
423 (normalize language)))
424 (setenv "LANGUAGE" language)
425 (apply invoke #$(file-append texinfo "/bin/makeinfo")
426 "-o" (string-append #$output "/"
430 (apply invoke #$(file-append texinfo "/bin/makeinfo")
433 (string-append #$output "/"
436 (if (string=? language "en")
438 (string-append "." language))
442 ;; Make sure images are available.
444 (string-append #$output "/" (normalize language)
447 (string-append #$output "/" (normalize language)
448 "/html_node/images"))))
449 (filter (compose file-exists? language->texi-file-name)
452 (let* ((name (string-append manual "-html-manual"))
453 (manual (computed-file name build)))
454 (syntax-highlighted-html manual
455 #:name (string-append name "-highlighted"))))
457 (define* (pdf-manual source #:key (languages %languages)
462 "Return the HTML manuals built from SOURCE for all LANGUAGES, with the given
464 (define manual-source
465 (texinfo-manual-source source
467 #:languages languages
470 ;; FIXME: This union works, except for the table of contents of non-English
471 ;; manuals, which contains escape sequences like "^^ca^^fe" instead of
475 ;; (texlive-union (list texlive-tex-texinfo
476 ;; texlive-generic-epsf
477 ;; texlive-fonts-ec)))
480 (with-imported-modules '((guix build utils))
482 (use-modules (guix build utils)
486 (define (normalize language) ;XXX: deduplicate
487 ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
488 (string-map (match-lambda
491 (string-downcase language)))
493 ;; Install a UTF-8 locale so that 'makeinfo' is at ease.
494 (setenv "GUIX_LOCPATH"
495 #+(file-append glibc-utf8-locales "/lib/locale"))
496 (setenv "LC_ALL" "en_US.utf8")
498 (string-append #+(file-append texlive "/bin") ":"
499 #+(file-append texinfo "/bin") ":"
501 ;; Below are command-line tools needed by
502 ;; 'texi2dvi' and friends.
503 #+(file-append sed "/bin") ":"
504 #+(file-append grep "/bin") ":"
505 #+(file-append coreutils "/bin") ":"
506 #+(file-append gawk "/bin") ":"
507 #+(file-append tar "/bin") ":"
508 #+(file-append diffutils "/bin")))
510 (setvbuf (current-output-port) 'line)
511 (setvbuf (current-error-port) 'line)
513 (setenv "HOME" (getcwd)) ;for kpathsea/mktextfm
515 ;; 'SOURCE_DATE_EPOCH' is honored by pdftex.
516 (setenv "SOURCE_DATE_EPOCH" "1")
518 (for-each (lambda (language)
519 (let ((opts `("--pdf"
522 ,(if (string=? language "en")
523 (string-append #$manual-source "/"
525 (string-append #$manual-source "/"
526 #$manual "." language ".texi")))))
527 (format #t "building PDF manual for language '~a'...~%"
529 (mkdir-p (string-append #$output "/"
530 (normalize language)))
531 (setenv "LANGUAGE" language)
534 ;; FIXME: Unfortunately building PDFs for non-Latin
535 ;; alphabets doesn't work:
536 ;; <https://lists.gnu.org/archive/html/help-texinfo/2012-01/msg00014.html>.
537 (guard (c ((invoke-error? c)
538 (format (current-error-port)
539 "~%~%Failed to produce \
540 PDF for language '~a'!~%~%"
542 (apply invoke #$(file-append texinfo "/bin/makeinfo")
544 (string-append #$output "/"
547 (if (string=? language "en")
555 (computed-file (string-append manual "-pdf-manual") build))
557 (define (guix-manual-text-domain source languages)
558 "Return the PO files for LANGUAGES of the 'guix-manual' text domain taken
561 (file-append* source "/po/doc"))
564 (with-imported-modules '((guix build utils))
566 (use-modules (guix build utils))
569 (for-each (lambda (language)
571 (string-append #$output "/" language
575 (invoke #+(file-append gnu-gettext "/bin/msgfmt")
577 (string-append directory "/guix-manual.mo")
578 (string-append #$po-directory "/guix-manual."
580 '#$(delete "en" languages)))))
582 (computed-file "guix-manual-po" build))
584 (define* (html-manual-indexes source
585 #:key (languages %languages)
588 (title (if (string=? "guix" manual)
589 "GNU Guix Reference Manual"
590 "GNU Guix Cookbook"))
593 (with-extensions (list guile-json-3)
594 (with-imported-modules '((guix build utils))
596 (use-modules (guix build utils)
604 (define (normalize language) ;XXX: deduplicate
605 ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
606 (string-map (match-lambda
609 (string-downcase language)))
611 (define-syntax-rule (with-language language exp ...)
612 (let ((lang (getenv "LANGUAGE")))
615 (setenv "LANGUAGE" language)
616 (setlocale LC_MESSAGES))
620 (setenv "LANGUAGE" lang)
621 (unsetenv "LANGUAGE"))
622 (setlocale LC_MESSAGES)))))
624 ;; (put 'with-language 'scheme-indent-function 1)
625 (define* (translate str language
626 #:key (domain "guix-manual"))
629 (bindtextdomain "guix-manual"
630 #+(guix-manual-text-domain
633 (bindtextdomain "iso_639-3" ;language names
634 #+(file-append iso-codes
636 (write (gettext ,str ,domain))))
638 (with-language language
639 ;; Since the 'gettext' function caches msgid translations,
640 ;; regardless of $LANGUAGE, we have to spawn a new process each
641 ;; time we want to translate to a different language. Bah!
642 (let* ((pipe (open-pipe* OPEN_READ
643 #+(file-append guile-2.2
645 "-c" (object->string exp)))
650 (define (seconds->string seconds language)
651 (let* ((time (make-time time-utc 0 seconds))
652 (date (time-utc->date time)))
653 (with-language language (date->string date "~e ~B ~Y"))))
655 (define (guix-url path)
656 (string-append #$%web-site-url path))
658 (define (sxml-index language title body)
659 ;; FIXME: Avoid duplicating styling info from guix-artwork.git.
660 `(html (@ (lang ,language))
662 (title ,(string-append title " — GNU Guix"))
663 (meta (@ (charset "UTF-8")))
664 (meta (@ (name "viewport") (content "width=device-width, initial-scale=1.0")))
666 (link (@ (rel "prefetch") (href ,(guix-url "menu/index.html"))))
668 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/elements.css"))))
669 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/common.css"))))
670 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/messages.css"))))
671 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/navbar.css"))))
672 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/breadcrumbs.css"))))
673 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/buttons.css"))))
674 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/footer.css"))))
676 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/page.css"))))
677 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/post.css")))))
679 (header (@ (class "navbar"))
680 (h1 (a (@ (class "branding")
681 (href #$%web-site-url)))
682 (span (@ (class "a11y-offset"))
684 (nav (@ (class "menu"))))
685 (nav (@ (class "breadcrumbs"))
686 (a (@ (class "crumb")
687 (href #$%web-site-url))
692 (define (language-index language)
694 (translate #$title language))
700 (@ (class "page centered-block limit-width"))
702 (p (@ (class "post-metadata centered-text"))
704 ,(seconds->string #$date language))
708 (li (a (@ (href "html_node"))
709 "HTML, with one page per node"))
713 (if (string=? language
719 "HTML, entirely on one page"))
720 ,@(if (member language '("ru" "zh_CN"))
722 `((li (a (@ (href ,(string-append
724 (if (string=? language "en")
731 (define %iso639-languages
733 (assoc-ref (call-with-input-file
734 #+(file-append iso-codes
735 "/share/iso-codes/json/iso_639-3.json")
739 (define (language-code->name code)
740 "Return the full name of a language from its ISO-639-3 code."
741 (let ((code (match (string-index code #\_)
743 (index (string-take code index)))))
744 (any (lambda (language)
745 (and (string=? (or (assoc-ref language "alpha_2")
746 (assoc-ref language "alpha_3"))
748 (assoc-ref language "name")))
751 (define (top-level-index languages)
752 (define title #$title)
757 (@ (class "page centered-block limit-width"))
760 "This document is available in the following
763 ,@(map (lambda (language)
764 `(li (a (@ (href ,(normalize language)))
766 (language-code->name language)
768 #:domain "iso_639-3"))))
771 (define (write-html file sxml)
772 (call-with-output-file file
774 (display "<!DOCTYPE html>\n" port)
775 (sxml->xml sxml port))))
777 (setenv "GUIX_LOCPATH"
778 #+(file-append glibc-utf8-locales "/lib/locale"))
779 (setenv "LC_ALL" "en_US.utf8")
780 (setlocale LC_ALL "en_US.utf8")
782 (for-each (lambda (language)
784 (string-append #$output "/"
785 (normalize language)))
788 (write-html (string-append directory "/index.html")
789 (language-index language)))
792 (write-html (string-append #$output "/index.html")
793 (top-level-index '#$languages))))))
795 (computed-file "html-indexes" build))
797 (define* (pdf+html-manual source
798 #:key (languages %languages)
800 (date (time-second (current-time time-utc)))
802 "Return the union of the HTML and PDF manuals, as well as the indexes."
803 (directory-union (string-append manual "-manual")
807 #:languages languages
810 (list html-manual-indexes
811 html-manual pdf-manual))
814 (define (latest-commit+date directory)
815 "Return two values: the last commit ID (a hex string) for DIRECTORY, and its
816 commit date (an integer)."
817 (let* ((repository (repository-open directory))
818 (head (repository-head repository))
819 (oid (reference-target head))
820 (commit (commit-lookup repository oid)))
821 ;; TODO: Use (git describe) when it's widely available.
822 (values (oid->string oid) (commit-time commit))))
825 (let* ((root (canonicalize-path
826 (string-append (current-source-directory) "/..")))
827 (commit date (latest-commit+date root)))
828 (format (current-error-port)
829 "building manual from work tree around commit ~a, ~a~%"
831 (let* ((time (make-time time-utc 0 date))
832 (date (time-utc->date time)))
833 (date->string date "~e ~B ~Y")))
834 (pdf+html-manual (local-file root "guix" #:recursive? #t
835 #:select? (git-predicate root))
836 #:version (or (getenv "GUIX_MANUAL_VERSION")
837 (string-take commit 7))