Skip to content

proposal and clean integration with bundle && rspec2 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
doc
.garlic
coverage/*
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source "http://rubygems.org"
gem "nokogiri"
gem "rack", "~>1.1"
gem "rspec", :require => "spec"
gem 'htmlentities'
31 changes: 10 additions & 21 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,32 @@
rspec_base = File.expand_path(File.dirname(__FILE__) + '/../rspec/lib')
$LOAD_PATH.unshift(rspec_base) if File.exist?(rspec_base) and !$LOAD_PATH.include?(rspec_base)

require 'spec/rake/spectask'
require 'spec/rake/verify_rcov'
require 'rcov'
require 'rspec/core/rake_task'

PluginName = "truncate_html"

task :default => :spec

desc "Run the specs for #{PluginName}"
Spec::Rake::SpecTask.new(:spec) do |t|
t.spec_files = FileList['spec/**/*_spec.rb']
t.spec_opts = ["--colour"]
end
RSpec::Core::RakeTask.new(:spec)

desc "Generate RCov report for #{PluginName}"
Spec::Rake::SpecTask.new(:rcov) do |t|
t.spec_files = FileList['spec/**/*_spec.rb']
RSpec::Core::RakeTask.new(:rcov) do |t|
# t.spec_files = FileList['spec/**/*_spec.rb']
t.rcov = true
t.rcov_dir = 'doc/coverage'
t.rcov_opts = ['--text-report', '--exclude', "/Library/Ruby,spec/,rcov.rb,#{File.expand_path(File.join(File.dirname(__FILE__),'../../..'))}"]
end

namespace :rcov do
desc "Verify RCov threshold for #{PluginName}"
RCov::VerifyTask.new(:verify => "rcov") do |t|
t.threshold = 100.0
t.index_html = File.join(File.dirname(__FILE__), 'doc/coverage/index.html')
end
# t.rcov_dir = 'doc/coverage'
t.rcov_opts = ['--text-report', '--exclude', "/Library/Ruby,spec/,rcov.rb,#{File.expand_path(File.join(File.dirname(__FILE__),'../../..'))}"]
end

# the following tasks are for CI and doc building
begin
require 'hanna/rdoctask'
require 'garlic/tasks'
require 'grancher/task'

task :cruise => ['garlic:all', 'doc:publish']

Rake::RDocTask.new(:doc) do |d|
d.options << '--all'
d.rdoc_dir = 'doc'
Expand All @@ -51,7 +40,7 @@ begin
task :publish => :doc do
Rake::Task['doc:push'].invoke unless uptodate?('.git/refs/heads/gh-pages', 'doc')
end

Grancher::Task.new(:push) do |g|
g.keep_all
g.directory 'doc', 'doc'
Expand Down
1 change: 1 addition & 0 deletions autotest/discover.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Autotest.add_discovery { "rspec2" }
20 changes: 10 additions & 10 deletions lib/truncate_html_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
module TruncateHtmlHelper
# raised when tags could not be fixed up by nokogiri
class InvalidHtml < RuntimeError; end

# you may set this to either 'html4', or 'xhtml1'
mattr_accessor :flavor
class << self
alias_method :flavour=, :flavor=
alias_method :flavour, :flavor
end

self.flavor = 'html4'

# Truncates html respecting tags and html entities.
#
# The API is the same as ActionView::Helpers::TextHelper#truncate. It uses Rexml for the parsing, and HtmlEntities for entity awareness. If Rexml raises a ParseException, then Hpricot is used to fixup the tags, and we try again
Expand All @@ -25,12 +25,12 @@ def truncate_html(input, *args)
options = args.extract_options!
length = options[:length] || args[0] || 30
omission = options[:omission] || args[1] || '&hellip;'

begin
parser = REXML::Parsers::PullParser.new(input)
encoder = HTMLEntities.new(TruncateHtmlHelper.flavor)
tags, output, chars_remaining = [], '', length

while parser.has_next? && chars_remaining > 0
element = parser.pull
case element.event_type
Expand All @@ -41,25 +41,25 @@ def truncate_html(input, *args)
output << "</#{tags.pop}>"
when :text
text = encoder.decode(element[0])
output << encoder.encode(text.first(chars_remaining))
output << encoder.encode(text[0,chars_remaining])
chars_remaining -= text.length
output << omission if chars_remaining < 0
end
end

tags.reverse.each {|tag| output << "</#{tag}>" }
output

rescue REXML::ParseException => e
fixed_up = Nokogiri::HTML.fragment(input).to_html
raise ::TruncateHtmlHelper::InvalidHtml, "Could not fixup invalid html. #{e.message}" if fixed_up == input
input = fixed_up
retry
end
end

private
def rexml_element_to_tag(element)
"<#{element[0]}#{element[1].inject(""){|m,(k,v)| m << %{ #{k}="#{v}"}} unless element[1].empty?}>"
end
end
end
28 changes: 16 additions & 12 deletions spec/helpers/truncate_html_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe TruncateHtmlHelper do
include TruncateHtmlHelper

describe "examples from Rails doc" do
it "'Once upon a time in a world far far away'" do
truncate_html("Once upon a time in a world far far away").should == "Once upon a time in a world fa&hellip;"
Expand All @@ -16,22 +16,22 @@
truncate_html("And they found that many people were sleeping better.", :length => 25, :omission => "(clipped)").should == "And they found that many (clipped)"
end
end

describe "use cases" do
def self.with_length_should_equal(n, str)
it "#{n}, should equal #{str}" do
truncate_html(@html, :length => n).should == str
end
end

describe "html: <p>Hello <strong>World</strong></p>, length: " do
before { @html = '<p>Hello <strong>World</strong></p>' }

with_length_should_equal 3, '<p>Hel&hellip;</p>'
with_length_should_equal 7, '<p>Hello <strong>W&hellip;</strong></p>'
with_length_should_equal 11, '<p>Hello <strong>World</strong></p>'
end

describe 'html: <p>Hello &amp; <span class="foo">Goodbye</span> <br /> Hi</p>, length: ' do
before { @html = '<p>Hello &amp; <span class="foo">Goodbye</span> <br /> Hi</p>' }

Expand All @@ -42,31 +42,35 @@ def self.with_length_should_equal(n, str)

describe '(incorrect) html: <p>Hello <strong>World</p><div>And Hi, length: ' do
before { @html = '<p>Hello <strong>World</p><div>And Hi' }

with_length_should_equal 10, '<p>Hello <strong>Worl&hellip;</strong></p>'
with_length_should_equal 30, '<p>Hello <strong>World</strong></p><div>And Hi</div>'
end
end

it "should not convert ' to &apos; (html4 compat)" do
truncate_html("30's").should == "30's"
end


it "should support chinese comma" do
truncate_html("年,,年,,年,,年,,年,,",6).should == "年,&hellip;"
end

describe "when TruncateHtmlHelper.flavour = 'xhtml1'" do
before do
TruncateHtmlHelper.flavour = 'xhtml1'
end

after do
TruncateHtmlHelper.flavour = 'html4'
end

it "should convert ' to &apos;" do
truncate_html("30's").should == "30&apos;s"
end

it "should translate across the atlantic" do
TruncateHtmlHelper.flavor.should == 'xhtml1'
end
end
end
end
15 changes: 4 additions & 11 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@

require 'rubygems'

# if we're in a rails env, use that, otherwise use rubygems to create a spec env
begin
require "#{__DIR__}/../../../../config/environment"
rescue LoadError
require 'activesupport'
require 'actionpack'
require 'action_view'
$LOAD_PATH << "#{__DIR__}/../lib"
require "#{__DIR__}/../init"
end
require 'rails/all'
$LOAD_PATH << "#{__DIR__}/../lib"
require "#{__DIR__}/../init"

require 'spec'
require 'rspec'

# dependencies of truncate_html
require 'htmlentities'
Expand Down