1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2019-2022 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2020 Björn Höfling <bjoern.hoefling@bjoernhoefling.de>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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:
24 ;; guix build -f build.scm
26 ;; The result is a directory hierarchy that can be used as the manual/
27 ;; sub-directory of the web site.
36 (gnu packages compression)
38 (gnu packages gettext)
40 (gnu packages guile-xyz)
41 (gnu packages iso-codes)
42 (gnu packages texinfo)
51 (@@ (guix self) file-append*))
53 (define translated-texi-manuals
54 (@@ (guix self) translate-texi-manuals))
57 (@@ (guix self) info-manual))
60 ;; The manual to build--i.e., the base name of a .texi file, such as "guix"
61 ;; or "guix-cookbook".
62 (or (getenv "GUIX_MANUAL")
65 (define %manual-languages
66 ;; Available translations for the 'guix-manual' text domain.
67 '("de" "en" "es" "fr" "ru" "zh_CN"))
69 (define %cookbook-languages
70 ;; Available translations for the 'guix-cookbook' text domain.
71 '("de" "en" "fr" "sk"))
74 ;; Available translations for the document being built.
75 (if (string=? %manual "guix-cookbook")
79 (define (texinfo-manual-images source)
80 "Return a directory containing all the images used by the user manual, taken
81 from SOURCE, the root of the source tree."
83 (module-ref (resolve-interface '(gnu packages graphviz))
87 (file-append* source "doc/images"))
90 (with-imported-modules '((guix build utils))
92 (use-modules (guix build utils)
95 (define (dot->image dot-file format)
96 (invoke #+(file-append graphviz "/bin/dot")
97 "-T" format "-Gratio=.9" "-Gnodesep=.005"
98 "-Granksep=.00005" "-Nfontsize=9"
99 "-Nheight=.1" "-Nwidth=.1"
100 "-o" (string-append #$output "/"
101 (basename dot-file ".dot")
107 (for-each (lambda (dot-file)
108 (for-each (cut dot->image dot-file <>)
110 (find-files #$images "\\.dot$"))
113 (for-each (lambda (png-file)
114 (install-file png-file #$output))
115 (find-files #$images "\\.png$")))))
117 (computed-file "texinfo-manual-images" build))
119 (define* (texinfo-manual-source source #:key
121 (languages %languages)
123 "Gather all the source files of the Texinfo manuals from SOURCE--.texi file
124 as well as images, OS examples, and translations."
125 (define documentation
126 (file-append* source "doc"))
129 (file-append* source "gnu/system/examples"))
132 (with-imported-modules '((guix build utils))
134 (use-modules (guix build utils)
137 (define (make-version-texi language)
138 ;; Create the 'version.texi' file for LANGUAGE.
139 (let ((file (if (string=? language "en")
141 (string-append "version-" language ".texi"))))
142 (call-with-output-file (string-append #$output "/" file)
144 (let* ((version #$version)
145 (time (make-time time-utc 0 #$date))
146 (date (time-utc->date time)))
149 @set UPDATED-MONTH ~a
152 (date->string date "~e ~B ~Y")
153 (date->string date "~B ~Y")
154 version version))))))
156 (install-file #$(file-append documentation "/htmlxref.cnf")
159 (for-each (lambda (texi)
160 (install-file texi #$output))
161 (append (find-files #$documentation "\\.(texi|scm|json)$")
162 (find-files #$(translated-texi-manuals source)
165 ;; Create 'version.texi'.
166 (for-each make-version-texi '#$languages)
168 ;; Copy configuration templates that the manual includes.
169 (for-each (lambda (template)
172 #$output "/os-config-"
173 (basename template ".tmpl")
175 (find-files #$examples "\\.tmpl$"))
177 (symlink #$(texinfo-manual-images source)
178 (string-append #$output "/images")))))
180 (computed-file "texinfo-manual-source" build))
182 (define %web-site-url
183 ;; URL of the web site home page.
184 (or (getenv "GUIX_WEB_SITE_URL")
187 (define %makeinfo-html-options
188 ;; Options passed to 'makeinfo --html'.
189 '("--css-ref=https://www.gnu.org/software/gnulib/manual.css"
190 "-c" "EXTRA_HEAD=<meta name=\"viewport\" \
191 content=\"width=device-width, initial-scale=1\" />"))
193 (define (normalize-language-code language) ;XXX: deduplicate
194 ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
195 (string-map (match-lambda
198 (string-downcase language)))
200 (define* (html-manual-identifier-index manual base-url
202 (name "html-manual-identifier-index"))
203 "Return an index of all the identifiers that appear in MANUAL, a
204 makeinfo-generated manual. The index is a file that contains an alist; each
205 key is an identifier and the associated value is the URL reference pointing to
206 that identifier. The URL is constructed by concatenating BASE-URL to the
209 (with-extensions (list guile-lib)
210 (with-imported-modules '((guix build utils))
212 (use-modules (guix build utils)
219 (ice-9 pretty-print))
221 (%strict-tokenizer? #t)
224 (let ((prefix (string-append #$manual "/")))
226 ;; Return the URL for FILE.
227 (let ((file (string-drop file (string-length prefix)))
229 (if (string-null? base)
231 (string-append base "/" file))))))
233 (define (underscore-decode str)
234 ;; Decode STR, an "underscore-encoded" string as produced by
235 ;; makeinfo for indexes, such as "_0025base_002dservices" for
239 (match (string-index str #\_)
241 (string-concatenate-reverse (cons str result)))
243 (let ((char (string->number
244 (substring str (+ index 1) (+ index 5))
246 (loop (string-drop str (+ index 5))
247 (append (list (string (integer->char char))
248 (string-take str index))
251 (define (anchor-id->key id)
252 ;; Convert ID, an anchor ID such as
253 ;; "index-pam_002dlimits_002dservice" to the corresponding key,
254 ;; "pam-limits-service" in this example. Drop the suffix of
255 ;; duplicate anchor IDs like "operating_002dsystem-1".
256 (let ((id (if (any (cut string-suffix? <> id)
257 '("-1" "-2" "-3" "-4" "-5"))
258 (string-drop-right id 2)
261 (string-drop id (string-length "index-")))))
263 (define* (collect-anchors file #:optional (anchors '()))
264 ;; Collect the anchors that appear in FILE, a makeinfo-generated
265 ;; file. Grab those from <dt> tags, which corresponds to
266 ;; Texinfo @deftp, @defvr, etc. Return ANCHORS augmented with
267 ;; more name/reference pairs.
268 (define string-or-entity?
271 (('*ENTITY* _ ...) #t)
274 (define (worthy-entry? lst)
276 ;; Scheme Variable: <strong>x</strong>
278 ;; <code>cups-configuration</code> parameter: …
279 (let loop ((lst lst))
281 (((? string-or-entity?) rest ...)
283 ((('strong _ ...) _ ...)
285 ((('span ('@ ('class "symbol-definition-category"))
286 (? string-or-entity?) ...) rest ...)
291 (let ((shtml (call-with-input-file file html->shtml)))
292 (let loop ((shtml shtml)
295 (('dt ('@ ('id id) _ ...) rest ...)
296 (if (and (string-prefix? "index-" id)
297 (worthy-entry? rest))
298 (alist-cons (anchor-id->key id)
299 (string-append (file-url file)
303 ((tag ('@ _ ...) body ...)
304 (fold loop anchors body))
306 (fold loop anchors body))
309 (define (html-files directory)
310 ;; Return the list of HTML files under DIRECTORY.
311 (map (cut string-append directory "/" <>)
312 (or (scandir #$manual (lambda (file)
313 (string-suffix? ".html" file)))
318 (n-par-map (parallel-job-count)
319 (cut collect-anchors <>)
320 (html-files #$manual)))
322 (((key1 . url1) (key2 . url2))
323 (if (string=? key1 key2)
325 (string<? key1 key2))))))
327 (call-with-output-file #$output
329 (display ";; Identifier index for the manual.\n\n"
331 (pretty-print anchors port)))))))
333 (computed-file name build))
335 (define* (html-identifier-indexes manual directory-suffix
336 #:key (languages %languages)
337 (manual-name %manual)
338 (base-url (const "")))
339 (map (lambda (language)
340 (let ((language (normalize-language-code language)))
342 (html-manual-identifier-index
343 (file-append manual "/" language directory-suffix)
345 #:name (string-append manual-name "-html-index-"
349 (define* (syntax-highlighted-html input
351 (name "highlighted-syntax")
352 (languages %languages)
354 (html-identifier-indexes input ""
358 (html-identifier-indexes input
363 "/static/base/css/code.css"))
364 "Return a derivation called NAME that processes all the HTML files in INPUT
365 to (1) add them a link to SYNTAX-CSS-URL, and (2) highlight the syntax of all
366 its <pre class=\"lisp\"> blocks (as produced by 'makeinfo --html')."
368 (with-extensions (list guile-lib guile-syntax-highlight)
369 (with-imported-modules '((guix build utils))
371 (use-modules (htmlprag)
373 (syntax-highlight scheme)
374 (syntax-highlight lexers)
382 (%strict-tokenizer? #t)
384 (define (pair-open/close lst)
385 ;; Pair 'open' and 'close' tags produced by 'highlights' and
386 ;; produce nested 'paren' tags instead.
391 ((('open open) rest ...)
394 (loop rest (+ 1 level) '()))
395 (lambda (inner close rest)
397 (cons `(paren ,level ,open ,inner ,close)
399 ((('close str) rest ...)
401 (values (reverse result) str rest)
403 (format (current-error-port)
404 "warning: extra closing paren; context:~% ~y~%"
406 (loop rest 0 (cons `(close ,str) result)))))
408 (loop rest level (cons item result)))
411 (format (current-error-port)
412 "warning: missing ~a closing parens; context:~% ~y%"
413 level (reverse result)))
414 (values (reverse result) "" '())))))
416 (define (highlights->sxml* highlights anchors)
417 ;; Like 'highlights->sxml', but handle nested 'paren tags. This
418 ;; allows for paren matching highlights via appropriate CSS
419 ;; "hover" properties. When a symbol is encountered, look it up
420 ;; in ANCHORS, a vhash, and emit the corresponding href, if any.
421 (define (tag->class tag)
422 (string-append "syntax-" (symbol->string tag)))
425 ((? string? str) str)
426 (('paren level open (body ...) close)
427 `(span (@ (class ,(string-append "syntax-paren"
428 (number->string level))))
430 (span (@ (class "syntax-symbol"))
431 ,@(highlights->sxml* body anchors))
434 ;; Check whether we can emit a hyperlink for TEXT.
435 (match (vhash-assoc text anchors)
437 `(span (@ (class ,(tag->class 'symbol))) ,text))
439 `(a (@ (class ,(tag->class 'symbol)) (href ,target))
442 `(span (@ (class ,(tag->class tag))) ,text)))
445 (define entity->string
452 (e (pk 'unknown-entity e) (primitive-exit 2))))
454 (define (concatenate-snippets pieces)
455 ;; Concatenate PIECES, which contains strings and entities,
456 ;; replacing entities with their corresponding string.
457 (let loop ((pieces pieces)
461 (string-concatenate-reverse strings))
462 (((? string? str) . rest)
463 (loop rest (cons str strings)))
464 ((('*ENTITY* "additional" entity) . rest)
465 (loop rest (cons (entity->string entity) strings)))
466 ((('span _ lst ...) . rest) ;for <span class="roman">
467 (loop (append lst rest) strings))
468 ((('var name) . rest) ;for @var{name} within @lisp
469 (loop rest (cons name strings))) ;XXX: losing formatting
471 (pk 'unsupported-code-snippet something)
472 (primitive-exit 1)))))
474 (define (highlight-definition id category symbol args)
475 ;; Produce stylable HTML for the given definition (an @deftp,
476 ;; @deffn, or similar).
477 `(dt (@ (id ,id) (class "symbol-definition"))
478 (span (@ (class "symbol-definition-category"))
480 (span (@ (class "symbol-definition-prototype"))
481 ,symbol " " ,@args)))
485 (string-every char-set:whitespace obj)))
487 (define (syntax-highlight sxml anchors)
488 ;; Recurse over SXML and syntax-highlight code snippets.
489 (let loop ((sxml sxml))
491 (('*TOP* decl body ...)
492 `(*TOP* ,decl ,@(map loop body)))
495 (link (@ (rel "stylesheet")
497 (href #$syntax-css-url)))))
498 (('pre ('@ ('class "lisp")) code-snippet ...)
499 `(pre (@ (class "lisp"))
502 (highlight lex-scheme
503 (concatenate-snippets code-snippet)))
506 ;; Replace the ugly <strong> used for @deffn etc., which
507 ;; translate to <dt>, with more stylable markup.
508 (('dt (@ ('id id)) category ... ('strong thing))
509 (highlight-definition id category thing '()))
510 (('dt (@ ('id id)) category ... ('strong thing)
511 (? space?) ('em args ...))
512 (highlight-definition id category thing args))
514 ((tag ('@ attributes ...) body ...)
515 `(,tag (@ ,@attributes) ,@(map loop body)))
517 `(,tag ,@(map loop body)))
521 (define (process-html file anchors)
522 ;; Parse FILE and perform syntax highlighting for its Scheme
523 ;; snippets. Install the result to #$output.
524 (format (current-error-port) "processing ~a...~%" file)
525 (let* ((shtml (call-with-input-file file html->shtml))
526 (highlighted (syntax-highlight shtml anchors))
527 (base (string-drop file (string-length #$input)))
528 (target (string-append #$output base)))
529 (mkdir-p (dirname target))
530 (call-with-output-file target
532 (write-shtml-as-html highlighted port)))))
534 (define (copy-as-is file)
535 ;; Copy FILE as is to #$output.
536 (let* ((base (string-drop file (string-length #$input)))
537 (target (string-append #$output base)))
538 (mkdir-p (dirname target))
541 (if (eq? 'symlink (stat:type (lstat file)))
542 (symlink (readlink file) target)
545 (let ((errno (system-error-errno args)))
546 (pk 'error-link file target (strerror errno))
547 (primitive-exit 3))))))
549 (define (html? file stat)
550 (string-suffix? ".html" file))
552 (define language+node-anchors
554 ((language files ...)
556 (fold (lambda (file vhash)
557 (let ((alist (call-with-input-file file read)))
558 ;; Use 'fold-right' so that the first entry
559 ;; wins (e.g., "car" from "Pairs" rather than
560 ;; from "rnrs base" in the Guile manual).
561 (fold-right (match-lambda*
562 (((key . value) vhash)
563 (vhash-cons key value vhash)))
569 (define mono-node-anchors
570 ;; List of language/vhash pairs, where each vhash maps an
571 ;; identifier to the corresponding URL in a single-page manual.
572 (map language+node-anchors '#$mono-node-indexes))
574 (define multi-node-anchors
575 ;; Likewise for split-node manuals.
576 (map language+node-anchors '#$split-node-indexes))
578 ;; Install a UTF-8 locale so we can process UTF-8 files.
579 (setenv "GUIX_LOCPATH"
580 #+(file-append glibc-utf8-locales "/lib/locale"))
581 (setlocale LC_ALL "en_US.utf8")
583 ;; First process the mono-node 'guix.html' files.
584 (for-each (match-lambda
585 ((language . anchors)
586 (let ((files (find-files
587 (string-append #$input "/" language)
588 "^guix(-cookbook|)(\\.[a-zA-Z_-]+)?\\.html$")))
589 (n-par-for-each (parallel-job-count)
590 (cut process-html <> anchors)
594 ;; Process the multi-node HTML files.
595 (for-each (match-lambda
596 ((language . anchors)
597 (let ((files (find-files
598 (string-append #$input "/" language
601 (n-par-for-each (parallel-job-count)
602 (cut process-html <> anchors)
606 ;; Last, copy non-HTML files as is.
608 (find-files #$input (negate html?)))))))
610 (computed-file name build))
612 (define* (stylized-html source input
614 (languages %languages)
616 (manual-css-url "/static/base/css/manual.css"))
617 "Process all the HTML files in INPUT; add them MANUAL-CSS-URL as a <style>
618 link, and add a menu to choose among LANGUAGES. Use the Guix PO files found
621 (with-extensions (list guile-lib)
622 (with-imported-modules `((guix build utils)
624 => ,(localization-helper-module
627 (use-modules (htmlprag)
634 (define* (menu-dropdown #:key (label "Item") (url "#") (items '()))
635 ;; Return an SHTML <li> element representing a dropdown for the
636 ;; navbar. LABEL is the text of the dropdown menu, and ITEMS is
637 ;; the list of items in this menu.
638 (define id "visible-dropdown")
641 (@ (class "navbar-menu-item dropdown dropdown-btn"))
642 (input (@ (class "navbar-menu-hidden-input")
646 (label (@ (for ,id)) ,label)
647 (label (@ (for "all-dropdowns-hidden")) ,label)
649 (@ (class "navbar-submenu")
650 (id "navbar-submenu"))
651 (div (@ (class "navbar-submenu-triangle"))
655 (define (menu-item label url)
656 ;; Return an SHTML <li> element for a menu item with the given
658 `(li (a (@ (class "navbar-menu-item")
662 (define* (navigation-bar menus #:key split-node?)
663 ;; Return the navigation bar showing all of MENUS.
664 `(header (@ (class "navbar"))
665 (h1 (a (@ (class "branding")
666 (href ,(if split-node? ".." "#")))))
667 (nav (@ (class "navbar-menu"))
668 (input (@ (class "navbar-menu-hidden-input")
671 (id "all-dropdowns-hidden")))
674 ;; This is the button that shows up on small screen in
675 ;; lieu of the drop-down button.
676 (a (@ (class "navbar-menu-btn")
677 (href ,(if split-node? "../.." ".."))))))
679 (define* (base-language-url code manual
681 ;; Return the base URL of MANUAL for language CODE.
683 (string-append "../../" (normalize code) "/html_node")
684 (string-append "../" (normalize code) "/" manual
685 (if (string=? code "en")
687 (string-append "." code))
690 (define (language-menu-items file)
691 ;; Return the language menu items to be inserted in FILE.
693 (string-contains file "/html_node/"))
697 (menu-item (language-code->native-name code)
698 (base-language-url code #$manual
704 (if (string=? #$manual "guix-cookbook")
705 "https://translate.fedoraproject.org/projects/guix/documentation-cookbook/"
706 "https://translate.fedoraproject.org/projects/guix/documentation-manual/")))))
708 (define (stylized-html sxml file)
709 ;; Return SXML, which was read from FILE, with additional
712 (string-contains file "/html_node/"))
714 (let loop ((sxml sxml))
716 (('*TOP* decl body ...)
717 `(*TOP* ,decl ,@(map loop body)))
718 (('head elements ...)
719 ;; Add reference to our own manual CSS, which provides
720 ;; support for the language menu.
722 (link (@ (rel "stylesheet")
724 (href #$manual-css-url)))))
725 (('body ('@ attributes ...) elements ...)
726 `(body (@ ,@attributes)
728 ;; TODO: Add "Contribute" menu, to report
732 `(img (@ (alt "Language")
733 (src "/static/base/img/language-picker.svg")))
735 (language-menu-items file)))
736 #:split-node? split-node?)
738 ((tag ('@ attributes ...) body ...)
739 `(,tag (@ ,@attributes) ,@(map loop body)))
741 `(,tag ,@(map loop body)))
745 (define (process-html file)
746 ;; Parse FILE and add links to translations. Install the result
748 (format (current-error-port) "processing ~a...~%" file)
749 (let* ((shtml (parameterize ((%strict-tokenizer? #t))
750 (call-with-input-file file html->shtml)))
751 (processed (stylized-html shtml file))
752 (base (string-drop file (string-length #$input)))
753 (target (string-append #$output base)))
754 (mkdir-p (dirname target))
755 (call-with-output-file target
757 (write-shtml-as-html processed port)))))
759 ;; Install a UTF-8 locale so we can process UTF-8 files.
760 (setenv "GUIX_LOCPATH"
761 #+(file-append glibc-utf8-locales "/lib/locale"))
762 (setlocale LC_ALL "en_US.utf8")
763 (setenv "LC_ALL" "en_US.utf8")
764 (setvbuf (current-error-port) 'line)
766 (n-par-for-each (parallel-job-count)
768 (if (string-suffix? ".html" file)
770 ;; Copy FILE as is to #$output.
771 (let* ((base (string-drop file (string-length #$input)))
772 (target (string-append #$output base)))
773 (mkdir-p (dirname target))
774 (if (eq? 'symlink (stat:type (lstat file)))
775 (symlink (readlink file) target)
776 (copy-file file target)))))
777 (find-files #$input))))))
779 (computed-file "stylized-html-manual" build))
781 (define* (html-manual source #:key (languages %languages)
784 (mono-node-indexes (map list languages))
785 (split-node-indexes (map list languages))
787 (options %makeinfo-html-options))
788 "Return the HTML manuals built from SOURCE for all LANGUAGES, with the given
790 (define manual-source
791 (texinfo-manual-source source
793 #:languages languages
797 (texinfo-manual-images source))
800 (with-imported-modules '((guix build utils))
802 (use-modules (guix build utils)
805 (define (normalize language)
806 ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
807 (string-map (match-lambda
810 (string-downcase language)))
812 (define (language->texi-file-name language)
813 (if (string=? language "en")
814 (string-append #$manual-source "/"
816 (string-append #$manual-source "/"
817 #$manual "." language ".texi")))
819 ;; Install a UTF-8 locale so that 'makeinfo' is at ease.
820 (setenv "GUIX_LOCPATH"
821 #+(file-append glibc-utf8-locales "/lib/locale"))
822 (setenv "LC_ALL" "en_US.utf8")
824 (setvbuf (current-output-port) 'line)
825 (setvbuf (current-error-port) 'line)
827 ;; 'makeinfo' looks for "htmlxref.cnf" in the current directory, so
828 ;; copy it right here.
829 (copy-file (string-append #$manual-source "/htmlxref.cnf")
832 (for-each (lambda (language)
833 (let* ((texi (language->texi-file-name language))
835 "-c" ,(string-append "TOP_NODE_UP_URL=/manual/"
839 (format #t "building HTML manual for language '~a'...~%"
841 (mkdir-p (string-append #$output "/"
842 (normalize language)))
843 (setenv "LANGUAGE" language)
844 (apply invoke #$(file-append texinfo "/bin/makeinfo")
845 "-o" (string-append #$output "/"
849 (apply invoke #$(file-append texinfo "/bin/makeinfo")
852 (string-append #$output "/"
855 (if (string=? language "en")
857 (string-append "." language))
861 ;; Make sure images are available.
863 (string-append #$output "/" (normalize language)
866 (string-append #$output "/" (normalize language)
867 "/html_node/images"))))
868 (filter (compose file-exists? language->texi-file-name)
871 (let* ((name (string-append manual "-html-manual"))
872 (manual* (computed-file name build #:local-build? #f)))
873 (syntax-highlighted-html (stylized-html source manual*
874 #:languages languages
876 #:mono-node-indexes mono-node-indexes
877 #:split-node-indexes split-node-indexes
878 #:name (string-append name "-highlighted"))))
880 (define* (pdf-manual source #:key (languages %languages)
885 "Return the HTML manuals built from SOURCE for all LANGUAGES, with the given
887 (define manual-source
888 (texinfo-manual-source source
890 #:languages languages
893 ;; FIXME: This union works, except for the table of contents of non-English
894 ;; manuals, which contains escape sequences like "^^ca^^fe" instead of
898 ;; (texlive-updmap.cfg (list texlive-tex-texinfo
899 ;; texlive-generic-epsf
900 ;; texlive-fonts-ec)))
903 (with-imported-modules '((guix build utils))
905 (use-modules (guix build utils)
909 (define (normalize language) ;XXX: deduplicate
910 ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
911 (string-map (match-lambda
914 (string-downcase language)))
916 ;; Install a UTF-8 locale so that 'makeinfo' is at ease.
917 (setenv "GUIX_LOCPATH"
918 #+(file-append glibc-utf8-locales "/lib/locale"))
919 (setenv "LC_ALL" "en_US.utf8")
921 (string-append #+(file-append texlive "/bin") ":"
922 #+(file-append texinfo "/bin") ":"
924 ;; Below are command-line tools needed by
925 ;; 'texi2dvi' and friends.
926 #+(file-append sed "/bin") ":"
927 #+(file-append grep "/bin") ":"
928 #+(file-append coreutils "/bin") ":"
929 #+(file-append gawk "/bin") ":"
930 #+(file-append tar "/bin") ":"
931 #+(file-append diffutils "/bin")))
933 (setvbuf (current-output-port) 'line)
934 (setvbuf (current-error-port) 'line)
936 (setenv "HOME" (getcwd)) ;for kpathsea/mktextfm
938 ;; 'SOURCE_DATE_EPOCH' is honored by pdftex.
939 (setenv "SOURCE_DATE_EPOCH" "1")
941 (for-each (lambda (language)
942 (let ((opts `("--pdf"
945 ,(if (string=? language "en")
946 (string-append #$manual-source "/"
948 (string-append #$manual-source "/"
949 #$manual "." language ".texi")))))
950 (format #t "building PDF manual for language '~a'...~%"
952 (mkdir-p (string-append #$output "/"
953 (normalize language)))
954 (setenv "LANGUAGE" language)
957 ;; FIXME: Unfortunately building PDFs for non-Latin
958 ;; alphabets doesn't work:
959 ;; <https://lists.gnu.org/archive/html/help-texinfo/2012-01/msg00014.html>.
960 (guard (c ((invoke-error? c)
961 (format (current-error-port)
962 "~%~%Failed to produce \
963 PDF for language '~a'!~%~%"
965 (apply invoke #$(file-append texinfo "/bin/makeinfo")
967 (string-append #$output "/"
970 (if (string=? language "en")
978 (computed-file (string-append manual "-pdf-manual") build
981 (define* (guix-manual-text-domain source
982 #:optional (languages %manual-languages))
983 "Return the PO files for LANGUAGES of the 'guix-manual' text domain taken
986 (file-append* source "/po/doc"))
989 (with-imported-modules '((guix build utils))
991 (use-modules (guix build utils))
994 (for-each (lambda (language)
996 (string-append #$output "/" language
1000 (invoke #+(file-append gnu-gettext "/bin/msgfmt")
1002 (string-append directory "/guix-manual.mo")
1003 (string-append #$po-directory "/guix-manual."
1005 '#$(delete "en" languages)))))
1007 (computed-file "guix-manual-po" build))
1009 (define* (localization-helper-module source
1010 #:optional (languages %languages))
1011 "Return a file-like object for use as the (localization) module. SOURCE
1012 must be the Guix top-level source directory, from which PO files are taken."
1014 (with-extensions (list guile-json-3)
1016 (define-module (localization)
1018 #:use-module (srfi srfi-1)
1019 #:use-module (srfi srfi-19)
1020 #:use-module (ice-9 match)
1021 #:use-module (ice-9 popen)
1026 language-code->native-name
1029 (define (normalize language) ;XXX: deduplicate
1030 ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
1031 (string-map (match-lambda
1034 (string-downcase language)))
1036 (define-syntax-rule (with-language language exp ...)
1037 (let ((lang (getenv "LANGUAGE")))
1040 (setenv "LANGUAGE" language)
1041 (setlocale LC_MESSAGES))
1045 (setenv "LANGUAGE" lang)
1046 (unsetenv "LANGUAGE"))
1047 (setlocale LC_MESSAGES)))))
1049 ;; (put 'with-language 'scheme-indent-function 1)
1050 (define* (translate str language
1051 #:key (domain "guix-manual"))
1054 (bindtextdomain "guix-manual"
1055 #+(guix-manual-text-domain source))
1056 (bindtextdomain "iso_639-3" ;language names
1057 #+(file-append iso-codes
1059 (setenv "LANGUAGE" ,language)
1060 (write (gettext ,str ,domain))))
1062 ;; Since the 'gettext' function caches msgid translations,
1063 ;; regardless of $LANGUAGE, we have to spawn a new process each
1064 ;; time we want to translate to a different language. Bah!
1065 (let* ((pipe (open-pipe* OPEN_READ
1066 #+(file-append guile-3.0
1068 "-c" (object->string exp)))
1073 (define %iso639-languages
1075 (assoc-ref (call-with-input-file
1076 #+(file-append iso-codes
1077 "/share/iso-codes/json/iso_639-3.json")
1081 (define (language-code->name code)
1082 "Return the full name of a language from its ISO-639-3 code."
1083 (let ((code (match (string-index code #\_)
1085 (index (string-take code index)))))
1086 (any (lambda (language)
1087 (and (string=? (or (assoc-ref language "alpha_2")
1088 (assoc-ref language "alpha_3"))
1090 (assoc-ref language "name")))
1091 %iso639-languages)))
1093 (define (language-code->native-name code)
1094 "Return the name of language CODE in that language."
1095 (translate (language-code->name code) code
1096 #:domain "iso_639-3"))
1098 (define (seconds->string seconds language)
1099 (let* ((time (make-time time-utc 0 seconds))
1100 (date (time-utc->date time)))
1101 (with-language language (date->string date "~e ~B ~Y")))))))
1103 (scheme-file "localization.scm" content))
1105 (define* (html-manual-indexes source
1106 #:key (languages %languages)
1109 (title (if (string=? "guix" manual)
1110 "GNU Guix Reference Manual"
1111 "GNU Guix Cookbook"))
1114 (with-imported-modules `((guix build utils)
1116 => ,(localization-helper-module
1119 (use-modules (guix build utils)
1124 (define (guix-url path)
1125 (string-append #$%web-site-url path))
1127 (define (sxml-index language title body)
1128 ;; FIXME: Avoid duplicating styling info from guix-artwork.git.
1129 `(html (@ (lang ,language))
1131 (title ,(string-append title " — GNU Guix"))
1132 (meta (@ (charset "UTF-8")))
1133 (meta (@ (name "viewport") (content "width=device-width, initial-scale=1.0")))
1135 (link (@ (rel "prefetch") (href ,(guix-url "menu/index.html"))))
1137 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/elements.css"))))
1138 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/common.css"))))
1139 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/messages.css"))))
1140 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/navbar.css"))))
1141 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/breadcrumbs.css"))))
1142 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/buttons.css"))))
1143 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/footer.css"))))
1145 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/page.css"))))
1146 (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/post.css")))))
1148 (header (@ (class "navbar"))
1149 (h1 (a (@ (class "branding")
1150 (href #$%web-site-url)))
1151 (span (@ (class "a11y-offset"))
1153 (nav (@ (class "menu"))))
1154 (nav (@ (class "breadcrumbs"))
1155 (a (@ (class "crumb")
1156 (href #$%web-site-url))
1161 (define (language-index language)
1163 (translate #$title language))
1169 (@ (class "page centered-block limit-width"))
1171 (p (@ (class "post-metadata centered-text"))
1173 ,(seconds->string #$date language))
1177 (li (a (@ (href "html_node"))
1178 "HTML, with a separate page per node"))
1182 (if (string=? language
1188 "HTML, entirely on one page"))
1189 ,@(if (member language '("ru" "zh_CN"))
1191 `((li (a (@ (href ,(string-append
1193 (if (string=? language "en")
1200 (define (top-level-index languages)
1201 (define title #$title)
1206 (@ (class "page centered-block limit-width"))
1209 "This document is available in the following
1212 ,@(map (lambda (language)
1213 `(li (a (@ (href ,(normalize language)))
1214 ,(language-code->native-name language))))
1217 (define (write-html file sxml)
1218 (call-with-output-file file
1220 (display "<!DOCTYPE html>\n" port)
1221 (sxml->xml sxml port))))
1223 (setenv "GUIX_LOCPATH"
1224 #+(file-append glibc-utf8-locales "/lib/locale"))
1225 (setenv "LC_ALL" "en_US.utf8")
1226 (setlocale LC_ALL "en_US.utf8")
1228 (for-each (lambda (language)
1230 (string-append #$output "/"
1231 (normalize language)))
1234 (write-html (string-append directory "/index.html")
1235 (language-index language)))
1238 (write-html (string-append #$output "/index.html")
1239 (top-level-index '#$languages)))))
1241 (computed-file "html-indexes" build))
1243 (define* (pdf+html-manual source
1244 #:key (languages %languages)
1246 (date (time-second (current-time time-utc)))
1247 (mono-node-indexes (map list %languages))
1248 (split-node-indexes (map list %languages))
1250 "Return the union of the HTML and PDF manuals, as well as the indexes."
1251 (directory-union (string-append manual "-manual")
1255 #:languages languages
1258 (list html-manual-indexes
1259 (lambda (source . args)
1260 (apply html-manual source
1261 #:mono-node-indexes mono-node-indexes
1262 #:split-node-indexes split-node-indexes
1267 (define (latest-commit+date directory)
1268 "Return two values: the last commit ID (a hex string) for DIRECTORY, and its
1269 commit date (an integer)."
1270 (let* ((repository (repository-open directory))
1271 (head (repository-head repository))
1272 (oid (reference-target head))
1273 (commit (commit-lookup repository oid)))
1274 ;; TODO: Use (git describe) when it's widely available.
1275 (values (oid->string oid) (commit-time commit))))
1282 (define guile-manual
1283 ;; The Guile manual as HTML, including both the mono-node "guile.html" and
1284 ;; the split-node "html_node" directory.
1285 (let ((guile guile-3.0-latest))
1286 (computed-file (string-append "guile-manual-" (package-version guile))
1287 (with-imported-modules '((guix build utils))
1289 (use-modules (guix build utils)
1293 (string-append #+tar "/bin:"
1296 (invoke "tar" "xf" #$(package-source guile))
1297 (mkdir-p (string-append #$output "/en/html_node"))
1299 (let* ((texi (find-files "." "^guile\\.texi$"))
1300 (documentation (match texi
1301 ((file) (dirname file)))))
1302 (with-directory-excursion documentation
1303 (invoke "makeinfo" "--html" "--no-split"
1304 "-o" (string-append #$output
1307 (invoke "makeinfo" "--html" "-o" "split"
1311 (string-append #$output "/en/html_node")))))))))
1313 (define %guile-manual-base-url
1314 "https://www.gnu.org/software/guile/manual")
1316 (define (for-all-languages index)
1317 (map (lambda (language)
1318 (list language index))
1321 (define guile-mono-node-indexes
1322 ;; The Guile manual is only available in English so use the same index in
1325 (html-manual-identifier-index (file-append guile-manual "/en")
1326 %guile-manual-base-url
1327 #:name "guile-html-index-en")))
1329 (define guile-split-node-indexes
1331 (html-manual-identifier-index (file-append guile-manual "/en/html_node")
1332 (string-append %guile-manual-base-url
1334 #:name "guile-html-index-en")))
1336 (define (merge-index-alists alist1 alist2)
1337 "Merge ALIST1 and ALIST2, both of which are list of tuples like:
1339 (LANGUAGE INDEX1 INDEX2 ...)
1341 where LANGUAGE is a string like \"en\" and INDEX1 etc. are indexes as returned
1342 by 'html-identifier-indexes'."
1343 (let ((languages (delete-duplicates
1344 (append (match alist1
1345 (((languages . _) ...)
1348 (((languages . _) ...)
1350 (map (lambda (language)
1352 (append (or (assoc-ref alist1 language) '())
1353 (or (assoc-ref alist2 language) '()))))
1357 (let* ((root (canonicalize-path
1358 (string-append (current-source-directory) "/..")))
1359 (commit date (latest-commit+date root))
1360 (version (or (getenv "GUIX_MANUAL_VERSION")
1361 (string-take commit 7)))
1362 (select? (let ((vcs? (git-predicate root)))
1364 (and (vcs? file stat)
1365 ;; Filter out this file.
1366 (not (string=? (basename file) "build.scm"))))))
1367 (source (local-file root "guix" #:recursive? #t
1368 #:select? select?)))
1376 (define guix-mono-node-indexes
1377 ;; Alist of indexes for GUIX-MANUAL, where each key is a language code and
1378 ;; each value is a file-like object containing the identifier index.
1379 (html-identifier-indexes guix-manual ""
1380 #:manual-name "guix"
1381 #:base-url (if (string=? %manual "guix")
1384 "/manual/devel/" <>))
1385 #:languages %languages))
1387 (define guix-split-node-indexes
1388 ;; Likewise for the split-node variant of GUIX-MANUAL.
1389 (html-identifier-indexes guix-manual "/html_node"
1390 #:manual-name "guix"
1391 #:base-url (if (string=? %manual "guix")
1396 #:languages %languages))
1398 (define mono-node-indexes
1399 (merge-index-alists guix-mono-node-indexes guile-mono-node-indexes))
1401 (define split-node-indexes
1402 (merge-index-alists guix-split-node-indexes guile-split-node-indexes))
1404 (format (current-error-port)
1405 "building manual from work tree around commit ~a, ~a~%"
1407 (let* ((time (make-time time-utc 0 date))
1408 (date (time-utc->date time)))
1409 (date->string date "~e ~B ~Y")))
1411 (pdf+html-manual source
1412 ;; Always use the identifier indexes of GUIX-MANUAL and
1413 ;; GUILE-MANUAL. Both "guix" and "guix-cookbook" can
1414 ;; contain links to definitions that appear in either of
1415 ;; these two manuals.
1416 #:mono-node-indexes mono-node-indexes
1417 #:split-node-indexes split-node-indexes