2015-12-24 13:43:07 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
|
|
|
# HTML filter that replaces milestone references with links.
|
|
|
|
class MilestoneReferenceFilter < AbstractReferenceFilter
|
|
|
|
def self.object_class
|
|
|
|
Milestone
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_object(project, id)
|
2016-04-01 00:54:00 +00:00
|
|
|
project.milestones.find_by(iid: id)
|
2015-12-24 13:43:07 +00:00
|
|
|
end
|
|
|
|
|
2016-03-31 02:12:34 +00:00
|
|
|
def references_in(text, pattern = Milestone.reference_pattern)
|
|
|
|
text.gsub(pattern) do |match|
|
|
|
|
project = project_from_ref($~[:project])
|
2016-04-01 00:54:00 +00:00
|
|
|
params = milestone_params($~[:milestone_iid].to_i, $~[:milestone_name])
|
2016-03-31 02:12:34 +00:00
|
|
|
milestone = project.milestones.find_by(params)
|
|
|
|
|
|
|
|
if milestone
|
2016-04-01 00:54:00 +00:00
|
|
|
yield match, milestone.iid, $~[:project], $~
|
2016-03-31 02:12:34 +00:00
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_for_object(milestone, project)
|
2016-03-24 16:00:26 +00:00
|
|
|
h = Gitlab::Routing.url_helpers
|
2015-12-24 13:43:07 +00:00
|
|
|
h.namespace_project_milestone_url(project.namespace, project, milestone,
|
|
|
|
only_path: context[:only_path])
|
|
|
|
end
|
2016-03-31 02:12:34 +00:00
|
|
|
|
2016-04-01 00:54:00 +00:00
|
|
|
def object_link_text(object, matches)
|
|
|
|
if context[:project] == object.project
|
|
|
|
super
|
|
|
|
else
|
|
|
|
"#{super} <i>in #{escape_once(object.project.name_with_namespace)}</i>".
|
|
|
|
html_safe
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def milestone_params(iid, name)
|
2016-03-31 02:12:34 +00:00
|
|
|
if name
|
|
|
|
{ name: name.tr('"', '') }
|
|
|
|
else
|
2016-04-01 00:54:00 +00:00
|
|
|
{ iid: iid }
|
2016-03-31 02:12:34 +00:00
|
|
|
end
|
|
|
|
end
|
2015-12-24 13:43:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|