Permalink
Browse files

Update yard-spec-plugin for new yard release

  • Loading branch information...
lsegal committed Nov 2, 2009
1 parent 675f1fb commit ea96363d01825df152f98d891a073ea6e58e2282
View
@@ -0,0 +1,3 @@
+.DS_Store
+.yardoc
+
View
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2007-2009 Loren Segal
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,30 @@
+= Embedding RSpec Specifications in YARD Documentation
+
+This plugin demonstrates how RSpec tests can be embedded within standard documentation
+using only a small amount of plugin code. The example generates documentation
+for the following {String#pig_latin} method and RSpec tests:
+
+ # Run `yardoc -e ../lib/yard-spec example_code.rb`
+
+ class String
+ # Pig latin of a String
+ def pig_latin
+ self[1..-1] + self[0] + "ay"
+ end
+ end
+
+ #
+ # Specs
+ #
+ describe String, '#pig_latin' do
+ it "should be a pig!" do
+ "hello".pig_latin.should == "ellohay"
+ end
+
+ it "should fail to be a pig!" do
+ "hello".pig_latin.should == "hello"
+ end
+ end
+
+View the "Specifications" section within the {String#pig_latin} method to see
+these tests.
View
@@ -1,3 +1,25 @@
+require 'rubygems'
+require 'rake/gempackagetask'
+require 'spec'
+require 'spec/rake/spectask'
require 'yard'
-YARD::Rake::YardocTask.new
+WINDOWS = (PLATFORM =~ /win32|cygwin/ ? true : false) rescue false
+SUDO = WINDOWS ? '' : 'sudo'
+
+task :default => :install
+
+load 'yard-rspec.gemspec'
+Rake::GemPackageTask.new(SPEC) do |pkg|
+ pkg.gem_spec = SPEC
+ pkg.need_zip = true
+ pkg.need_tar = true
+end
+
+desc "Install the gem locally"
+task :install => :package do
+ sh "#{SUDO} gem install pkg/#{SPEC.name}-#{SPEC.version}.gem --local --no-rdoc --no-ri"
+ sh "rm -rf pkg/yard-#{SPEC.version}" unless ENV['KEEP_FILES']
+end
+
+YARD::Rake::YardocTask.new
File renamed without changes.
View
@@ -11,12 +11,14 @@ def pig_latin
#
# Specs
#
-describe String, '#pig_latin' do
- it "should be a pig!" do
- "hello".pig_latin.should == "ellohay"
- end
+describe String do
+ describe '#pig_latin' do
+ it "should be a pig!" do
+ "hello".pig_latin.should == "ellohay"
+ end
- it "should fail to be a pig!" do
- "hello".pig_latin.should == "hello"
+ it "should fail to be a pig!" do
+ "hello".pig_latin.should == "hello"
+ end
end
end
View
@@ -0,0 +1,3 @@
+YARD::Templates::Engine.register_template_path File.dirname(__FILE__) + '/../templates'
+require File.join(File.dirname(__FILE__), 'yard-spec', 'handler') if RUBY19
+require File.join(File.dirname(__FILE__), 'yard-spec', 'legacy')
View
@@ -0,0 +1,32 @@
+class RSpecDescribeHandler < YARD::Handlers::Ruby::Base
+ handles method_call(:describe)
+
+ def process
+ objname = statement.parameters.first.jump(:string_content).source
+ if statement.parameters[1]
+ src = statement.parameters[1].jump(:string_content).source
+ objname += (src[0] == "#" ? "" : "::") + src
+ end
+ obj = {:spec => owner ? (owner[:spec] || "") : ""}
+ obj[:spec] += objname
+ parse_block(statement.last.last, owner: obj)
+ rescue YARD::Handlers::NamespaceMissingError
+ end
+end
+
+class RSpecItHandler < YARD::Handlers::Ruby::Base
+ handles method_call(:it)
+
+ def process
+ return if owner.nil?
+ obj = P(owner[:spec])
+ return if obj.is_a?(Proxy)
+
+ (obj[:specifications] ||= []) << {
+ name: statement.parameters.first.jump(:string_content).source,
+ file: statement.file,
+ line: statement.line,
+ source: statement.last.last.source.chomp
+ }
+ end
+end
View
@@ -0,0 +1,31 @@
+class LegacyRSpecDescribeHandler < YARD::Handlers::Ruby::Legacy::Base
+ MATCH = /\Adescribe\s+(.+?)\s+(do|\{)/
+ handles MATCH
+
+ def process
+ objname = statement.tokens.to_s[MATCH, 1].gsub(/["']/, '')
+ obj = {:spec => owner ? (owner[:spec] || "") : ""}
+ obj[:spec] += objname
+ parse_block :owner => obj
+ rescue YARD::Handlers::NamespaceMissingError
+ end
+end
+
+class LegacyRSpecItHandler < YARD::Handlers::Ruby::Legacy::Base
+ MATCH = /\Ait\s+['"](.+?)['"]\s+(do|\{)/
+ handles MATCH
+
+ def process
+ return if owner.nil?
+ obj = P(owner[:spec])
+ return if obj.is_a?(Proxy)
+
+ (obj[:specifications] ||= []) << {
+ :name => statement.tokens.to_s[MATCH, 1],
+ :file => parser.file,
+ :line => statement.line,
+ :source => statement.block.to_s
+ }
+ end
+end
+
View
@@ -1,53 +0,0 @@
-require 'yard'
-
-class RSpecDescribeHandler < YARD::Handlers::Ruby::Base
- handles method_call(:describe)
-
- def process
- name = statement.parameters.first.source
- if statement.parameters[1]
- src = statement.parameters[1].jump(:string_content).source
- name += (src[0] == "#" ? "" : "::") + src
- end
- object = P(name)
- ensure_loaded! object
- object[:specifications] ||= []
- parse_block(statement.last.last, owner: object)
- rescue YARD::Handlers::NamespaceMissingError
- end
-end
-
-class RSpecItHandler < YARD::Handlers::Ruby::Base
- handles method_call(:it)
-
- def process
- owner[:specifications] << {}.tap do |o|
- source = statement.source.chomp
- indent = source.split(/\r?\n/).last[/^([ \t]*)/, 1].length
- o[:source] = source.gsub(/^[ \t]{#{indent}}/, '')
- o[:name] = statement.parameters.first.jump(:string_content).source
- o[:file] = statement.file
- o[:line] = statement.line
- end
- end
-end
-
-module YARD
- module Generators
- class MethodGenerator
- before_section :specs, :has_specs?
-
- def sections_for(object)
- [:header, [:title, [G(MethodSignatureGenerator), :aliases],
- G(DeprecatedGenerator), G(DocstringGenerator), :specs,
- G(TagsGenerator), G(SourceGenerator)]]
- end
-
- def has_specs?(object)
- object[:specifications] && object[:specifications].size > 0 ? true : false
- end
- end
- end
-end
-
-YARD::Generators::Base.register_template_path File.dirname(__FILE__) + '/../templates/'
@@ -1,28 +0,0 @@
-<div class="section specs">
- <h3>Specifications</h3>
- <ul>
- <% for spec in object[:specifications] %>
- <li>
- <%= spec[:name] %>
- <span>(<a class="source_link" href="#">View source</a>)</span>
- <div class="source_code">
- <table>
- <tr>
- <td>
- <pre class="lines">
-
-
-<%= h format_lines(object) %></pre>
- </td>
- <td>
- <pre class="code"><span class="info file"># File '<%= h spec[:file] %>', line <%= spec[:line] %></span>
-
-<%= html_syntax_highlight spec[:source] %></pre>
- </td>
- </tr>
- </table>
- </div>
- </li>
- <% end %>
- </ul>
-</div>
@@ -0,0 +1,28 @@
+<% if object[:specifications] %>
+<div class="tags">
+ <h3>Specifications:</h3>
+ <ul class="specs">
+ <% for spec in object[:specifications] %>
+ <li><%= spec[:name] %>
+ <div class="source_code">
+ <table>
+ <tr>
+ <td>
+ <pre class="lines">
+
+
+<%= spec[:source].split("\n").size.times.to_a.map {|i| spec[:line] + i }.join("\n") %></pre>
+ </td>
+ <td>
+ <pre class="code"><span class="info file"># File '<%= h spec[:file] %>', line <%= spec[:line] %></span>
+
+<%= html_syntax_highlight format_source(spec[:source]) %></pre>
+ </td>
+ </tr>
+ </table>
+ </div>
+ </li>
+ <% end %>
+ </ul>
+</div>
+<% end %>
@@ -0,0 +1,4 @@
+def init
+ super
+ sections.last.place(:specs).before(:source)
+end
@@ -0,0 +1,9 @@
+<% if object[:specifications] %>
+
+Specifications:
+---------------
+
+<% for spec in object[:specifications] %>
+<%= indent wrap("- " + spec[:name]) %>
+<% end %>
+<% end %>
View
@@ -0,0 +1,15 @@
+SPEC = Gem::Specification.new do |s|
+ s.name = "yard-rspec"
+ s.summary = "YARD plugin to list RSpec specifications inside documentation"
+ s.version = "0.1"
+ s.date = "2009-09-15"
+ s.author = "Loren Segal"
+ s.email = "lsegal@soen.ca"
+ s.homepage = "http://yard.soen.ca"
+ s.platform = Gem::Platform::RUBY
+ s.files = Dir.glob("{example,lib,templates}/**/*") + ['LICENSE', 'README.rdoc', 'Rakefile']
+ s.require_paths = ['lib']
+ s.has_rdoc = 'yard'
+ s.rubyforge_project = 'yard-rspec'
+ s.add_dependency 'yard'
+end

0 comments on commit ea96363

Please sign in to comment.