Ruby on rails Ruby中的字符串替换:对imageboard的绿色文本支持

Ruby on rails Ruby中的字符串替换:对imageboard的绿色文本支持,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我正试图为我的Rails imageboard提供绿色文本支持(尽管应该提到,这严格来说是一个Ruby问题,而不是Rails问题) 基本上,我的代码所做的是: 1.逐行切碎一篇文章 2.看每行的第一个字符。如果是“>”,请启动绿色文本 3.在行尾,关闭绿色文本 4.把线重新拼起来 我的代码如下所示: def filter_comment(c) #use for both OP's and comments c1 = c.content str1 = '<p class = "unkfu

我正试图为我的Rails imageboard提供绿色文本支持(尽管应该提到,这严格来说是一个Ruby问题,而不是Rails问题)

基本上,我的代码所做的是:
1.逐行切碎一篇文章
2.看每行的第一个字符。如果是“>”,请启动绿色文本
3.在行尾,关闭绿色文本
4.把线重新拼起来

我的代码如下所示:

def filter_comment(c) #use for both OP's and comments

c1 = c.content

str1 = '<p class = "unkfunc">' #open greentext
str2 = '</p>' #close greentext

if c1 != nil
arr_lines = c1.split('\n') #split the text into lines

arr_lines.each do |a|
  if a[0] == ">"
    a.insert(0, str1) #add the greentext tag
    a << str2 #close the greentext tag

  end
end

c1 = ""

arr_lines.each do |a|
  strtmp = '\n'
  if arr_lines.index(a) == (arr_lines.size - 1) #recombine the lines into text
    strtmp = ""
  end
  c1 += a + strtmp
end


c2 = c1.gsub("\n", '<br/>').html_safe

end
def filter_comment(c)#用于OP和comment
c1=碳含量
str1='

'#打开绿色文本 str2='

'#关闭绿色文本 如果c1!=无 arr_lines=c1。拆分('\n')#将文本拆分为行 每排排一个| 如果[0]==“>” a、 插入(0,str1)#添加绿色文本标记
a我怀疑你的问题在于CSS(或者HTML),而不是Ruby。生成的HTML看起来正确吗?

我认为问题在于在数组中拆分行

   names = "Alice \n Bob \n Eve"
   names_a = names.split('\n')
   => ["Alice \n Bob \n Eve"]
注意:遇到此字符串时,\n该字符串未被拆分

现在让我们试试这个

  names = "Alice \n Bob \n Eve"
  names_a = names.split(/\n/)
  => ["Alice ", " Bob ", " Eve"]
或此“\n”用双引号括起来。(感谢Eric的评论)

这在数组中被拆分。现在,您可以检查并附加所需的数据

也许这就是你想要的

def filter_comment(c) #use for both OP's and comments

c1 = c.content

str1 = '<p class = "unkfunc">' #open greentext
str2 = '</p>' #close greentext

if c1 != nil
arr_lines = c1.split(/\n/) #split the text into lines

arr_lines.each do |a|
  if a[0] == ">"
    a.insert(0, str1) #add the greentext tag 
     # Use a.insert id you want the existing ">" appended to it <p class = "unkfunc">>
     # Or else just assign a[0] = str1 
    a << str2 #close the greentext tag

  end
end

c1 = arr_lines.join('<br/>')
c2 = c1.html_safe  

end
def filter_comment(c)#用于OP和comment
c1=碳含量
str1='

'#打开绿色文本 str2='

'#关闭绿色文本 如果c1!=无 arr_lines=c1。拆分(/\n/)#将文本拆分为行 每排排一个| 如果[0]==“>” a、 插入(0,str1)#添加绿色文本标记 #如果要将现有的“>”附加到该id中,请使用.insert id

> #或者只分配一个[0]=str1


一个旁注,可能是你的问题,没有太深入

尝试使用join()将阵列重新连接到一起


生成的HTML是什么样子的?可以提供一个示例,说明您试图通过什么输入来实现最终结果?我认为这是一个Ruby代码问题。HTML在错误的地方被弄乱了。正确地说,绿色文本看起来像“

>这意味着

”,但当Ruby执行其操作时,标记放置错误。或者在您的示例中,c1=arr_lines.join(“
”)
def filter_comment(c) #use for both OP's and comments

c1 = c.content

str1 = '<p class = "unkfunc">' #open greentext
str2 = '</p>' #close greentext

if c1 != nil
arr_lines = c1.split(/\n/) #split the text into lines

arr_lines.each do |a|
  if a[0] == ">"
    a.insert(0, str1) #add the greentext tag 
     # Use a.insert id you want the existing ">" appended to it <p class = "unkfunc">>
     # Or else just assign a[0] = str1 
    a << str2 #close the greentext tag

  end
end

c1 = arr_lines.join('<br/>')
c2 = c1.html_safe  

end
c1 = arr_lines.join('\n')