build.scm 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019-2022 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 not translated in the same languages as the manual
  59. (if (string=? %manual "guix-cookbook")
  60. '("de" "en" "fr")
  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. ("nbsp" " ")
  396. (e (pk 'unknown-entity e) (primitive-exit 2))))
  397. (define (concatenate-snippets pieces)
  398. ;; Concatenate PIECES, which contains strings and entities,
  399. ;; replacing entities with their corresponding string.
  400. (let loop ((pieces pieces)
  401. (strings '()))
  402. (match pieces
  403. (()
  404. (string-concatenate-reverse strings))
  405. (((? string? str) . rest)
  406. (loop rest (cons str strings)))
  407. ((('*ENTITY* "additional" entity) . rest)
  408. (loop rest (cons (entity->string entity) strings)))
  409. ((('span _ lst ...) . rest) ;for <span class="roman">
  410. (loop (append lst rest) strings))
  411. ((('var name) . rest) ;for @var{name} within @lisp
  412. (loop rest (cons name strings))) ;XXX: losing formatting
  413. (something
  414. (pk 'unsupported-code-snippet something)
  415. (primitive-exit 1)))))
  416. (define (highlight-definition id category symbol args)
  417. ;; Produce stylable HTML for the given definition (an @deftp,
  418. ;; @deffn, or similar).
  419. `(dt (@ (id ,id) (class "symbol-definition"))
  420. (span (@ (class "symbol-definition-category"))
  421. ,@category)
  422. (span (@ (class "symbol-definition-prototype"))
  423. ,symbol " " ,@args)))
  424. (define (space? obj)
  425. (and (string? obj)
  426. (string-every char-set:whitespace obj)))
  427. (define (syntax-highlight sxml anchors)
  428. ;; Recurse over SXML and syntax-highlight code snippets.
  429. (let loop ((sxml sxml))
  430. (match sxml
  431. (('*TOP* decl body ...)
  432. `(*TOP* ,decl ,@(map loop body)))
  433. (('head things ...)
  434. `(head ,@things
  435. (link (@ (rel "stylesheet")
  436. (type "text/css")
  437. (href #$syntax-css-url)))))
  438. (('pre ('@ ('class "lisp")) code-snippet ...)
  439. `(pre (@ (class "lisp"))
  440. ,@(highlights->sxml*
  441. (pair-open/close
  442. (highlight lex-scheme
  443. (concatenate-snippets code-snippet)))
  444. anchors)))
  445. ;; Replace the ugly <strong> used for @deffn etc., which
  446. ;; translate to <dt>, with more stylable markup.
  447. (('dt (@ ('id id)) category ... ('strong thing))
  448. (highlight-definition id category thing '()))
  449. (('dt (@ ('id id)) category ... ('strong thing)
  450. (? space?) ('em args ...))
  451. (highlight-definition id category thing args))
  452. ((tag ('@ attributes ...) body ...)
  453. `(,tag (@ ,@attributes) ,@(map loop body)))
  454. ((tag body ...)
  455. `(,tag ,@(map loop body)))
  456. ((? string? str)
  457. str))))
  458. (define (process-html file anchors)
  459. ;; Parse FILE and perform syntax highlighting for its Scheme
  460. ;; snippets. Install the result to #$output.
  461. (format (current-error-port) "processing ~a...~%" file)
  462. (let* ((shtml (call-with-input-file file html->shtml))
  463. (highlighted (syntax-highlight shtml anchors))
  464. (base (string-drop file (string-length #$input)))
  465. (target (string-append #$output base)))
  466. (mkdir-p (dirname target))
  467. (call-with-output-file target
  468. (lambda (port)
  469. (write-shtml-as-html highlighted port)))))
  470. (define (copy-as-is file)
  471. ;; Copy FILE as is to #$output.
  472. (let* ((base (string-drop file (string-length #$input)))
  473. (target (string-append #$output base)))
  474. (mkdir-p (dirname target))
  475. (catch 'system-error
  476. (lambda ()
  477. (if (eq? 'symlink (stat:type (lstat file)))
  478. (symlink (readlink file) target)
  479. (link file target)))
  480. (lambda args
  481. (let ((errno (system-error-errno args)))
  482. (pk 'error-link file target (strerror errno))
  483. (primitive-exit 3))))))
  484. (define (html? file stat)
  485. (string-suffix? ".html" file))
  486. (define language+node-anchors
  487. (match-lambda
  488. ((language files ...)
  489. (cons language
  490. (fold (lambda (file vhash)
  491. (let ((alist (call-with-input-file file read)))
  492. ;; Use 'fold-right' so that the first entry
  493. ;; wins (e.g., "car" from "Pairs" rather than
  494. ;; from "rnrs base" in the Guile manual).
  495. (fold-right (match-lambda*
  496. (((key . value) vhash)
  497. (vhash-cons key value vhash)))
  498. vhash
  499. alist)))
  500. vlist-null
  501. files)))))
  502. (define mono-node-anchors
  503. ;; List of language/vhash pairs, where each vhash maps an
  504. ;; identifier to the corresponding URL in a single-page manual.
  505. (map language+node-anchors '#$mono-node-indexes))
  506. (define multi-node-anchors
  507. ;; Likewise for split-node manuals.
  508. (map language+node-anchors '#$split-node-indexes))
  509. ;; Install a UTF-8 locale so we can process UTF-8 files.
  510. (setenv "GUIX_LOCPATH"
  511. #+(file-append glibc-utf8-locales "/lib/locale"))
  512. (setlocale LC_ALL "en_US.utf8")
  513. ;; First process the mono-node 'guix.html' files.
  514. (for-each (match-lambda
  515. ((language . anchors)
  516. (let ((files (find-files
  517. (string-append #$input "/" language)
  518. "^guix(-cookbook|)(\\.[a-zA-Z_-]+)?\\.html$")))
  519. (n-par-for-each (parallel-job-count)
  520. (cut process-html <> anchors)
  521. files))))
  522. mono-node-anchors)
  523. ;; Process the multi-node HTML files.
  524. (for-each (match-lambda
  525. ((language . anchors)
  526. (let ((files (find-files
  527. (string-append #$input "/" language
  528. "/html_node")
  529. "\\.html$")))
  530. (n-par-for-each (parallel-job-count)
  531. (cut process-html <> anchors)
  532. files))))
  533. multi-node-anchors)
  534. ;; Last, copy non-HTML files as is.
  535. (for-each copy-as-is
  536. (find-files #$input (negate html?)))))))
  537. (computed-file name build))
  538. (define* (stylized-html source input
  539. #:key
  540. (languages %languages)
  541. (manual %manual)
  542. (manual-css-url "/static/base/css/manual.css"))
  543. "Process all the HTML files in INPUT; add them MANUAL-CSS-URL as a <style>
  544. link, and add a menu to choose among LANGUAGES. Use the Guix PO files found
  545. in SOURCE."
  546. (define build
  547. (with-extensions (list guile-lib)
  548. (with-imported-modules `((guix build utils)
  549. ((localization)
  550. => ,(localization-helper-module
  551. source languages)))
  552. #~(begin
  553. (use-modules (htmlprag)
  554. (localization)
  555. (guix build utils)
  556. (srfi srfi-1)
  557. (ice-9 match)
  558. (ice-9 threads))
  559. (define* (menu-dropdown #:key (label "Item") (url "#") (items '()))
  560. ;; Return an SHTML <li> element representing a dropdown for the
  561. ;; navbar. LABEL is the text of the dropdown menu, and ITEMS is
  562. ;; the list of items in this menu.
  563. (define id "visible-dropdown")
  564. `(li
  565. (@ (class "navbar-menu-item dropdown dropdown-btn"))
  566. (input (@ (class "navbar-menu-hidden-input")
  567. (type "radio")
  568. (name "dropdown")
  569. (id ,id)))
  570. (label (@ (for ,id)) ,label)
  571. (label (@ (for "all-dropdowns-hidden")) ,label)
  572. (div
  573. (@ (class "navbar-submenu")
  574. (id "navbar-submenu"))
  575. (div (@ (class "navbar-submenu-triangle"))
  576. " ")
  577. (ul ,@items))))
  578. (define (menu-item label url)
  579. ;; Return an SHTML <li> element for a menu item with the given
  580. ;; LABEL and URL.
  581. `(li (a (@ (class "navbar-menu-item")
  582. (href ,url))
  583. ,label)))
  584. (define* (navigation-bar menus #:key split-node?)
  585. ;; Return the navigation bar showing all of MENUS.
  586. `(header (@ (class "navbar"))
  587. (h1 (a (@ (class "branding")
  588. (href ,(if split-node? ".." "#")))))
  589. (nav (@ (class "navbar-menu"))
  590. (input (@ (class "navbar-menu-hidden-input")
  591. (type "radio")
  592. (name "dropdown")
  593. (id "all-dropdowns-hidden")))
  594. (ul ,@menus))
  595. ;; This is the button that shows up on small screen in
  596. ;; lieu of the drop-down button.
  597. (a (@ (class "navbar-menu-btn")
  598. (href ,(if split-node? "../.." ".."))))))
  599. (define* (base-language-url code manual
  600. #:key split-node?)
  601. ;; Return the base URL of MANUAL for language CODE.
  602. (if split-node?
  603. (string-append "../../" (normalize code) "/html_node")
  604. (string-append "../" (normalize code) "/" manual
  605. (if (string=? code "en")
  606. ""
  607. (string-append "." code))
  608. ".html")))
  609. (define (language-menu-items file)
  610. ;; Return the language menu items to be inserted in FILE.
  611. (define split-node?
  612. (string-contains file "/html_node/"))
  613. (append
  614. (map (lambda (code)
  615. (menu-item (language-code->native-name code)
  616. (base-language-url code #$manual
  617. #:split-node?
  618. split-node?)))
  619. '#$%languages)
  620. (list
  621. (menu-item "⊕"
  622. (if (string=? #$manual "guix-cookbook")
  623. "https://translate.fedoraproject.org/projects/guix/documentation-cookbook/"
  624. "https://translate.fedoraproject.org/projects/guix/documentation-manual/")))))
  625. (define (stylized-html sxml file)
  626. ;; Return SXML, which was read from FILE, with additional
  627. ;; styling.
  628. (define split-node?
  629. (string-contains file "/html_node/"))
  630. (let loop ((sxml sxml))
  631. (match sxml
  632. (('*TOP* decl body ...)
  633. `(*TOP* ,decl ,@(map loop body)))
  634. (('head elements ...)
  635. ;; Add reference to our own manual CSS, which provides
  636. ;; support for the language menu.
  637. `(head ,@elements
  638. (link (@ (rel "stylesheet")
  639. (type "text/css")
  640. (href #$manual-css-url)))))
  641. (('body ('@ attributes ...) elements ...)
  642. `(body (@ ,@attributes)
  643. ,(navigation-bar
  644. ;; TODO: Add "Contribute" menu, to report
  645. ;; errors, etc.
  646. (list (menu-dropdown
  647. #:label
  648. `(img (@ (alt "Language")
  649. (src "/static/base/img/language-picker.svg")))
  650. #:items
  651. (language-menu-items file)))
  652. #:split-node? split-node?)
  653. ,@elements))
  654. ((tag ('@ attributes ...) body ...)
  655. `(,tag (@ ,@attributes) ,@(map loop body)))
  656. ((tag body ...)
  657. `(,tag ,@(map loop body)))
  658. ((? string? str)
  659. str))))
  660. (define (process-html file)
  661. ;; Parse FILE and add links to translations. Install the result
  662. ;; to #$output.
  663. (format (current-error-port) "processing ~a...~%" file)
  664. (let* ((shtml (parameterize ((%strict-tokenizer? #t))
  665. (call-with-input-file file html->shtml)))
  666. (processed (stylized-html shtml file))
  667. (base (string-drop file (string-length #$input)))
  668. (target (string-append #$output base)))
  669. (mkdir-p (dirname target))
  670. (call-with-output-file target
  671. (lambda (port)
  672. (write-shtml-as-html processed port)))))
  673. ;; Install a UTF-8 locale so we can process UTF-8 files.
  674. (setenv "GUIX_LOCPATH"
  675. #+(file-append glibc-utf8-locales "/lib/locale"))
  676. (setlocale LC_ALL "en_US.utf8")
  677. (setenv "LC_ALL" "en_US.utf8")
  678. (setvbuf (current-error-port) 'line)
  679. (n-par-for-each (parallel-job-count)
  680. (lambda (file)
  681. (if (string-suffix? ".html" file)
  682. (process-html file)
  683. ;; Copy FILE as is to #$output.
  684. (let* ((base (string-drop file (string-length #$input)))
  685. (target (string-append #$output base)))
  686. (mkdir-p (dirname target))
  687. (if (eq? 'symlink (stat:type (lstat file)))
  688. (symlink (readlink file) target)
  689. (copy-file file target)))))
  690. (find-files #$input))))))
  691. (computed-file "stylized-html-manual" build))
  692. (define* (html-manual source #:key (languages %languages)
  693. (version "0.0")
  694. (manual %manual)
  695. (mono-node-indexes (map list languages))
  696. (split-node-indexes (map list languages))
  697. (date 1)
  698. (options %makeinfo-html-options))
  699. "Return the HTML manuals built from SOURCE for all LANGUAGES, with the given
  700. makeinfo OPTIONS."
  701. (define manual-source
  702. (texinfo-manual-source source
  703. #:version version
  704. #:languages languages
  705. #:date date))
  706. (define images
  707. (texinfo-manual-images source))
  708. (define build
  709. (with-imported-modules '((guix build utils))
  710. #~(begin
  711. (use-modules (guix build utils)
  712. (ice-9 match))
  713. (define (normalize language)
  714. ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
  715. (string-map (match-lambda
  716. (#\_ #\-)
  717. (chr chr))
  718. (string-downcase language)))
  719. (define (language->texi-file-name language)
  720. (if (string=? language "en")
  721. (string-append #$manual-source "/"
  722. #$manual ".texi")
  723. (string-append #$manual-source "/"
  724. #$manual "." language ".texi")))
  725. ;; Install a UTF-8 locale so that 'makeinfo' is at ease.
  726. (setenv "GUIX_LOCPATH"
  727. #+(file-append glibc-utf8-locales "/lib/locale"))
  728. (setenv "LC_ALL" "en_US.utf8")
  729. (setvbuf (current-output-port) 'line)
  730. (setvbuf (current-error-port) 'line)
  731. ;; 'makeinfo' looks for "htmlxref.cnf" in the current directory, so
  732. ;; copy it right here.
  733. (copy-file (string-append #$manual-source "/htmlxref.cnf")
  734. "htmlxref.cnf")
  735. (for-each (lambda (language)
  736. (let* ((texi (language->texi-file-name language))
  737. (opts `("--html"
  738. "-c" ,(string-append "TOP_NODE_UP_URL=/manual/"
  739. language)
  740. #$@options
  741. ,texi)))
  742. (format #t "building HTML manual for language '~a'...~%"
  743. language)
  744. (mkdir-p (string-append #$output "/"
  745. (normalize language)))
  746. (setenv "LANGUAGE" language)
  747. (apply invoke #$(file-append texinfo "/bin/makeinfo")
  748. "-o" (string-append #$output "/"
  749. (normalize language)
  750. "/html_node")
  751. opts)
  752. (apply invoke #$(file-append texinfo "/bin/makeinfo")
  753. "--no-split"
  754. "-o"
  755. (string-append #$output "/"
  756. (normalize language)
  757. "/" #$manual
  758. (if (string=? language "en")
  759. ""
  760. (string-append "." language))
  761. ".html")
  762. opts)
  763. ;; Make sure images are available.
  764. (symlink #$images
  765. (string-append #$output "/" (normalize language)
  766. "/images"))
  767. (symlink #$images
  768. (string-append #$output "/" (normalize language)
  769. "/html_node/images"))))
  770. (filter (compose file-exists? language->texi-file-name)
  771. '#$languages)))))
  772. (let* ((name (string-append manual "-html-manual"))
  773. (manual* (computed-file name build #:local-build? #f)))
  774. (syntax-highlighted-html (stylized-html source manual*
  775. #:languages languages
  776. #:manual manual)
  777. #:mono-node-indexes mono-node-indexes
  778. #:split-node-indexes split-node-indexes
  779. #:name (string-append name "-highlighted"))))
  780. (define* (pdf-manual source #:key (languages %languages)
  781. (version "0.0")
  782. (manual %manual)
  783. (date 1)
  784. (options '()))
  785. "Return the HTML manuals built from SOURCE for all LANGUAGES, with the given
  786. makeinfo OPTIONS."
  787. (define manual-source
  788. (texinfo-manual-source source
  789. #:version version
  790. #:languages languages
  791. #:date date))
  792. ;; FIXME: This union works, except for the table of contents of non-English
  793. ;; manuals, which contains escape sequences like "^^ca^^fe" instead of
  794. ;; accented letters.
  795. ;;
  796. ;; (define texlive
  797. ;; (texlive-updmap.cfg (list texlive-tex-texinfo
  798. ;; texlive-generic-epsf
  799. ;; texlive-fonts-ec)))
  800. (define build
  801. (with-imported-modules '((guix build utils))
  802. #~(begin
  803. (use-modules (guix build utils)
  804. (srfi srfi-34)
  805. (ice-9 match))
  806. (define (normalize language) ;XXX: deduplicate
  807. ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
  808. (string-map (match-lambda
  809. (#\_ #\-)
  810. (chr chr))
  811. (string-downcase language)))
  812. ;; Install a UTF-8 locale so that 'makeinfo' is at ease.
  813. (setenv "GUIX_LOCPATH"
  814. #+(file-append glibc-utf8-locales "/lib/locale"))
  815. (setenv "LC_ALL" "en_US.utf8")
  816. (setenv "PATH"
  817. (string-append #+(file-append texlive "/bin") ":"
  818. #+(file-append texinfo "/bin") ":"
  819. ;; Below are command-line tools needed by
  820. ;; 'texi2dvi' and friends.
  821. #+(file-append sed "/bin") ":"
  822. #+(file-append grep "/bin") ":"
  823. #+(file-append coreutils "/bin") ":"
  824. #+(file-append gawk "/bin") ":"
  825. #+(file-append tar "/bin") ":"
  826. #+(file-append diffutils "/bin")))
  827. (setvbuf (current-output-port) 'line)
  828. (setvbuf (current-error-port) 'line)
  829. (setenv "HOME" (getcwd)) ;for kpathsea/mktextfm
  830. ;; 'SOURCE_DATE_EPOCH' is honored by pdftex.
  831. (setenv "SOURCE_DATE_EPOCH" "1")
  832. (for-each (lambda (language)
  833. (let ((opts `("--pdf"
  834. "-I" "."
  835. #$@options
  836. ,(if (string=? language "en")
  837. (string-append #$manual-source "/"
  838. #$manual ".texi")
  839. (string-append #$manual-source "/"
  840. #$manual "." language ".texi")))))
  841. (format #t "building PDF manual for language '~a'...~%"
  842. language)
  843. (mkdir-p (string-append #$output "/"
  844. (normalize language)))
  845. (setenv "LANGUAGE" language)
  846. ;; FIXME: Unfortunately building PDFs for non-Latin
  847. ;; alphabets doesn't work:
  848. ;; <https://lists.gnu.org/archive/html/help-texinfo/2012-01/msg00014.html>.
  849. (guard (c ((invoke-error? c)
  850. (format (current-error-port)
  851. "~%~%Failed to produce \
  852. PDF for language '~a'!~%~%"
  853. language)))
  854. (apply invoke #$(file-append texinfo "/bin/makeinfo")
  855. "--pdf" "-o"
  856. (string-append #$output "/"
  857. (normalize language)
  858. "/" #$manual
  859. (if (string=? language "en")
  860. ""
  861. (string-append "."
  862. language))
  863. ".pdf")
  864. opts))))
  865. '#$languages))))
  866. (computed-file (string-append manual "-pdf-manual") build
  867. #:local-build? #f))
  868. (define (guix-manual-text-domain source languages)
  869. "Return the PO files for LANGUAGES of the 'guix-manual' text domain taken
  870. from SOURCE."
  871. (define po-directory
  872. (file-append* source "/po/doc"))
  873. (define build
  874. (with-imported-modules '((guix build utils))
  875. #~(begin
  876. (use-modules (guix build utils))
  877. (mkdir-p #$output)
  878. (for-each (lambda (language)
  879. (define directory
  880. (string-append #$output "/" language
  881. "/LC_MESSAGES"))
  882. (mkdir-p directory)
  883. (invoke #+(file-append gnu-gettext "/bin/msgfmt")
  884. "-c" "-o"
  885. (string-append directory "/guix-manual.mo")
  886. (string-append #$po-directory "/guix-manual."
  887. language ".po")))
  888. '#$(delete "en" languages)))))
  889. (computed-file "guix-manual-po" build))
  890. (define* (localization-helper-module source
  891. #:optional (languages %languages))
  892. "Return a file-like object for use as the (localization) module. SOURCE
  893. must be the Guix top-level source directory, from which PO files are taken."
  894. (define content
  895. (with-extensions (list guile-json-3)
  896. #~(begin
  897. (define-module (localization)
  898. #:use-module (json)
  899. #:use-module (srfi srfi-1)
  900. #:use-module (srfi srfi-19)
  901. #:use-module (ice-9 match)
  902. #:use-module (ice-9 popen)
  903. #:export (normalize
  904. with-language
  905. translate
  906. language-code->name
  907. language-code->native-name
  908. seconds->string))
  909. (define (normalize language) ;XXX: deduplicate
  910. ;; Normalize LANGUAGE. For instance, "zh_CN" becomes "zh-cn".
  911. (string-map (match-lambda
  912. (#\_ #\-)
  913. (chr chr))
  914. (string-downcase language)))
  915. (define-syntax-rule (with-language language exp ...)
  916. (let ((lang (getenv "LANGUAGE")))
  917. (dynamic-wind
  918. (lambda ()
  919. (setenv "LANGUAGE" language)
  920. (setlocale LC_MESSAGES))
  921. (lambda () exp ...)
  922. (lambda ()
  923. (if lang
  924. (setenv "LANGUAGE" lang)
  925. (unsetenv "LANGUAGE"))
  926. (setlocale LC_MESSAGES)))))
  927. ;; (put 'with-language 'scheme-indent-function 1)
  928. (define* (translate str language
  929. #:key (domain "guix-manual"))
  930. (define exp
  931. `(begin
  932. (bindtextdomain "guix-manual"
  933. #+(guix-manual-text-domain
  934. source
  935. languages))
  936. (bindtextdomain "iso_639-3" ;language names
  937. #+(file-append iso-codes
  938. "/share/locale"))
  939. (setenv "LANGUAGE" ,language)
  940. (write (gettext ,str ,domain))))
  941. ;; Since the 'gettext' function caches msgid translations,
  942. ;; regardless of $LANGUAGE, we have to spawn a new process each
  943. ;; time we want to translate to a different language. Bah!
  944. (let* ((pipe (open-pipe* OPEN_READ
  945. #+(file-append guile-3.0
  946. "/bin/guile")
  947. "-c" (object->string exp)))
  948. (str (read pipe)))
  949. (close-pipe pipe)
  950. str))
  951. (define %iso639-languages
  952. (vector->list
  953. (assoc-ref (call-with-input-file
  954. #+(file-append iso-codes
  955. "/share/iso-codes/json/iso_639-3.json")
  956. json->scm)
  957. "639-3")))
  958. (define (language-code->name code)
  959. "Return the full name of a language from its ISO-639-3 code."
  960. (let ((code (match (string-index code #\_)
  961. (#f code)
  962. (index (string-take code index)))))
  963. (any (lambda (language)
  964. (and (string=? (or (assoc-ref language "alpha_2")
  965. (assoc-ref language "alpha_3"))
  966. code)
  967. (assoc-ref language "name")))
  968. %iso639-languages)))
  969. (define (language-code->native-name code)
  970. "Return the name of language CODE in that language."
  971. (translate (language-code->name code) code
  972. #:domain "iso_639-3"))
  973. (define (seconds->string seconds language)
  974. (let* ((time (make-time time-utc 0 seconds))
  975. (date (time-utc->date time)))
  976. (with-language language (date->string date "~e ~B ~Y")))))))
  977. (scheme-file "localization.scm" content))
  978. (define* (html-manual-indexes source
  979. #:key (languages %languages)
  980. (version "0.0")
  981. (manual %manual)
  982. (title (if (string=? "guix" manual)
  983. "GNU Guix Reference Manual"
  984. "GNU Guix Cookbook"))
  985. (date 1))
  986. (define build
  987. (with-imported-modules `((guix build utils)
  988. ((localization)
  989. => ,(localization-helper-module
  990. source languages)))
  991. #~(begin
  992. (use-modules (guix build utils)
  993. (localization)
  994. (sxml simple)
  995. (srfi srfi-1))
  996. (define (guix-url path)
  997. (string-append #$%web-site-url path))
  998. (define (sxml-index language title body)
  999. ;; FIXME: Avoid duplicating styling info from guix-artwork.git.
  1000. `(html (@ (lang ,language))
  1001. (head
  1002. (title ,(string-append title " — GNU Guix"))
  1003. (meta (@ (charset "UTF-8")))
  1004. (meta (@ (name "viewport") (content "width=device-width, initial-scale=1.0")))
  1005. ;; Menu prefetch.
  1006. (link (@ (rel "prefetch") (href ,(guix-url "menu/index.html"))))
  1007. ;; Base CSS.
  1008. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/elements.css"))))
  1009. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/common.css"))))
  1010. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/messages.css"))))
  1011. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/navbar.css"))))
  1012. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/breadcrumbs.css"))))
  1013. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/buttons.css"))))
  1014. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/footer.css"))))
  1015. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/page.css"))))
  1016. (link (@ (rel "stylesheet") (href ,(guix-url "static/base/css/post.css")))))
  1017. (body
  1018. (header (@ (class "navbar"))
  1019. (h1 (a (@ (class "branding")
  1020. (href #$%web-site-url)))
  1021. (span (@ (class "a11y-offset"))
  1022. "Guix"))
  1023. (nav (@ (class "menu"))))
  1024. (nav (@ (class "breadcrumbs"))
  1025. (a (@ (class "crumb")
  1026. (href #$%web-site-url))
  1027. "Home"))
  1028. ,body
  1029. (footer))))
  1030. (define (language-index language)
  1031. (define title
  1032. (translate #$title language))
  1033. (sxml-index
  1034. language title
  1035. `(main
  1036. (article
  1037. (@ (class "page centered-block limit-width"))
  1038. (h2 ,title)
  1039. (p (@ (class "post-metadata centered-text"))
  1040. #$version " — "
  1041. ,(seconds->string #$date language))
  1042. (div
  1043. (ul
  1044. (li (a (@ (href "html_node"))
  1045. "HTML, with a separate page per node"))
  1046. (li (a (@ (href
  1047. ,(string-append
  1048. #$manual
  1049. (if (string=? language
  1050. "en")
  1051. ""
  1052. (string-append "."
  1053. language))
  1054. ".html")))
  1055. "HTML, entirely on one page"))
  1056. ,@(if (member language '("ru" "zh_CN"))
  1057. '()
  1058. `((li (a (@ (href ,(string-append
  1059. #$manual
  1060. (if (string=? language "en")
  1061. ""
  1062. (string-append "."
  1063. language))
  1064. ".pdf"))))
  1065. "PDF")))))))))
  1066. (define (top-level-index languages)
  1067. (define title #$title)
  1068. (sxml-index
  1069. "en" title
  1070. `(main
  1071. (article
  1072. (@ (class "page centered-block limit-width"))
  1073. (h2 ,title)
  1074. (div
  1075. "This document is available in the following
  1076. languages:\n"
  1077. (ul
  1078. ,@(map (lambda (language)
  1079. `(li (a (@ (href ,(normalize language)))
  1080. ,(language-code->native-name language))))
  1081. languages)))))))
  1082. (define (write-html file sxml)
  1083. (call-with-output-file file
  1084. (lambda (port)
  1085. (display "<!DOCTYPE html>\n" port)
  1086. (sxml->xml sxml port))))
  1087. (setenv "GUIX_LOCPATH"
  1088. #+(file-append glibc-utf8-locales "/lib/locale"))
  1089. (setenv "LC_ALL" "en_US.utf8")
  1090. (setlocale LC_ALL "en_US.utf8")
  1091. (for-each (lambda (language)
  1092. (define directory
  1093. (string-append #$output "/"
  1094. (normalize language)))
  1095. (mkdir-p directory)
  1096. (write-html (string-append directory "/index.html")
  1097. (language-index language)))
  1098. '#$languages)
  1099. (write-html (string-append #$output "/index.html")
  1100. (top-level-index '#$languages)))))
  1101. (computed-file "html-indexes" build))
  1102. (define* (pdf+html-manual source
  1103. #:key (languages %languages)
  1104. (version "0.0")
  1105. (date (time-second (current-time time-utc)))
  1106. (mono-node-indexes (map list %languages))
  1107. (split-node-indexes (map list %languages))
  1108. (manual %manual))
  1109. "Return the union of the HTML and PDF manuals, as well as the indexes."
  1110. (directory-union (string-append manual "-manual")
  1111. (map (lambda (proc)
  1112. (proc source
  1113. #:date date
  1114. #:languages languages
  1115. #:version version
  1116. #:manual manual))
  1117. (list html-manual-indexes
  1118. (lambda (source . args)
  1119. (apply html-manual source
  1120. #:mono-node-indexes mono-node-indexes
  1121. #:split-node-indexes split-node-indexes
  1122. args))
  1123. pdf-manual))
  1124. #:copy? #t))
  1125. (define (latest-commit+date directory)
  1126. "Return two values: the last commit ID (a hex string) for DIRECTORY, and its
  1127. commit date (an integer)."
  1128. (let* ((repository (repository-open directory))
  1129. (head (repository-head repository))
  1130. (oid (reference-target head))
  1131. (commit (commit-lookup repository oid)))
  1132. ;; TODO: Use (git describe) when it's widely available.
  1133. (values (oid->string oid) (commit-time commit))))
  1134. ;;;
  1135. ;;; Guile manual.
  1136. ;;;
  1137. (define guile-manual
  1138. ;; The Guile manual as HTML, including both the mono-node "guile.html" and
  1139. ;; the split-node "html_node" directory.
  1140. (let ((guile guile-3.0-latest))
  1141. (computed-file (string-append "guile-manual-" (package-version guile))
  1142. (with-imported-modules '((guix build utils))
  1143. #~(begin
  1144. (use-modules (guix build utils)
  1145. (ice-9 match))
  1146. (setenv "PATH"
  1147. (string-append #+tar "/bin:"
  1148. #+xz "/bin:"
  1149. #+texinfo "/bin"))
  1150. (invoke "tar" "xf" #$(package-source guile))
  1151. (mkdir-p (string-append #$output "/en/html_node"))
  1152. (let* ((texi (find-files "." "^guile\\.texi$"))
  1153. (documentation (match texi
  1154. ((file) (dirname file)))))
  1155. (with-directory-excursion documentation
  1156. (invoke "makeinfo" "--html" "--no-split"
  1157. "-o" (string-append #$output
  1158. "/en/guile.html")
  1159. "guile.texi")
  1160. (invoke "makeinfo" "--html" "-o" "split"
  1161. "guile.texi")
  1162. (copy-recursively
  1163. "split"
  1164. (string-append #$output "/en/html_node")))))))))
  1165. (define %guile-manual-base-url
  1166. "https://www.gnu.org/software/guile/manual")
  1167. (define (for-all-languages index)
  1168. (map (lambda (language)
  1169. (list language index))
  1170. %languages))
  1171. (define guile-mono-node-indexes
  1172. ;; The Guile manual is only available in English so use the same index in
  1173. ;; all languages.
  1174. (for-all-languages
  1175. (html-manual-identifier-index (file-append guile-manual "/en")
  1176. %guile-manual-base-url
  1177. #:name "guile-html-index-en")))
  1178. (define guile-split-node-indexes
  1179. (for-all-languages
  1180. (html-manual-identifier-index (file-append guile-manual "/en/html_node")
  1181. (string-append %guile-manual-base-url
  1182. "/html_node")
  1183. #:name "guile-html-index-en")))
  1184. (define (merge-index-alists alist1 alist2)
  1185. "Merge ALIST1 and ALIST2, both of which are list of tuples like:
  1186. (LANGUAGE INDEX1 INDEX2 ...)
  1187. where LANGUAGE is a string like \"en\" and INDEX1 etc. are indexes as returned
  1188. by 'html-identifier-indexes'."
  1189. (let ((languages (delete-duplicates
  1190. (append (match alist1
  1191. (((languages . _) ...)
  1192. languages))
  1193. (match alist2
  1194. (((languages . _) ...)
  1195. languages))))))
  1196. (map (lambda (language)
  1197. (cons language
  1198. (append (or (assoc-ref alist1 language) '())
  1199. (or (assoc-ref alist2 language) '()))))
  1200. languages)))
  1201. (let* ((root (canonicalize-path
  1202. (string-append (current-source-directory) "/..")))
  1203. (commit date (latest-commit+date root))
  1204. (version (or (getenv "GUIX_MANUAL_VERSION")
  1205. (string-take commit 7)))
  1206. (select? (let ((vcs? (git-predicate root)))
  1207. (lambda (file stat)
  1208. (and (vcs? file stat)
  1209. ;; Filter out this file.
  1210. (not (string=? (basename file) "build.scm"))))))
  1211. (source (local-file root "guix" #:recursive? #t
  1212. #:select? select?)))
  1213. (define guix-manual
  1214. (html-manual source
  1215. #:manual "guix"
  1216. #:version version
  1217. #:date date))
  1218. (define guix-mono-node-indexes
  1219. ;; Alist of indexes for GUIX-MANUAL, where each key is a language code and
  1220. ;; each value is a file-like object containing the identifier index.
  1221. (html-identifier-indexes guix-manual ""
  1222. #:manual-name "guix"
  1223. #:base-url (if (string=? %manual "guix")
  1224. (const "")
  1225. (cut string-append
  1226. "/manual/devel/" <>))
  1227. #:languages %languages))
  1228. (define guix-split-node-indexes
  1229. ;; Likewise for the split-node variant of GUIX-MANUAL.
  1230. (html-identifier-indexes guix-manual "/html_node"
  1231. #:manual-name "guix"
  1232. #:base-url (if (string=? %manual "guix")
  1233. (const "")
  1234. (cut string-append
  1235. "/manual/devel/" <>
  1236. "/html_node"))
  1237. #:languages %languages))
  1238. (define mono-node-indexes
  1239. (merge-index-alists guix-mono-node-indexes guile-mono-node-indexes))
  1240. (define split-node-indexes
  1241. (merge-index-alists guix-split-node-indexes guile-split-node-indexes))
  1242. (format (current-error-port)
  1243. "building manual from work tree around commit ~a, ~a~%"
  1244. commit
  1245. (let* ((time (make-time time-utc 0 date))
  1246. (date (time-utc->date time)))
  1247. (date->string date "~e ~B ~Y")))
  1248. (pdf+html-manual source
  1249. ;; Always use the identifier indexes of GUIX-MANUAL and
  1250. ;; GUILE-MANUAL. Both "guix" and "guix-cookbook" can
  1251. ;; contain links to definitions that appear in either of
  1252. ;; these two manuals.
  1253. #:mono-node-indexes mono-node-indexes
  1254. #:split-node-indexes split-node-indexes
  1255. #:version version
  1256. #:date date))