Class: Thredded::ContentFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/thredded/content_formatter.rb

Overview

Generates HTML from content source.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_context, pipeline_options = {}) ⇒ ContentFormatter

Returns a new instance of ContentFormatter.

Parameters:

  • view_context (Object)

    the context of the rendering view.

  • pipeline_options (Hash) (defaults to: {})


88
89
90
91
# File 'lib/thredded/content_formatter.rb', line 88

def initialize(view_context, pipeline_options = {})
  @view_context = view_context
  @pipeline_options = pipeline_options
end

Class Method Details

.pipeline_filtersObject

All the HTML::Pipeline filters, read-only.



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/thredded/content_formatter.rb', line 72

def self.pipeline_filters
  filters = [
    *before_markup_filters,
    *markup_filters,
    *after_markup_filters,
    *sanitization_filters,
    *after_sanitization_filters
  ]
  # Changing the result in-place has no effect on the ContentFormatter output,
  # and is most likely the result of a programmer error.
  # Freeze the array so that in-place changes raise an error.
  filters.freeze
end

.quote_content(content) ⇒ String

Returns a quote containing the formatted content.

Parameters:

  • content (String)

Returns:

  • (String)

    a quote containing the formatted content



105
106
107
108
109
110
111
112
# File 'lib/thredded/content_formatter.rb', line 105

def self.quote_content(content)
  result = String.new(content)
  result.gsub!(/^(?!$)/, '> ')
  result.gsub!(/^$/, '>')
  result << "\n" unless result.end_with?("\n")
  result << "\n"
  result
end

Instance Method Details

#format_content(content) ⇒ String

Returns formatted and sanitized html-safe content.

Parameters:

  • content (String)

Returns:

  • (String)

    formatted and sanitized html-safe content.



95
96
97
98
99
100
101
# File 'lib/thredded/content_formatter.rb', line 95

def format_content(content)
  pipeline = HTML::Pipeline.new(content_pipeline_filters, content_pipeline_options.deep_merge(@pipeline_options))
  result = pipeline.call(content, view_context: @view_context)
  # rubocop:disable Rails/OutputSafety
  result[:output].to_s.html_safe
  # rubocop:enable Rails/OutputSafety
end