2013-10-20 01:05:20 -04:00
|
|
|
module SearchHelper
|
2014-01-18 07:16:46 -05:00
|
|
|
def search_autocomplete_opts(term)
|
2013-10-20 01:05:20 -04:00
|
|
|
return unless current_user
|
2014-01-18 07:16:46 -05:00
|
|
|
|
|
|
|
resources_results = [
|
|
|
|
groups_autocomplete(term),
|
2014-02-13 15:45:51 -05:00
|
|
|
projects_autocomplete(term)
|
2014-01-18 07:16:46 -05:00
|
|
|
].flatten
|
|
|
|
|
2016-09-07 09:16:22 -04:00
|
|
|
search_pattern = Regexp.new(Regexp.escape(term), "i")
|
|
|
|
|
2014-01-18 07:16:46 -05:00
|
|
|
generic_results = project_autocomplete + default_autocomplete + help_autocomplete
|
2016-09-07 09:16:22 -04:00
|
|
|
generic_results.select! { |result| result[:label] =~ search_pattern }
|
2014-01-18 07:16:46 -05:00
|
|
|
|
2013-10-20 01:05:20 -04:00
|
|
|
[
|
2014-01-18 07:16:46 -05:00
|
|
|
resources_results,
|
|
|
|
generic_results
|
2013-12-09 09:26:43 -05:00
|
|
|
].flatten.uniq do |item|
|
|
|
|
item[:label]
|
2014-01-18 07:16:46 -05:00
|
|
|
end
|
2013-10-20 01:05:20 -04:00
|
|
|
end
|
|
|
|
|
2016-04-15 12:00:53 -04:00
|
|
|
def search_entries_info(collection, scope, term)
|
2016-04-26 04:02:28 -04:00
|
|
|
return unless collection.count > 0
|
2016-04-15 09:13:21 -04:00
|
|
|
|
2016-04-26 04:02:28 -04:00
|
|
|
from = collection.offset_value + 1
|
|
|
|
to = collection.offset_value + collection.length
|
|
|
|
count = collection.total_count
|
|
|
|
|
|
|
|
"Showing #{from} - #{to} of #{count} #{scope.humanize(capitalize: false)} for \"#{term}\""
|
2016-04-15 09:13:21 -04:00
|
|
|
end
|
|
|
|
|
2016-09-14 06:29:23 -04:00
|
|
|
def parse_search_result(result)
|
|
|
|
ref = nil
|
|
|
|
filename = nil
|
|
|
|
basename = nil
|
|
|
|
startline = 0
|
|
|
|
|
|
|
|
result.each_line.each_with_index do |line, index|
|
|
|
|
if line =~ /^.*:.*:\d+:/
|
|
|
|
ref, filename, startline = line.split(':')
|
|
|
|
startline = startline.to_i - index
|
|
|
|
extname = Regexp.escape(File.extname(filename))
|
|
|
|
basename = filename.sub(/#{extname}$/, '')
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
data = ""
|
|
|
|
|
|
|
|
result.each_line do |line|
|
|
|
|
data << line.sub(ref, '').sub(filename, '').sub(/^:-\d+-/, '').sub(/^::\d+:/, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
OpenStruct.new(
|
|
|
|
filename: filename,
|
|
|
|
basename: basename,
|
|
|
|
ref: ref,
|
|
|
|
startline: startline,
|
|
|
|
data: data
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-10-20 01:05:20 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
# Autocomplete results for various settings pages
|
|
|
|
def default_autocomplete
|
|
|
|
[
|
2016-03-24 16:19:43 -04:00
|
|
|
{ category: "Settings", label: "Profile settings", url: profile_path },
|
|
|
|
{ category: "Settings", label: "SSH Keys", url: profile_keys_path },
|
|
|
|
{ category: "Settings", label: "Dashboard", url: root_path },
|
|
|
|
{ category: "Settings", label: "Admin Section", url: admin_root_path },
|
2013-10-20 01:05:20 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for internal help pages
|
|
|
|
def help_autocomplete
|
|
|
|
[
|
2016-05-16 18:06:26 -04:00
|
|
|
{ category: "Help", label: "API Help", url: help_page_path("api/README") },
|
2016-08-25 10:19:14 -04:00
|
|
|
{ category: "Help", label: "Markdown Help", url: help_page_path("user/markdown") },
|
2016-07-14 06:04:03 -04:00
|
|
|
{ category: "Help", label: "Permissions Help", url: help_page_path("user/permissions") },
|
2016-05-16 18:06:26 -04:00
|
|
|
{ category: "Help", label: "Public Access Help", url: help_page_path("public_access/public_access") },
|
|
|
|
{ category: "Help", label: "Rake Tasks Help", url: help_page_path("raketasks/README") },
|
|
|
|
{ category: "Help", label: "SSH Keys Help", url: help_page_path("ssh/README") },
|
|
|
|
{ category: "Help", label: "System Hooks Help", url: help_page_path("system_hooks/system_hooks") },
|
|
|
|
{ category: "Help", label: "Webhooks Help", url: help_page_path("web_hooks/web_hooks") },
|
|
|
|
{ category: "Help", label: "Workflow Help", url: help_page_path("workflow/README") },
|
2013-10-20 01:05:20 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for the current project, if it's defined
|
|
|
|
def project_autocomplete
|
|
|
|
if @project && @project.repository.exists? && @project.repository.root_ref
|
2016-05-10 22:58:06 -04:00
|
|
|
ref = @ref || @project.repository.root_ref
|
2013-10-20 01:05:20 -04:00
|
|
|
|
|
|
|
[
|
2016-03-24 16:19:43 -04:00
|
|
|
{ category: "Current Project", label: "Files", url: namespace_project_tree_path(@project.namespace, @project, ref) },
|
|
|
|
{ category: "Current Project", label: "Commits", url: namespace_project_commits_path(@project.namespace, @project, ref) },
|
|
|
|
{ category: "Current Project", label: "Network", url: namespace_project_network_path(@project.namespace, @project, ref) },
|
|
|
|
{ category: "Current Project", label: "Graph", url: namespace_project_graph_path(@project.namespace, @project, ref) },
|
|
|
|
{ category: "Current Project", label: "Issues", url: namespace_project_issues_path(@project.namespace, @project) },
|
|
|
|
{ category: "Current Project", label: "Merge Requests", url: namespace_project_merge_requests_path(@project.namespace, @project) },
|
|
|
|
{ category: "Current Project", label: "Milestones", url: namespace_project_milestones_path(@project.namespace, @project) },
|
|
|
|
{ category: "Current Project", label: "Snippets", url: namespace_project_snippets_path(@project.namespace, @project) },
|
|
|
|
{ category: "Current Project", label: "Members", url: namespace_project_project_members_path(@project.namespace, @project) },
|
|
|
|
{ category: "Current Project", label: "Wiki", url: namespace_project_wikis_path(@project.namespace, @project) },
|
2013-10-20 01:05:20 -04:00
|
|
|
]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for the current user's groups
|
2014-01-18 07:16:46 -05:00
|
|
|
def groups_autocomplete(term, limit = 5)
|
2016-02-04 06:38:25 -05:00
|
|
|
current_user.authorized_groups.search(term).limit(limit).map do |group|
|
2014-01-18 07:16:46 -05:00
|
|
|
{
|
2016-03-24 16:19:43 -04:00
|
|
|
category: "Groups",
|
2016-03-08 02:56:43 -05:00
|
|
|
id: group.id,
|
|
|
|
label: "#{search_result_sanitize(group.name)}",
|
2014-01-18 07:16:46 -05:00
|
|
|
url: group_path(group)
|
|
|
|
}
|
2013-10-20 01:05:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Autocomplete results for the current user's projects
|
2014-01-18 07:16:46 -05:00
|
|
|
def projects_autocomplete(term, limit = 5)
|
2016-02-04 06:38:25 -05:00
|
|
|
current_user.authorized_projects.search_by_title(term).
|
2014-09-25 12:41:49 -04:00
|
|
|
sorted_by_stars.non_archived.limit(limit).map do |p|
|
2014-01-18 07:16:46 -05:00
|
|
|
{
|
2016-03-24 16:19:43 -04:00
|
|
|
category: "Projects",
|
2016-03-08 02:56:43 -05:00
|
|
|
id: p.id,
|
|
|
|
value: "#{search_result_sanitize(p.name)}",
|
|
|
|
label: "#{search_result_sanitize(p.name_with_namespace)}",
|
2015-01-24 13:02:58 -05:00
|
|
|
url: namespace_project_path(p.namespace, p)
|
2014-01-18 07:16:46 -05:00
|
|
|
}
|
2013-11-06 10:13:21 -05:00
|
|
|
end
|
|
|
|
end
|
2014-01-18 07:16:46 -05:00
|
|
|
|
2014-01-18 09:06:20 -05:00
|
|
|
def search_result_sanitize(str)
|
2014-01-18 07:16:46 -05:00
|
|
|
Sanitize.clean(str)
|
|
|
|
end
|
2014-08-27 02:57:50 -04:00
|
|
|
|
2016-08-05 22:03:01 -04:00
|
|
|
def search_filter_path(options = {})
|
2014-08-27 02:57:50 -04:00
|
|
|
exist_opts = {
|
|
|
|
search: params[:search],
|
|
|
|
project_id: params[:project_id],
|
|
|
|
group_id: params[:group_id],
|
2016-07-22 12:42:24 -04:00
|
|
|
scope: params[:scope],
|
|
|
|
repository_ref: params[:repository_ref]
|
2014-08-27 02:57:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
options = exist_opts.merge(options)
|
2014-08-27 04:55:42 -04:00
|
|
|
search_path(options)
|
2014-08-27 02:57:50 -04:00
|
|
|
end
|
2014-08-27 05:37:31 -04:00
|
|
|
|
2016-10-06 17:55:27 -04:00
|
|
|
# Sanitize a HTML field for search display. Most tags are stripped out and the
|
|
|
|
# maximum length is set to 200 characters.
|
|
|
|
def search_md_sanitize(object, field)
|
|
|
|
html = markdown_field(object, field)
|
|
|
|
html = Truncato.truncate(
|
|
|
|
html,
|
|
|
|
count_tags: false,
|
|
|
|
count_tail: false,
|
|
|
|
max_length: 200
|
|
|
|
)
|
|
|
|
|
|
|
|
# Truncato's filtered_tags and filtered_attributes are not quite the same
|
2014-08-28 04:06:19 -04:00
|
|
|
sanitize(html, tags: %w(a p ol ul li pre code))
|
2014-08-27 05:37:31 -04:00
|
|
|
end
|
2013-10-20 01:05:20 -04:00
|
|
|
end
|