File: rd2

package info (click to toggle)
rdtool 0.6.22-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 724 kB
  • ctags: 1,162
  • sloc: ruby: 8,017; lisp: 347; makefile: 55; sh: 30
file content (279 lines) | stat: -rwxr-xr-x 6,016 bytes parent folder | download
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#! /usr/bin/ruby1.8 -Kn
# -*- coding: ASCII-8BIT -*-
=begin
= NAME
rd2 - converter from RD to other mark-up language.
= SYNOPSIS
  rd2 [-r <visitor>] [options] <file>

= DESCRIPTION
rd2 inputs from ((|<file>|)) and outputs into (({STDOUT})). you can
choose ((|<visitor>|)) to select output format. For example, use
"rd/rd2html-lib.rb" to translate it into HTML, and "rd/rd2man-lib.rb"
to tranlate it into roff with man macros.

= OPTIONS
please read output of 
  % rd2 --help
and
  % rd2 -r rd/rd2html-lib.rb --help

= FILES
  * ~/.rd2rc - User configration file.

= SEE ALSO
ruby(1)
=end

require "kconv"
require "optparse"
require "rd/rdfmt"
require "rd/visitor"
require "rd/version"

module Kconv
  NAME2CONST = {
    "iso-2022-jp" => Kconv::JIS,
    "euc-jp" => Kconv::EUC,
    "shift_jis" => Kconv::SJIS,
  }
  NAME_ALIAS = {
    'jis' => 'iso-2022-jp',
    'euc' => 'euc-jp',
    'sjis' => 'shift_jis',
    'shift-jis' => 'shift_jis',
  }
  if defined?(Kconv::UTF8)
    NAME2CONST['utf-8'] = Kconv::UTF8
    NAME_ALIAS['utf8'] = 'utf-8'
  end
end

include RD

SYSTEM_NAME = "RDtool -- rd2"
SYSTEM_VERSION = "$Version: 0.6.21$" #"
RD2_VERSION = Version.new_from_version_string(SYSTEM_NAME, SYSTEM_VERSION)

# global vars
$Visitor_Class = nil
$Visitor = nil
$RD2_Sub_OptionParser = nil

# local vars
include_path = []
with_part = []
output_file = nil
output_index = nil
out_code = nil
from_rdo = nil

# user option
$DEFAULT_FORMAT_LIB = "rd/rd2html-lib"
$RC = {}
$RC["filter"] = Hash.new(RD::INCLUDE_FILTER)
begin
  if test(?r, File.expand_path("~/.rd2rc"))
    load "~/.rd2rc"
    #  STDERR << "#{$0}: loading ~/.rd2rc\n"
  else
    load "rd/dot.rd2rc"
    #  STDERR << "#{$0}: loading rd/dot.rd2rc\n"
  end
rescue
  load "rd/dot.rd2rc"
end

# initialize OptionParser
ARGV.options do |q|
  q.banner = "Usage: #{$0} [options] rd-file > output\n"
  q.on_head("global options:")
  
  q.on("-rLIB", "--require=LIB",
       String,
       "choose format library.") do |i|
    # require LIB
    require i
    if $Visitor_Class && !$Visitor
      $Visitor = $Visitor_Class.new()
      if $RD2_Sub_OptionParser    
	require $RD2_Sub_OptionParser
	$RD2_Sub_OptionParser = nil
      end
    end
  end

  q.on("-oNAME",
       String,
       "indicate base name of output file") do |i|
    output_file = i
  end

  q.on("--out-code=KCODE",
       Kconv::NAME2CONST.keys, Kconv::NAME_ALIAS,
       "character encoding of output.(jis|euc|sjis|utf8)") do |i|
    out_code = i
  end
      
  q.on("--output-index",
       "output method index file (*.rmi)") do |i|
    output_index = true
  end

  q.on("-IPATH", "--include-path=PATH",
       String,
       "add PATH to list of include path") do |i|
    # add to include path
    include_path.unshift(i)
  end

  # accept "PART:FILTER" and "PART"
  q.on("--with-part=PART",
       /(\w+)(?:\s*:\s*(\w+))?/,
       "include PART with Filter") do |src, part, filter|
    with_part.push([part, filter || part])
    unless include_path.index(RD::RDTree.tmp_dir)
      include_path.push(RD::RDTree.tmp_dir)
    end
  end

  q.on("--from-rdo",
       "load from *.rdo instead of *.rd") do
    from_rdo = true
  end

  q.on("--version",
       "print versions.") do
    STDERR.puts RD2_VERSION
    STDERR.puts Tree::version
    STDERR.puts Visitor::version
    STDERR.puts $Visitor_Class.version if $Visitor_Class
    exit(0)
  end

  q.on_tail("--help",
	    "print this message") do
    STDERR.print(q.to_s)
    exit(0)
  end
end # OptionParser.new

# require format lib implicitly
if /rd2.+/ =~ File.basename($0, ".*").downcase
  visitor_lib = "rd/" + $& + "-lib.rb"
  require visitor_lib
  require $RD2_Sub_OptionParser  if $RD2_Sub_OptionParser
  # make visitor
  $Visitor = $Visitor_Class.new()
end

begin
  ARGV.parse!
rescue
  STDERR.print("Error: " + $!.inspect + "\n")
  STDERR.print(ARGV.options.to_s)
  exit(1)
end

unless $Visitor_Class
  require $DEFAULT_FORMAT_LIB
  $Visitor = $Visitor_Class.new
end

# make tree (but not parsed yet)
if from_rdo
  rdos = []
  ARGV.each do |path|
    rdos.push(File.open(path))
    dirname = File.dirname(path)
    include_path.push(dirname, dirname + "/include")
  end
  tree = RDTree.new_from_rdo(*rdos)
  tree.include_path = include_path
else 
  # input from ARGF
  src = readlines
  if src.find{|i| /\S/ === i } and !src.find{|i| /^=begin\b/ === i }
    src.unshift("=begin\n").push("=end\n")
  end

  # set Include_Path
  if ARGF.filename
    dir = File.dirname(ARGF.filename)
  else
    dir = "."
  end
  include_path.push(dir)
  include_path.push(dir + "/include")
  
  tree = RDTree.new(src, include_path, nil)
  
  # filter set tree.filter
  with_part.each do |i|
    tree.filter[i[0]] = $RC["filter"][i[1]]
  end
  
  # parse
  begin
    tree.parse
  rescue Racc::ParseError
    STDERR.puts($!.message)
    exit(10)
  end
end

# filter: set visitor.include_suffix
with_part.each do |i|
  $Visitor.include_suffix.push(i[0])
end

# file base name setup
$Visitor.filename = output_file if output_file

# character encoding
if out_code
  begin
    $Visitor.charcode = out_code
    $Visitor.lang = "ja"
  rescue NameError
  end
end

# output  
out = $Visitor.visit(tree)

# character encoding convert
out = Kconv.kconv(out, Kconv::NAME2CONST[out_code], Kconv::AUTO) if out_code

if output_file
  filename = output_file + "." + $Visitor.class::OUTPUT_SUFFIX
  file = open(filename, "w")
  file.print(out)
  file.close
  STDERR.print("#{$0}: output to #{filename}...\n")
else
  print(out)
end

# RMI
if output_index
  if output_file
    require "rd/rd2rmi-lib"
    rmivisitor = RD2RMIVisitor.new
    rmivisitor.filename = output_file
    rmi = rmivisitor.visit(tree)
    filename = output_file + ".rmi"
    file = open(filename, "w")
    file.print(rmi)
    file.close
    STDERR.print("#{$0}: output to #{filename}...\n")
  else
    raise %Q[Error: option "--output-index" must be used with option "-oNAME"]
  end
end


# filter: remove tmp file
Dir.glob("#{RD::RDTree.tmp_dir}/rdtmp.#{$$}.*.*").each do |i|
  File.delete(i)
end