build.scm 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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 compression)
  34. (gnu packages gawk)
  35. (gnu packages gettext)
  36. (gnu packages guile)
  37. (gnu packages guile-xyz)
  38. (gnu packages iso-codes)
  39. (gnu packages texinfo)
  40. (gnu packages tex)
  41. (ice-9 match)
  42. (srfi srfi-1)
  43. (srfi srfi-19)
  44. (srfi srfi-26)
  45. (srfi srfi-71))
  46. (define file-append*
  47. (@@ (guix self) file-append*))
  48. (define translated-texi-manuals
  49. (let ((translated (@@ (guix self) translate-texi-manuals)))
  50. (lambda (source)
  51. (let ((result (translated source)))
  52. ;; Build with 'guile-3.0-latest', which is linked against
  53. ;; 'libgc/disable-munmap', to avoid the dreaded "mmap(PROT_NONE)
  54. ;; failed" crash: <https://bugs.gnu.org/47428>.
  55. (computed-file (computed-file-name result)
  56. (computed-file-gexp result)
  57. #:options (computed-file-options result)
  58. #:guile guile-3.0-latest)))))
  59. (define info-manual
  60. (@@ (guix self) info-manual))
  61. (define %manual
  62. ;; The manual to build--i.e., the base name of a .texi file, such as "guix"
  63. ;; or "guix-cookbook".
  64. (or (getenv "GUIX_MANUAL")
  65. "guix"))
  66. (define %languages
  67. ;; The cookbook is not translated in the same languages as the manual
  68. (if (string=? %manual "guix-cookbook")
  69. '("de" "en" "fr")
  70. '("de" "en" "es" "fr" "ru" "zh_CN")))
  71. (define (texinfo-manual-images source)
  72. "Return a directory containing all the images used by the user manual, taken
  73. from SOURCE, the root of the source tree."
  74. (define graphviz
  75. (module-ref (resolve-interface '(gnu packages graphviz))
  76. 'graphviz))
  77. (define images
  78. (file-append* source "doc/images"))
  79. (define build
  80. (with-imported-modules '((guix build utils))
  81. #~(begin
  82. (use-modules (guix build utils)
  83. (srfi srfi-26))
  84. (define (dot->image dot-file format)
  85. (invoke #+(file-append graphviz "/bin/dot")
  86. "-T" format "-Gratio=.9" "-Gnodesep=.005"
  87. "-Granksep=.00005" "-Nfontsize=9"
  88. "-Nheight=.1" "-Nwidth=.1"
  89. "-o" (string-append #$output "/"
  90. (basename dot-file ".dot")
  91. "." format)
  92. dot-file))
  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. ;; Copy other PNGs.
  100. (for-each (lambda (png-file)
  101. (install-file png-file #$output))
  102. (find-files #$images "\\.png$")))))
  103. (computed-file "texinfo-manual-images" build))
  104. (define* (texinfo-manual-source source #:key
  105. (version "0.0")
  106. (languages %languages)
  107. (date 1))
  108. "Gather all the source files of the Texinfo manuals from SOURCE--.texi file
  109. as well as images, OS examples, and translations."
  110. (define documentation
  111. (file-append* source "doc"))
  112. (define examples
  113. (file-append* source "gnu/system/examples"))
  114. (define build
  115. (with-imported-modules '((guix build utils))
  116. #~(begin
  117. (use-modules (guix build utils)
  118. (srfi srfi-19))
  119. (define (make-version-texi language)
  120. ;; Create the 'version.texi' file for LANGUAGE.
  121. (let ((file (if (string=? language "en")
  122. "version.texi"
  123. (string-append "version-" language ".texi"))))
  124. (call-with-output-file (string-append #$output "/" file)
  125. (lambda (port)
  126. (let* ((version #$version)
  127. (time (make-time time-utc 0 #$date))
  128. (date (time-utc->date time)))
  129. (format port "
  130. @set UPDATED ~a
  131. @set UPDATED-MONTH ~a
  132. @set EDITION ~a
  133. @set VERSION ~a\n"
  134. (date->string date "~e ~B ~Y")
  135. (date->string date "~B ~Y")
  136. version version))))))
  137. (install-file #$(file-append documentation "/htmlxref.cnf")
  138. #$output)
  139. (for-each (lambda (texi)
  140. (install-file texi #$output))
  141. (append (find-files #$documentation "\\.(texi|scm|json)$")
  142. (find-files #$(translated-texi-manuals source)
  143. "\\.texi$")))
  144. ;; Create 'version.texi'.
  145. (for-each make-version-texi '#$languages)
  146. ;; Copy configuration templates that the manual includes.
  147. (for-each (lambda (template)
  148. (copy-file template
  149. (string-append
  150. #$output "/os-config-"
  151. (basename template ".tmpl")
  152. ".texi")))
  153. (find-files #$examples "\\.tmpl$"))
  154. (symlink #$(texinfo-manual-images source)
  155. (string-append #$output "/images")))))
  156. (computed-file "texinfo-manual-source" build))
  157. (define %web-site-url
  158. ;; URL of the web site home page.
  159. (or (getenv "GUIX_WEB_SITE_URL")
  160. "/software/guix/"))
  161. (define %makeinfo-html-options
  162. ;; Options passed to 'makeinfo --html'.
  163. '("--css-ref=https://www.gnu.org/software/gnulib/manual.css"
  164. "-c" "EXTRA_HEAD=<meta name=\"viewport\" \
  165. content=\"width=device-width, initial-scale=1\" />"))
  166. (define (normalize-language-code language) ;XXX: deduplicate
  167. ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
  168. (string-map (match-lambda
  169. (#\_ #\-)
  170. (chr chr))
  171. (string-downcase language)))
  172. (define* (html-manual-identifier-index manual base-url
  173. #:key
  174. (name "html-manual-identifier-index"))
  175. "Return an index of all the identifiers that appear in MANUAL, a
  176. makeinfo-generated manual. The index is a file that contains an alist; each
  177. key is an identifier and the associated value is the URL reference pointing to
  178. that identifier. The URL is constructed by concatenating BASE-URL to the
  179. actual file name."
  180. (define build
  181. (with-extensions (list guile-lib)
  182. (with-imported-modules '((guix build utils))
  183. #~(begin
  184. (use-modules (guix build utils)
  185. (htmlprag)
  186. (srfi srfi-1)
  187. (srfi srfi-26)
  188. (ice-9 ftw)
  189. (ice-9 match)
  190. (ice-9 threads)
  191. (ice-9 pretty-print))
  192. (%strict-tokenizer? #t)
  193. (define file-url
  194. (let ((prefix (string-append #$manual "/")))
  195. (lambda (file)
  196. ;; Return the URL for FILE.
  197. (let ((file (string-drop file (string-length prefix)))
  198. (base #$base-url))
  199. (if (string-null? base)
  200. file
  201. (string-append base "/" file))))))
  202. (define (underscore-decode str)
  203. ;; Decode STR, an "underscore-encoded" string as produced by
  204. ;; makeinfo for indexes, such as "_0025base_002dservices" for
  205. ;; "%base-services".
  206. (let loop ((str str)
  207. (result '()))
  208. (match (string-index str #\_)
  209. (#f
  210. (string-concatenate-reverse (cons str result)))
  211. (index
  212. (let ((char (string->number
  213. (substring str (+ index 1) (+ index 5))
  214. 16)))
  215. (loop (string-drop str (+ index 5))
  216. (append (list (string (integer->char char))
  217. (string-take str index))
  218. result)))))))
  219. (define (anchor-id->key id)
  220. ;; Convert ID, an anchor ID such as
  221. ;; "index-pam_002dlimits_002dservice" to the corresponding key,
  222. ;; "pam-limits-service" in this example. Drop the suffix of
  223. ;; duplicate anchor IDs like "operating_002dsystem-1".
  224. (let ((id (if (any (cut string-suffix? <> id)
  225. '("-1" "-2" "-3" "-4" "-5"))
  226. (string-drop-right id 2)
  227. id)))
  228. (underscore-decode
  229. (string-drop id (string-length "index-")))))
  230. (define* (collect-anchors file #:optional (anchors '()))
  231. ;; Collect the anchors that appear in FILE, a makeinfo-generated
  232. ;; file. Grab those from <dt> tags, which corresponds to
  233. ;; Texinfo @deftp, @defvr, etc. Return ANCHORS augmented with
  234. ;; more name/reference pairs.
  235. (define string-or-entity?
  236. (match-lambda
  237. ((? string?) #t)
  238. (('*ENTITY* _ ...) #t)
  239. (_ #f)))
  240. (define (worthy-entry? lst)
  241. ;; Attempt to match:
  242. ;; Scheme Variable: <strong>x</strong>
  243. ;; but not:
  244. ;; <code>cups-configuration</code> parameter: …
  245. (let loop ((lst lst))
  246. (match lst
  247. (((? string-or-entity?) rest ...)
  248. (loop rest))
  249. ((('strong _ ...) _ ...)
  250. #t)
  251. ((('span ('@ ('class "symbol-definition-category"))
  252. (? string-or-entity?) ...) rest ...)
  253. #t)
  254. (x
  255. #f))))
  256. (let ((shtml (call-with-input-file file html->shtml)))
  257. (let loop ((shtml shtml)
  258. (anchors anchors))
  259. (match shtml
  260. (('dt ('@ ('id id) _ ...) rest ...)
  261. (if (and (string-prefix? "index-" id)
  262. (worthy-entry? rest))
  263. (alist-cons (anchor-id->key id)
  264. (string-append (file-url file)
  265. "#" id)
  266. anchors)
  267. anchors))
  268. ((tag ('@ _ ...) body ...)
  269. (fold loop anchors body))
  270. ((tag body ...)
  271. (fold loop anchors body))
  272. (_ anchors)))))
  273. (define (html-files directory)
  274. ;; Return the list of HTML files under DIRECTORY.
  275. (map (cut string-append directory "/" <>)
  276. (scandir #$manual (lambda (file)
  277. (string-suffix? ".html" file)))))
  278. (define anchors
  279. (sort (concatenate
  280. (n-par-map (parallel-job-count)
  281. (cut collect-anchors <>)
  282. (html-files #$manual)))
  283. (match-lambda*
  284. (((key1 . url1) (key2 . url2))
  285. (if (string=? key1 key2)
  286. (string<? url1 url2)
  287. (string<? key1 key2))))))
  288. (call-with-output-file #$output
  289. (lambda (port)
  290. (display ";; Identifier index for the manual.\n\n"
  291. port)
  292. (pretty-print anchors port)))))))
  293. (computed-file name build))
  294. (define* (html-identifier-indexes manual directory-suffix
  295. #:key (languages %languages)
  296. (manual-name %manual)
  297. (base-url (const "")))
  298. (map (lambda (language)
  299. (let ((language (normalize-language-code language)))
  300. (list language
  301. (html-manual-identifier-index
  302. (file-append manual "/" language directory-suffix)
  303. (base-url language)
  304. #:name (string-append manual-name "-html-index-"
  305. language)))))
  306. languages))
  307. (define* (syntax-highlighted-html input
  308. #:key
  309. (name "highlighted-syntax")
  310. (languages %languages)
  311. (mono-node-indexes
  312. (html-identifier-indexes input ""
  313. #:languages
  314. languages))
  315. (split-node-indexes
  316. (html-identifier-indexes input
  317. "/html_node"
  318. #:languages
  319. languages))
  320. (syntax-css-url
  321. "/static/base/css/code.css"))
  322. "Return a derivation called NAME that processes all the HTML files in INPUT
  323. to (1) add them a link to SYNTAX-CSS-URL, and (2) highlight the syntax of all
  324. its <pre class=\"lisp\"> blocks (as produced by 'makeinfo --html')."
  325. (define build
  326. (with-extensions (list guile-lib guile-syntax-highlight)
  327. (with-imported-modules '((guix build utils))
  328. #~(begin
  329. (use-modules (htmlprag)
  330. (syntax-highlight)
  331. (syntax-highlight scheme)
  332. (syntax-highlight lexers)
  333. (guix build utils)
  334. (srfi srfi-1)
  335. (srfi srfi-26)
  336. (ice-9 match)
  337. (ice-9 threads)
  338. (ice-9 vlist))
  339. (%strict-tokenizer? #t)
  340. (define (pair-open/close lst)
  341. ;; Pair 'open' and 'close' tags produced by 'highlights' and
  342. ;; produce nested 'paren' tags instead.
  343. (let loop ((lst lst)
  344. (level 0)
  345. (result '()))
  346. (match lst
  347. ((('open open) rest ...)
  348. (call-with-values
  349. (lambda ()
  350. (loop rest (+ 1 level) '()))
  351. (lambda (inner close rest)
  352. (loop rest level
  353. (cons `(paren ,level ,open ,inner ,close)
  354. result)))))
  355. ((('close str) rest ...)
  356. (if (> level 0)
  357. (values (reverse result) str rest)
  358. (begin
  359. (format (current-error-port)
  360. "warning: extra closing paren; context:~% ~y~%"
  361. (reverse result))
  362. (loop rest 0 (cons `(close ,str) result)))))
  363. ((item rest ...)
  364. (loop rest level (cons item result)))
  365. (()
  366. (when (> level 0)
  367. (format (current-error-port)
  368. "warning: missing ~a closing parens; context:~% ~y%"
  369. level (reverse result)))
  370. (values (reverse result) "" '())))))
  371. (define (highlights->sxml* highlights anchors)
  372. ;; Like 'highlights->sxml', but handle nested 'paren tags. This
  373. ;; allows for paren matching highlights via appropriate CSS
  374. ;; "hover" properties. When a symbol is encountered, look it up
  375. ;; in ANCHORS, a vhash, and emit the corresponding href, if any.
  376. (define (tag->class tag)
  377. (string-append "syntax-" (symbol->string tag)))
  378. (map (match-lambda
  379. ((? string? str) str)
  380. (('paren level open (body ...) close)
  381. `(span (@ (class ,(string-append "syntax-paren"
  382. (number->string level))))
  383. ,open
  384. (span (@ (class "syntax-symbol"))
  385. ,@(highlights->sxml* body anchors))
  386. ,close))
  387. (('symbol text)
  388. ;; Check whether we can emit a hyperlink for TEXT.
  389. (match (vhash-assoc text anchors)
  390. (#f
  391. `(span (@ (class ,(tag->class 'symbol))) ,text))
  392. ((_ . target)
  393. `(a (@ (class ,(tag->class 'symbol)) (href ,target))
  394. ,text))))
  395. ((tag text)
  396. `(span (@ (class ,(tag->class tag))) ,text)))
  397. highlights))
  398. (define entity->string
  399. (match-lambda
  400. ("rArr" "⇒")
  401. ("rarr" "→")
  402. ("hellip" "…")
  403. ("rsquo" "’")
  404. (e (pk 'unknown-entity e) (primitive-exit 2))))
  405. (define (concatenate-snippets pieces)
  406. ;; Concatenate PIECES, which contains strings and entities,
  407. ;; replacing entities with their corresponding string.
  408. (let loop ((pieces pieces)
  409. (strings '()))
  410. (match pieces
  411. (()
  412. (string-concatenate-reverse strings))
  413. (((? string? str) . rest)
  414. (loop rest (cons str strings)))
  415. ((('*ENTITY* "additional" entity) . rest)
  416. (loop rest (cons (entity->string entity) strings)))
  417. ((('span _ lst ...) . rest) ;for <span class="roman">
  418. (loop (append lst rest) strings))
  419. ((('var name) . rest) ;for @var{name} within @lisp
  420. (loop rest (cons name strings))) ;XXX: losing formatting
  421. (something
  422. (pk 'unsupported-code-snippet something)
  423. (primitive-exit 1)))))
  424. (define (highlight-definition id category symbol args)
  425. ;; Produce stylable HTML for the given definition (an @deftp,
  426. ;; @deffn, or similar).
  427. `(dt (@ (id ,id) (class "symbol-definition"))
  428. (span (@ (class "symbol-definition-category"))
  429. ,@category)
  430. (span (@ (class "symbol-definition-prototype"))
  431. ,symbol " " ,@args)))
  432. (define (space? obj)
  433. (and (string? obj)
  434. (string-every char-set:whitespace obj)))
  435. (define (syntax-highlight sxml anchors)
  436. ;; Recurse over SXML and syntax-highlight code snippets.
  437. (let loop ((sxml sxml))
  438. (match sxml
  439. (('*TOP* decl body ...)
  440. `(*TOP* ,decl ,@(map loop body)))
  441. (('head things ...)
  442. `(head ,@things
  443. (link (@ (rel "stylesheet")
  444. (type "text/css")
  445. (href #$syntax-css-url)))))
  446. (('pre ('@ ('class "lisp")) code-snippet ...)
  447. `(pre (@ (class "lisp"))
  448. ,@(highlights->sxml*
  449. (pair-open/close
  450. (highlight lex-scheme
  451. (concatenate-snippets code-snippet)))
  452. anchors)))
  453. ;; Replace the ugly <strong> used for @deffn etc., which
  454. ;; translate to <dt>, with more stylable markup.
  455. (('dt (@ ('id id)) category ... ('strong thing))
  456. (highlight-definition id category thing '()))
  457. (('dt (@ ('id id)) category ... ('strong thing)
  458. (? space?) ('em args ...))
  459. (highlight-definition id category thing args))
  460. ((tag ('@ attributes ...) body ...)
  461. `(,tag (@ ,@attributes) ,@(map loop body)))
  462. ((tag body ...)
  463. `(,tag ,@(map loop body)))
  464. ((? string? str)
  465. str))))
  466. (define (process-html file anchors)
  467. ;; Parse FILE and perform syntax highlighting for its Scheme
  468. ;; snippets. Install the result to #$output.
  469. (format (current-error-port) "processing ~a...~%" file)
  470. (let* ((shtml (call-with-input-file file html->shtml))
  471. (highlighted (syntax-highlight shtml anchors))
  472. (base (string-drop file (string-length #$input)))
  473. (target (string-append #$output base)))
  474. (mkdir-p (dirname target))
  475. (call-with-output-file target
  476. (lambda (port)
  477. (write-shtml-as-html highlighted port)))))
  478. (define (copy-as-is file)
  479. ;; Copy FILE as is to #$output.
  480. (let* ((base (string-drop file (string-length #$input)))
  481. (target (string-append #$output base)))
  482. (mkdir-p (dirname target))
  483. (catch 'system-error
  484. (lambda ()
  485. (if (eq? 'symlink (stat:type (lstat file)))
  486. (symlink (readlink file) target)
  487. (link file target)))
  488. (lambda args
  489. (let ((errno (system-error-errno args)))
  490. (pk 'error-link file target (strerror errno))
  491. (primitive-exit 3))))))
  492. (define (html? file stat)
  493. (string-suffix? ".html" file))
  494. (define language+node-anchors
  495. (match-lambda
  496. ((language files ...)
  497. (cons language
  498. (fold (lambda (file vhash)
  499. (let ((alist (call-with-input-file file read)))
  500. ;; Use 'fold-right' so that the first entry
  501. ;; wins (e.g., "car" from "Pairs" rather than
  502. ;; from "rnrs base" in the Guile manual).
  503. (fold-right (match-lambda*
  504. (((key . value) vhash)
  505. (vhash-cons key value vhash)))
  506. vhash
  507. alist)))
  508. vlist-null
  509. files)))))
  510. (define mono-node-anchors
  511. ;; List of language/vhash pairs, where each vhash maps an
  512. ;; identifier to the corresponding URL in a single-page manual.
  513. (map language+node-anchors '#$mono-node-indexes))
  514. (define multi-node-anchors
  515. ;; Likewise for split-node manuals.
  516. (map language+node-anchors '#$split-node-indexes))
  517. ;; Install a UTF-8 locale so we can process UTF-8 files.
  518. (setenv "GUIX_LOCPATH"
  519. #+(file-append glibc-utf8-locales "/lib/locale"))
  520. (setlocale LC_ALL "en_US.utf8")
  521. ;; First process the mono-node 'guix.html' files.
  522. (for-each (match-lambda
  523. ((language . anchors)
  524. (let ((files (find-files
  525. (string-append #$input "/" language)
  526. "^guix(-cookbook|)(\\.[a-zA-Z_-]+)?\\.html$")))
  527. (n-par-for-each (parallel-job-count)
  528. (cut process-html <> anchors)
  529. files))))
  530. mono-node-anchors)
  531. ;; Process the multi-node HTML files.
  532. (for-each (match-lambda
  533. ((language . anchors)
  534. (let ((files (find-files
  535. (string-append #$input "/" language
  536. "/html_node")
  537. "\\.html$")))
  538. (n-par-for-each (parallel-job-count)
  539. (cut process-html <> anchors)
  540. files))))
  541. multi-node-anchors)
  542. ;; Last, copy non-HTML files as is.
  543. (for-each copy-as-is
  544. (find-files #$input (negate html?)))))))
  545. (computed-file name build))
  546. (define* (html-manual source #:key (languages %languages)
  547. (version "0.0")
  548. (manual %manual)
  549. (mono-node-indexes (map list languages))
  550. (split-node-indexes (map list languages))
  551. (date 1)
  552. (options %makeinfo-html-options))
  553. "Return the HTML manuals built from SOURCE for all LANGUAGES, with the given
  554. makeinfo OPTIONS."
  555. (define manual-source
  556. (texinfo-manual-source source
  557. #:version version
  558. #:languages languages
  559. #:date date))
  560. (define images
  561. (texinfo-manual-images source))
  562. (define build
  563. (with-imported-modules '((guix build utils))
  564. #~(begin
  565. (use-modules (guix build utils)
  566. (ice-9 match))
  567. (define (normalize language)
  568. ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
  569. (string-map (match-lambda
  570. (#\_ #\-)
  571. (chr chr))
  572. (string-downcase language)))
  573. (define (language->texi-file-name language)
  574. (if (string=? language "en")
  575. (string-append #$manual-source "/"
  576. #$manual ".texi")
  577. (string-append #$manual-source "/"
  578. #$manual "." language ".texi")))
  579. ;; Install a UTF-8 locale so that 'makeinfo' is at ease.
  580. (setenv "GUIX_LOCPATH"
  581. #+(file-append glibc-utf8-locales "/lib/locale"))
  582. (setenv "LC_ALL" "en_US.utf8")
  583. (setvbuf (current-output-port) 'line)
  584. (setvbuf (current-error-port) 'line)
  585. ;; 'makeinfo' looks for "htmlxref.cnf" in the current directory, so
  586. ;; copy it right here.
  587. (copy-file (string-append #$manual-source "/htmlxref.cnf")
  588. "htmlxref.cnf")
  589. (for-each (lambda (language)
  590. (let* ((texi (language->texi-file-name language))
  591. (opts `("--html"
  592. "-c" ,(string-append "TOP_NODE_UP_URL=/manual/"
  593. language)
  594. #$@options
  595. ,texi)))
  596. (format #t "building HTML manual for language '~a'...~%"
  597. language)
  598. (mkdir-p (string-append #$output "/"
  599. (normalize language)))
  600. (setenv "LANGUAGE" language)
  601. (apply invoke #$(file-append texinfo "/bin/makeinfo")
  602. "-o" (string-append #$output "/"
  603. (normalize language)
  604. "/html_node")
  605. opts)
  606. (apply invoke #$(file-append texinfo "/bin/makeinfo")
  607. "--no-split"
  608. "-o"
  609. (string-append #$output "/"
  610. (normalize language)
  611. "/" #$manual
  612. (if (string=? language "en")
  613. ""
  614. (string-append "." language))
  615. ".html")
  616. opts)
  617. ;; Make sure images are available.
  618. (symlink #$images
  619. (string-append #$output "/" (normalize language)
  620. "/images"))
  621. (symlink #$images
  622. (string-append #$output "/" (normalize language)
  623. "/html_node/images"))))
  624. (filter (compose file-exists? language->texi-file-name)
  625. '#$languages)))))
  626. (let* ((name (string-append manual "-html-manual"))
  627. (manual (computed-file name build)))
  628. (syntax-highlighted-html manual
  629. #:mono-node-indexes mono-node-indexes
  630. #:split-node-indexes split-node-indexes
  631. #:name (string-append name "-highlighted"))))
  632. (define* (pdf-manual source #:key (languages %languages)
  633. (version "0.0")
  634. (manual %manual)
  635. (date 1)
  636. (options '()))
  637. "Return the HTML manuals built from SOURCE for all LANGUAGES, with the given
  638. makeinfo OPTIONS."
  639. (define manual-source
  640. (texinfo-manual-source source
  641. #:version version
  642. #:languages languages
  643. #:date date))
  644. ;; FIXME: This union works, except for the table of contents of non-English
  645. ;; manuals, which contains escape sequences like "^^ca^^fe" instead of
  646. ;; accented letters.
  647. ;;
  648. ;; (define texlive
  649. ;; (texlive-updmap.cfg (list texlive-tex-texinfo
  650. ;; texlive-generic-epsf
  651. ;; texlive-fonts-ec)))
  652. (define build
  653. (with-imported-modules '((guix build utils))
  654. #~(begin
  655. (use-modules (guix build utils)
  656. (srfi srfi-34)
  657. (ice-9 match))
  658. (define (normalize language) ;XXX: deduplicate
  659. ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
  660. (string-map (match-lambda
  661. (#\_ #\-)
  662. (chr chr))
  663. (string-downcase language)))
  664. ;; Install a UTF-8 locale so that 'makeinfo' is at ease.
  665. (setenv "GUIX_LOCPATH"
  666. #+(file-append glibc-utf8-locales "/lib/locale"))
  667. (setenv "LC_ALL" "en_US.utf8")
  668. (setenv "PATH"
  669. (string-append #+(file-append texlive "/bin") ":"
  670. #+(file-append texinfo "/bin") ":"
  671. ;; Below are command-line tools needed by
  672. ;; 'texi2dvi' and friends.
  673. #+(file-append sed "/bin") ":"
  674. #+(file-append grep "/bin") ":"
  675. #+(file-append coreutils "/bin") ":"
  676. #+(file-append gawk "/bin") ":"
  677. #+(file-append tar "/bin") ":"
  678. #+(file-append diffutils "/bin")))
  679. (setvbuf (current-output-port) 'line)
  680. (setvbuf (current-error-port) 'line)
  681. (setenv "HOME" (getcwd)) ;for kpathsea/mktextfm
  682. ;; 'SOURCE_DATE_EPOCH' is honored by pdftex.
  683. (setenv "SOURCE_DATE_EPOCH" "1")
  684. (for-each (lambda (language)
  685. (let ((opts `("--pdf"
  686. "-I" "."
  687. #$@options
  688. ,(if (string=? language "en")
  689. (string-append #$manual-source "/"
  690. #$manual ".texi")
  691. (string-append #$manual-source "/"
  692. #$manual "." language ".texi")))))
  693. (format #t "building PDF manual for language '~a'...~%"
  694. language)
  695. (mkdir-p (string-append #$output "/"
  696. (normalize language)))
  697. (setenv "LANGUAGE" language)
  698. ;; FIXME: Unfortunately building PDFs for non-Latin
  699. ;; alphabets doesn't work:
  700. ;; <https://lists.gnu.org/archive/html/help-texinfo/2012-01/msg00014.html>.
  701. (guard (c ((invoke-error? c)
  702. (format (current-error-port)
  703. "~%~%Failed to produce \
  704. PDF for language '~a'!~%~%"
  705. language)))
  706. (apply invoke #$(file-append texinfo "/bin/makeinfo")
  707. "--pdf" "-o"
  708. (string-append #$output "/"
  709. (normalize language)
  710. "/" #$manual
  711. (if (string=? language "en")
  712. ""
  713. (string-append "."
  714. language))
  715. ".pdf")
  716. opts))))
  717. '#$languages))))
  718. (computed-file (string-append manual "-pdf-manual") build))
  719. (define (guix-manual-text-domain source languages)
  720. "Return the PO files for LANGUAGES of the 'guix-manual' text domain taken
  721. from SOURCE."
  722. (define po-directory
  723. (file-append* source "/po/doc"))
  724. (define build
  725. (with-imported-modules '((guix build utils))
  726. #~(begin
  727. (use-modules (guix build utils))
  728. (mkdir-p #$output)
  729. (for-each (lambda (language)
  730. (define directory
  731. (string-append #$output "/" language
  732. "/LC_MESSAGES"))
  733. (mkdir-p directory)
  734. (invoke #+(file-append gnu-gettext "/bin/msgfmt")
  735. "-c" "-o"
  736. (string-append directory "/guix-manual.mo")
  737. (string-append #$po-directory "/guix-manual."
  738. language ".po")))
  739. '#$(delete "en" languages)))))
  740. (computed-file "guix-manual-po" build))
  741. (define* (html-manual-indexes source
  742. #:key (languages %languages)
  743. (version "0.0")
  744. (manual %manual)
  745. (title (if (string=? "guix" manual)
  746. "GNU Guix Reference Manual"
  747. "GNU Guix Cookbook"))
  748. (date 1))
  749. (define build
  750. (with-extensions (list guile-json-3)
  751. (with-imported-modules '((guix build utils))
  752. #~(begin
  753. (use-modules (guix build utils)
  754. (json)
  755. (ice-9 match)
  756. (ice-9 popen)
  757. (sxml simple)
  758. (srfi srfi-1)
  759. (srfi srfi-19))
  760. (define (normalize language) ;XXX: deduplicate
  761. ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
  762. (string-map (match-lambda
  763. (#\_ #\-)
  764. (chr chr))
  765. (string-downcase language)))
  766. (define-syntax-rule (with-language language exp ...)
  767. (let ((lang (getenv "LANGUAGE")))
  768. (dynamic-wind
  769. (lambda ()
  770. (setenv "LANGUAGE" language)
  771. (setlocale LC_MESSAGES))
  772. (lambda () exp ...)
  773. (lambda ()
  774. (if lang
  775. (setenv "LANGUAGE" lang)
  776. (unsetenv "LANGUAGE"))
  777. (setlocale LC_MESSAGES)))))
  778. ;; (put 'with-language 'scheme-indent-function 1)
  779. (define* (translate str language
  780. #:key (domain "guix-manual"))
  781. (define exp
  782. `(begin
  783. (bindtextdomain "guix-manual"
  784. #+(guix-manual-text-domain
  785. source
  786. languages))
  787. (bindtextdomain "iso_639-3" ;language names
  788. #+(file-append iso-codes
  789. "/share/locale"))
  790. (write (gettext ,str ,domain))))
  791. (with-language language
  792. ;; Since the 'gettext' function caches msgid translations,
  793. ;; regardless of $LANGUAGE, we have to spawn a new process each
  794. ;; time we want to translate to a different language. Bah!
  795. (let* ((pipe (open-pipe* OPEN_READ
  796. #+(file-append guile-2.2
  797. "/bin/guile")
  798. "-c" (object->string exp)))
  799. (str (read pipe)))
  800. (close-pipe pipe)
  801. str)))
  802. (define (seconds->string seconds language)
  803. (let* ((time (make-time time-utc 0 seconds))
  804. (date (time-utc->date time)))
  805. (with-language language (date->string date "~e ~B ~Y"))))
  806. (define (guix-url path)
  807. (string-append #$%web-site-url path))
  808. (define (sxml-index language title body)
  809. ;; FIXME: Avoid duplicating styling info from guix-artwork.git.
  810. `(html (@ (lang ,language))
  811. (head
  812. (title ,(string-append title " — GNU Guix"))
  813. (meta (@ (charset "UTF-8")))
  814. (meta (@ (name "viewport") (content "width=device-width, initial-scale=1.0")))
  815. ;; Menu prefetch.
  816. (link (@ (rel "prefetch") (href ,(guix-url "menu/index.html"))))
  817. ;; Base CSS.
  818. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/elements.css"))))
  819. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/common.css"))))
  820. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/messages.css"))))
  821. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/navbar.css"))))
  822. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/breadcrumbs.css"))))
  823. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/buttons.css"))))
  824. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/footer.css"))))
  825. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/page.css"))))
  826. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/post.css")))))
  827. (body
  828. (header (@ (class "navbar"))
  829. (h1 (a (@ (class "branding")
  830. (href #$%web-site-url)))
  831. (span (@ (class "a11y-offset"))
  832. "Guix"))
  833. (nav (@ (class "menu"))))
  834. (nav (@ (class "breadcrumbs"))
  835. (a (@ (class "crumb")
  836. (href #$%web-site-url))
  837. "Home"))
  838. ,body
  839. (footer))))
  840. (define (language-index language)
  841. (define title
  842. (translate #$title language))
  843. (sxml-index
  844. language title
  845. `(main
  846. (article
  847. (@ (class "page centered-block limit-width"))
  848. (h2 ,title)
  849. (p (@ (class "post-metadata centered-text"))
  850. #$version " — "
  851. ,(seconds->string #$date language))
  852. (div
  853. (ul
  854. (li (a (@ (href "html_node"))
  855. "HTML, with a separate page per node"))
  856. (li (a (@ (href
  857. ,(string-append
  858. #$manual
  859. (if (string=? language
  860. "en")
  861. ""
  862. (string-append "."
  863. language))
  864. ".html")))
  865. "HTML, entirely on one page"))
  866. ,@(if (member language '("ru" "zh_CN"))
  867. '()
  868. `((li (a (@ (href ,(string-append
  869. #$manual
  870. (if (string=? language "en")
  871. ""
  872. (string-append "."
  873. language))
  874. ".pdf"))))
  875. "PDF")))))))))
  876. (define %iso639-languages
  877. (vector->list
  878. (assoc-ref (call-with-input-file
  879. #+(file-append iso-codes
  880. "/share/iso-codes/json/iso_639-3.json")
  881. json->scm)
  882. "639-3")))
  883. (define (language-code->name code)
  884. "Return the full name of a language from its ISO-639-3 code."
  885. (let ((code (match (string-index code #\_)
  886. (#f code)
  887. (index (string-take code index)))))
  888. (any (lambda (language)
  889. (and (string=? (or (assoc-ref language "alpha_2")
  890. (assoc-ref language "alpha_3"))
  891. code)
  892. (assoc-ref language "name")))
  893. %iso639-languages)))
  894. (define (top-level-index languages)
  895. (define title #$title)
  896. (sxml-index
  897. "en" title
  898. `(main
  899. (article
  900. (@ (class "page centered-block limit-width"))
  901. (h2 ,title)
  902. (div
  903. "This document is available in the following
  904. languages:\n"
  905. (ul
  906. ,@(map (lambda (language)
  907. `(li (a (@ (href ,(normalize language)))
  908. ,(translate
  909. (language-code->name language)
  910. language
  911. #:domain "iso_639-3"))))
  912. languages)))))))
  913. (define (write-html file sxml)
  914. (call-with-output-file file
  915. (lambda (port)
  916. (display "<!DOCTYPE html>\n" port)
  917. (sxml->xml sxml port))))
  918. (setenv "GUIX_LOCPATH"
  919. #+(file-append glibc-utf8-locales "/lib/locale"))
  920. (setenv "LC_ALL" "en_US.utf8")
  921. (setlocale LC_ALL "en_US.utf8")
  922. (for-each (lambda (language)
  923. (define directory
  924. (string-append #$output "/"
  925. (normalize language)))
  926. (mkdir-p directory)
  927. (write-html (string-append directory "/index.html")
  928. (language-index language)))
  929. '#$languages)
  930. (write-html (string-append #$output "/index.html")
  931. (top-level-index '#$languages))))))
  932. (computed-file "html-indexes" build))
  933. (define* (pdf+html-manual source
  934. #:key (languages %languages)
  935. (version "0.0")
  936. (date (time-second (current-time time-utc)))
  937. (mono-node-indexes (map list %languages))
  938. (split-node-indexes (map list %languages))
  939. (manual %manual))
  940. "Return the union of the HTML and PDF manuals, as well as the indexes."
  941. (directory-union (string-append manual "-manual")
  942. (map (lambda (proc)
  943. (proc source
  944. #:date date
  945. #:languages languages
  946. #:version version
  947. #:manual manual))
  948. (list html-manual-indexes
  949. (lambda (source . args)
  950. (apply html-manual source
  951. #:mono-node-indexes mono-node-indexes
  952. #:split-node-indexes split-node-indexes
  953. args))
  954. pdf-manual))
  955. #:copy? #t))
  956. (define (latest-commit+date directory)
  957. "Return two values: the last commit ID (a hex string) for DIRECTORY, and its
  958. commit date (an integer)."
  959. (let* ((repository (repository-open directory))
  960. (head (repository-head repository))
  961. (oid (reference-target head))
  962. (commit (commit-lookup repository oid)))
  963. ;; TODO: Use (git describe) when it's widely available.
  964. (values (oid->string oid) (commit-time commit))))
  965. ;;;
  966. ;;; Guile manual.
  967. ;;;
  968. (define guile-manual
  969. ;; The Guile manual as HTML, including both the mono-node "guile.html" and
  970. ;; the split-node "html_node" directory.
  971. (let ((guile guile-3.0-latest))
  972. (computed-file (string-append "guile-manual-" (package-version guile))
  973. (with-imported-modules '((guix build utils))
  974. #~(begin
  975. (use-modules (guix build utils)
  976. (ice-9 match))
  977. (setenv "PATH"
  978. (string-append #+tar "/bin:"
  979. #+xz "/bin:"
  980. #+texinfo "/bin"))
  981. (invoke "tar" "xf" #$(package-source guile))
  982. (mkdir-p (string-append #$output "/en/html_node"))
  983. (let* ((texi (find-files "." "^guile\\.texi$"))
  984. (documentation (match texi
  985. ((file) (dirname file)))))
  986. (with-directory-excursion documentation
  987. (invoke "makeinfo" "--html" "--no-split"
  988. "-o" (string-append #$output
  989. "/en/guile.html")
  990. "guile.texi")
  991. (invoke "makeinfo" "--html" "-o" "split"
  992. "guile.texi")
  993. (copy-recursively
  994. "split"
  995. (string-append #$output "/en/html_node")))))))))
  996. (define %guile-manual-base-url
  997. "https://www.gnu.org/software/guile/manual")
  998. (define (for-all-languages index)
  999. (map (lambda (language)
  1000. (list language index))
  1001. %languages))
  1002. (define guile-mono-node-indexes
  1003. ;; The Guile manual is only available in English so use the same index in
  1004. ;; all languages.
  1005. (for-all-languages
  1006. (html-manual-identifier-index (file-append guile-manual "/en")
  1007. %guile-manual-base-url
  1008. #:name "guile-html-index-en")))
  1009. (define guile-split-node-indexes
  1010. (for-all-languages
  1011. (html-manual-identifier-index (file-append guile-manual "/en/html_node")
  1012. (string-append %guile-manual-base-url
  1013. "/html_node")
  1014. #:name "guile-html-index-en")))
  1015. (define (merge-index-alists alist1 alist2)
  1016. "Merge ALIST1 and ALIST2, both of which are list of tuples like:
  1017. (LANGUAGE INDEX1 INDEX2 ...)
  1018. where LANGUAGE is a string like \"en\" and INDEX1 etc. are indexes as returned
  1019. by 'html-identifier-indexes'."
  1020. (let ((languages (delete-duplicates
  1021. (append (match alist1
  1022. (((languages . _) ...)
  1023. languages))
  1024. (match alist2
  1025. (((languages . _) ...)
  1026. languages))))))
  1027. (map (lambda (language)
  1028. (cons language
  1029. (append (or (assoc-ref alist1 language) '())
  1030. (or (assoc-ref alist2 language) '()))))
  1031. languages)))
  1032. (let* ((root (canonicalize-path
  1033. (string-append (current-source-directory) "/..")))
  1034. (commit date (latest-commit+date root))
  1035. (version (or (getenv "GUIX_MANUAL_VERSION")
  1036. (string-take commit 7)))
  1037. (select? (let ((vcs? (git-predicate root)))
  1038. (lambda (file stat)
  1039. (and (vcs? file stat)
  1040. ;; Filter out this file.
  1041. (not (string=? (basename file) "build.scm"))))))
  1042. (source (local-file root "guix" #:recursive? #t
  1043. #:select? select?)))
  1044. (define guix-manual
  1045. (html-manual source
  1046. #:manual "guix"
  1047. #:version version
  1048. #:date date))
  1049. (define guix-mono-node-indexes
  1050. ;; Alist of indexes for GUIX-MANUAL, where each key is a language code and
  1051. ;; each value is a file-like object containing the identifier index.
  1052. (html-identifier-indexes guix-manual ""
  1053. #:manual-name "guix"
  1054. #:base-url (if (string=? %manual "guix")
  1055. (const "")
  1056. (cut string-append
  1057. "/manual/devel/" <>))
  1058. #:languages %languages))
  1059. (define guix-split-node-indexes
  1060. ;; Likewise for the split-node variant of GUIX-MANUAL.
  1061. (html-identifier-indexes guix-manual "/html_node"
  1062. #:manual-name "guix"
  1063. #:base-url (if (string=? %manual "guix")
  1064. (const "")
  1065. (cut string-append
  1066. "/manual/devel/" <>
  1067. "/html_node"))
  1068. #:languages %languages))
  1069. (define mono-node-indexes
  1070. (merge-index-alists guix-mono-node-indexes guile-mono-node-indexes))
  1071. (define split-node-indexes
  1072. (merge-index-alists guix-split-node-indexes guile-split-node-indexes))
  1073. (format (current-error-port)
  1074. "building manual from work tree around commit ~a, ~a~%"
  1075. commit
  1076. (let* ((time (make-time time-utc 0 date))
  1077. (date (time-utc->date time)))
  1078. (date->string date "~e ~B ~Y")))
  1079. (pdf+html-manual source
  1080. ;; Always use the identifier indexes of GUIX-MANUAL and
  1081. ;; GUILE-MANUAL. Both "guix" and "guix-cookbook" can
  1082. ;; contain links to definitions that appear in either of
  1083. ;; these two manuals.
  1084. #:mono-node-indexes mono-node-indexes
  1085. #:split-node-indexes split-node-indexes
  1086. #:version version
  1087. #:date date))