Class: Thredded::ContentFormatter
- Inherits:
-
Object
- Object
- Thredded::ContentFormatter
- Defined in:
- lib/thredded/content_formatter.rb
Overview
Generates HTML from content source.
Class Attribute Summary collapse
-
.after_markup_filters ⇒ Object
Filters that run after processing the markup.
-
.after_sanitization_filters ⇒ Object
Filters that run after sanitization input: sanitized html, output: html.
-
.allowlist ⇒ Object
Sanitization allowlist options.
-
.before_markup_filters ⇒ Object
Filters that run before processing the markup.
-
.markup_filters ⇒ Object
Markup filters, such as BBCode, Markdown, Autolink, etc.
-
.sanitization_filters ⇒ Object
Filters that sanitize the resulting HTML.
Class Method Summary collapse
-
.pipeline_filters ⇒ Object
All the HTML::Pipeline filters, read-only.
-
.quote_content(content) ⇒ String
A quote containing the formatted content.
- .sanitization_filter_allowlist_config ⇒ Object
-
.sanitization_filter_uses_allowlist? ⇒ Boolean
TODO: v1.1: only allow html-pipeline >= 2.14.1 and drop this.
Instance Method Summary collapse
-
#format_content(content) ⇒ String
Formatted and sanitized html-safe content.
-
#initialize(view_context, pipeline_options = {}) ⇒ ContentFormatter
constructor
A new instance of ContentFormatter.
Constructor Details
#initialize(view_context, pipeline_options = {}) ⇒ ContentFormatter
Returns a new instance of ContentFormatter.
118 119 120 121 |
# File 'lib/thredded/content_formatter.rb', line 118 def initialize(view_context, = {}) @view_context = view_context @pipeline_options = end |
Class Attribute Details
.after_markup_filters ⇒ Object
Filters that run after processing the markup. input: html, output: html.
23 24 25 |
# File 'lib/thredded/content_formatter.rb', line 23 def after_markup_filters @after_markup_filters end |
.after_sanitization_filters ⇒ Object
Filters that run after sanitization input: sanitized html, output: html
31 32 33 |
# File 'lib/thredded/content_formatter.rb', line 31 def after_sanitization_filters @after_sanitization_filters end |
.allowlist ⇒ Object
Sanitization allowlist options.
8 9 10 |
# File 'lib/thredded/content_formatter.rb', line 8 def allowlist @allowlist end |
.before_markup_filters ⇒ Object
Filters that run before processing the markup. input: markup, output: markup.
15 16 17 |
# File 'lib/thredded/content_formatter.rb', line 15 def before_markup_filters @before_markup_filters end |
.markup_filters ⇒ Object
Markup filters, such as BBCode, Markdown, Autolink, etc. input: markup, output: html.
19 20 21 |
# File 'lib/thredded/content_formatter.rb', line 19 def markup_filters @markup_filters end |
.sanitization_filters ⇒ Object
Filters that sanitize the resulting HTML. input: html, output: sanitized html.
27 28 29 |
# File 'lib/thredded/content_formatter.rb', line 27 def sanitization_filters @sanitization_filters end |
Class Method Details
.pipeline_filters ⇒ Object
All the HTML::Pipeline filters, read-only.
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/thredded/content_formatter.rb', line 102 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.
135 136 137 138 139 140 141 142 |
# File 'lib/thredded/content_formatter.rb', line 135 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_config ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/thredded/content_formatter.rb', line 38 def sanitization_filter_allowlist_config if sanitization_filter_uses_allowlist? HTML::Pipeline::SanitizationFilter::ALLOWLIST else HTML::Pipeline::SanitizationFilter::WHITELIST end end |
.sanitization_filter_uses_allowlist? ⇒ Boolean
TODO: v1.1: only allow html-pipeline >= 2.14.1 and drop this
34 35 36 |
# File 'lib/thredded/content_formatter.rb', line 34 def sanitization_filter_uses_allowlist? defined?(HTML::Pipeline::SanitizationFilter::ALLOWLIST) end |
Instance Method Details
#format_content(content) ⇒ String
Returns formatted and sanitized html-safe content.
125 126 127 128 129 130 131 |
# File 'lib/thredded/content_formatter.rb', line 125 def format_content(content) pipeline = HTML::Pipeline.new(content_pipeline_filters, .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 |