build.scm 42 KB

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