Module: Kiosk::ContentTeaser
- Included in:
- WordPress::Post
- Defined in:
- lib/kiosk/content_teaser.rb
Instance Method Summary collapse
-
#teaser(horizon, options = {}) ⇒ Object
Returns a teaser of the content with roughly the given length.
Instance Method Details
#teaser(horizon, options = {}) ⇒ Object
Returns a teaser of the content with roughly the given length.
The content is parsed as an HTML fragment and only text nodes are considered to have length in this context. As a result, the returned string will not have a length matching the given horizon
exactly, but should render as such.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/kiosk/content_teaser.rb', line 12 def (horizon, = {}) if doc = content_document # Traverse all text nodes until we reach the limit length = 0 # Find the boundary node, where the text length crosses the horizon node = doc.xpath('descendant::text()').detect do |n| (length += n.content.length) >= horizon end if node # Truncate the content of the boundary text node node.content = node.content.truncate(node.content.length - (length - horizon), ) # Remove all following nodes from the document # Note that this mark-and-sweep method is due to the catch 22 of # relative DOM traversal (next, parent, etc.) and node removal. nodes_to_remove = [] nodes_to_remove << node while node = (node.next || (node.parent && node.parent.next)) nodes_to_remove.each { |n| n.remove if n.parent } end doc.to_html else "" end end |