Class: HTML::Pipeline
- Inherits:
-
Object
- Object
- HTML::Pipeline
- Defined in:
- lib/html/pipeline.rb,
lib/html/pipeline/filter.rb,
lib/html/pipeline/version.rb,
lib/html/pipeline/toc_filter.rb,
lib/html/pipeline/camo_filter.rb,
lib/html/pipeline/text_filter.rb,
lib/html/pipeline/body_content.rb,
lib/html/pipeline/emoji_filter.rb,
lib/html/pipeline/https_filter.rb,
lib/html/pipeline/textile_filter.rb,
lib/html/pipeline/@mention_filter.rb,
lib/html/pipeline/autolink_filter.rb,
lib/html/pipeline/markdown_filter.rb,
lib/html/pipeline/email_reply_filter.rb,
lib/html/pipeline/sanitization_filter.rb,
lib/html/pipeline/image_max_width_filter.rb,
lib/html/pipeline/plain_text_input_filter.rb,
lib/html/pipeline/syntax_highlight_filter.rb
Overview
GitHub HTML processing filters and utilities. This module includes a small framework for defining DOM based content filters and applying them to user provided content.
See HTML::Pipeline::Filter for information on building filters.
Construct a Pipeline for running multiple HTML filters. A pipeline is created once with one to many filters, and it then can be ‘call`ed many times over the course of its lifetime with input.
filters - Array of Filter objects. Each must respond to call(doc,
context) and return the modified DocumentFragment or a
String containing HTML markup. Filters are performed in the
order provided.
default_context - The default context hash. Values specified here will be merged
into values from the each individual pipeline run. Can NOT be
nil. Default: empty Hash.
result_class - The default Class of the result object for individual
calls. Default: Hash. Protip: Pass in a Struct to get
some semblance of type safety.
Defined Under Namespace
Classes: AutolinkFilter, BodyContent, CamoFilter, EmailReplyFilter, EmojiFilter, Filter, HttpsFilter, ImageMaxWidthFilter, MarkdownFilter, MentionFilter, PlainTextInputFilter, SanitizationFilter, SyntaxHighlightFilter, TableOfContentsFilter, TextFilter, TextileFilter
Constant Summary collapse
- DocumentFragment =
Our DOM implementation.
Nokogiri::HTML::DocumentFragment
- VERSION =
"0.0.6"
Instance Attribute Summary collapse
-
#filters ⇒ Object
readonly
Public: Returns an Array of Filter objects for this Pipeline.
Class Method Summary collapse
-
.parse(document_or_html) ⇒ Object
Parse a String into a DocumentFragment object.
Instance Method Summary collapse
-
#call(html, context = {}, result = nil) ⇒ Object
Apply all filters in the pipeline to the given HTML.
-
#initialize(filters, default_context = {}, result_class = nil) ⇒ Pipeline
constructor
A new instance of Pipeline.
-
#to_document(input, context = {}, result = nil) ⇒ Object
Like call but guarantee the value returned is a DocumentFragment.
-
#to_html(input, context = {}, result = nil) ⇒ Object
Like call but guarantee the value returned is a string of HTML markup.
Constructor Details
#initialize(filters, default_context = {}, result_class = nil) ⇒ Pipeline
Returns a new instance of Pipeline.
63 64 65 66 67 68 |
# File 'lib/html/pipeline.rb', line 63 def initialize(filters, default_context = {}, result_class = nil) raise ArgumentError, "default_context cannot be nil" if default_context.nil? @filters = filters.flatten.freeze @default_context = default_context.freeze @result_class = result_class || Hash end |
Instance Attribute Details
#filters ⇒ Object (readonly)
Public: Returns an Array of Filter objects for this Pipeline.
61 62 63 |
# File 'lib/html/pipeline.rb', line 61 def filters @filters end |
Class Method Details
.parse(document_or_html) ⇒ Object
Parse a String into a DocumentFragment object. When a DocumentFragment is provided, return it verbatim.
51 52 53 54 55 56 57 58 |
# File 'lib/html/pipeline.rb', line 51 def self.parse(document_or_html) document_or_html ||= '' if document_or_html.is_a?(String) DocumentFragment.parse(document_or_html) else document_or_html end end |
Instance Method Details
#call(html, context = {}, result = nil) ⇒ Object
Apply all filters in the pipeline to the given HTML.
html - A String containing HTML or a DocumentFragment object. context - The context hash passed to each filter. See the Filter docs
for more info on possible values. This object MUST NOT be modified
in place by filters. Use the Result for passing state back.
result - The result Hash passed to each filter for modification. This
is where Filters store extracted information from the content.
Returns the result Hash after being filtered by this Pipeline. Contains an :output key with the DocumentFragment or String HTML markup based on the output of the last filter in the pipeline.
82 83 84 85 86 87 88 |
# File 'lib/html/pipeline.rb', line 82 def call(html, context = {}, result = nil) context = @default_context.merge(context) context = context.freeze result ||= @result_class.new result[:output] = @filters.inject(html) { |doc, filter| filter.call(doc, context, result) } result end |
#to_document(input, context = {}, result = nil) ⇒ Object
Like call but guarantee the value returned is a DocumentFragment. Pipelines may return a DocumentFragment or a String. Callers that need a DocumentFragment should use this method.
93 94 95 96 |
# File 'lib/html/pipeline.rb', line 93 def to_document(input, context = {}, result = nil) result = call(input, context, result) HTML::Pipeline.parse(result[:output]) end |
#to_html(input, context = {}, result = nil) ⇒ Object
Like call but guarantee the value returned is a string of HTML markup.
99 100 101 102 103 104 105 106 107 |
# File 'lib/html/pipeline.rb', line 99 def to_html(input, context = {}, result = nil) result = call(input, context, result = nil) output = result[:output] if output.respond_to?(:to_html) output.to_html else output.to_s end end |