# $Id: rss.rb,v 1.2 2003/12/10 16:42:21 not Exp $ #2003/12/27 微修正 # pを跨いでdescriptionする・他 =begin ndiary.conf での設定 # RSS RSS_FILENAME = 'http://www14.cds.ne.jp/~not/tawagoto/index.rdf' RSS_CREATER = 'not(のとや)' RSS_TITLE = 'nonsense talk' RSS_LINK = 'http://www14.cds.ne.jp/~not/tawagoto/' RSS_LINK_BASE = 'http://www14.cds.ne.jp/~not/tawagoto/' # 記事毎の url の基準となる url RSS_DESCRIPTION = '本買い日記だったり nDiary開発日誌だったり' RSS_DESCRIPTION_SIZE = 400 # byte. 0 の場合は を出力しない # Optional 省略可 RSS_DELETE_HEADCHAR = true # トピックの行頭文字を削除するかどうか (true / false) RSS_DCDATE_TYPE = 0 # トピック毎の dc:date の扱い # 0: dc:date はつけない # 1: *.diary の更新時刻を元にする。同じ日のトピックは全て同じに # 2: nDiary実行時の時刻を元にする # 前回実行時から変化がないトピックは前回の時刻を保持 # 3: *.diary の更新時刻を元にする # 前回実行時から変化がないトピックは前回の時刻を保持 # 3,4 の場合は LOG_DIRECTORY に rss_cache というファイルが作成されます RSS_RIGHTS = 'Copyright ...' # 著作権表示 RSS_IMAGE = 'banner.png' # イメージファイルへの URI (バナー等) RSS_CSS = './rss.xsl' # スタイルシートファイルへの URI RSS_CCPL = '' # CCPL オプション # http://www.alles.or.jp/~spiegel/docs/cc-about.html 参照 # 'by' , 'by-nd' , 'by-nd-nc' , 'by-nc' , 'by-nc-sa' , 'by-sa' , 'nd' , # 'nd-nc' , 'nc' , 'nc-sa' , 'sa' =end class Filter begin require 'digest/md5' rescue LoadError require 'md5' end def rss(str, type) return unless @diary.kind_of?(LastDiary) @rss ||= [] @rss_flag ||= false @rss_flag2 ||= false case type when :TOPIC topic = str.gsub(/<\/?([!\w]+)[^>]*>/i, '') if @diary.config['RSS_DELETE_HEADCHAR'] != false then /^.\s*(.+)$/ =~ topic topic = $1 end @rss << [topic, @diary.date, @diary.instance_eval{@cntTopic + 1}, '', ''] @rss_flag = true @rss_flag2 = true when :P, :CODE, :BLOCKQUOTE, :DL, :UL, :QUOTE if @rss_flag then desc = str.dup desc.gsub!(/<\/?([!\w]+)[^>]*>/i, '') desc = CGI::escapeHTML(desc) # desc.gsub!(/[\n\t ]/,'') #これよりは desc.gsub!(/[\s ]+/m,' ') #こんなのの方が好み desc = @rss[-1][3] + desc #この行追加 if @diary.config['RSS_DESCRIPTION_SIZE'].nil? then desc_size = 400 else desc_size = @diary.config['RSS_DESCRIPTION_SIZE'].to_i desc_size = 400 if desc_size < 0 end if desc.size > desc_size desc = desc[0..desc_size] desc.chop! unless desc.sub!(/.$/,'') desc << '...' @rss_flag = nil #この行追加 end unless desc.empty? then @rss[-1][3] = desc # @rss_flag = nil #この行コメントアウト end if @rss_flag2 then @rss[-1][4] << str end end when :ONEDAY @rss_flag = nil @rss_flag2 = nil when :HTML filename = @diary.config['RSS_FILENAME'].to_s creater = @diary.config['RSS_CREATER'].to_s title = @diary.config['RSS_TITLE'].to_s link = @diary.config['RSS_LINK'].to_s link_base = @diary.config['RSS_LINK_BASE'].to_s desc = @diary.config['RSS_DESCRIPTION'].to_s rights = @diary.config['RSS_RIGHTS'].to_s image = @diary.config['RSS_IMAGE'].to_s css = @diary.config['RSS_CSS'].to_s ccpl = @diary.config['RSS_CCPL'].to_s.downcase if filename.empty? then $stderr.puts "Filter error: RSS_FILENAME is empty." return end rights = %Q! #{rights}\n! unless rights.empty? image_detail = '' if not image.empty? then image_detail << %Q! \n! image_detail << %Q! #{title}\n! image_detail << %Q! #{link}\n! image_detail << %Q! #{image}\n! image_detail << %Q! \n! image = %Q! \n! end css = %Q!\n! unless css.empty? ccpl_detail = '' case ccpl when 'by' ccpl_detail = < CCPL_END ccpl = %Q! \n! when 'by-nd' ccpl_detail = < CCPL_END cpl = %Q! \n! when 'by-nd-nc' ccpl_detail = < CCPL_END ccpl = %Q! \n! when 'by-nc' ccpl_detail = < CCPL_END ccpl = %Q! \n! when 'by-nc-sa' ccpl_detail = < CCPL_END ccpl = %Q! \n! when 'by-sa' ccpl_detail = < CCPL_END ccpl = %Q! \n! when 'nd' ccpl_detail = < CCPL_END ccpl = %Q! \n! when 'nd-nc' ccpl_detail = < \n! CCPL_END ccpl = %Q! \n! when 'nc' ccpl_detail = < CCPL_END ccpl = %Q! \n! when 'nc-sa' ccpl_detail = < CCPL_END ccpl = %Q! \n! when 'sa' ccpl_detail = < CCPL_END ccpl = %Q! \n! else ccpl = '' end index = '' items = '' rss_cache = {} begin open(@diary.logDirectory + 'rss_cache'){ |f| f.each{ |line| topic, dcdate, md5 = line.chomp.split(/\t/,3) rss_cache[topic] = [dcdate, md5] } } rescue rss_cache = {} end @rss.each{ |t, date, tn, d, str| l = @diary.date2monthlyfilelink(date, true) + "_t#{tn}" dcdate = '' case @diary.config['RSS_DCDATE_TYPE'].to_i when 1, 3 dcdate = File::mtime(@diary.allDiaries[date]).strftime('%Y-%m-%dT%H:%M:%S+09:00') when 2 dcdate = @diary.instance_eval{@now}.strftime('%Y-%m-%dT%H:%M:%S+09:00') end if @diary.config['RSS_DCDATE_TYPE'].to_i >= 2 then key = sprintf("%s_t%03d", date, tn) md5 = Digest::MD5::new(str.strip).hexdigest rescue md5 = MD5::new(str.strip).hexdigest if rss_cache.key?(key) and rss_cache[key][1] == md5 then dcdate = rss_cache[key][0] end rss_cache[key] = [dcdate, md5] end index << %Q! \n! items << %Q! \n! items << %Q! #{t}\n! items << %Q! #{link_base+l}\n! items << %Q! #{d}\n! unless d.empty? items << %Q! #{dcdate}! unless dcdate.empty? items << %Q! \n! } unless rss_cache.keys.empty? then open(@diary.logDirectory + 'rss_cache', 'w'){ |f| rss_cache.keys.sort.reverse[0..50].each{ |t| # 50 が保存しとくトピックの数 f.puts [t, rss_cache[t][0], rss_cache[t][1]].join("\t") } } end rss = < ja #{title} #{creater} #{rights}#{ccpl}#{image} #{link} #{desc} daily #{@diary.instance_eval{@now}.strftime('%Y-%m-%dT%H:%M:%S+09:00')} #{index} #{items}#{image_detail}#{ccpl_detail} RSS_END rss_filename = @diary.outputDirectory + File::basename(filename) open(rss_filename, 'w'){ |f| begin rss = rss.toutf8 f.puts '' f.print rss rescue # f.puts '' f.puts '' #この行修正 f.print rss.toeuc end } end end end