Generates nicely-formatted ruby source code given a ruby abstract syntax tree (from seattlerb's ruby_parser).
{}[http://travis-ci.org/rubiety/ruby_scribe]
= Ruby Scribe
Generates nicely-formatted ruby source code given a ruby abstract syntax tree (from seattlerb’s ruby_parser).
== Introduction
Ruby Scribe attempts to intelligently format code (from an AST) much as a real developer would, through a series of configurable option.
== Approach
To approach creating a solid pretty-printer, I used the Rails codebase as “ideal” coding style for the emitter with default parameters. As I continued to refine ruby_scribe,
I ran it (with no AST transformations) against every ruby file in the Rails codebase. The smaller the remaining git diff after each iteration, the better the emitter was.
There are even some instances I think ruby_scribe emits more consistent code than what currently exists in the Rails codebase.
== Example
Imagine this crappily-formatted Ruby code:
module RubyScribe
# My Comment
class Sample < Base;
def method; do_something_here; end
end
Parse that with RubyParser:
sexp = RubyParser.new.parse(File.read(“bad_code.rb”))
Then emit it with Ruby Scribe:
RubyScribe::Emitter.new.emit(sexp)
And out pops this, nice and clean:
module RubyScribe
# My Comment
class Sample < Base
def method
do_something_here
end
end
end
== Usage
The entire project simply takes an incoming Sexp object (from the ruby_parser project) and emits a single string. To do this just use an Emitter:
RubyScribe::Emitter.new.emit(sexp) # => “module Something…”
== Emitter Implementation
The +Emitter+ class is nothing but a bunch of recursion. The main emit method is a big case block to offload handling of individual types to separate methods which handle and compose a big string, all through recursion.
To extend or implement your own Emitter, just subclass +RubyScribe::Emitter+ and override the necessary methods.
== Known Issues
== Future Features