Skip to content

Commit e96a73a

Browse files
committed
Changing to use all rake for build
1 parent b6a75ad commit e96a73a

File tree

12 files changed

+144
-140
lines changed

12 files changed

+144
-140
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
doc/*
1+
doc
22
.garlic

History.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* Make API compatible with rails 2.2
2+
3+
* Initial release

License.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2008-2009 Ian White - [email protected]
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.rdoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
= {ianwhite}[http://github.com/ianwhite] / {truncate_html}[http://github.com/truncate_html]
1+
= {ianwhite}[http://github.com/ianwhite] / {truncate_html}[http://github.com/ianwhite/truncate_html]
22

3-
Ardes::TruncateHtml is just like the vanilla truncate rails helper, except it respects tags and html entities, and returns valid html.
3+
TruncateHtml is just like the vanilla truncate rails helper, except it respects tags and html entities, and returns valid html.
44

55
Rexml and HtmlEntities are used for the parsing. If the input is invalid html, then hpricot is used to fix it up (but is not used when the input is fine).
66

Rakefile

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@ $LOAD_PATH.unshift(rspec_base) if File.exist?(rspec_base) and !$LOAD_PATH.includ
44

55
require 'spec/rake/spectask'
66
require 'spec/rake/verify_rcov'
7-
require 'hanna/rdoctask'
8-
require 'garlic/tasks'
97

108
plugin_name = "truncate_html"
119

1210
task :default => :spec
1311

14-
task :cruise => ['garlic:all', 'doc:push'] do
15-
puts "The build is GOOD"
16-
end
17-
1812
desc "Run the specs for #{plugin_name}"
1913
Spec::Rake::SpecTask.new(:spec) do |t|
2014
t.spec_files = FileList['spec/**/*_spec.rb']
@@ -37,50 +31,47 @@ namespace :rcov do
3731
end
3832
end
3933

40-
task :doc => 'doc:build'
41-
42-
namespace :doc do
43-
def sha
44-
`git log -1 --pretty=format:"%h"`
45-
end
46-
47-
Rake::RDocTask.new(:build) do |d|
48-
d.rdoc_dir = 'doc'
49-
d.main = 'README.rdoc'
50-
d.title = "#{plugin_name} API Docs (#{sha})"
51-
d.rdoc_files.include('README.rdoc', 'History.txt', 'License.txt', 'Todo.txt', 'lib/**/*.rb')
52-
end
34+
# the following tasks are for CI and doc building, so if dependencies don't
35+
# exist just load these tasks
36+
begin
37+
require 'hanna/rdoctask'
38+
require 'garlic/tasks'
39+
require 'grancher/task'
5340

54-
task :push => 'doc:build' do
55-
# if sha != `cat`
56-
#
57-
# mv 'doc', 'newdoc'
58-
# on_gh_pages do
59-
# if doc_changed_sha?('newdoc', 'doc')
60-
# puts "doc has changed, pushing to gh-pages"
61-
# `rm -rf doc && mv newdoc doc`
62-
# `git add doc`
63-
# `git commit -a -m "Update API docs"`
64-
# `git push`
65-
# else
66-
# puts "doc is unchanged"
67-
# rm_rf 'newdoc'
68-
# end
69-
# end
41+
task :cruise => ['garlic:all', 'doc:publish_new'] do
42+
puts "The build is GOOD"
7043
end
44+
45+
task :doc => 'doc:build'
46+
47+
namespace :doc do
48+
def repo_sha
49+
`git log -1 --pretty=format:"%h"`
50+
end
7151

72-
def doc_changed_sha?(docpath1, docpath2)
73-
`cat #{docpath1}/index.html | grep "<title>"` != `cat #{docpath2}/index.html | grep "<title>"`
74-
end
52+
def doc_sha
53+
File.read('doc/index.html').match(/<title>.*?\((\w{7})\)<\/title>/m)[1] rescue nil
54+
end
55+
56+
task :publish_new do
57+
Rake::Task.invoke('doc:publish') if doc_sha != repo_sha
58+
end
59+
60+
Rake::RDocTask.new(:build) do |d|
61+
d.rdoc_dir = 'doc'
62+
d.main = 'README.rdoc'
63+
d.title = "#{plugin_name} API Docs (#{repo_sha})"
64+
d.rdoc_files.include('README.rdoc', 'History.txt', 'License.txt', 'Todo.txt', 'lib/**/*.rb')
65+
end
7566

76-
def on_gh_pages(&block)
77-
`git branch -m gh-pages orig-gh-pages > /dev/null 2>&1`
78-
`git checkout -b gh-pages origin/gh-pages`
79-
`git pull`
80-
yield
81-
ensure
82-
`git checkout master`
83-
`git branch -D gh-pages`
84-
`git branch -m orig-gh-pages gh-pages > /dev/null 2>&1`
67+
Grancher::Task.new(:publish) => ['doc:build'] do |g|
68+
if doc_sha != repo_sha
69+
g.keep 'index.html'
70+
g.directory 'doc', 'doc'
71+
g.branch = 'gh-pages'
72+
g.push_to = 'origin'
73+
end
74+
end
8575
end
86-
end
76+
rescue LoadError
77+
end

Todo.txt

Whitespace-only changes.

garlic.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
run do
1919
cd "vendor/plugins/truncate_html" do
20-
sh "rake spec:rcov:verify"
20+
sh "rake rcov:verify"
2121
end
2222
end
2323
end

init.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# init
2-
require 'ardes/truncate_html_helper'
1+
require 'truncate_html_helper'
32

4-
ActionView::Base.send :include, Ardes::TruncateHtmlHelper
3+
ActionView::Base.send :include, TruncateHtmlHelper

lib/ardes/truncate_html_helper.rb

Lines changed: 0 additions & 60 deletions
This file was deleted.

lib/truncate_html_helper.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require "rexml/parsers/pullparser"
2+
require 'rubygems'
3+
require "htmlentities"
4+
require "hpricot"
5+
6+
module TruncateHtmlHelper
7+
# you may set this to either 'html4', or 'xhtml1'
8+
mattr_accessor :flavor
9+
self.flavor = 'html4'
10+
11+
# Truncates html respecting tags and html entities.
12+
#
13+
# 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
14+
#
15+
# Examples:
16+
# truncate_html '<p>Hello <strong>World</strong></p>', :length => 7 # => '<p>Hello <strong>W&hellip;</strong></p>'
17+
# truncate_html '<p>Hello &amp; Goodbye</p>', :length => 7 # => '<p>Hello &amp;&hellip;</p>'
18+
def truncate_html(input, *args)
19+
# support both 2.2 & earlier APIs
20+
options = args.extract_options!
21+
length = options[:length] || args[0] || 30
22+
omission = options[:omission] || args[1] || '&hellip;'
23+
24+
begin
25+
parser = REXML::Parsers::PullParser.new(input)
26+
encoder = HTMLEntities.new(TruncateHtmlHelper.flavor)
27+
tags, output, chars_remaining = [], '', length
28+
29+
while parser.has_next? && chars_remaining > 0
30+
element = parser.pull
31+
case element.event_type
32+
when :start_element
33+
output << rexml_element_to_tag(element)
34+
tags.push element[0]
35+
when :end_element
36+
output << "</#{tags.pop}>"
37+
when :text
38+
text = encoder.decode(element[0])
39+
output << encoder.encode(text.first(chars_remaining))
40+
chars_remaining -= text.length
41+
output << omission if chars_remaining < 0
42+
end
43+
end
44+
45+
tags.reverse.each {|tag| output << "</#{tag}>" }
46+
output
47+
48+
rescue REXML::ParseException
49+
input = Hpricot(input, :fixup_tags => true).to_html
50+
retry
51+
end
52+
end
53+
54+
private
55+
def rexml_element_to_tag(element)
56+
"<#{element[0]}#{element[1].inject(""){|m,(k,v)| m << %{ #{k}="#{v}"}} unless element[1].empty?}>"
57+
end
58+
end

0 commit comments

Comments
 (0)