html_to_plain_text 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/html_to_plain_text.rb +3 -1
- data/spec/html_to_plain_text_spec.rb +12 -0
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
data/lib/html_to_plain_text.rb
CHANGED
@@ -18,6 +18,7 @@ module HtmlToPlainText
|
|
18
18
|
LI = "li".freeze
|
19
19
|
NUMBERS = ["1", "a"]
|
20
20
|
ABSOLUTE_URL_PATTERN = /^[a-z]+:\/\/[a-z0-9]/i
|
21
|
+
HTML_PATTERN = /[<&]/
|
21
22
|
|
22
23
|
# Helper instance method for converting HTML into plain text. This method simply calls HtmlToPlainText.plain_text.
|
23
24
|
def plain_text(html)
|
@@ -27,7 +28,8 @@ module HtmlToPlainText
|
|
27
28
|
class << self
|
28
29
|
# Convert some HTML into a plain text approximation.
|
29
30
|
def plain_text(html)
|
30
|
-
return if html.nil?
|
31
|
+
return nil if html.nil?
|
32
|
+
return html.dup unless html.match(HTML_PATTERN)
|
31
33
|
body = Nokogiri::HTML::Document.parse(html).css("body").first
|
32
34
|
return unless body
|
33
35
|
convert_node_to_plain_text(body).strip.gsub(/\r(\n?)/, "\n")
|
@@ -90,4 +90,16 @@ describe HtmlToPlainText do
|
|
90
90
|
html = "This & that"
|
91
91
|
HtmlToPlainText.plain_text(html).should == "This & that"
|
92
92
|
end
|
93
|
+
|
94
|
+
it "should handle nil" do
|
95
|
+
HtmlToPlainText.plain_text(nil).should == nil
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should handle empty text" do
|
99
|
+
HtmlToPlainText.plain_text("").should == ""
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should handle non-html text" do
|
103
|
+
HtmlToPlainText.plain_text("test").should == "test"
|
104
|
+
end
|
93
105
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: html_to_plain_text
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Brian Durand
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-06-
|
18
|
+
date: 2011-06-21 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|