Register now and start sharing your code snippets.
-->

How to parse Ruby source code documentation with RDoc and a custom RDoc generator

Ruby posted about 1 year ago by christian

This is a skeleton for an RDoc generator that extends the existing HtmlGenerator. This means we get the same documentation as seen at, for example, http://api.rubyonrails.org/; with links and HTML formatted documentation.

It can be used for doing whatever you would like and can imagine doing with RDoc documentation. Currently it prints out the files, modules, classes and methods found in the processesed files.

To use it, create a new file named custom_generator.rb in the Ruby installation and the subfolder /rdoc/generators. Then put the following code in the file:

   1  require 'rdoc/generators/html_generator'
   2  
   3  module Generators
   4  
   5    class HTMLGenerator
   6    
   7      # We don't need a template
   8      def load_html_template
   9      end
  10  
  11      def generate(toplevels)             
  12        @toplevels  = toplevels
  13        @files      = []
  14        @classes    = []
  15  
  16        build_indices
  17        
  18        puts "===================="
  19        puts "Files"
  20        puts "===================="
  21        
  22        @files.each do |item|
  23          puts item.name
  24          #values = file.value_hash
  25          #puts item.description
  26        end
  27        
  28        puts "===================="
  29        puts "Modules and classes"
  30        puts "===================="           
  31        
  32        @classes.each do |item|
  33          puts item.name
  34          #values = file.value_hash
  35          #puts item.description
  36        end
  37        
  38        puts "===================="
  39        puts "Methods"
  40        puts "===================="      
  41        
  42        HtmlMethod.all_methods.each do |item|
  43          puts item.name
  44        end
  45      end
  46    end
  47    
  48    class HtmlFile
  49      # Add a description method, after all HtmlMethod has it...
  50      def description
  51        value_hash if @values.size == 0      
  52        @values["description"]
  53      end
  54    end
  55    class HtmlClass
  56      # Add a description method, after all HtmlMethod has it...
  57      def description
  58        value_hash if @values.size == 0      
  59        @values["description"]
  60      end
  61    end
  62    
  63    class CUSTOMGenerator < HTMLGenerator
  64    end
  65  
  66  end

Then run the custom generator by using the fmt parameter:

   1  rdoc --fmt custom lib/base64.rb lib/pp.rb

You can also control RDoc programatically, with the following code:

   1  #!/usr/bin/env ruby
   2  require 'rdoc/rdoc'
   3  
   4  `rm -rf doc`
   5  
   6  begin
   7    r = RDoc::RDoc.new
   8    r.document(['--inline-source', '--fmt', 'custom'] + ARGV)
   9  rescue RDoc::RDocError => e
  10    $stderr.puts e.message
  11    exit(1)
  12  end

Tagged ruby, rdoc, generator, documentation