将附加到rails中的关键字。

时间:2022-09-28 21:15:16

Basically, there are some keywords that i want to pick out and assign a link to them dynamically.

基本上,有一些关键字我想挑选和分配一个链接到他们动态。

How should i go about in doing this?

我该怎么做呢?

Let's say i have something like this;

假设有这样的东西;

<p>ruby, rails, php, python, django and sinatra</p>

and i would like to assign links to keywords like ruby and python, so the end results should look like this;

我想为ruby和python等关键字分配链接,最终结果应该是这样的;

<p><a href="http://www.ruby-lang.org">ruby</a>, rails, php, <a href="http://www.python.org">python</a>, django and sinatra</p>

Any help or suggestion would be much appreciated!

如有任何帮助或建议,我们将不胜感激!

5 个解决方案

#1


4  

One way of doing it, assuming you only want to link whole words:

一种方法,假设你只想链接整个单词:

html = "<p>ruby, rails, php, python, django and sinatra</p>"

word_links = { "ruby"   => "http://www.ruby-lang.org",
               "python" => "http://www.python.org" }

html.gsub(/\w+/) do |word|
  word_links[word] ? "<a href=\"#{word_links[word]}\">#{word}</a>" : word
end

If the text you are operating on is rather large, but the list of words you want to link is pretty small, this may be optimized a little by doing substitution on matching words only:

如果你正在操作的文本相当大,但是你想要链接的单词列表非常小,这可以通过替换匹配的单词进行优化:

html.gsub(/\b(#{word_links.keys.join('|')})\b/,
          "<a href=\"#{word_links[$1]}\">#{$1}</a>")

Just make sure you don't have any keywords that can appear inside an opening tag (like inside an attribute value for example, which would break completely with this solution.) For a truly robust solution you will have to use a proper HTML parser and just replace content text.

只要确保没有任何关键字可以出现在开始的标记中(例如,在属性值中出现,这个解决方案将会完全崩溃)。要获得真正健壮的解决方案,您必须使用适当的HTML解析器,并只需替换内容文本。

#2


3  

If you want to do it on the server side before the content is rendered to the browser then you can do it like

如果您想在将内容呈现给浏览器之前在服务器端进行此操作,那么您可以像这样做

str = "<p>ruby, rails, php, python, django and sinatra scruby rubysd 12ruby'; ruby </p>"

link_hash = {'ruby' => "<a href = 'http://www.ruby-lang.org'>ruby</a>", 'python' => "<a href='http://www.python.org'>python</a>" }

link_hash.each_pair do |key, value|
  str = str.gsub(/\b#{key}\b/, value)
end

str #=> "<p><a href = 'http://www.ruby-lang.org'>ruby</a>, rails, php, <a href='http://www.python.org'>python</a>, django and sinatra scruby rubysd 12ruby'; <a href = 'http://www.ruby-lang.org'>ruby</a> </p>"

#3


0  

Simply use text replacement? Replace "ruby" with "<a href="http://ruby-lang.org">ruby</a>", and done. See the String.replace in the javascript reference .

简单地使用文本替换?用"ruby"替换"ruby",完成。看到字符串。在javascript引用中替换。

#4


0  

If you wanted to be particularly fancy, I think you could use an after filter:

如果你想特别喜欢,我想你可以用一个后滤镜:

class MyController < ApplicationController
  after_filter :link_stuff

  protected

  def link_stuff
    # You'd want to do this more carefully, so as to only get "ruby"s in text, 
    # not in tags etc.
    response.body.gsub(/ruby/, "<a href = 'http://www.ruby-lang.org'>ruby</a>")
  end
end

#5


0  

You can do like this

你可以这样做

first get the values as an array (or list) Ex: ruby php python etc..

首先获取作为数组(或列表)的值,例如:ruby php python等等。

send this array to a helper

将此数组发送给助手

def link_builder(arr) html_string = ""

def link_builder(arr) html_string = ""

for element in arr

元素的加勒比海盗

 check whether your word needs a wrapper. you might want to do a table check 

 if warpper_needed?

    html_string = html_string + <a href='link'>elment</a>
 else
    html_string = html_string + <p>element</p>
 end

end

结束

html_string

html_string

end

结束

Hope you got the idea, (this might not work as it is)

希望你能明白我的意思(这可能行不通)

cheers, sameera

欢呼,sameera

#1


4  

One way of doing it, assuming you only want to link whole words:

一种方法,假设你只想链接整个单词:

html = "<p>ruby, rails, php, python, django and sinatra</p>"

word_links = { "ruby"   => "http://www.ruby-lang.org",
               "python" => "http://www.python.org" }

html.gsub(/\w+/) do |word|
  word_links[word] ? "<a href=\"#{word_links[word]}\">#{word}</a>" : word
end

If the text you are operating on is rather large, but the list of words you want to link is pretty small, this may be optimized a little by doing substitution on matching words only:

如果你正在操作的文本相当大,但是你想要链接的单词列表非常小,这可以通过替换匹配的单词进行优化:

html.gsub(/\b(#{word_links.keys.join('|')})\b/,
          "<a href=\"#{word_links[$1]}\">#{$1}</a>")

Just make sure you don't have any keywords that can appear inside an opening tag (like inside an attribute value for example, which would break completely with this solution.) For a truly robust solution you will have to use a proper HTML parser and just replace content text.

只要确保没有任何关键字可以出现在开始的标记中(例如,在属性值中出现,这个解决方案将会完全崩溃)。要获得真正健壮的解决方案,您必须使用适当的HTML解析器,并只需替换内容文本。

#2


3  

If you want to do it on the server side before the content is rendered to the browser then you can do it like

如果您想在将内容呈现给浏览器之前在服务器端进行此操作,那么您可以像这样做

str = "<p>ruby, rails, php, python, django and sinatra scruby rubysd 12ruby'; ruby </p>"

link_hash = {'ruby' => "<a href = 'http://www.ruby-lang.org'>ruby</a>", 'python' => "<a href='http://www.python.org'>python</a>" }

link_hash.each_pair do |key, value|
  str = str.gsub(/\b#{key}\b/, value)
end

str #=> "<p><a href = 'http://www.ruby-lang.org'>ruby</a>, rails, php, <a href='http://www.python.org'>python</a>, django and sinatra scruby rubysd 12ruby'; <a href = 'http://www.ruby-lang.org'>ruby</a> </p>"

#3


0  

Simply use text replacement? Replace "ruby" with "<a href="http://ruby-lang.org">ruby</a>", and done. See the String.replace in the javascript reference .

简单地使用文本替换?用"ruby"替换"ruby",完成。看到字符串。在javascript引用中替换。

#4


0  

If you wanted to be particularly fancy, I think you could use an after filter:

如果你想特别喜欢,我想你可以用一个后滤镜:

class MyController < ApplicationController
  after_filter :link_stuff

  protected

  def link_stuff
    # You'd want to do this more carefully, so as to only get "ruby"s in text, 
    # not in tags etc.
    response.body.gsub(/ruby/, "<a href = 'http://www.ruby-lang.org'>ruby</a>")
  end
end

#5


0  

You can do like this

你可以这样做

first get the values as an array (or list) Ex: ruby php python etc..

首先获取作为数组(或列表)的值,例如:ruby php python等等。

send this array to a helper

将此数组发送给助手

def link_builder(arr) html_string = ""

def link_builder(arr) html_string = ""

for element in arr

元素的加勒比海盗

 check whether your word needs a wrapper. you might want to do a table check 

 if warpper_needed?

    html_string = html_string + <a href='link'>elment</a>
 else
    html_string = html_string + <p>element</p>
 end

end

结束

html_string

html_string

end

结束

Hope you got the idea, (this might not work as it is)

希望你能明白我的意思(这可能行不通)

cheers, sameera

欢呼,sameera