Skip to content

Instantly share code, notes, and snippets.

@aredridel
Forked from tealtan/combine_highlight.rb
Created July 2, 2013 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aredridel/5906720 to your computer and use it in GitHub Desktop.
Save aredridel/5906720 to your computer and use it in GitHub Desktop.
module ApplicationHelper
def combine_highlight(selected_text, surrounding_text)
if surrounding_text.include? selected_text
split_text_array = surrounding_text.split(selected_text)
if split_text_array[0].blank?
if split_text_array[1].blank?
content_tag(:p, content_tag(:span, selected_text, class:"selected"))
else
content_tag(:p, content_tag(:span, selected_text, class:"selected") + split_text_array[1].html_safe)
end
else
if split_text_array[1].blank?
content_tag(:p, split_text_array[0].html_safe + content_tag(:span, selected_text, class:"selected"))
else
content_tag(:p, split_text_array[0].html_safe + content_tag(:span, selected_text, class:"selected") + split_text_array[1].html_safe)
end
end
else
content_tag(:p, surrounding_text, class:"selected")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment