build.scm 53 KB

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