Added v 0.2.3 snapshot.
[twitter4r-core.git] / config / rcov_morpher.rb
blobc1b7f4e453ee3d3501b51b52e24960bb6a40f3c6
1 module RCov; end
3 require('erb')
4 require('tidy')
5 require('hpricot')
7 require(File.join(File.dirname(__FILE__), '..', 'lib', 'twitter'))
9 class File #:nodoc:
10   class << self
11     # Reads in local file from current directory.
12     def read_local(file)
13       self.read(self.join(self.dirname(__FILE__), file))
14     end
15   end
16 end
18 # Follows <tt>Strategy</tt> design pattern
19 class RCov::OutputMorpher
20   # transforms given list of <tt>files</tt> based on new template.
21   def transform(files, output_dir = '../web/rcov/', template = 'templates/rcov.rhtml')
22     files.each do |file|
23       data = File.read(file)
24       fname = File.basename(file)
25       xml = Tidy.open(:show_warnings => true) do |tidy|
26         tidy.options.output_xml = true
27         tidy.clean(data)
28       end
29       
30       rhtml = ERB.new(File.read_local(template), 0)
31       parser = Hpricot.parse(xml)
32       legend_content = parser.find_element('pre')
33       table_content = parser.find_element('table')
34       code_content = table_content.next_sibling
35       index = (fname == 'index.html')
36       version = Twitter::Version.to_version
37       File.delete(fname) if File.exists?(fname)
38       File.open("#{output_dir}/#{fname}", 'w') do |f|
39         f.puts(rhtml.result(binding))
40       end
41     end
42   end
43 end
45 module RCov
46   class << self
47     # returns html files to morph based on tidy configuration file
48     def configure_morpher(tidy_conf = 'tidy.yml')
49       overrides = YAML.load(File.read_local('tidy.yml'))
50       Tidy.path = overrides['path'] || '/usr/lib/libtidy-0.99.so.0'
51       Dir.glob(overrides['html_glob'] || 'doc/rcov/**/*.html')      
52     end
53   end
54 end
56 if __FILE__ == $0
57   RCov::OutputMorpher.new.transform(RCov.configure_morpher)
58 end