effective_posts 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3db4764899f69a68c93c3f9b69be51473a176ef1
4
- data.tar.gz: 90877459ce8fb72ffceba8470d3743a86777e89c
3
+ metadata.gz: 07e96e05a9ae8a0c782867865c4b12641fc4f178
4
+ data.tar.gz: 680ad65a0e4e13be0ea6aff3a4e67145cba761fc
5
5
  SHA512:
6
- metadata.gz: 32483bded255aeb0cd7c91320fc56e8ead284b3f9cd321d2f5d78c0ca4f3c2b5affc4402a94e5fe4bc9223eb0922fac17da2102d6fd58fc9545f2d20d97c5211
7
- data.tar.gz: 98e933b31798a9c6a3127cb48e2a4089998ae74c1d2fe64757688598f50ed9b381d6c3c641720135899de529175c070205a1e78a015754d2f62ffb55863ee01a
6
+ metadata.gz: d3d2fbdbbcd54ff20a16f66bb308764a97f532bea475000638b5866470775bca8d390de9eeb1b4b344d398ac3de9aa1339052e0a201b2b919086e2ca654b9752
7
+ data.tar.gz: 535787d615ac93871d370ef2323dfacbcdb634f629cdf0e635ae579c5659e01f639013915076f2bbaf63d0dea56fae89366bf9f21d933529495d00562021a98c
@@ -83,6 +83,15 @@ module Admin
83
83
  redirect_to effective_posts.admin_posts_path
84
84
  end
85
85
 
86
+ def excerpts
87
+ @page_title = 'Post Excerpts'
88
+
89
+ EffectivePosts.authorized?(self, :index, Effective::Post)
90
+
91
+ @posts = Effective::Post.includes(:regions)
92
+ end
93
+
94
+
86
95
  private
87
96
 
88
97
  def post_params
@@ -13,23 +13,38 @@ module EffectivePostsHelper
13
13
  end
14
14
 
15
15
  # Only supported options are:
16
- # :length => 200 to set the max length of the content if the ReadMore divider is not present
17
- # :label => 'Read Lots More' to set the label of the 'Read More' link
18
- def post_excerpt(post, options = {})
16
+ # :label => 'Read more' to set the label of the 'Read More' link
17
+ # :omission => '...' passed to the final text node's truncate
18
+ # :length => 200 to set the max inner_text length of the content
19
+ # All other options are passed to the link_to 'Read more'
20
+ def post_excerpt(post, opts = {})
19
21
  content = effective_region(post, :content, :editable => false) { '<p>Default content</p>'.html_safe }
20
22
 
23
+ options = {
24
+ :label => 'Read more',
25
+ :omission => '...',
26
+ :length => 200
27
+ }.merge(opts)
28
+
21
29
  divider = content.index(Effective::Snippets::ReadMoreDivider::TOKEN)
22
30
  length = options.delete(:length)
31
+ omission = options.delete(:omission)
23
32
 
24
33
  if divider.present?
25
34
  content[0...divider] + readmore_link(post, options)
26
- elsif length.present? && content.length > length
27
- truncate_html(content, length, readmore_link(post, options))
35
+ elsif length.present?
36
+ truncate_html(content, length, omission) + readmore_link(post, options)
28
37
  else
29
38
  content
30
39
  end.html_safe
31
40
  end
32
41
 
42
+ def readmore_link(post, options)
43
+ content_tag(:p, class: 'post-read-more') do
44
+ link_to((options.delete(:label) || 'Read more'), effective_posts.post_path(post), options)
45
+ end
46
+ end
47
+
33
48
  def link_to_post_category(category, options = {})
34
49
  category = category.to_s.downcase
35
50
 
@@ -42,9 +57,4 @@ module EffectivePostsHelper
42
57
  EffectivePosts.use_category_routes ? "/#{category}/#{post.to_param}" : effective_posts.post_path(post, category: category.to_s)
43
58
  end
44
59
 
45
- def readmore_link(post, options)
46
- content_tag(:p, class: 'post-read-more') do
47
- link_to((options.delete(:label) || 'Read more'), effective_posts.post_path(post), options)
48
- end
49
- end
50
60
  end
@@ -10,36 +10,41 @@ module EffectiveTruncateHtmlHelper
10
10
  doc.inner_html.html_safe
11
11
  end
12
12
 
13
- def truncate_html(text, max = 200, read_more = '', ellipsis = '...')
14
- doc = Nokogiri::HTML::DocumentFragment.parse(text)
15
- length = doc.inner_text.length
16
-
17
- while length > max
18
- element = doc
19
- element = element.last_element_child while element.last_element_child.present?
20
- # element is now the last nested element (i.e. an HTML node, NOT a text node) in the HTML, or doc itself if doc has no elements (text node)
13
+ # Truncates HTML or text to a certain inner_text character limit.
14
+ #
15
+ # If given HTML, the underlying markup may be much longer than length, but the displayed text
16
+ # will be no longer than (length + omission) characters.
17
+ def truncate_html(text, length = 200, omission = '...')
18
+ Nokogiri::HTML::DocumentFragment.parse(text).tap { |doc| _truncate_node(doc, length, omission) }.inner_html
19
+ end
21
20
 
22
- if (length - element.inner_text.length) > max # If deleting this entire node means we're still over max, do it
23
- element.remove
24
- else # If we truncate this node, we'll be under max
25
- if element.name == 'a'
26
- element.remove # I don't want to cut a link in half
27
- elsif element.children.length == 1 # There must be a text node here. Can there be more than 1 text node?
28
- textNode = element.children.first
29
- textNode.content = truncate(textNode.content, length: (max - length), separator: ' ', omission: ellipsis)
30
- break # Break out of our while loop, as our ellipsis might invalidate our looping condition
31
- else # Unexpected, so just remove the whole thing
32
- Rails.logger.info "effective_posts, Unexpected number of last_element_child children"
33
- element.remove
34
- end
21
+ def _truncate_node(node, length, omission)
22
+ if node.inner_text.length <= length
23
+ # Do nothing, we're already reached base case
24
+ elsif node.name == 'a'
25
+ node.remove # I don't want to truncate anything in a link
26
+ elsif node.children.blank?
27
+ # I need to truncate myself, and I'm certainly a text node
28
+ if node.text?
29
+ node.content = truncate(node.content, length: length, separator: ' ', omission: omission)
30
+ else
31
+ Rails.logger.info '[WARNING] effective_posts: unexpected node in children.blank? recursive condition'
32
+ node.remove
33
+ end
34
+ else # Go through all the children, and delete anything after the length has been reached
35
+ child_length = 0
36
+ node.children.each do |child|
37
+ child_length > length ? (child.remove) : (child_length += child.inner_text.length)
35
38
  end
36
39
 
37
- length = doc.inner_text.length
38
- end
40
+ # We have now removed all nodes after length, but the last node is longer than our length
41
+ # child_length is the inner_text length of all included nodes
42
+ # And we only have to truncate the last child to get under length
39
43
 
40
- # Clean up any empty tags
41
- doc.last_element_child.remove while doc.last_element_child.try(:inner_html) == ''
44
+ child = node.children.last
45
+ child_max_length = length - (child_length - child.inner_text.length)
42
46
 
43
- (doc.inner_html + read_more.to_s).html_safe
47
+ _truncate_node(child, child_max_length, omission)
48
+ end
44
49
  end
45
50
  end
@@ -0,0 +1,19 @@
1
+ %h2= @page_title
2
+
3
+ - @posts.find_each do |post|
4
+ .row
5
+ .col-sm-12
6
+ %h2.post-title
7
+ %a{:href => effective_post_path(post)}
8
+ = simple_effective_region post, :title do
9
+ = post.title
10
+ .row
11
+ .col-sm-6
12
+ %pre= effective_region(post, :content, :editable => false)
13
+
14
+ .col-sm-6
15
+ %pre= post_excerpt(post)
16
+
17
+ .row
18
+ .col-sm-12
19
+ %hr
data/config/routes.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  EffectivePosts::Engine.routes.draw do
2
2
  namespace :admin do
3
3
  resources :posts, :except => [:show]
4
+ match 'posts/excerpts', :to => 'posts#excerpts', :via => [:get]
4
5
  end
5
6
 
6
7
  scope :module => 'effective' do
@@ -1,3 +1,3 @@
1
1
  module EffectivePosts
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_posts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-18 00:00:00.000000000 Z
11
+ date: 2015-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -176,6 +176,7 @@ files:
176
176
  - app/views/admin/posts/_actions.html.haml
177
177
  - app/views/admin/posts/_form.html.haml
178
178
  - app/views/admin/posts/edit.html.haml
179
+ - app/views/admin/posts/excerpts.html.haml
179
180
  - app/views/admin/posts/index.html.haml
180
181
  - app/views/admin/posts/new.html.haml
181
182
  - app/views/effective/posts/_post.html.haml