Change soft-fail to use the config, rather than env
[rbx.git] / lib / rdoc / ri / writer.rb
blob92aaa1c2da966f9af521208260c4851a68694769
1 require 'fileutils'
2 require 'rdoc/ri'
4 class RDoc::RI::Writer
6   def self.class_desc_path(dir, class_desc)
7     File.join(dir, "cdesc-" + class_desc.name + ".yaml")
8   end
10   ##
11   # Convert a name from internal form (containing punctuation) to an external
12   # form (where punctuation is replaced by %xx)
14   def self.internal_to_external(name)
15     if ''.respond_to? :ord then
16       name.gsub(/\W/) { "%%%02x" % $&[0].ord }
17     else
18       name.gsub(/\W/) { "%%%02x" % $&[0] }
19     end
20   end
22   ##
23   # And the reverse operation
25   def self.external_to_internal(name)
26     name.gsub(/%([0-9a-f]{2,2})/) { $1.to_i(16).chr }
27   end
29   def initialize(base_dir)
30     @base_dir = base_dir
31   end
33   def remove_class(class_desc)
34     FileUtils.rm_rf(path_to_dir(class_desc.full_name))
35   end
37   def add_class(class_desc)
38     dir = path_to_dir(class_desc.full_name)
39     FileUtils.mkdir_p(dir)
40     class_file_name = self.class.class_desc_path(dir, class_desc)
41     File.open(class_file_name, "w") do |f|
42       f.write(class_desc.serialize)
43     end
44   end
46   def add_method(class_desc, method_desc)
47     dir = path_to_dir(class_desc.full_name)
48     file_name = self.class.internal_to_external(method_desc.name)
49     meth_file_name = File.join(dir, file_name)
50     if method_desc.is_singleton
51       meth_file_name += "-c.yaml"
52     else
53       meth_file_name += "-i.yaml"
54     end
56     File.open(meth_file_name, "w") do |f|
57       f.write(method_desc.serialize)
58     end
59   end
61   private
63   def path_to_dir(class_name)
64     File.join(@base_dir, *class_name.split('::'))
65   end
67 end