2
;;; extract - filter bilingual texinfo document
4
;;; Copyright(C) 2001-2003 by Shiro Kawai (shiro@acm.org)
6
;;; Permission to use, copy, modify, distribute this software and
7
;;; accompanying documentation for any purpose is hereby granted,
8
;;; provided that existing copyright notices are retained in all
9
;;; copies and that this notice is included verbatim in all
11
;;; This software is provided as is, without express or implied
12
;;; warranty. In no circumstances the author(s) shall be liable
13
;;; for any damages arising out of the use of this software.
15
;;; $Id: extract,v 1.16 2006/04/07 11:29:48 shirok Exp $
26
(string-trim-both (call-with-input-file "../VERSION" port->string)))
27
(define *node-table* '())
28
(define *header-table* '())
31
(let ((current-node #f)
36
(#/^@node\s+([^,]+)/ (#f node)
37
(set! current-node (string-trim-right node)))
38
(#/^@(chapter|(sub)*section|appendix\w*)\s+(.*)/ (#f #f #f header)
39
(set! current-header (string-trim-right header)))
40
(#/^@c NODE\s+([^,]*)(,(.*))?/ (#f jnode #f jheader)
41
(let* ((jn (string-trim-right jnode))
42
(jh (if jheader (string-trim-both jheader) jn)))
43
(push! *node-table* (cons current-node jn))
44
(push! *header-table* (cons current-header jh))))
45
(#/^@include\s+(\S+)/ (#f file)
46
(with-input-from-file file (cut scan-nodes) :encoding 'euc-jp))
50
(define (filter pattern-in pattern-out)
54
(pattern-in () (in (read-line)))
55
(pattern-out () (out (read-line)))
56
(#/^@include\s+(\S+)/ (#f file)
57
(with-input-from-file file (cut filter pattern-in pattern-out)
60
(#/^@c COMMON$/ () (in (read-line)))
61
(test (lambda _ (eq? *lang* 'en))
62
(display (regexp-replace-all #/@VERSION@/ line *version*))
63
(newline) (in (read-line)))
64
(#/^@node\s+(.*)$/ (#f nodedesc)
65
(process-node nodedesc) (in (read-line)))
66
(#/^@(chapter|(sub)*section|appendix\w*)\s+(.*)/ (#f cmd #f header)
67
(process-header cmd header) (in (read-line)))
68
(#/^\* ([^:]+)::(.*)?/ (#f node desc)
69
(process-menu node #f desc) (in (read-line)))
70
(#/^\* ([^:]+):\s+([^)]+\))\.(.*)?/ (#f tag node desc)
71
(process-menu node tag desc) (in (read-line)))
73
(regexp-replace-all #/@VERSION@/
74
(regexp-replace-all #/(@x?ref)\{([^\}]+)\}/ line process-ref)
82
(pattern-in () (in (read-line)))
83
(#/^@c COMMON$/ () (in (read-line)))
84
(else (out (read-line)))))
88
(define (process-node nodedesc)
93
(cond ((assoc (string-trim-both name) *node-table*) => cdr)
95
(string-split nodedesc #\,))
99
(define (process-header cmd header)
100
(format #t "@~a ~a\n"
102
(cond ((assoc (string-trim-both header) *header-table*) => cdr)
105
(define (process-menu node tag desc)
107
(format #t "* ~a: ~a. ~a\n"
109
(cond ((assoc (string-trim-both node) *node-table*) => cdr)
111
(string-trim-both (or desc "")))
112
(format #t "* ~a:: ~a\n"
113
(cond ((assoc (string-trim-both node) *node-table*) => cdr)
115
(string-trim-both (or desc "")))))
117
(define (process-ref match)
118
(let ((cmd (rxmatch-substring match 1))
119
(node (rxmatch-substring match 2)))
122
(cond ((assoc (string-trim-both node) *node-table*) => cdr)
126
(display "Usage: extract [-en|-jp][-o outfile] infile\n")
130
(let ((a (parse-options (cdr args)
131
(("o=s" (outfile) (set! *outfile* outfile))
132
("en" () (set! *lang* 'en))
133
("jp" () (set! *lang* 'jp))
138
((en) (filter #/^@c EN$/ #/^@c JP$/))
139
((jp) (filter #/^@c JP$/ #/^@c EN$/))))
141
(define outenc (if (eq? *lang* 'jp) 'euc-jp 'utf8))
143
(unless (= (length a) 1) (usage))
145
(when (eq? *lang* 'jp)
146
(with-input-from-file (car a) scan-nodes :encoding 'euc-jp))
148
(with-input-from-file (car a)
151
(with-output-to-file *outfile* do-it :encoding outenc)
152
(let1 out (open-output-conversion-port
153
(current-output-port) outenc)
154
(with-output-to-port out do-it)
155
(close-output-port out))))