nokogiri_truncate_html 0.0.4 → 0.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5ccfb47fb48ccba5a0b7667ce4564161fbdc5ea6
4
- data.tar.gz: 0193fcd8aa25a3a63e389413e873b73d00e2c331
3
+ metadata.gz: ad6a7de52fb5b441032153acfaf0651a695e0d64
4
+ data.tar.gz: 958fe6a59782c75791dc2af20a89c34e20056739
5
5
  SHA512:
6
- metadata.gz: 5382c4ea20e670694ae5f143109f21e56515ab39aac9b5b8843b7dc0de94fb32fcff1763ba87625be6991ca1b248921bc4c692f86b58c3cc3eccb21c943861f6
7
- data.tar.gz: e8017017d4b5485943e7ac387395e7d3069a5dfae2fefebb99b3fea84d1f84ab574f63055d8826afe3e26d3e60dd3bc3be7e33acf77dbc200aec9fdfa8f88200
6
+ metadata.gz: d49c7a295c0f314e9288c09f10d06fc820fa9b849a89b0c3acffa61a9d57ca2f07f6adeb9eb56d6d801c13b9c1f86477ed864e137df2e7d895b5d6868b55245d
7
+ data.tar.gz: ffac697365c2b1d14c3e95a795817836f2a151ef8a66c754602a551a6e1f15279fc01f5563b1d063d7579bd9625cbf498f6aa9349e356222321639d30dff9b8f
@@ -1,27 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'nokogiri'
2
4
  require 'cgi'
3
5
 
4
6
  module NokogiriTruncateHtml
5
- class TruncateFinished < Exception
6
- end
7
-
8
7
  class TruncateDocument < Nokogiri::XML::SAX::Document
9
-
10
8
  def initialize
11
- # We use the xhtml "flavour" so that we can decode
12
- # apostrophes. That is the only difference between
13
- # xhtml1 and html4.
14
- @encoder = HTMLEntities.new('xhtml1')
15
9
  @discard_first_element = false
16
10
  end
17
11
 
18
- def length=(length)
19
- @length = length
20
- end
21
-
22
- def omission=(omission)
23
- @omission = omission
24
- end
12
+ attr_writer :length, :omission
25
13
 
26
14
  def output
27
15
  while @tags.size > 0
@@ -31,12 +19,11 @@ module NokogiriTruncateHtml
31
19
  end
32
20
 
33
21
  def start_document
34
- @output, @chars_remaining, @tags = '', @length, []
22
+ @output, @chars_remaining, @tags = String.new, @length, []
35
23
  @discard_first_element = false
36
24
  end
37
25
 
38
- def characters(string)
39
- text = @encoder.decode(string)
26
+ def characters(text)
40
27
  @output << CGI.escapeHTML(text[0, @chars_remaining])
41
28
  @chars_remaining -= text.length
42
29
  if @chars_remaining < 0
@@ -45,16 +32,33 @@ module NokogiriTruncateHtml
45
32
  end
46
33
  end
47
34
 
35
+ HTML_TAG = 'html'.freeze
36
+ BODY_TAG = 'body'.freeze
37
+ BR_TAG = 'br'.freeze
38
+ EMBED_TAG = 'embed'.freeze
39
+ HR_TAG = 'hr'.freeze
40
+ IMG_TAG = 'img'.freeze
41
+ INPUT_TAG = 'input'.freeze
42
+ PARAM_TAG = 'param'.freeze
43
+
48
44
  def start_element(name, attrs = [])
49
45
  unless @discard_first_element
50
- return if %w(html body).include? name
46
+ return if name == HTML_TAG || name == BODY_TAG
51
47
  return @discard_first_element = true
52
48
  end
53
49
 
54
- if %w(br embed hr img input param).include? name
55
- @output << "<#{name}#{' ' if attrs.size > 0 }#{attrs.map { |attr,val| "#{attr}=\"#{val}\"" }.join(' ')} />"
50
+ @output << "<#{name}"
51
+ unless attrs.empty?
52
+ attrs.each do |attr, val|
53
+ @output << " #{attr}=\"#{val}\""
54
+ end
55
+ end
56
+
57
+ if name == BR_TAG || name == EMBED_TAG || name == HR_TAG ||
58
+ name == IMG_TAG || name == INPUT_TAG || name == PARAM_TAG
59
+ @output << ' />'
56
60
  else
57
- @output << "<#{name}#{' ' if attrs.size > 0 }#{attrs.map { |attr,val| "#{attr}=\"#{val}\"" }.join(' ')}>"
61
+ @output << '>'
58
62
  @tags.push name
59
63
  end
60
64
  end
@@ -4,9 +4,6 @@ require 'nokogiri_truncate_html/truncate_document'
4
4
 
5
5
  module NokogiriTruncateHtml
6
6
  module TruncateHtmlHelper
7
- mattr_accessor :parser
8
- mattr_accessor :document
9
-
10
7
  # Truncates html respecting tags and html entities.
11
8
  #
12
9
  # The API is the same as ActionView::Helpers::TextHelper#truncate. It uses Nokogiri and HtmlEntities for entity awareness.
@@ -15,8 +12,8 @@ module NokogiriTruncateHtml
15
12
  # truncate_html '<p>Hello <strong>World</strong></p>', :length => 7 # => '<p>Hello <strong>W&hellip;</strong></p>'
16
13
  # truncate_html '<p>Hello &amp; Goodbye</p>', :length => 7 # => '<p>Hello &amp;&hellip;</p>'
17
14
  def truncate_html(input, *args)
18
- self.document ||= TruncateDocument.new#(TruncateHtmlHelper.flavor) #, length, omission)
19
- self.parser ||= Nokogiri::HTML::SAX::Parser.new(document)
15
+ document = truncate_html_document
16
+ parser = truncate_html_parser
20
17
 
21
18
  # support both 2.2 & earlier APIs
22
19
  options = args.extract_options!
@@ -25,12 +22,22 @@ module NokogiriTruncateHtml
25
22
 
26
23
  # Adding div around the input is a hack. It gets removed in TruncateDocument.
27
24
  input = "<div>#{input}</div>"
28
- self.document.length = length
29
- self.document.omission = omission
25
+ document.length = length
26
+ document.omission = omission
30
27
  catch(:truncate_finished) do
31
- self.parser.parse_memory(input)
28
+ parser.parse_memory(input)
32
29
  end
33
- self.document.output.html_safe
30
+ document.output.html_safe
31
+ end
32
+
33
+ private
34
+
35
+ def truncate_html_document
36
+ Thread.current[:truncate_html_document] ||= TruncateDocument.new
37
+ end
38
+
39
+ def truncate_html_parser
40
+ Thread.current[:truncate_html_parser] ||= Nokogiri::HTML::SAX::Parser.new(truncate_html_document)
34
41
  end
35
42
  end
36
43
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nokogiri_truncate_html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian White
@@ -9,62 +9,48 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-18 00:00:00.000000000 Z
12
+ date: 2017-01-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - '>='
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
20
  version: 1.5.8
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - '>='
25
+ - - ">="
26
26
  - !ruby/object:Gem::Version
27
27
  version: 1.5.8
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: activesupport
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - '>='
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: 3.2.13
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - '>='
39
+ - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: 3.2.13
42
- - !ruby/object:Gem::Dependency
43
- name: htmlentities
44
- requirement: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - '>='
47
- - !ruby/object:Gem::Version
48
- version: 4.3.1
49
- type: :runtime
50
- prerelease: false
51
- version_requirements: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - '>='
54
- - !ruby/object:Gem::Version
55
- version: 4.3.1
56
42
  - !ruby/object:Gem::Dependency
57
43
  name: rspec
58
44
  requirement: !ruby/object:Gem::Requirement
59
45
  requirements:
60
- - - '>'
46
+ - - ">"
61
47
  - !ruby/object:Gem::Version
62
48
  version: '2'
63
49
  type: :development
64
50
  prerelease: false
65
51
  version_requirements: !ruby/object:Gem::Requirement
66
52
  requirements:
67
- - - '>'
53
+ - - ">"
68
54
  - !ruby/object:Gem::Version
69
55
  version: '2'
70
56
  description: truncate_html helper that is html and html entities friendly
@@ -86,17 +72,17 @@ require_paths:
86
72
  - lib
87
73
  required_ruby_version: !ruby/object:Gem::Requirement
88
74
  requirements:
89
- - - '>='
75
+ - - ">="
90
76
  - !ruby/object:Gem::Version
91
77
  version: '0'
92
78
  required_rubygems_version: !ruby/object:Gem::Requirement
93
79
  requirements:
94
- - - '>='
80
+ - - ">="
95
81
  - !ruby/object:Gem::Version
96
82
  version: '0'
97
83
  requirements: []
98
84
  rubyforge_project:
99
- rubygems_version: 2.4.4
85
+ rubygems_version: 2.5.2
100
86
  signing_key:
101
87
  specification_version: 4
102
88
  summary: truncate_html helper that is html and html entities friendly