Created
July 23, 2008 16:41
-
-
Save adamsanderson/1810 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
# From PragDave: | |
# http://pragdave.blogs.pragprog.com/pragdave/2008/04/babydoc.html | |
require 'ripper' | |
# This class handles parser events, extracting | |
# comments and attaching them to class definitions | |
class BabyRDoc < Ripper::Filter | |
def initialize(*) | |
super | |
reset_state | |
end | |
def on_default(event, token, output) | |
reset_state | |
output | |
end | |
def on_sp(token, output) output end | |
alias on_nil on_sp | |
def on_comment(comment, output) | |
@comment << comment.sub(/^\s*#\s*/, " ") | |
output | |
end | |
def on_kw(name, output) | |
@expecting_class_name = (name == 'class') | |
output | |
end | |
def on_const(name, output) | |
if @expecting_class_name | |
output << "#{name}:\n" | |
output << @comment | |
end | |
reset_state | |
output | |
end | |
private | |
def reset_state | |
@comment = "" | |
@expecting_class_name = false | |
end | |
end | |
BabyRDoc.new(File.read(__FILE__)).parse(STDOUT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment