【问题标题】:How to apply additional inline style to html tags in ruby?如何将额外的内联样式应用于 ruby​​ 中的 html 标签?
【发布时间】:2013-07-01 15:05:44
【问题描述】:

我有一个 html 字符串。在那个字符串中,我想解析所有 <p> 标签并应用额外的内联样式。

其他样式:style="margin:0px;padding:0px;" 或其他样式

案例 1:

输入字符串:<p>some string</p>

输出字符串:<p style="margin:0px;padding:0px;">some string</p>

案例 2:

输入字符串:<p style="text-align:right;" >some string</p>

输出字符串:<p style="text-align:right;margin:0px;padding:0px;">some string</p>

案例 3:

输入字符串:<p align="justify">some string</p>

输出字符串:<p style="margin:0px;padding:0px;" align="justify">some string</p>

现在我正在使用这样的正则表达式

myHtmlString.gsub("<p", "<p style = \"margin:0px;padding:0px\"")

除了删除以前的样式外,效果很好。我正在使用 Ruby (ROR)。

我需要帮助来稍微调整一下。

【问题讨论】:

  • html 不能被正则表达式解析。为什么不使用 xml 解析器?
  • 可以被正则表达式解析。你只是不想这样做,原因是outlined here
  • @hd1 一些基本的、可预测的 html 字符串可以通过正则表达式解析(毕竟你可以尝试用正则表达式解析任何文本字符串),但是 html 不是常规语言,所以任意 html正则表达式无法解析
  • 如果您阅读了我提供的链接,@sgroves,它几乎说明了您所写的内容。
  • @hd1 很酷。那我们就同意了。

标签: ruby-on-rails ruby regex gsub


【解决方案1】:

您可以使用 Nokogiri 执行此操作,方法是在相关节点上设置 [:style]

require "nokogiri"

inputs = [
  '<p>some string</p>',
  '<p style="text-align:right;" >some string</p>',
  '<p align="justify">some string</p>'
]

inputs.each do |input|
  noko = Nokogiri::HTML::fragment(input)
  noko.css("p").each do |tag|
    tag[:style] = (tag[:style] || "") + "margin:0px;padding:0px;"
  end
  puts noko.to_html
end

这将遍历与 css 选择器 p 匹配的所有元素,并根据需要设置 style 属性。

输出:

<p style="margin:0px;padding:0px;">some string</p>
<p style="text-align:right;margin:0px;padding:0px;">some string</p>
<p align="justify" style="margin:0px;padding:0px;">some string</p>

【讨论】:

    【解决方案2】:

    我建议不要为此使用正则表达式,因为通常正则表达式无法正确解析 HTML。也就是说,只要您的输入数据是一致的,正则表达式仍然可以工作。您想使用括号匹配 p 元素的 style 属性中已有的任何内容,然后将其插入替换字符串中:

    myHtmlString.gsub(/<p( style="(.*)")?/,
                      "<p style=\"#{$2};margin:0px;padding:0px\"")
    

    匹配模式的工作原理如下:

    /        #regex delimiter
    <p       #match start of p tag
    (        #open paren used to group, everything in this group gets saved in $1
     style=" #open style attribute
    (.*)     #group contents of style attribute, gets saved to $2
    "        #close style attribute
    )?       #question mark makes everything in the paren group optional
    /        #regex delimiter
    

    【讨论】:

    • 这里不考虑上面的case3。
    • @deepak 不是吗?我没有测试过这个,但我不明白为什么它不会。此正则表达式不查找除style 之外的属性,因此它将在p 标记上的任何其他属性之前插入新的style 属性。它不会删除任何现有属性。
    【解决方案3】:

    我最终做了这样的事情,我必须在发送电子邮件之前这样做。我知道这不是最好的方法,但值得在这里分享。 @sgroves 和 @Dobert 提供的解决方案非常好,很有帮助。

    但我不想包括 Nokogiri,尽管我仅从以上 2 个解决方案中选择了这个想法。谢谢。

    这是我的代码(我是 ROR 新手,所以这里没什么特别的,我在 HAML 块中使用它)

     myString.gsub!(/<p[^>]*>/) do |match|
       match1 = match
       style1_arr = match1.scan(/style=".*"/)
       unless style1_arr.blank?
         style1 = style1_arr.first.sub("style=", "").gsub(/\"/, "").to_s
         style2 = style1 + "margin:0px;padding:0px;"
         match2 = match1.sub(/style=".*"/, "style=\"#{style2.to_s}\"")
       else
         match2 = match1.sub(/<p/, "<p style = \"margin:0px;padding:0px;\"")
       end
     end
    

    现在myString 将被更新字符串。(注意gsub 之后的!)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      相关资源
      最近更新 更多