Sha256: 8474c20dbeb94392e46706b291c793c503c93243bdaec81b2455b5fa6bd1bcb3

Contents?: true

Size: 955 Bytes

Versions: 2

Compression:

Stored size: 955 Bytes

Contents

require 'action_view'
include ActionView::Helpers::TextHelper

class Translighterate
  def self.highlight(text, phrases, options = {})
    text = sanitize(text) if options.fetch(:sanitize, true)

    if text.blank? || phrases.blank?
      text || ""
    else
      match = Array(phrases).map do |p|
        Regexp === p ? p.to_s : Regexp.escape(p)
      end.join('|')

      # Transliteration trick from:
      #  https://github.com/cubing/worldcubeassociation.org/issues/238#issuecomment-234702800
      transliterated = text.mb_chars.normalize(:kd).gsub(/[\p{Mn}]/,'').to_s
      transliterated.gsub(/(#{match})(?![^<]*?>)/i) do |found|
        original_text_found = text[Range.new(*$~.offset(0), true)]
        if block_given?
          yield original_text_found
        else
          highlighter = options.fetch(:highlighter, '<mark>\1</mark>')
          highlighter.gsub(/\\1/, original_text_found)
        end
      end
    end.html_safe
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
translighterate-0.0.3 lib/translighterate.rb
translighterate-0.0.2 lib/translighterate.rb