class Search < Vanilla::Dynasnip
def search_form
%{
}
end
def get(*args)
if app.request.params[:q]
search
else
search_form
end
end
def post(*args)
search
end
private
def search
term = app.request.params[:q]
if term =~ /[^\w\s\-_\.]/
"Please only use characters, spaces, hyphens, underscores and periods in your search term
"
else
matches = `fgrep -r "#{term}" #{app.config.site_soups.join(" ")}`.split("\n")
search_form + "Results
" + if matches.any?
grouped_matches = {}
matches.each do |match|
next if match =~ /::/
parts = match.split(":")
snip = File.basename(parts.shift.split(".").first)
context = parts.join
grouped_matches[snip] ||= []
grouped_matches[snip] << context
end
snips = grouped_matches.keys.sort_by { |s| grouped_matches[s].length }.reverse
"" + snips.map do |snip|
contexts = grouped_matches[snip].map! do |context|
start = context.index(term)
context.gsub("{", "{").gsub("}", "}").
insert(start + term.length, "").
insert(start, %{}).strip
end
%{- {l #{snip}} → #{contexts.join(" … ")}
}
end.join + "
"
else
%{No matches for "#{term}"
}
end
end
end
self
end