peppercorn 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .yardoc
6
+ doc/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in peppercorn.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,16 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'yard' do
5
+ watch(%r{app/.+\.rb})
6
+ watch(%r{lib/.+\.rb})
7
+ watch(%r{ext/.+\.(c|cpp|rb)})
8
+ end
9
+
10
+ guard 'rspec', :version => 2 do
11
+ watch(%r{^spec/.+_spec\.rb$})
12
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
13
+ watch('spec/spec_helper.rb') { "spec" }
14
+ end
15
+
16
+
data/Procfile ADDED
@@ -0,0 +1,2 @@
1
+ guard: guard
2
+ yard: yard server -p 3001
data/README ADDED
@@ -0,0 +1,9 @@
1
+ == License
2
+
3
+ Copyright (c) 2011 Wade West
4
+
5
+ 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:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ 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.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,57 @@
1
+ require 'nokogiri'
2
+
3
+ module Peppercorn
4
+ # Peppercorn extension to the String class
5
+ module String
6
+
7
+ # Truncate a string to "length" words
8
+ # @since 0.0.1
9
+ # @param [Fixnum] length the number of word to truncate on
10
+ # @param [Hash] opts hash of truncation options
11
+ # @option opts [String, nil] :tail ("…") the string to append to the truncated string
12
+ # @return [String] the truncated string
13
+ def truncate(length=30, opts={})
14
+ opts = Peppercorn::DEFAULT_TRUNCATION_OPTIONS.merge(opts)
15
+ tokens = scan(/\W*\w+\W*/)
16
+ string_tokens = tokens[0...length]
17
+ string = string_tokens.join
18
+ overran = length <= tokens.size
19
+ string = string.strip_end if opts[:strip]
20
+ string << opts[:tail].to_s if overran
21
+ if opts[:return_node]
22
+ string = Nokogiri::HTML::DocumentFragment.parse(string)
23
+ string = string.child if string.children.size == 1
24
+ end
25
+ return opts[:return_hash] ? {:text => string, :overran => overran, :count => [length, tokens.size].min} : string
26
+ end
27
+
28
+ # Strips all whitespaces from the end of the string
29
+ # @since 0.0.1
30
+ # @return [String] the stripped version of the string
31
+ def strip_end
32
+ return sub(/(\s+)$/, '')
33
+ end
34
+
35
+ # Returns the string with all HTML tags removed
36
+ # @since 0.0.1
37
+ # @return [String] the string stripped of all HTML
38
+ def strip_html
39
+ return Nokogiri::HTML::DocumentFragment.parse(self).inner_text
40
+ end
41
+
42
+ # Truncate a string of html to "length" words
43
+ # @since 0.0.1
44
+ # @param (see #truncate)
45
+ # @return [String] the truncated html
46
+ # @option opts [String, Nokogiri::XML::Node, nil] :tail ("&#8230;") string or xml node to append to the truncated string
47
+ def truncate_html(length=30, opts={})
48
+ string = Nokogiri::HTML::DocumentFragment.parse(self).truncate(length, opts)
49
+ return string
50
+ end
51
+ end
52
+ end
53
+
54
+ # Include Peppercorn::String in String
55
+ class String
56
+ include Peppercorn::String
57
+ end
@@ -0,0 +1,4 @@
1
+ module Peppercorn
2
+ # The current peppercorn version
3
+ VERSION = "0.0.1"
4
+ end
@@ -0,0 +1,82 @@
1
+ require 'nokogiri'
2
+
3
+ module Peppercorn
4
+
5
+ module XML
6
+
7
+ # Extensions to Nokogiri::XML::Node
8
+ module Node
9
+
10
+ # (see Peppercorn::String#truncate_html)
11
+ def truncate(length, opts={})
12
+ opts = Peppercorn::DEFAULT_TRUNCATION_OPTIONS.merge(opts)
13
+
14
+ doc = self
15
+ target = nil
16
+ count = 0
17
+ overran = false
18
+
19
+ if doc.text?
20
+ result = content.truncate(length, opts.merge(Peppercorn::TRUNCATE_CHILD_OPTS))
21
+ target = result[:text]
22
+ count = result[:count]
23
+ overran = result[:overran]
24
+ else
25
+ target = doc.clone
26
+ target.children.remove
27
+ doc.children.each do |child|
28
+ result = child.truncate(length-count, opts.merge(Peppercorn::TRUNCATE_CHILD_OPTS))
29
+ count += result[:count]
30
+ overran = result[:overran]
31
+ target << result[:text]
32
+ break if overran
33
+ end
34
+ end
35
+
36
+ return nil if target.nil?
37
+
38
+
39
+ if opts[:strip]
40
+ last_text_node = target.last_text_node
41
+ last_text_node.content = last_text_node.content.sub(/(\s+)$/, '') if last_text_node
42
+ end
43
+
44
+ if !opts[:tail].to_s.empty? and overran
45
+ append_to = target.children.last
46
+ loop do
47
+ if append_to.text? or append_to.description.inline? # We want to append to the last block-level element
48
+ append_to = append_to.parent
49
+ break;
50
+ end
51
+ append_to = append_to.children.last
52
+ end
53
+ append_to << Nokogiri::HTML::DocumentFragment.parse(opts[:tail])
54
+ end
55
+
56
+ target = target.inner_html unless opts[:return_node]
57
+ return opts[:return_hash] ? {:text => target, :count => [count, length].min, :overran => overran} : target
58
+
59
+ end
60
+
61
+ # Returns the last child node that is a text node
62
+ # @return [Nokogiri::XML::Node] the last text node
63
+ # @since 0.0.1
64
+ def last_text_node
65
+ return self if text? and !content.empty?
66
+
67
+ children.reverse.each do |child|
68
+ ltn = child.last_text_node
69
+ return ltn unless ltn.nil?
70
+ end
71
+
72
+ return nil
73
+ end
74
+
75
+ end
76
+ end
77
+ end
78
+
79
+ # Include Peppercorn::XML::Node in Nokogiri::XML::Node (http://nokogiri.org)
80
+ class Nokogiri::XML::Node
81
+ include Peppercorn::XML::Node
82
+ end
@@ -0,0 +1,5 @@
1
+ module Peppercorn
2
+ # Extensions to Nokogiri::XML
3
+ module XML
4
+ end
5
+ end
data/lib/peppercorn.rb ADDED
@@ -0,0 +1,31 @@
1
+ require "peppercorn/version"
2
+
3
+ # The peppercorn gem is a library for helping out with HTML, XML, and Strings.
4
+ #
5
+ # Peppercorn added some methods to the String and Nokogiri::XML::Node classes to do its work.
6
+ module Peppercorn
7
+
8
+ # Default options for truncating strings and html
9
+ # * WARNING: The undocumented options could change or be removed with any release without notice *
10
+ DEFAULT_TRUNCATION_OPTIONS = {
11
+ # Documented options
12
+ :tail => "&#8230;",
13
+ # Undocumented options
14
+ :strip => true,
15
+ :return_hash => false,
16
+ :return_node => false
17
+ }
18
+
19
+
20
+ # Options to override when recursively truncating a child node
21
+ TRUNCATE_CHILD_OPTS = {
22
+ :tail => nil,
23
+ :return_hash => true,
24
+ :return_node => true,
25
+ :strip => false
26
+ }
27
+
28
+ end
29
+
30
+ require "peppercorn/string"
31
+ require "peppercorn/xml/node"
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "peppercorn/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "peppercorn"
7
+ s.version = Peppercorn::VERSION
8
+ s.authors = ["Wade West"]
9
+ s.email = ["wwest81@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{A simple gem to truncate HTML, with other features coming in the future.}
12
+ s.description = %q{A simple gem to truncate HTML, with other features coming in the future.}
13
+
14
+ s.rubyforge_project = "peppercorn"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "yard", "~> 0.7"
23
+ s.add_development_dependency "rspec", "~> 2.7"
24
+ s.add_development_dependency "guard-rspec", "~> 0.5"
25
+ s.add_development_dependency "guard-yard", "~> 1.0"
26
+ # s.add_runtime_dependency "rest-client"
27
+ # s.add_dependency "htmlentities"
28
+ s.add_dependency "nokogiri"
29
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe String do
4
+
5
+ before do
6
+ @string = "Hello World, I make HTML truncation easy."
7
+ end
8
+
9
+ it "should properly truncate a string" do
10
+ @string.truncate(5).should eql "Hello World, I make HTML&#8230;"
11
+ end
12
+
13
+ it "should not truncate a string shorter than length" do
14
+ @string.truncate(30).should eql @string
15
+ end
16
+
17
+ it "should properly truncate some simple HTML" do
18
+ "<em>#{@string}</em>".truncate_html(5).should eql "<em>Hello World, I make HTML</em>&#8230;"
19
+ end
20
+
21
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe String do
4
+
5
+ before do
6
+ @string = "<p><em class='strong'>Hello World</em>, I <i>can</i> make HTML truncation easy.</p>"
7
+ @html = Nokogiri::HTML::DocumentFragment.parse @string
8
+ end
9
+
10
+ it "should properly truncate some simple HTML" do
11
+ @html.truncate(6).should eql "<p><em class=\"strong\">Hello World</em>, I <i>can</i> make HTML&#8230;</p>"
12
+ end
13
+
14
+ it "should not truncate some HTML that is shorter than length" do
15
+ @html.truncate(30).should eql @html.to_s
16
+ end
17
+
18
+ end
@@ -0,0 +1 @@
1
+ require 'peppercorn'
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: peppercorn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Wade West
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-26 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: yard
16
+ requirement: &70354207037860 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.7'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70354207037860
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70354207035900 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.7'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70354207035900
36
+ - !ruby/object:Gem::Dependency
37
+ name: guard-rspec
38
+ requirement: &70354207034020 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '0.5'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70354207034020
47
+ - !ruby/object:Gem::Dependency
48
+ name: guard-yard
49
+ requirement: &70354207032280 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70354207032280
58
+ - !ruby/object:Gem::Dependency
59
+ name: nokogiri
60
+ requirement: &70354207031480 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *70354207031480
69
+ description: A simple gem to truncate HTML, with other features coming in the future.
70
+ email:
71
+ - wwest81@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - Guardfile
79
+ - Procfile
80
+ - README
81
+ - Rakefile
82
+ - lib/peppercorn.rb
83
+ - lib/peppercorn/string.rb
84
+ - lib/peppercorn/version.rb
85
+ - lib/peppercorn/xml.rb
86
+ - lib/peppercorn/xml/node.rb
87
+ - peppercorn.gemspec
88
+ - spec/peppercorn/string_spec.rb
89
+ - spec/peppercorn/xml/node_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: ''
92
+ licenses: []
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project: peppercorn
111
+ rubygems_version: 1.8.10
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: A simple gem to truncate HTML, with other features coming in the future.
115
+ test_files:
116
+ - spec/peppercorn/string_spec.rb
117
+ - spec/peppercorn/xml/node_spec.rb
118
+ - spec/spec_helper.rb
119
+ has_rdoc: