2
我使用Ruby的内置RSS库生成RSS源,这似乎在生成提要时转义HTML。对于某些元素,我更喜欢通过将其包装在CDATA块中来保留原始HTML。在生成的RSS源中将内容编码为CDATA
的最小工作示例:
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>
这比我最终与之合作的解决方案更清洁,即使它是猴子补丁。 – Doches
'class RSS :: Rss :: Channel :: Item'是在Ruby 2.3上为我工作的类。 – Todd