Module: NokogiriTruncateHtml::TruncateHtmlHelper
- Defined in:
- lib/nokogiri_truncate_html/truncate_html_helper.rb
Instance Method Summary collapse
-
#truncate_html(input, *args) ⇒ Object
Truncates html respecting tags and html entities.
Instance Method Details
#truncate_html(input, *args) ⇒ Object
Truncates html respecting tags and html entities.
The API is the same as ActionView::Helpers::TextHelper#truncate. It uses Nokogiri and HtmlEntities for entity awareness.
Examples:
truncate_html '<p>Hello <strong>World</strong></p>', :length => 7 # => '<p>Hello <strong>W…</strong></p>'
truncate_html '<p>Hello & Goodbye</p>', :length => 7 # => '<p>Hello &…</p>'
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/nokogiri_truncate_html/truncate_html_helper.rb', line 14 def truncate_html(input, *args) document = truncate_html_document parser = truncate_html_parser # support both 2.2 & earlier APIs = args. length = [:length] || args[0] || 30 omission = [:omission] || args[1] || '…' # Adding div around the input is a hack. It gets removed in TruncateDocument. input = "<div>#{input}</div>" document.length = length document.omission = omission catch(:truncate_finished) do parser.parse_memory(input) end document.output.html_safe end |