Created
May 11, 2009 21:12
-
-
Save quirkey/110191 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# jsdoc.rb (by quirkey/Aaron Quint) | |
# | |
# Simple Documentation generator for JavaScript Class files. | |
# | |
# usage : | |
# ruby jsdoc.rb FILE | |
# | |
# looks for files formatted like | |
# | |
# MyClass = Class.extend({ | |
# ... | |
# // My method does this | |
# // with multi line comments | |
# myMethod: function(arg1, arg2) { | |
# ... | |
# }, | |
# | |
# _noDoc: function() { | |
# ... | |
# } | |
# | |
# }); | |
require 'rubygems' | |
require 'haml' | |
require 'rdoc/markup/to_html' | |
file_path = ARGV.shift | |
file = File.read(file_path) | |
klass_regex = /(.*)\s+=\s+([\w\d\.]+)\.extend\(/i | |
function_regexp = /(\/\/(.*)|(([\w\d_\$]+)\:\s*function\s*\(([\w\d\s,]+)?\))|(function\s+([\w\d_\$]+)\(([\w\d\s,]+)?\)))/im | |
klass = {:klass => 'Top Level', :parent => 'Object'} | |
context = nil | |
current = nil | |
comment = "" | |
doc_methods = {} | |
file.each do |line| | |
if klass_match = line.match(klass_regex) | |
klass = { | |
:klass => klass_match[1].to_s.strip, | |
:parent => klass_match[2].to_s.strip, | |
:doc => "" | |
} | |
if context == :comment | |
klass[:doc] = comment | |
comment = "" | |
end | |
else | |
line_match = line.match(function_regexp).to_a | |
if !line_match.empty? | |
current = ((line_match[0] =~ /^\/\//) ? :comment : :method) | |
if current == :comment | |
this_comment = line_match[2].to_s.strip | |
if context == :comment | |
comment << "\n" << this_comment | |
else | |
comment = this_comment | |
end | |
elsif current == :method | |
meth = { | |
:klass => klass, | |
:name => line_match[4].to_s, | |
:args => line_match[5].to_s.split(',').collect {|a| a.strip } | |
} | |
if context == :comment | |
meth[:doc] = comment | |
comment = "" | |
end | |
if !meth[:doc].nil? && meth[:doc].strip != '' | |
doc_methods[klass] ||= [] | |
doc_methods[klass] << meth | |
end | |
end | |
else | |
current = nil | |
end | |
context = current | |
end | |
end | |
template = <<-EOT | |
- doc.each do |klass, meths| | |
%h2.klass | |
=klass[:klass] | |
- if klass[:parent] | |
< | |
=klass[:parent] | |
=convert(klass[:doc] || "") | |
-meths.each do |meth| | |
%div.meth | |
%h3 | |
=meth[:name] | |
- if !meth[:args].empty? | |
%em | |
( | |
=meth[:args].join(', ') | |
) | |
- else | |
() | |
%p=meth[:doc] | |
EOT | |
rdoc = RDoc::Markup::ToHtml.new | |
puts Haml::Engine.new(template).to_html(rdoc, {:doc => doc_methods}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment