2015-12-15 14:51:16 +00:00
|
|
|
# Helper methods for Banzai filter specs
|
2015-04-03 00:46:43 +00:00
|
|
|
#
|
|
|
|
# Must be included into specs manually
|
2015-06-02 17:27:53 +00:00
|
|
|
module FilterSpecHelper
|
2015-04-03 00:46:43 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
# Perform `call` on the described class
|
|
|
|
#
|
2015-06-02 17:27:53 +00:00
|
|
|
# Automatically passes the current `project` value, if defined, to the context
|
|
|
|
# if none is provided.
|
2015-04-03 00:46:43 +00:00
|
|
|
#
|
2015-06-02 17:27:53 +00:00
|
|
|
# html - HTML String to pass to the filter's `call` method.
|
2015-12-15 14:51:16 +00:00
|
|
|
# context - Hash context for the filter. (default: {project: project})
|
2015-04-03 00:46:43 +00:00
|
|
|
#
|
2015-06-02 17:27:53 +00:00
|
|
|
# Returns a Nokogiri::XML::DocumentFragment
|
2015-12-15 14:51:16 +00:00
|
|
|
def filter(html, context = {})
|
2015-06-02 17:27:53 +00:00
|
|
|
if defined?(project)
|
2015-12-15 14:51:16 +00:00
|
|
|
context.reverse_merge!(project: project)
|
2015-06-02 17:27:53 +00:00
|
|
|
end
|
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
described_class.call(html, context)
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
2015-04-16 18:05:13 +00:00
|
|
|
|
2015-04-15 19:27:29 +00:00
|
|
|
# Run text through HTML::Pipeline with the current filter and return the
|
|
|
|
# result Hash
|
|
|
|
#
|
|
|
|
# body - String text to run through the pipeline
|
2015-12-15 14:51:16 +00:00
|
|
|
# context - Hash context for the filter. (default: {project: project})
|
2015-04-15 19:27:29 +00:00
|
|
|
#
|
2015-06-02 17:27:53 +00:00
|
|
|
# Returns the Hash
|
2015-12-15 14:51:16 +00:00
|
|
|
def pipeline_result(body, context = {})
|
|
|
|
context.reverse_merge!(project: project) if defined?(project)
|
2015-04-15 19:27:29 +00:00
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
pipeline = HTML::Pipeline.new([described_class], context)
|
2015-04-15 19:27:29 +00:00
|
|
|
pipeline.call(body)
|
|
|
|
end
|
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
def reference_pipeline(context = {})
|
|
|
|
context.reverse_merge!(project: project) if defined?(project)
|
2015-10-07 15:00:48 +00:00
|
|
|
|
2015-12-01 14:51:27 +00:00
|
|
|
filters = [
|
2015-12-15 14:51:16 +00:00
|
|
|
Banzai::Filter::AutolinkFilter,
|
2015-12-01 14:51:27 +00:00
|
|
|
described_class,
|
2015-12-15 14:51:16 +00:00
|
|
|
Banzai::Filter::ReferenceGathererFilter
|
2015-12-01 14:51:27 +00:00
|
|
|
]
|
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
HTML::Pipeline.new(filters, context)
|
2015-12-01 14:51:27 +00:00
|
|
|
end
|
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
def reference_pipeline_result(body, context = {})
|
|
|
|
reference_pipeline(context).call(body)
|
2015-12-01 14:51:27 +00:00
|
|
|
end
|
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
def reference_filter(html, context = {})
|
|
|
|
reference_pipeline(context).to_document(html)
|
2015-10-07 15:00:48 +00:00
|
|
|
end
|
|
|
|
|
2015-06-02 17:27:53 +00:00
|
|
|
# Modify a String reference to make it invalid
|
|
|
|
#
|
|
|
|
# Commit SHAs get reversed, IDs get incremented by 1, all other Strings get
|
|
|
|
# their word characters reversed.
|
|
|
|
#
|
|
|
|
# reference - String reference to modify
|
|
|
|
#
|
|
|
|
# Returns a String
|
|
|
|
def invalidate_reference(reference)
|
|
|
|
if reference =~ /\A(.+)?.\d+\z/
|
|
|
|
# Integer-based reference with optional project prefix
|
|
|
|
reference.gsub(/\d+\z/) { |i| i.to_i + 1 }
|
2016-01-30 00:49:07 +00:00
|
|
|
elsif reference =~ /\A(.+@)?(\h{7,40}\z)/
|
2015-06-02 17:27:53 +00:00
|
|
|
# SHA-based reference with optional prefix
|
2016-01-30 00:49:07 +00:00
|
|
|
reference.gsub(/\h{7,40}\z/) { |v| v.reverse }
|
2015-06-02 17:27:53 +00:00
|
|
|
else
|
|
|
|
reference.gsub(/\w+\z/) { |v| v.reverse }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Shortcut to Rails' auto-generated routes helpers, to avoid including the
|
|
|
|
# module
|
|
|
|
def urls
|
2016-03-24 16:00:26 +00:00
|
|
|
Gitlab::Routing.url_helpers
|
2015-06-02 17:27:53 +00:00
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|