Debian Codegraph @

doc/extract

raw
1 ;;;
2 ;;; extract - filter bilingual texinfo document
3 ;;;
4 ;;; Copyright(C) 2001-2014 by Shiro Kawai ([email protected])
5 ;;;
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
10 ;;; distributions.
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.
14 ;;;
15 ;;; $Id: extract,v 1.5 2006-11-09 23:56:20 shirok Exp $
16 ;;;
17
18 (use gauche.parseopt)
19 (use gauche.charconv)
20 (use srfi-1)
21 (use srfi-13)
22 (use file.util)
23
24 (define *outfile* #f)
25 (define *lang* 'en)
26 (define *version*
27 (string-trim-both (file->string (or (find (cut sys-access <> R_OK)
28 '("../VERSION"
29 "./VERSION"))
30 (error "No VERSION file?")))))
31 (define *node-table* '())
32 (define *header-table* '())
33
34 (define (scan-nodes)
35 (let ((current-node #f)
36 (current-header #f))
37 (port-for-each
38 (lambda (line)
39 (rxmatch-case line
40 (#/^@node\s+([^,]+)/ (#f node)
41 (set! current-node (string-trim-right node)))
42 (#/^@(chapter|(sub)*section|appendix\w*)\s+(.*)/ (#f #f #f header)
43 (set! current-header (string-trim-right header)))
44 (#/^@c NODE\s+([^,]*)(,(.*))?/ (#f jnode #f jheader)
45 (let* ((jn (string-trim-right jnode))
46 (jh (if jheader (string-trim-both jheader) jn)))
47 (push! *node-table* (cons current-node jn))
48 (push! *header-table* (cons current-header jh))))
49 (#/^@include\s+(\S+)/ (#f file)
50 (with-input-from-file file (cut scan-nodes) :encoding 'utf-8))
51 ))
52 read-line)))
53
54 (define (filter pattern-in pattern-out)
55 (define (in line)
56 (rxmatch-case line
57 (test eof-object?)
58 (pattern-in () (in (read-line)))
59 (pattern-out () (out (read-line)))
60 (#/^@include\s+(\S+)/ (#f file)
61 (with-input-from-file file (cut filter pattern-in pattern-out)
62 :encoding 'utf-8)
63 (in (read-line)))
64 (#/^@c COMMON$/ () (in (read-line)))
65 (test (lambda _ (eq? *lang* 'en))
66 (display (regexp-replace-all #/@VERSION@/ line *version*))
67 (newline) (in (read-line)))
68 (#/^@c / () (in (read-line))) ; other comment line
69 (#/^@node\s+(.*)$/ (#f nodedesc)
70 (process-node nodedesc) (in (read-line)))
71 (#/^@(chapter|(sub)*section|appendix\w*)\s+(.*)/ (#f cmd #f header)
72 (process-header cmd header) (in (read-line)))
73 (#/^\* ([^:]+)::(.*)?/ (#f node desc)
74 (process-menu node #f desc) (in (read-line)))
75 (#/^\* ([^:]+):\s+([^)]+\))\.(.*)?/ (#f tag node desc)
76 (process-menu node tag desc) (in (read-line)))
77 (else (display
78 (regexp-replace-all #/@VERSION@/
79 (regexp-replace-all #/(@x?ref)\{([^\}]+)\}/ line process-ref)
80 *version*))
81 (newline)
82 (in (read-line)))))
83
84 (define (out line)
85 (rxmatch-case line
86 (test eof-object?)
87 (pattern-in () (in (read-line)))
88 (#/^@c COMMON$/ () (in (read-line)))
89 (else (out (read-line)))))
90
91 (in (read-line)))
92
93 (define (process-node nodedesc)
94 (display "@node ")
95 (display
96 (string-join
97 (map (lambda (name)
98 (cond ((assoc (string-trim-both name) *node-table*) => cdr)
99 (else name)))
100 (string-split nodedesc #\,))
101 ", "))
102 (newline))
103
104 (define (process-header cmd header)
105 (format #t "@~a ~a\n"
106 cmd
107 (cond ((assoc (string-trim-both header) *header-table*) => cdr)
108 (else header))))
109
110 (define (process-menu node tag desc)
111 (if tag
112 (format #t "* ~a: ~a. ~a\n"
113 tag
114 (cond ((assoc (string-trim-both node) *node-table*) => cdr)
115 (else node))
116 (string-trim-both (or desc "")))
117 (format #t "* ~a:: ~a\n"
118 (cond ((assoc (string-trim-both node) *node-table*) => cdr)
119 (else node))
120 (string-trim-both (or desc "")))))
121
122 (define (process-ref match)
123 (let ((cmd (rxmatch-substring match 1))
124 (node (rxmatch-substring match 2)))
125 (format #f "~a{~a}"
126 cmd
127 (cond ((assoc (string-trim-both node) *node-table*) => cdr)
128 (else node)))))
129
130 (define (usage)
131 (display "Usage: extract [-en|-jp][-o outfile] infile\n")
132 (exit 1))
133
134 (define (main args)
135 (let ((a (parse-options (cdr args)
136 (("o=s" (outfile) (set! *outfile* outfile))
137 ("en" () (set! *lang* 'en))
138 ("jp" () (set! *lang* 'jp))
139 (else _ (usage))))))
140
141 (define (do-it)
142 (case *lang*
143 ((en) (filter #/^@c EN$/ #/^@c JP$/))
144 ((jp) (filter #/^@c JP$/ #/^@c EN$/))))
145
146 (unless (= (length a) 1) (usage))
147
148 (when (eq? *lang* 'jp)
149 (with-input-from-file (car a) scan-nodes :encoding 'utf-8))
150
151 (with-input-from-file (car a)
152 (lambda ()
153 (if *outfile*
154 (with-output-to-file *outfile* do-it :encoding 'utf-8)
155 (let1 out (open-output-conversion-port
156 (current-output-port) 'utf-8)
157 (with-output-to-port out do-it)
158 (close-output-port out))))
159 :encoding 'utf-8)
160 0))
161
162 ;; Local variables:
163 ;; mode: Scheme
164 ;; end: