Class: Decidim::HtmlTruncation
- Inherits:
-
Object
- Object
- Decidim::HtmlTruncation
- Includes:
- SanitizeHelper
- Defined in:
- decidim-core/app/services/decidim/html_truncation.rb
Instance Method Summary collapse
-
#initialize(text, options = {}) ⇒ HtmlTruncation
constructor
Truncates html content text - Content to be truncated options - Hash with the options max_length: An Integer maximum number of characters tail: Suffix to add after truncation count_tags: A boolean which tells if html tags should be calculated to max length, otherwise just content count_tail: A boolean value that determines whether max_length contains the tail tail_before_final_tag: A boolean, add tail before final tag if true, otherwise add tail where content is cut.
-
#perform ⇒ Object
Perform truncation to the html content (text) added in constructor Returns truncated String.
Methods included from SanitizeHelper
#decidim_escape_translated, #decidim_html_escape, #decidim_rich_text, #decidim_sanitize, #decidim_sanitize_admin, #decidim_sanitize_editor, #decidim_sanitize_editor_admin, #decidim_sanitize_newsletter, #decidim_sanitize_translated, #decidim_url_escape, included
Constructor Details
#initialize(text, options = {}) ⇒ HtmlTruncation
Truncates html content text - Content to be truncated options - Hash with the options
max_length: An Integer maximum number of characters
tail: Suffix to add after truncation
count_tags: A boolean which tells if html tags should be calculated to max length, otherwise just content
count_tail: A boolean value that determines whether max_length contains the tail
tail_before_final_tag: A boolean, add tail before final tag if true, otherwise add tail where content is cut.
15 16 17 18 19 20 21 22 23 24 |
# File 'decidim-core/app/services/decidim/html_truncation.rb', line 15 def initialize(text, = {}) @options = { max_length: [:max_length] || 30, tail: [:tail] || "...", count_tags: [:count_tags] || false, count_tail: [:count_tail] || false, tail_before_final_tag: [:tail_before_final_tag] || false } @text = text end |
Instance Method Details
#perform ⇒ Object
Perform truncation to the html content (text) added in constructor Returns truncated String
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'decidim-core/app/services/decidim/html_truncation.rb', line 28 def perform @document = Nokogiri::HTML::DocumentFragment.parse(@text) @tail_added = false @remaining = initial_remaining @cut = false cut_children(document, [:count_tags]) add_tail(document) if @remaining.negative? && !@tail_added escape_html_from_content(document) # Nokogiri's to_html escapes " to " and we do not want extra & so we have to unescape. CGI.unescape_html(document.to_html).gsub("\n", "") end |