在生成的RSS源中将内容编码为CDATA

时间:2014-07-13 12:48:16

标签: ruby rss cdata

我使用Ruby的内置RSS库生成RSS提要,这在生成提要时似乎逃脱了HTML。对于某些元素,我更喜欢通过将其包装在CDATA块中来保留原始HTML。

最小的工作示例:

require 'rss/2.0'

feed = RSS::Rss.new("2.0")
feed.channel = RSS::Rss::Channel.new

feed.channel.title = "Title & Show"
feed.channel.link = "http://foo.net"
feed.channel.description = "<strong>Description</strong>"

item = RSS::Rss::Channel::Item.new
item.title = "Foo & Bar"
item.description = "<strong>About</strong>"

feed.channel.items << item

puts feed

...生成以下RSS:

<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>Title &amp; Show</title>
    <link>http://foo.net</link>
    <description>&lt;strong&gt;Description&lt;/strong&gt;</description>
    <item>
      <title>Foo &amp; Bar</title>
      <description>&lt;strong&gt;About&lt;/strong&gt;</description>
    </item>
  </channel>
</rss>

我不想对频道和项目描述进行HTML编码,而是保留原始HTML并将其包装在CDATA块中,例如:

<description><![CDATA[<strong>Description</strong>]]></description>

1 个答案:

答案 0 :(得分:0)

monkey-patching the element-generating method适用于我:

require 'rss/2.0'

class RSS::Rss::Channel
  def description_element need_convert, indent
    markup = "#{indent}<description>"
    markup << "<![CDATA[#{@description}]]>"
    markup << "</description>"
    markup
  end
end

# ...

这可以防止调用Utils.html_escape来逃避一些特殊实体。