File: extract

package info (click to toggle)
gauche-c-wrapper 0.6.1-18
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,440 kB
  • sloc: ansic: 17,899; sh: 14,025; asm: 6,456; lisp: 5,485; yacc: 2,109; makefile: 520; exp: 194; cpp: 157; objc: 144; perl: 2
file content (161 lines) | stat: -rwxr-xr-x 5,352 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
;;;
;;; extract - filter bilingual texinfo document
;;;
;;;  Copyright(C) 2001-2003 by Shiro Kawai (shiro@acm.org)
;;;
;;;  Permission to use, copy, modify, distribute this software and
;;;  accompanying documentation for any purpose is hereby granted,
;;;  provided that existing copyright notices are retained in all
;;;  copies and that this notice is included verbatim in all
;;;  distributions.
;;;  This software is provided as is, without express or implied
;;;  warranty.  In no circumstances the author(s) shall be liable
;;;  for any damages arising out of the use of this software.
;;;
;;;  $Id: extract,v 1.16 2006/04/07 11:29:48 shirok Exp $
;;;

(use gauche.regexp)
(use gauche.parseopt)
(use gauche.charconv)
(use srfi-13)

(define *outfile* #f)
(define *lang* 'en)
(define *version*
  (string-trim-both (call-with-input-file "../VERSION" port->string)))
(define *node-table* '())
(define *header-table* '())

(define (scan-nodes)
  (let ((current-node #f)
        (current-header #f))
    (port-for-each
     (lambda (line)
       (rxmatch-case line
         (#/^@node\s+([^,]+)/ (#f node)
          (set! current-node (string-trim-right node)))
         (#/^@(chapter|(sub)*section|appendix\w*)\s+(.*)/ (#f #f #f header)
          (set! current-header (string-trim-right header)))
         (#/^@c NODE\s+([^,]*)(,(.*))?/ (#f jnode #f jheader)
          (let* ((jn (string-trim-right jnode))
                 (jh (if jheader (string-trim-both jheader) jn)))
            (push! *node-table* (cons current-node jn))
            (push! *header-table* (cons current-header jh))))
         (#/^@include\s+(\S+)/ (#f file)
           (with-input-from-file file (cut scan-nodes) :encoding 'utf8))
         ))
     read-line)))

(define (filter pattern-in pattern-out)
  (define (in line)
    (rxmatch-case line
      (test eof-object?)
      (pattern-in () (in (read-line)))
      (pattern-out () (out (read-line)))
      (#/^@include\s+(\S+)/ (#f file)
        (with-input-from-file file (cut filter pattern-in pattern-out)
                              :encoding 'utf8)
        (in (read-line)))
      (#/^@c COMMON$/ () (in (read-line)))
      (test (lambda _ (eq? *lang* 'en))
            (display (regexp-replace-all #/@VERSION@/ line *version*))
            (newline) (in (read-line)))
      (#/^@node\s+(.*)$/ (#f nodedesc)
        (process-node nodedesc) (in (read-line)))
      (#/^@(chapter|(sub)*section|appendix\w*)\s+(.*)/ (#f cmd #f header)
        (process-header cmd header) (in (read-line)))
      (#/^\* ([^:]+)::(.*)?/ (#f node desc)
        (process-menu node #f desc) (in (read-line)))
      (#/^\* ([^:]+):\s+([^)]+\))\.(.*)?/ (#f tag node desc)
        (process-menu node tag desc) (in (read-line)))
      (else (display
             (regexp-replace-all #/@VERSION@/
              (regexp-replace-all #/(@x?ref)\{([^\}]+)\}/ line process-ref)
              *version*))
            (newline)
            (in (read-line)))))

  (define (out line)
    (rxmatch-case line
      (test eof-object?)
      (pattern-in ()  (in (read-line)))
      (#/^@c COMMON$/ () (in (read-line)))
      (else (out (read-line)))))

  (in (read-line)))

(define (process-node nodedesc)
  (display "@node ")
  (display
   (string-join
    (map (lambda (name)
           (cond ((assoc (string-trim-both name) *node-table*) => cdr)
                 (else name)))
         (string-split nodedesc #\,))
    ", "))
  (newline))

(define (process-header cmd header)
  (format #t "@~a ~a\n"
          cmd
          (cond ((assoc (string-trim-both header) *header-table*) => cdr)
                (else header))))

(define (process-menu node tag desc)
  (if tag
    (format #t "* ~a: ~a.  ~a\n"
            tag
            (cond ((assoc (string-trim-both node) *node-table*) => cdr)
                  (else node))
            (string-trim-both (or desc "")))
    (format #t "* ~a::  ~a\n"
            (cond ((assoc (string-trim-both node) *node-table*) => cdr)
                  (else node))
            (string-trim-both (or desc "")))))

(define (process-ref match)
  (let ((cmd  (rxmatch-substring match 1))
        (node (rxmatch-substring match 2)))
    (format #f "~a{~a}"
            cmd
            (cond ((assoc (string-trim-both node) *node-table*) => cdr)
                  (else node)))))

(define (usage)
  (display "Usage: extract [-en|-jp][-o outfile] infile\n")
  (exit 1))

(define (main args)
  (let ((a (parse-options (cdr args)
             (("o=s" (outfile) (set! *outfile* outfile))
              ("en"  () (set! *lang* 'en))
              ("jp"  () (set! *lang* 'jp))
              (else _ (usage))))))

    (define (do-it)
      (case *lang*
        ((en) (filter #/^@c EN$/ #/^@c JP$/))
        ((jp) (filter #/^@c JP$/ #/^@c EN$/))))

    (define outenc (if (eq? *lang* 'jp) 'utf8 'utf8))
    
    (unless (= (length a) 1) (usage))

    (when (eq? *lang* 'jp)
      (with-input-from-file (car a) scan-nodes :encoding 'utf8))
    
    (with-input-from-file (car a)
      (lambda ()
        (if *outfile*
          (with-output-to-file *outfile* do-it :encoding outenc)
          (let1 out (open-output-conversion-port
                     (current-output-port) outenc)
            (with-output-to-port out do-it)
            (close-output-port out))))
      :encoding 'utf8)
    0))

;; Local variables:
;; mode: Scheme
;; end: