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.
-
.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.
-
.whitelist ⇒ Object
Sanitization whitelist options.
Class Method Summary collapse
-
.pipeline_filters ⇒ Object
All the HTML::Pipeline filters, read-only.
-
.quote_content(content) ⇒ String
A quote containing the formatted content.
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.
102 103 104 105 |
# File 'lib/thredded/content_formatter.rb', line 102 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.
20 21 22 |
# File 'lib/thredded/content_formatter.rb', line 20 def after_markup_filters @after_markup_filters end |
.after_sanitization_filters ⇒ Object
Filters that run after sanitization input: sanitized html, output: html
28 29 30 |
# File 'lib/thredded/content_formatter.rb', line 28 def after_sanitization_filters @after_sanitization_filters end |
.before_markup_filters ⇒ Object
Filters that run before processing the markup. input: markup, output: markup.
12 13 14 |
# File 'lib/thredded/content_formatter.rb', line 12 def before_markup_filters @before_markup_filters end |
.markup_filters ⇒ Object
Markup filters, such as BBCode, Markdown, Autolink, etc. input: markup, output: html.
16 17 18 |
# File 'lib/thredded/content_formatter.rb', line 16 def markup_filters @markup_filters end |
.sanitization_filters ⇒ Object
Filters that sanitize the resulting HTML. input: html, output: sanitized html.
24 25 26 |
# File 'lib/thredded/content_formatter.rb', line 24 def sanitization_filters @sanitization_filters end |
.whitelist ⇒ Object
Sanitization whitelist options.
8 9 10 |
# File 'lib/thredded/content_formatter.rb', line 8 def whitelist @whitelist end |
Class Method Details
.pipeline_filters ⇒ Object
All the HTML::Pipeline filters, read-only.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/thredded/content_formatter.rb', line 86 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.
119 120 121 122 123 124 125 126 |
# File 'lib/thredded/content_formatter.rb', line 119 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.
109 110 111 112 113 114 115 |
# File 'lib/thredded/content_formatter.rb', line 109 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 |