nokogiri_html_helpers 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +2 -0
- data/nokogiri_html_helpers.rb +9 -0
- data/strip_empty_block_html_helper.rb +11 -0
- data/strip_empty_block_html_helper_spec.rb +9 -0
- data/truncate_html_helper.rb +48 -0
- data/truncate_html_helper_spec.rb +25 -0
- data.tar.gz.sig +1 -0
- metadata +98 -0
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e66012cd8347fd288f9067dc5d1c36fc2edff7eb
|
4
|
+
data.tar.gz: 2918b4d2d2a1eee4d80f18ce771f5b909698b9d2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 93b08a8d948d0048a1189e4e0fea190a06e4793644d500d82b077b673acc7ea2a86b951e0f52cbfa9c9b512be5ddceb1636557cf1f20db2ac790691a59bffddb
|
7
|
+
data.tar.gz: d00b001165037a4101383c1eb10a52f79c5daaa42662b989d437a6a00f14aa29c5002b5d3847fba33f0fc910f9ffa780a0bd18a4ccea27cb250321edc887b986
|
checksums.yaml.gz.sig
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
autoload :TruncateHtmlHelper, "./truncate_html_helper"
|
2
|
+
autoload :StripEmptyBlockHtmlHelper, "./strip_empty_block_html_helper"
|
3
|
+
|
4
|
+
module NokogiriHtmlHelpers
|
5
|
+
class Engine < Rails::Engine
|
6
|
+
def find_root_with_flag(*); File.expand_path("..", __FILE__); end
|
7
|
+
self.paths["app/helpers"] = ""
|
8
|
+
end if defined? Rails
|
9
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module StripEmptyBlockHtmlHelper
|
2
|
+
def strip_empty_block_html(html)
|
3
|
+
Nokogiri::HTML::DocumentFragment.parse(html).tap do |fragment|
|
4
|
+
fragment.traverse do |node|
|
5
|
+
if node.element? and node.description and node.description.block? and node.content.empty?
|
6
|
+
node.remove
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end.to_html
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require "./strip_empty_block_html_helper"
|
2
|
+
|
3
|
+
describe StripEmptyBlockHtmlHelper do
|
4
|
+
include StripEmptyBlockHtmlHelper
|
5
|
+
|
6
|
+
specify "It should remove empty block-level elements" do
|
7
|
+
strip_empty_block_html("<p>Thing</p><p><span><!-- img was here --></span></p><p>Stuff</p>").should == "<p>Thing</p><p>Stuff</p>"
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require "nokogiri"
|
2
|
+
|
3
|
+
module TruncateHtmlHelper
|
4
|
+
def truncate_html(html, options={}, &block)
|
5
|
+
if html
|
6
|
+
length = options.fetch(:length, 30)
|
7
|
+
omission = options.fetch(:omission, "…")
|
8
|
+
|
9
|
+
omission &&= Nokogiri::HTML::DocumentFragment.parse(omission)
|
10
|
+
|
11
|
+
remaining = length
|
12
|
+
remaining -= omission.content.length if omission
|
13
|
+
|
14
|
+
fragment = Nokogiri::HTML::DocumentFragment.parse(html)
|
15
|
+
fragment.traverse do |node|
|
16
|
+
if node.text? or node.cdata?
|
17
|
+
remaining -= node.content.length
|
18
|
+
if remaining < 0
|
19
|
+
node.content = node.content[0...remaining]
|
20
|
+
end
|
21
|
+
if node.content.empty?
|
22
|
+
node.remove
|
23
|
+
end
|
24
|
+
elsif node.element? and remaining <= 0 and not node.children.any?
|
25
|
+
node.remove
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# If there's a block, use it instead of the omission
|
30
|
+
if block_given? && remaining < 0
|
31
|
+
omission = Nokogiri::HTML::DocumentFragment.parse(capture(&block))
|
32
|
+
end
|
33
|
+
|
34
|
+
# Add the omission only if we have truncated
|
35
|
+
if omission && remaining < 0
|
36
|
+
last_child = fragment.children.last
|
37
|
+
# If the last element is block, level, tuck the omission inside
|
38
|
+
if last_child.element? and last_child.description and last_child.description.block?
|
39
|
+
last_child << omission
|
40
|
+
else
|
41
|
+
fragment << omission
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
fragment.to_html
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "./truncate_html_helper"
|
2
|
+
|
3
|
+
describe TruncateHtmlHelper do
|
4
|
+
include TruncateHtmlHelper
|
5
|
+
|
6
|
+
specify "basic truncation inside block-level element" do
|
7
|
+
truncate_html("<p>A <strong>thing</strong> which gets truncated.</p><p>With trailing elements</p>", length: 8).should == "<p>A <strong>thing</strong>…</p>"
|
8
|
+
end
|
9
|
+
|
10
|
+
specify "empty elements are preserved, but danglers are removed" do
|
11
|
+
truncate_html("<p>And<br>line<br>breaks<br>are<br>preserved.</p>", length: 8).should == "<p>And<br>line…</p>"
|
12
|
+
end
|
13
|
+
|
14
|
+
specify "omissions are outside inline elements" do
|
15
|
+
truncate_html("<strong>Thing</strong> and <em>stuff</em>", length: 6).should == "<strong>Thing</strong>…"
|
16
|
+
end
|
17
|
+
|
18
|
+
specify "default length is 30" do
|
19
|
+
truncate_html("a" * 50).should == "a" * 29 << "…"
|
20
|
+
end
|
21
|
+
|
22
|
+
specify "alternate omission text" do
|
23
|
+
truncate_html("<div>a<em>b</em>cdef</div>", length: 6, omission: "...").should == "<div>a<em>b</em>c...</div>"
|
24
|
+
end
|
25
|
+
end
|
data.tar.gz.sig
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
i2���dH�N�SƷE⎟e���wE� ��}ș��ê���݈�����8��)������5M�m�^uf�F�Qko�AӂV�z1�^�O=vc��
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nokogiri_html_helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samuel Cochran
|
8
|
+
autorequire:
|
9
|
+
bindir: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OQknO1aJZl
|
27
|
+
hp3Jijjd/vpxFuwalQT6JcN9V0iz81KiI+9KPRDxedX4zK9PD4Be+ZJgZBOalk6U
|
28
|
+
gQM0w4nYX8KATozCgvkFvKOQrNBEa+BqPundb8r047VRuK9iPGaJJeBpBgXgCNQc
|
29
|
+
iAw+LFjcoeiI5naoCBHZ4uHGY0lXrxxcep3F/GFvKKYGrrI9c2j5awd0DaEzrCel
|
30
|
+
gtfuNEm5Q0pV0/XpT5PsXz/rGnsR4CS7BaIcx+bAwl5pRopCzCHfl5Igay1vefPZ
|
31
|
+
-----END CERTIFICATE-----
|
32
|
+
date: 2013-10-17 00:00:00.000000000 Z
|
33
|
+
dependencies:
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: nokogiri
|
36
|
+
requirement: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.6.0
|
41
|
+
type: :runtime
|
42
|
+
prerelease: false
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.6.0
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rspec
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.0'
|
62
|
+
description: Fast HTML helpers using Nokogiri
|
63
|
+
email: sj26@sj26.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- nokogiri_html_helpers.rb
|
69
|
+
- strip_empty_block_html_helper.rb
|
70
|
+
- truncate_html_helper.rb
|
71
|
+
- strip_empty_block_html_helper_spec.rb
|
72
|
+
- truncate_html_helper_spec.rb
|
73
|
+
homepage:
|
74
|
+
licenses: []
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- .
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.0.3
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Fast HTML helpers using Nokogiri
|
96
|
+
test_files:
|
97
|
+
- strip_empty_block_html_helper_spec.rb
|
98
|
+
- truncate_html_helper_spec.rb
|
metadata.gz.sig
ADDED
Binary file
|