Module: Models::Utilities::Html

Extended by:
ActiveSupport::Concern
Included in:
Article, Item, ProductLine
Defined in:
app/concerns/models/utilities/html.rb

Overview

Provides HTML cleaning and conversion utilities for models.
Included by: Item, Article, ProductLine

Instance Method Summary collapse

Instance Method Details

#clean_and_compress_html(html) ⇒ Object



12
13
14
15
16
17
18
# File 'app/concerns/models/utilities/html.rb', line 12

def clean_and_compress_html(html)
  # Parse the HTML
  cleaned_html = html.gsub(/[\r\n]/, "").gsub(/[[:space:]]+/, " ")
  fragment = Loofah.fragment(cleaned_html)
  fragment.scrub!(:prune)
  cleaned_html = fragment.to_s.strip
end

#html_to_text(html, remove_empty_lines: true) ⇒ Object

Converts an html to text and remove empty lines



21
22
23
24
25
26
27
28
29
# File 'app/concerns/models/utilities/html.rb', line 21

def html_to_text(html, remove_empty_lines: true)
  return unless html.present?

  s = ActionText::Content.new(html).to_plain_text
  if remove_empty_lines
    s = s.lines.map(&:strip).reject(&:empty?).join("\n")
  end
  s
end