Module: Workbench::HtmlHelper
- Defined in:
- app/helpers/workbench/html_helper.rb
Overview
Basic, non model dependent helpers that inject HTML, these should all depend on Rails helpers, if they don't they should go into Utilities
Instance Method Summary collapse
-
#mark_tag(string, term, html_safe: true) ⇒ String?
TODO: see also highlight().
Instance Method Details
#mark_tag(string, term, html_safe: true) ⇒ String?
TODO: see also highlight()
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/helpers/workbench/html_helper.rb', line 14 def mark_tag(string, term, html_safe: true) return nil if string.nil? term = term.to_s.strip s = if term.blank? string else # Prefer matching the whole term contiguously, but tags injected around # name parts (e.g. italics split around subgenus parens) can break the # term across multiple non-tag pieces, so also fall back to matching # its individual words, same as the word-based fragments used to build # the autocomplete matches themselves. alternatives = ([term] + Utilities::Strings.alphanumeric_strings(term)).uniq t = Regexp.new(alternatives.map { |a| Regexp.escape(a) }.join('|'), Regexp::IGNORECASE) string.split(/(<[^>]*>)/).map { |piece| next piece if piece.start_with?('<') # We split on the html entities content_tag could produce (though at # time of writing these could also come from user input). piece.split(/(&(?:amp|lt|gt|quot|\#39);)/).map { |chunk| chunk.start_with?('&') ? chunk : chunk.gsub(t) { |m| content_tag(:mark, m) } }.join }.join end html_safe ? s.html_safe : String.new(s) end |