lib/html/pipeline.rb in html-pipeline-2.7.1 vs lib/html/pipeline.rb in html-pipeline-2.7.2

- old
+ new

@@ -1,7 +1,7 @@ -require "nokogiri" -require "active_support/xml_mini/nokogiri" # convert Documents to hashes +require 'nokogiri' +require 'active_support/xml_mini/nokogiri' # convert Documents to hashes module HTML # 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. @@ -41,16 +41,16 @@ autoload :SyntaxHighlightFilter, 'html/pipeline/syntax_highlight_filter' autoload :TextileFilter, 'html/pipeline/textile_filter' autoload :TableOfContentsFilter, 'html/pipeline/toc_filter' autoload :TextFilter, 'html/pipeline/text_filter' - class MissingDependencyError < LoadError; end + class MissingDependencyError < RuntimeError; end def self.require_dependency(name, requirer) require name rescue LoadError => e raise MissingDependencyError, - "Missing dependency '#{name}' for #{requirer}. See README.md for details.\n#{e.class.name}: #{e}" + "Missing dependency '#{name}' for #{requirer}. See README.md for details.\n#{e.class.name}: #{e}" end # Our DOM implementation. DocumentFragment = Nokogiri::HTML::DocumentFragment @@ -83,11 +83,11 @@ # Public: Default instrumentation service for new pipeline objects. attr_accessor :default_instrumentation_service end def initialize(filters, default_context = {}, result_class = nil) - raise ArgumentError, "default_context cannot be nil" if default_context.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 @instrumentation_service = self.class.default_instrumentation_service end @@ -106,13 +106,13 @@ # output of the last filter in the pipeline. def call(html, context = {}, result = nil) context = @default_context.merge(context) context = context.freeze result ||= @result_class.new - payload = default_payload :filters => @filters.map(&:name), - :context => context, :result => result - instrument "call_pipeline.html_pipeline", payload do + payload = default_payload filters: @filters.map(&:name), + context: context, result: result + instrument 'call_pipeline.html_pipeline', payload do result[:output] = @filters.inject(html) do |doc, filter| perform_filter(filter, doc, context, result) end end @@ -123,13 +123,13 @@ # # The filter is instrumented. # # Returns the result of the filter. def perform_filter(filter, doc, context, result) - payload = default_payload :filter => filter.name, - :context => context, :result => result - instrument "call_filter.html_pipeline", payload do + payload = default_payload filter: filter.name, + context: context, result: result + instrument 'call_filter.html_pipeline', payload do filter.call(doc, context, result) end end # Like call but guarantee the value returned is a DocumentFragment. @@ -176,17 +176,17 @@ # # Accepts a Hash of additional payload data to be merged. # # Returns a Hash. def default_payload(payload = {}) - {:pipeline => instrumentation_name}.merge(payload) + { pipeline: instrumentation_name }.merge(payload) end end end # XXX nokogiri monkey patches for 1.8 -if not ''.respond_to?(:force_encoding) +unless ''.respond_to?(:force_encoding) class Nokogiri::XML::Node # Work around an issue with utf-8 encoded data being erroneously converted to # ... some other shit when replacing text nodes. See 'utf-8 output 2' in # user_content_test.rb for details. def replace_with_encoding_fix(replacement) @@ -194,11 +194,11 @@ replacement = document.fragment("<div>#{replacement}</div>").children.first.children end replace_without_encoding_fix(replacement) end - alias_method :replace_without_encoding_fix, :replace - alias_method :replace, :replace_with_encoding_fix + alias replace_without_encoding_fix replace + alias replace replace_with_encoding_fix def swap(replacement) replace(replacement) self end