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