1

After searching the RDoc documentation I can't find how to document a file / a top-level method in RDoc...

Suppose I have the following code :

## 
# File documentation.
# File:: foo.rb
# Date:: 09/05/2018
##
require 'stuff'

##
# Class documentation
class Foo
     # Stuff
end

##
# Method documentation
def foo_method()
    # Stuff too
end

Running RDoc with this code will produce documentation for class Foo only, where I would like to have documentation for the file foo.rb and for top-level method foo_method().

So my question is : How can I make RDoc documenting files and top-level method ?

Astariul
  • 2,190
  • 4
  • 24
  • 41

1 Answers1

0

Still couldn't find how to do that.

As a workaround, I made a Ruby script that read my others Ruby files (containing the documentation), parse it, and create some mockup module where I insert the parsed documentation.

Here is the script if someone is ever interested in :

#:stopdoc:
FILES = ['features/step_definitions/*.rb', 'features/support/*.rb', 'lib/ffi/*.rb', 'lib/*.rb']

FILE_NAME = 'docs/files.rb'
TOP_LEVEL_NAME = 'docs/top_level.rb'

def gen_files(content)
    match = content.scan(/(##[^`]+File::[ ]*([\w]+)[^`]+?##)/)
    File.open(FILE_NAME, 'a') do |f|
        f << "\n"
        f << match[0][0]
        f << "\n"
        f << "module #{match[0][1].capitalize}\n\nend\n"
    end
end

def gen_top_level(content)
    match = content.scan(/(##[\t ]*[\r]*\n(#[^\r\n]*[\r\n\t ]*)*##[\t ]*[\r]*\n){1}([^\r\n]+)/)
    File.open(TOP_LEVEL_NAME, 'a') do |f|
        match[1 .. -1].each do |m|      # Skip the file description
            f << "\n"
            next if (m[2].include?('module') || m[2].include?('class'))
            f << m[0]
            name = nil
            unless m[2].scan(/def [^`]+/).empty?
                name = m[2]
                name = "#{name}\n\nend"
            end
            name = m[2] unless m[2].scan(/[A-Z_]+[ ]?=[ ]?[^`]+/).empty?
            if name.nil?
                name = m[2].gsub(/do([^`]*)/, '').gsub(/[^\d|\w]/, '_').gsub(/_+/, '_').gsub(/_$/, '')
                name = "def #{name}\n\nend"
            end
            f << name
            f << "\n"
        end
    end
end

File.write(FILE_NAME, "module Files #:nodoc:\n\n\n")
File.write(TOP_LEVEL_NAME, "module TopLevel\n\n\n")

FILES.each do |file_regex|
    Dir.glob(file_regex).each do |rb_f|
        gen_files(File.read(rb_f))
        gen_top_level(File.read(rb_f))
    end
end

File.open(FILE_NAME, 'a') do |f|
    f << "\n\n\nend"
end
File.open(TOP_LEVEL_NAME, 'a') do |f|
    f << "\n\n\nend"
end
#:startdoc:

In this script, I produce 2 files : one containing the Files documentation, one containing the top-level documentation. For top-level documentation, this script handle constants, method definitions, and Cucumber steps (Gherkin).

I'm still a bit stunned that such a tool as RDoc can't specify an option or something to parse documentation of top-level function..

I will not accept my answer, as it is just a not clean workaround

Astariul
  • 2,190
  • 4
  • 24
  • 41