2015-12-23 02:04:40 +05:30
|
|
|
module Banzai
|
|
|
|
module Filter
|
2015-09-11 14:41:01 +05:30
|
|
|
# Base class for GitLab Flavored Markdown reference filters.
|
|
|
|
#
|
|
|
|
# References within <pre>, <code>, <a>, and <style> elements are ignored.
|
|
|
|
#
|
|
|
|
# Context options:
|
|
|
|
# :project (required) - Current project, ignored if reference is cross-project.
|
|
|
|
# :only_path - Generate path-only links.
|
|
|
|
class ReferenceFilter < HTML::Pipeline::Filter
|
2018-03-17 18:26:18 +05:30
|
|
|
include RequestStoreReferenceCache
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
class << self
|
|
|
|
attr_accessor :reference_type
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a data attribute String to attach to a reference link
|
|
|
|
#
|
2015-10-24 18:46:33 +05:30
|
|
|
# attributes - Hash, where the key becomes the data attribute name and the
|
|
|
|
# value is the data attribute value
|
2015-09-11 14:41:01 +05:30
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
2015-10-24 18:46:33 +05:30
|
|
|
# data_attribute(project: 1, issue: 2)
|
2017-08-17 22:00:37 +05:30
|
|
|
# # => "data-reference-type=\"SomeReferenceFilter\" data-project=\"1\" data-issue=\"2\""
|
2015-10-24 18:46:33 +05:30
|
|
|
#
|
|
|
|
# data_attribute(project: 3, merge_request: 4)
|
2017-08-17 22:00:37 +05:30
|
|
|
# # => "data-reference-type=\"SomeReferenceFilter\" data-project=\"3\" data-merge-request=\"4\""
|
2015-09-11 14:41:01 +05:30
|
|
|
#
|
|
|
|
# Returns a String
|
2015-10-24 18:46:33 +05:30
|
|
|
def data_attribute(attributes = {})
|
2016-06-16 23:09:34 +05:30
|
|
|
attributes = attributes.reject { |_, v| v.nil? }
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
attributes[:reference_type] ||= self.class.reference_type
|
2017-09-10 17:25:29 +05:30
|
|
|
attributes[:container] ||= 'body'
|
|
|
|
attributes[:placement] ||= 'bottom'
|
2016-06-02 11:05:42 +05:30
|
|
|
attributes.delete(:original) if context[:no_original_data]
|
2017-08-17 22:00:37 +05:30
|
|
|
attributes.map do |key, value|
|
|
|
|
%Q(data-#{key.to_s.dasherize}="#{escape_once(value)}")
|
|
|
|
end.join(' ')
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def escape_once(html)
|
2016-01-14 18:37:52 +05:30
|
|
|
html.html_safe? ? html : ERB::Util.html_escape_once(html)
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def ignore_ancestor_query
|
|
|
|
@ignore_ancestor_query ||= begin
|
2015-09-11 14:41:01 +05:30
|
|
|
parents = %w(pre code a style)
|
|
|
|
parents << 'blockquote' if context[:ignore_blockquotes]
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
parents.map { |n| "ancestor::#{n}" }.join(' or ')
|
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
context[:project]
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def group
|
|
|
|
context[:group]
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def skip_project_check?
|
|
|
|
context[:skip_project_check]
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def reference_class(type)
|
2016-09-29 09:46:39 +05:30
|
|
|
"gfm gfm-#{type} has-tooltip"
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
# Ensure that a :project key exists in context
|
2015-09-11 14:41:01 +05:30
|
|
|
#
|
2016-06-02 11:05:42 +05:30
|
|
|
# Note that while the key might exist, its value could be nil!
|
|
|
|
def validate
|
|
|
|
needs :project
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
# Iterates over all <a> and text() nodes in a document.
|
2015-12-23 02:04:40 +05:30
|
|
|
#
|
2016-06-02 11:05:42 +05:30
|
|
|
# Nodes are skipped whenever their ancestor is one of the nodes returned
|
|
|
|
# by `ignore_ancestor_query`. Link tags are not processed if they have a
|
|
|
|
# "gfm" class or the "href" attribute is empty.
|
|
|
|
def each_node
|
2016-06-16 23:09:34 +05:30
|
|
|
return to_enum(__method__) unless block_given?
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
query = %Q{descendant-or-self::text()[not(#{ignore_ancestor_query})]
|
|
|
|
| descendant-or-self::a[
|
|
|
|
not(contains(concat(" ", @class, " "), " gfm ")) and not(@href = "")
|
|
|
|
]}
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
doc.xpath(query).each do |node|
|
|
|
|
yield node
|
|
|
|
end
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
# Returns an Array containing all HTML nodes.
|
|
|
|
def nodes
|
|
|
|
@nodes ||= each_node.to_a
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Yields the link's URL and inner HTML whenever the node is a valid <a> tag.
|
2016-06-02 11:05:42 +05:30
|
|
|
def yield_valid_link(node)
|
|
|
|
link = CGI.unescape(node.attr('href').to_s)
|
2017-08-17 22:00:37 +05:30
|
|
|
inner_html = node.inner_html
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
return unless link.force_encoding('UTF-8').valid_encoding?
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
yield link, inner_html
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def replace_text_when_pattern_matches(node, pattern)
|
|
|
|
return unless node.text =~ pattern
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
content = node.to_html
|
|
|
|
html = yield content
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
node.replace(html) unless content == html
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def replace_link_node_with_text(node, link)
|
|
|
|
html = yield
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
node.replace(html) unless html == node.text
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def replace_link_node_with_href(node, link)
|
|
|
|
html = yield
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
node.replace(html) unless html == link
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def text_node?(node)
|
|
|
|
node.is_a?(Nokogiri::XML::Text)
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def element_node?(node)
|
|
|
|
node.is_a?(Nokogiri::XML::Element)
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|