質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

1回答

357閲覧

rssのdescriptionにタグ付きの内容を出力したい。rubyでrss2.0を作成

takataka75

総合スコア95

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2018/01/06 19:04

rssモジュールを利用して
rubyでrss2.0を出力するプログラムを作っているのですが、
1か所、うまくいきません。

うまくいかない所は
descriptionの項目に「<CDATA[なんちゃら~」と書くと
なんちゃらの部分はhtml_escapeされて

rss

1 <description> 2 &lt;![CDATA[&lt;img src=https://amd.c.yimg.jp/im_siggCB0P2WlcKh4RxvwE3l0I3w---x150-y84-q90/amd/20180105-00000045-natalieo-000-0-thumb.jpg &gt;&lt;BR&gt;…BSプレミアム)にガンバレルーヤ、はなしょー、阿佐ヶ谷姉妹、平野ノラ、相席スタート・ケイら多数の女性芸人が出演する。この番組は久本雅美が昨年活躍した女性…&lt;BR&gt;1月5日(金) 14時38分  エンタメ総合(お笑いナタリー)]]&gt; 3 </description>

となってしまいます。
出力としては、↓というように、出力したいのですが、
うまくいきません。

rss

1 2 <description> 3<![CDATA[<img src=https://amd.c.yimg.jp/im_siggCB0P2WlcKh4RxvwE3l0I3w---x150-y84-q90/amd/20180105-00000045-natalieo-000-0-thumb.jpg ><BR>…BSプレミアム)にガンバレルーヤ、はなしょー、阿佐ヶ谷姉妹、平野ノラ、相席スタート・ケイら多数の女性芸人が出演する。この番組は久本雅美が昨年活躍した女性…<BR>1月5日(金) 14時38分  エンタメ総合(お笑いナタリー)]]> 4 </description>

モンキーパッチを入れると可能らしいという事で
http://motivation.drivendevelopment.jp/2009-12-02-1.html
をそのまま使ったのですが、
変わりませんでした。

rssモジュールでは無い方法もあるようですが、
rssモジュールでできるようにしたいです。
(rssモジュールの投稿日ソートを使っているので)

よろしくお願いします。

作成部分のコードは、こんな感じ
モンキーパッチの部分は
http://motivation.drivendevelopment.jp/2009-12-02-1.html
をそのまま入れています。

ruby

1 2 3 4 rss = RSS::Maker.make("2.0") do |xml| 5 xss = xml.xml_stylesheets.new_xml_stylesheet 6# xss.href = "http://example.com/index.xsl" 7 8 xml.items.do_sort = true 9 10 xss.href = http://hogehoge/index.xml" 11 xml.channel.title = "Sample" 12 xml.channel.link = 'http://hogehoge" 13 xml.channel.about = "http://hogehoge/news.xml" 14 xml.channel.description = "ほげほげ" 15 16 xml.items.new_item do |item| 17 item.title = "アイテムタイトル" 18 item.link = "http://hogehoge" 19 20 description ="" 21 description << "<img src=" + img_url + " >" 22 description << "<BR>" 23 description << "ほげほげ" 24 25#いろいろやってみた。けど・・・ 26 27# item.description do 28# item.cdata! description 29# end 30# item.description = "<![CDATA[" + description + "]]>" 31 32 item.content_encoded = description 33 34# xml.description do 35# xml.cdata do 36# xml << description 37# end 38# end 39 end 40 end 41

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

こんにちは。

stackoverflow に出ていた、
encode-content-as-cdata-in-generated-rss-feed
を参考にして、以下のような対応をやってみました。

修正前(パッチなし)

ご質問にあるコードに多少手を加えてあります。

ソースコード:

ruby

1require 'rss' 2 3img_url = 'http://example.com/test.jpg' 4 5rss = RSS::Maker.make('2.0') do |xml| 6 xss = xml.xml_stylesheets.new_xml_stylesheet 7 xss.href = 'http://example.com/index.xsl' 8 9 xml.items.do_sort = true 10 11 xss.href = 'http://example.com/index.xml' 12 xml.channel.title = 'Sample' 13 xml.channel.link = 'http://example.com' 14 xml.channel.about = 'http://example/news.xml' 15 xml.channel.description = 'ほげほげ' 16 17 xml.items.new_item do |item| 18 item.title = 'アイテムタイトル' 19 item.link = 'http://example.com' 20 21 description = '' 22 description << '<img src="' + img_url + '" >' 23 description << '<BR>' 24 description << 'ほげほげ' 25 26 item.description = description 27 item.content_encoded = description 28 29 end 30end 31 32puts rss

実行結果:

xml

1<?xml version="1.0" encoding="UTF-8"?> 2<rss version="2.0" 3 xmlns:content="http://purl.org/rss/1.0/modules/content/" 4 xmlns:dc="http://purl.org/dc/elements/1.1/" 5 xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" 6 xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> 7 <channel> 8 <title>Sample</title> 9 <link>http://example.com</link> 10 <description>ほげほげ</description> 11 <item> 12 <title>アイテムタイトル</title> 13 <link>http://example.com</link> 14 <description>&lt;img src=&quot;http://example.com/test.jpg&quot; &gt;&lt;BR&gt;ほげほげ</description> 15 <content:encoded>&lt;img src=&quot;http://example.com/test.jpg&quot; &gt;&lt;BR&gt;ほげほげ</content:encoded> 16 </item> 17 </channel> 18</rss>

修正後(パッチあり)

修正前のコードに、class RSS::Rss::Channel::Item へのパッチ
(以下の class RSS::Rss::Channel::Itemend の行)
を追加しました。その他は変更なしです。

ソースコード:

ruby

1require 'rss' 2 3class RSS::Rss::Channel::Item 4 5 def content_encoded_element need_convert, indent 6 markup = "#{indent}<content:encoded>" 7 markup << @description 8 markup << '</content:encoded>' 9 markup 10 end 11 12 def description_element need_convert, indent 13 markup = "#{indent}<description>" 14 markup << "<![CDATA[#{@description}]]>" 15 markup << '</description>' 16 markup 17 end 18end 19 20img_url = 'http://example.com/test.jpg' 21 22rss = RSS::Maker.make('2.0') do |xml| 23 xss = xml.xml_stylesheets.new_xml_stylesheet 24 xss.href = 'http://example.com/index.xsl' 25 26 xml.items.do_sort = true 27 28 xss.href = 'http://example.com/index.xml' 29 xml.channel.title = 'Sample' 30 xml.channel.link = 'http://example.com' 31 xml.channel.about = 'http://example/news.xml' 32 xml.channel.description = 'ほげほげ' 33 34 xml.items.new_item do |item| 35 item.title = 'アイテムタイトル' 36 item.link = 'http://example.com' 37 38 description = '' 39 description << '<img src="' + img_url + '" >' 40 description << '<BR>' 41 description << 'ほげほげ' 42 43 item.description = description 44 item.content_encoded = description 45 46 end 47end 48 49puts rss

実行結果:

xml

1<?xml version="1.0" encoding="UTF-8"?> 2<rss version="2.0" 3 xmlns:content="http://purl.org/rss/1.0/modules/content/" 4 xmlns:dc="http://purl.org/dc/elements/1.1/" 5 xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" 6 xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> 7 <channel> 8 <title>Sample</title> 9 <link>http://example.com</link> 10 <description>ほげほげ</description> 11 <item> 12 <title>アイテムタイトル</title> 13 <link>http://example.com</link> 14 <description><![CDATA[<img src="http://example.com/test.jpg" ><BR>ほげほげ]]></description> 15 <content:encoded><img src="http://example.com/test.jpg" ><BR>ほげほげ</content:encoded> 16 </item> 17 </channel> 18</rss>

以上、参考になれば幸いです。

投稿2018/01/06 21:40

jun68ykt

総合スコア9058

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

takataka75

2018/01/07 06:51

ありがとうございます。 見事一発で問題が解決しました。
jun68ykt

2018/01/07 06:56

解決されたようでよかったです!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問