Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Conflict resolved

  • Loading branch information...
commit fe22590f3a7b674fe422b155957413962740781c 2 parents 018e6a7 + 92ca496
Winton Welsh authored
View
4 README.markdown
@@ -19,10 +19,6 @@ Set up a fresh project
rake app_helpers
-### Update plugins (see config/plugins.rb)
-
- rake plugins:update
-
What does it do?
----------------
View
51 lib/app_helpers/truncate.rb
@@ -1,9 +1,52 @@
+require "hpricot"
+
module AppHelpers
-
- def better_truncate(text, length = 80, truncate_string = "...")
+
+ # Like the Rails _truncate_ helper but doesn't break HTML tags or entities.
+ def better_truncate(text, max_length = 30, ellipsis = "...")
return if text.nil?
- l = length - truncate_string.chars.length
- text.chars.length > length ? text[/\A.{#{l}}\w*\;?/m][/.*[\w\;]/m] + truncate_string : text
+
+ doc = Hpricot(text.to_s)
+ ellipsis_length = Hpricot(ellipsis).inner_text.chars.length
+ content_length = doc.inner_text.chars.length
+ actual_length = max_length - ellipsis_length
+
+ content_length > max_length ? doc.truncate(actual_length).inner_html + ellipsis : text.to_s
+ end
+
+ module HpricotTruncator
+ module NodeWithChildren
+ def truncate(max_length)
+ return self if inner_text.chars.length <= max_length
+ truncated_node = self.dup
+ truncated_node.children = []
+ each_child do |node|
+ remaining_length = max_length - truncated_node.inner_text.chars.length
+ break if remaining_length == 0
+ truncated_node.children << node.truncate(remaining_length)
+ end
+ truncated_node
+ end
+ end
+
+ module TextNode
+ def truncate(max_length)
+ # We're using String#scan because Hpricot doesn't distinguish entities.
+ Hpricot::Text.new(content.scan(/&#?[^\W_]+;|./).first(max_length).join)
+ end
+ end
+
+ module IgnoredTag
+ def truncate(max_length)
+ self
+ end
+ end
end
+
+ Hpricot::Doc.send(:include, HpricotTruncator::NodeWithChildren)
+ Hpricot::Elem.send(:include, HpricotTruncator::NodeWithChildren)
+ Hpricot::Text.send(:include, HpricotTruncator::TextNode)
+ Hpricot::BogusETag.send(:include, HpricotTruncator::IgnoredTag)
+ Hpricot::Comment.send(:include, HpricotTruncator::IgnoredTag)
end
View
2  resources/git/plugins.rb
@@ -4,7 +4,7 @@
{ :repo => 'git@github.com:winton/cookbook.git' },
{ :repo => 'git@github.com:winton/rails_widget.git' },
{ :repo => 'git@github.com:winton/widgets.git', :to => 'app/widgets/widgets' },
- { :repo => 'git://github.com/mislav/will_paginate.git', :depth => 1, :tag => '2.3.2' },
+ { :repo => 'git://github.com/mislav/will_paginate.git' },
'haml'
]
#{ :repo => 'git://github.com/rails/rails.git', :to => 'vendor/rails', :depth => 1 }
View
15 tasks/git.rake
@@ -1,13 +1,15 @@
namespace :app_helpers do
desc 'Copies git assets to app'
- task :git => [ 'app_helpers:git:ignore', 'app_helpers:git:plugins' ]
+ task :git => [ 'app_helpers:git:ignore', 'app_helpers:git:plugins', 'app_helpers:git:plugins:install' ]
namespace :git do
desc 'Copies .gitignore to app'
task :ignore do
- app_helper_resource 'git/ignore', '.gitignore'
+ unless File.exists?('.gitignore')
+ app_helper_resource 'git/ignore', '.gitignore'
+ end
end
desc 'Copy config/plugins.rb to app'
@@ -21,11 +23,14 @@ namespace :app_helpers do
`rm config/plugins.rb`
end
- namespace :plugins do
+ namespace :plugins do
desc 'Adds plugins defined in config/plugins.rb'
task :install do
- eval(File.read('config/plugins.rb')).each do |plugin|
- install_plugin plugin
+ puts "Review config/plugins.rb. Install plugins now? (y/n)"
+ if STDIN.gets.upcase.strip == 'Y'
+ eval(File.read('config/plugins.rb')).each do |plugin|
+ install_plugin plugin
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.