加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何忽略高亮功能中的重音

发布时间:2020-12-17 01:24:01 所属栏目:百科 来源:网络整理
导读:我有一个微型迷你搜索引擎,突出了我的rails应用程序中的搜索条件.搜索忽略重音,突出显示不区分大小写.几乎完美. 但是,例如,如果我有一个带有“p?odequeijo”文本的记录并搜索“pao de queijo”,则返回记录,但不突出显示iext.同样,如果我搜索“p?odede queijo
我有一个微型迷你搜索引擎,突出了我的rails应用程序中的搜索条件.搜索忽略重音,突出显示不区分大小写.几乎完美.
但是,例如,如果我有一个带有“p?odequeijo”文本的记录并搜索“pao de queijo”,则返回记录,但不突出显示iext.同样,如果我搜索“p?odede queijo”,则会返回记录但不会正确突出显示.

我的代码很简单:

<%= highlight(result_pessoa.observacoes,search_string,'<span style="background-color: yellow;">1</span>') %>

解决方法

我刚刚向Rails提交了补丁,解决了这个问题.

http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3593-patch-support-for-highlighting-with-ignoring-special-chars

# Highlights one or more +phrases+ everywhere in +text+ by inserting it into
  # a <tt>:highlighter</tt> string. The highlighter can be specialized by passing <tt>:highlighter</tt>
  # as a single-quoted string with 1 where the phrase is to be inserted (defaults to
  # '<strong class="highlight">1</strong>')
  #
  # ==== Examples
  #   highlight('You searched for: rails','rails')
  #   # => You searched for: <strong class="highlight">rails</strong>
  #
  #   highlight('You searched for: ruby,rails,dhh','actionpack')
  #   # => You searched for: ruby,dhh
  #
  #   highlight('You searched for: rails',['for','rails'],:highlighter => '<em>1</em>')
  #   # => You searched <em>for</em>: <em>rails</em>
  #
  #   highlight('You searched for: rails','rails',:highlighter => '<a href="search?q=1">1</a>')
  #   # => You searched for: <a href="search?q=rails">rails</a>
  #
  #   highlight('?umné diev?atá',['?umňe','dievca'],:ignore_special_chars => true)
  #   # => <strong class="highlight">?umné</strong> <strong class="highlight">diev?a</strong>tá  
  #
  # You can still use <tt>highlight</tt> with the old API that accepts the
  # +highlighter+ as its optional third parameter:
  #   highlight('You searched for: rails','<a href="search?q=1">1</a>')     # => You searched for: <a href="search?q=rails">rails</a>
  def highlight(text,phrases,*args)
    options = args.extract_options!
    unless args.empty?
      options[:highlighter] = args[0] || '<strong class="highlight">1</strong>'
    end
    options.reverse_merge!(:highlighter => '<strong class="highlight">1</strong>')

    if text.blank? || phrases.blank?
      text
    else
      haystack = text.clone
      match = Array(phrases).map { |p| Regexp.escape(p) }.join('|')
      if options[:ignore_special_chars]
        haystack = haystack.mb_chars.normalize(:kd)
        match = match.mb_chars.normalize(:kd).gsub(/[^x00-x7F]+/n,'').gsub(/w/,'[^x00-x7F]*')
      end
      highlighted = haystack.gsub(/(#{match})(?!(?:[^<]*?)(?:["'])[^<>]*>)/i,options[:highlighter])
      highlighted = highlighted.mb_chars.normalize(:kc) if options[:ignore_special_chars]
      highlighted
    end
  end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读