html_slicer 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -228,6 +228,7 @@ Controller:
228
228
  @article_paged = @article.paged.slice!(params[:slice])
229
229
 
230
230
  On a View just call the +slice+ helper:
231
+
231
232
  <%= slice @article_paged %>
232
233
 
233
234
  This will render several <tt>?slice=N</tt> slicing links surrounded by an HTML5 <+nav+> tag.
@@ -353,6 +354,24 @@ In fact, caching ActiveModel not so significant in most cases, but still works.
353
354
 
354
355
  == More
355
356
 
357
+ === Page breaks with links!
358
+
359
+ Passing the option <tt>:text_break => "..."</tt> is for mainly decorative purpose. Within the slice (page), the final textual content is complemented by ellipsis or any other symbol you like.
360
+ If we'd like to place the URL link at the end, we can pass static link as well ("Read mode" or something...), but what if we'd like to pass dynamic link generated within the +view+ using the full power of ActionView and +@template+?
361
+
362
+ So, we have:
363
+
364
+ @article = Article.find(1)
365
+ @article_paged = @article.paged.slice!(params[:slice])
366
+
367
+ On a View we call <tt>.to_s()</tt> method and pass it the block:
368
+
369
+ <%= @article_paged.to_s {|slicer, text| text << link_to("Read more", url_for(url), :class => "read-more")} %>
370
+
371
+ where:
372
+ * <tt>slicer</tt> is a slicer object itself.
373
+ * <tt>text</tt> is a final textual content of the current slice (page) which we complete with the generated link inside the block code.
374
+
356
375
  === Slicing a general String or ActiveModel (or any other) object
357
376
 
358
377
  There is no apecific approach to it. Just extend target class to HtmlSlicer::Installer and call the method slice as described before:
data/html_slicer.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.email = ["addagger@gmail.com"]
12
12
  s.homepage = %q{http://vkvon.ru/projects/html_slicer}
13
13
  s.summary = %q{HTML text pagination for Ruby on Rails}
14
- s.description = %q{HtmlSlicer is a smart way to cut an HTML text into pieces. It also provides on option to resize "width"/"height" attributes of HTML tags, such as <iframe>, <object>, <img> or any other.}
14
+ s.description = %q{HTML truncation & pagination for Rails 3. It also provides on option to resize "width"/"height" HTML tags (also as CSS elements in "style="), such as <iframe>, <object>, <img> or any other.}
15
15
 
16
16
  s.add_development_dependency "actionpack", ['>= 3.0.0']
17
17
  s.add_development_dependency "activesupport", ['>= 3.0.0']
@@ -6,10 +6,11 @@ module HtmlSlicer
6
6
  # During the runtime object is used as an +maps+ accessor too.
7
7
 
8
8
  class CachedStuff
9
- attr_reader :hexdigest, :resizing, :slicing
9
+ attr_reader :version, :hexdigest, :resizing, :slicing
10
10
  attr_accessor :changed, :time
11
11
 
12
12
  def initialize(text = nil)
13
+ @version = HtmlSlicer::VERSION
13
14
  @changed = false
14
15
  self.hexdigest_for = text if text
15
16
  end
@@ -46,7 +46,13 @@ module HtmlSlicer
46
46
  @cached_stuff ||=
47
47
  begin
48
48
  if options[:cache_to] # Getting recorded hash dump
49
- Marshal.load(Base64.decode64(@env.send(options[:cache_to])))
49
+ Marshal.load(Base64.decode64(@env.send(options[:cache_to]))).tap do |cached_stuff|
50
+ if cached_stuff.time < Date.new(2012,7,25)
51
+ #### CACHE OUT OF DATE ####
52
+ warn "WARNING: html_slicer's cached stuff for #{@env.class.name} records has become unacceptable because of code changes. Update each record again. Visit http://vkvon.ru/projects/html_slicer for further details."
53
+ raise Exception
54
+ end
55
+ end
50
56
  else
51
57
  raise Exception
52
58
  end
@@ -97,13 +103,9 @@ module HtmlSlicer
97
103
  self
98
104
  end
99
105
 
100
- def to_s
106
+ def to_s(&block)
101
107
  load!
102
- view(document.root, @current_slice)
103
- end
104
-
105
- def inspect
106
- to_s
108
+ view(document.root, @current_slice, &block)
107
109
  end
108
110
 
109
111
  def method_missing(*args, &block)
@@ -123,16 +125,16 @@ module HtmlSlicer
123
125
  # Return the current slice is a last or not?
124
126
  def last_slice?
125
127
  current_slice == slice_number
126
- end
128
+ end
127
129
 
128
130
  private
129
131
 
130
132
  # Return a textual representation of the node including all children.
131
- def view(node, slice)
133
+ def view(node, slice, &block)
132
134
  slice = slice.to_i
133
135
  case node
134
136
  when HTML::Tag then
135
- children_view = node.children.collect {|child| view(child, slice)}.compact.join
137
+ children_view = node.children.collect {|child| view(child, slice, &block)}.compact.join
136
138
  if resized?
137
139
  resizing.resize_node(node)
138
140
  end
@@ -159,7 +161,14 @@ module HtmlSlicer
159
161
  when HTML::Text then
160
162
  if sliced?
161
163
  if range = slicing.map.get(node, slice)
162
- "#{node.content[Range.new(*range)]}#{slicing.options.text_break unless range.last == -1 || !slicing.map.get(node, slice+1)}"
164
+ (range.is_a?(Array) ? node.content[Range.new(*range)] : node.content).tap do |export|
165
+ unless range == true || (range.is_a?(Array) && range.last == -1) # broken text
166
+ export << slicing.options.text_break if slicing.options.text_break
167
+ if block_given?
168
+ yield self, export
169
+ end
170
+ end
171
+ end
163
172
  end
164
173
  else
165
174
  node.to_s
@@ -167,7 +176,7 @@ module HtmlSlicer
167
176
  when HTML::CDATA then
168
177
  node.to_s
169
178
  when HTML::Node then
170
- node.children.collect {|child| view(child, slice)}.compact.join
179
+ node.children.collect {|child| view(child, slice, &block)}.compact.join
171
180
  end
172
181
  end
173
182
 
@@ -8,6 +8,7 @@ module HtmlSlicer
8
8
  include HtmlSlicer::Utilities::NodeIdent
9
9
 
10
10
  def commit(node, number, value)
11
+ value = true if value == [0, -1]
11
12
  self[node_identify(node)] ||= {}
12
13
  self[node_identify(node)].merge!(number => value)
13
14
  end
@@ -33,37 +34,33 @@ module HtmlSlicer
33
34
  def process_by_text!(root)
34
35
  units_count = 0
35
36
  parse(root) do |node|
36
- if node.is_a?(HTML::Text)
37
- if sliceable?(node)
38
- sanitize_content!(node)
39
- content = node.to_s
40
- begin
41
- start_index = 0
42
- last_index = 0
43
- content.scan(@options.unit) do
44
- if $~.begin(0) >= start_index
45
- units_count += 1
46
- index = $~.end(0)
47
- if units_count == @options.maximum
48
- units_count = 0
49
- if complete_regexp = @options.complete
50
- index = content.match(complete_regexp, index).try(:begin, 0)||index
51
- start_index = index
52
- end
53
- @map.commit(node, @slice_number, [last_index, index-1])
54
- last_index = index
55
- limited? ? raise(Exception) : @slice_number += 1
37
+ if node.is_a?(HTML::Text) && sliceable?(node)
38
+ sanitize_content!(node)
39
+ content = node.to_s
40
+ begin
41
+ start_index = 0
42
+ last_index = 0
43
+ content.scan(@options.unit) do
44
+ if $~.begin(0) >= start_index
45
+ units_count += 1
46
+ index = $~.end(0)
47
+ if units_count == @options.maximum
48
+ units_count = 0
49
+ if complete_regexp = @options.complete
50
+ index = content.match(complete_regexp, index).try(:begin, 0)||index
51
+ start_index = index
56
52
  end
53
+ @map.commit(node, @slice_number, [last_index, index-1])
54
+ last_index = index
55
+ limited? ? raise(Exception) : @slice_number += 1
57
56
  end
58
57
  end
59
- if units_count > 0
60
- @map.commit(node, @slice_number, [last_index, -1])
61
- end
62
- rescue Exception
63
- break
64
58
  end
65
- else
66
- @map.commit(node, @slice_number, [0, -1])
59
+ if units_count > 0
60
+ @map.commit(node, @slice_number, [last_index, -1])
61
+ end
62
+ rescue Exception
63
+ break
67
64
  end
68
65
  else
69
66
  @map.commit(node, @slice_number, true)
@@ -74,11 +71,7 @@ module HtmlSlicer
74
71
  def process_by_node!(root)
75
72
  units_count = 0
76
73
  parse(root) do |node|
77
- if node.is_a?(HTML::Text)
78
- @map.commit(node, @slice_number, [0, -1])
79
- else
80
- @map.commit(node, @slice_number, true)
81
- end
74
+ @map.commit(node, @slice_number, true)
82
75
  if node.match(@options.unit) && sliceable?(node)
83
76
  units_count += 1
84
77
  if units_count == @options.maximum
@@ -1,3 +1,3 @@
1
1
  module HtmlSlicer
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_slicer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-16 00:00:00.000000000 Z
12
+ date: 2012-07-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
@@ -43,9 +43,9 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: 3.0.0
46
- description: HtmlSlicer is a smart way to cut an HTML text into pieces. It also provides
47
- on option to resize "width"/"height" attributes of HTML tags, such as <iframe>,
48
- <object>, <img> or any other.
46
+ description: HTML truncation & pagination for Rails 3. It also provides on option
47
+ to resize "width"/"height" HTML tags (also as CSS elements in "style="), such as
48
+ <iframe>, <object>, <img> or any other.
49
49
  email:
50
50
  - addagger@gmail.com
51
51
  executables: []