build.scm 54 KB

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