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