Class: Thredded::ContentFormatter

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

Overview

Generates HTML from content source.

Class Attribute Summary collapse

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: {})


117
118
119
120
# File 'lib/thredded/content_formatter.rb', line 117

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

Class Attribute Details

.after_markup_filtersObject

Filters that run after processing the markup. input: html, output: html.



31
32
33
# File 'lib/thredded/content_formatter.rb', line 31

def after_markup_filters
  @after_markup_filters
end

.after_sanitization_filtersObject

Filters that run after sanitization input: sanitized html, output: html



39
40
41
# File 'lib/thredded/content_formatter.rb', line 39

def after_sanitization_filters
  @after_sanitization_filters
end

.allowlistObject

Sanitization allowlist options.



8
9
10
# File 'lib/thredded/content_formatter.rb', line 8

def allowlist
  @allowlist
end

.before_markup_filtersObject

Filters that run before processing the markup. input: markup, output: markup.



23
24
25
# File 'lib/thredded/content_formatter.rb', line 23

def before_markup_filters
  @before_markup_filters
end

.markup_filtersObject

Markup filters, such as BBCode, Markdown, Autolink, etc. input: markup, output: html.



27
28
29
# File 'lib/thredded/content_formatter.rb', line 27

def markup_filters
  @markup_filters
end

.sanitization_filtersObject

Filters that sanitize the resulting HTML. input: html, output: sanitized html.



35
36
37
# File 'lib/thredded/content_formatter.rb', line 35

def sanitization_filters
  @sanitization_filters
end

Class Method Details

.pipeline_filtersObject

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



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/thredded/content_formatter.rb', line 101

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



134
135
136
137
138
139
140
141
# File 'lib/thredded/content_formatter.rb', line 134

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

.sanitization_filter_allowlist_configObject



41
42
43
# File 'lib/thredded/content_formatter.rb', line 41

def sanitization_filter_allowlist_config
  HTML::Pipeline::SanitizationFilter::ALLOWLIST
end

.whitelistObject

Deprecated.

use allowlist

TODO: v2.0: drop alias and just use allowlist



12
13
14
15
16
17
18
19
# File 'lib/thredded/content_formatter.rb', line 12

def whitelist
  ActiveSupport::Deprecation.warn(<<~MESSAGE.squish)
    ContentFormatter.whitelist is deprecated and will be removed in Thredded 2.0.
    Use ContentFormatter.allowlist instead
  MESSAGE

  allowlist
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.



124
125
126
127
128
129
130
# File 'lib/thredded/content_formatter.rb', line 124

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