| 1 |
;; -*- coding:utf-8 -*- |
| 2 |
|
| 3 |
(use file.util) |
| 4 |
(use gauche.process) |
| 5 |
(use gauche.version) |
| 6 |
(use srfi.13) |
| 7 |
(use util.match) |
| 8 |
|
| 9 |
;;; |
| 10 |
;;; Call makeinfo to generate various document output |
| 11 |
;;; |
| 12 |
|
| 13 |
(define (main args) |
| 14 |
(if (match (cdr args) |
| 15 |
[("info" input makeinfo gzip) (do-info input makeinfo gzip)] |
| 16 |
[("html" input makeinfo) (do-html input makeinfo)] |
| 17 |
[("htmls" input makeinfo version) (do-htmls input makeinfo version)] |
| 18 |
[("pdf" input makeinfo) (do-pdf input makeinfo)] |
| 19 |
[("dvi" input makeinfo) (do-dvi input makeinfo)] |
| 20 |
[("epub" input makeinfo) (do-epub input makeinfo)] |
| 21 |
[_ (usage)]) |
| 22 |
0 1)) |
| 23 |
|
| 24 |
(define (usage) |
| 25 |
(print "Usage: gosh makedoc.scm command args ...") |
| 26 |
(print "Valid commands (and args):") |
| 27 |
(print " info input MAKEINFO GZIP - generate info doc") |
| 28 |
(print " html input MAKEINFO - generate single html") |
| 29 |
(print " htmls input MAKEINFO VERSION-STRING - generate html files in subdir") |
| 30 |
(print " pdf input MAKEINFO - generate pdf") |
| 31 |
(print " dvi input MAKEINFO - generate dvi") |
| 32 |
(print " epub input MAKEINFO - generate ePub") |
| 33 |
#f) |
| 34 |
|
| 35 |
(define (make-cmd cmd-list) |
| 36 |
(cond-expand |
| 37 |
[gauche.os.windows |
| 38 |
;; for MSYS (mintty) |
| 39 |
(if-let1 sh (sys-getenv "SHELL") |
| 40 |
`("cmd.exe" "/c" ,sh "-c" ,(string-join |
| 41 |
(map (^c (shell-escape-string (x->string c) 'posix)) |
| 42 |
cmd-list) |
| 43 |
" ")) |
| 44 |
`("cmd.exe" "/c" ,@cmd-list))] |
| 45 |
[else cmd-list])) |
| 46 |
|
| 47 |
(define (check-makeinfo-version makeinfo min-version :optional (quiet? #f)) |
| 48 |
(and-let* ([ makeinfo ] |
| 49 |
[msg (process-output->string (make-cmd `(,makeinfo --version)))] |
| 50 |
[vers (rxmatch->string #/\d+\.\d+(\.\d+)?/ msg)]) |
| 51 |
(rlet1 b (version<=? min-version vers) |
| 52 |
(when (and (not b) (not quiet?)) |
| 53 |
(warn "makeinfo version ~a or greater is required, but ~a's \ |
| 54 |
version is ~a. Skipping.\n" min-version makeinfo vers))))) |
| 55 |
|
| 56 |
(define (do-info input makeinfo gzip) |
| 57 |
(define info (path-swap-extension input "info")) |
| 58 |
(or (string-null? makeinfo) |
| 59 |
(string-null? gzip) |
| 60 |
(not (check-makeinfo-version makeinfo "5.0")) |
| 61 |
(and (do-process (make-cmd `(,makeinfo ,input))) |
| 62 |
(begin (remove-files (glob #"~|info|*.gz")) |
| 63 |
(do-process (make-cmd `(,gzip "-n" ,info ,@(glob #"~|info|-[0-9]*")))))))) |
| 64 |
|
| 65 |
(define (do-html input makeinfo) |
| 66 |
(or (string-null? makeinfo) |
| 67 |
(not (check-makeinfo-version makeinfo "5.0")) |
| 68 |
(do-process (make-cmd |
| 69 |
`(,makeinfo "--html" |
| 70 |
"--no-split" |
| 71 |
"--set-customization-variable" |
| 72 |
"TOP_NODE_UP_URL=https://practical-scheme.net/gauche" |
| 73 |
"-")) |
| 74 |
:redirects `((<< 0 ,(alter-top-node input)))))) |
| 75 |
|
| 76 |
(define (do-htmls input makeinfo version-info) |
| 77 |
(define draft? (#/DRAFT/ version-info)) |
| 78 |
(define ja? (#/j\.texi$/ input)) |
| 79 |
(define top-link |
| 80 |
(cond [draft? "https://github.com/shirok/Gauche"] |
| 81 |
[ja? "https://practical-scheme.net/gauche/memo-j.html"] |
| 82 |
[else "https://practical-scheme.net/gauche/memo.html"])) |
| 83 |
(define header-style (if draft? |
| 84 |
"width:100%;background-color:#f88;" |
| 85 |
"width:100%;background-color:#cfc;")) |
| 86 |
(define lang (if ja? "ja" "en")) |
| 87 |
(define draft-search (if draft? |
| 88 |
"<input type=\"hidden\" name=\"v\" value=\"draft\">" |
| 89 |
"")) |
| 90 |
(define header-div #"<div style=\"~|header-style|\">\ |
| 91 |
<form action=\"https://practical-scheme.net/gauche/man/\"\ |
| 92 |
style=\"padding:5px 10px\">\ |
| 93 |
<a href=\"~|top-link|\">For ~|version-info|</a>\ |
| 94 |
<span style=\"float: right\">\ |
| 95 |
Search (procedure/syntax/module): \ |
| 96 |
<input type=\"text\" name=\"p\">\ |
| 97 |
<input type=\"hidden\" name=\"l\" value=\"~|lang|\">\ |
| 98 |
~|draft-search|\ |
| 99 |
</span>\ |
| 100 |
</form>\ |
| 101 |
</div>") |
| 102 |
(define draft-mark (if draft? |
| 103 |
"<div style=\"position:fixed;top:150px;right:0px;\ |
| 104 |
font:bold 70px Helvetica,sans-serif;\ |
| 105 |
color:#f00;opacity:0.4;\ |
| 106 |
transform:rotate(-90deg)\">\ |
| 107 |
DRAFT\ |
| 108 |
</div>" |
| 109 |
"")) |
| 110 |
(or (string-null? makeinfo) |
| 111 |
(not (check-makeinfo-version makeinfo "5.0")) |
| 112 |
(do-process (make-cmd |
| 113 |
`(,makeinfo "--html" |
| 114 |
"--split=section" |
| 115 |
"--set-customization-variable" |
| 116 |
,#"AFTER_BODY_OPEN=~|header-div|<hr>" |
| 117 |
"--set-customization-variable" |
| 118 |
,#"PRE_BODY_CLOSE=<hr>~|header-div|~|draft-mark|" |
| 119 |
"--set-customization-variable" |
| 120 |
,#"TOP_NODE_UP_URL=~|top-link|" |
| 121 |
,@(cond-list |
| 122 |
[(check-makeinfo-version makeinfo "6.8" #t) @ |
| 123 |
`("--set-customization-variable" |
| 124 |
"FORMAT_MENU=menu" |
| 125 |
"--set-customization-variable" |
| 126 |
"CONTENTS_OUTPUT_LOCATION=inline")] |
| 127 |
[draft? @ |
| 128 |
`("-o" |
| 129 |
,#"~(path-sans-extension input)-draft")]) |
| 130 |
"-")) |
| 131 |
:redirects `((<< 0 ,(alter-top-node input)))))) |
| 132 |
|
| 133 |
;; For html, makeinfo generates "(dir)" link in the top node, which we don't |
| 134 |
;; need. TOP_NODE_UP_URL doesn't alter link for "Previous". |
| 135 |
;; So we employ this kludge to modify the Top node definition |
| 136 |
(define (alter-top-node input) |
| 137 |
($ (cut string-join <> "\n" 'suffix) |
| 138 |
$ map (^l (if (#/^@node Top,/ l) "@node Top" l)) |
| 139 |
$ file->string-list input)) |
| 140 |
|
| 141 |
;; For pdf and dvi: We need newer luatex (0.95 or later) to process Japanese. |
| 142 |
;; But even if you don't have that newer luatex, TeX can handle English |
| 143 |
;; version. So, for now, we conditionally switch TeX based on the input |
| 144 |
;; file name. We won't need the switching once everybody has newer versions. |
| 145 |
;; (NB: Also not you need texinfo-ja to process Japanese version; they've |
| 146 |
;; merged only recently (May 2016) and may not be available unless you're |
| 147 |
;; using bleeding edge. Cf. http://www.trueroad.jp/2016/05/14-01.html) |
| 148 |
|
| 149 |
(define (do-pdf input makeinfo) |
| 150 |
(cond-expand |
| 151 |
[gauche.sys.setenv |
| 152 |
(when (#/j\.texi$/ input) |
| 153 |
(sys-putenv "PDFTEX=luatex"))] |
| 154 |
[else]) |
| 155 |
(do-process (make-cmd `(,makeinfo "--pdf" "--Xopt" "--tidy" ,input)))) |
| 156 |
|
| 157 |
(define (do-dvi input makeinfo) |
| 158 |
(do-process (make-cmd `(,makeinfo "--dvi" "--Xopt" "--tidy" ,input)))) |
| 159 |
|
| 160 |
(define (do-epub input makeinfo) |
| 161 |
(do-process (make-cmd `(,makeinfo "--epub3" ,input)))) |