我使用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 & Show</title>
<link>http://foo.net</link>
<description><strong>Description</strong></description>
<item>
<title>Foo & Bar</title>
<description><strong>About</strong></description>
</item>
</channel>
</rss>
我不想对频道和项目描述进行HTML编码,而是保留原始HTML并将其包装在CDATA块中,例如:
<description><![CDATA[<strong>Description</strong>]]></description>
答案 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
来逃避一些特殊实体。