# blog-category.rb $Revision: 1.14 $ # # Usage: # # Select blog-category.rb in 'Preferece' -> 'sp'. # You should initialize the category index file in 'Prefernce' -> 'Categorize blogkit'. # You can read the detailed documentation in the same page. # # Copyright (c) 2003 Junichiro KITA # Distributed under the GPL # require 'pstore' def blog_category cat = @cgi.params['blogcategory'][0] if cat and !cat.empty? cat else nil end end # NOTICE: very illegal usage @title_procs.insert( 0, Proc::new {|d, t| categorized_title_of_day( d, t ) } ) def categorized_title_of_day( date, title ) r = '' cats, stripped = title.scan( /^((?:\[[^\]]+\])+)\s*(.*)/ )[0] if cats then cats.scan( /\[([^\]]+)\]+/ ).flatten.each do |c| r << %Q|[#{c}]| end else stripped = title end r + ' ' + stripped end if /^(latest|month|day|append|replace|comment|showcomment|saveconf|trackbackreceive|pingbackreceive)$/ =~ @mode eval(<<-'MODIFY_CLASS', TOPLEVEL_BINDING) module TDiary module Style module BaseDiary def stripped_title return '' unless @title stripped = @title.sub(/^(\[(.*?)\])+\s*/,'') end def categories return [] unless @title cat = /^(\[([^\[]+?)\])+/.match(@title).to_a[0] return [] unless cat cat.scan(/\[(.*?)\]/).collect do |c| c[0].split(/,/) end.flatten end end end class TDiaryMonth attr_reader :diaries end end MODIFY_CLASS end if category_param = blog_category and @mode == 'latest' eval(<<-'MODIFY_CLASS', TOPLEVEL_BINDING) module TDiary module Cache def cache_enable?( prefix ) false end def store_cache( cache, prefix ) end end end MODIFY_CLASS years = @years.collect{|y,ms| ms.collect{|m| "#{y}#{m}"}}.flatten.sort.reverse @diaries.keys.each {|date| years.delete(date[0, 6])} @diaries.delete_if do |date, diary| !diary.categories.include?(category_param) end cgi = CGI.new def cgi.referer; nil; end years.each do |ym| break if @diaries.keys.size >= @conf.latest_limit cgi.params['date'] = [ym] m = TDiaryMonth.new(cgi, '', @conf) m.diaries.delete_if do |date, diary| !diary.categories.include?(category_param) end @diaries.update(m.diaries) end end alias :navi_user_blog_category :navi_user def navi_user r = navi_user_blog_category r << %Q[#{navi_latest}\n] if @mode == 'latest' and blog_category r end def blog_category_cache "#{@cache_path}/blog_category" end def blog_category_cache_add(diary) PStore.new(blog_category_cache).transaction do |db| db['blog_category'] = Hash.new unless db.root?('blog_category') diary.categories.each do |c| db['blog_category'][c] = Hash.new unless db['blog_category'][c] db['blog_category'][c][diary.date.strftime('%Y%m%d')] = diary.stripped_title end end end def blog_category_cache_restore return nil unless File.exist?(blog_category_cache) cache = {} PStore.new(blog_category_cache).transaction do |db| cache.update(db['blog_category']) db.abort end cache end def blog_category_entry(limit = 20) return 'DO NOT USE IN SECURE MODE' if @conf.secure cache = blog_category_cache_restore return '' if (blog_category.nil? or cache.nil?) n_shown = @diaries.keys.size n_shown = @conf.latest_limit if n_shown > @conf.latest_limit dates = cache[blog_category].keys.sort.reverse[n_shown..-1] return '' if dates.empty? r = "\n" end def blog_category_form cache = blog_category_cache_restore return '' if cache.nil? r = <
HTML end add_update_proc do if @mode != 'comment' diary = @diaries[@date.strftime('%Y%m%d')] blog_category_cache_add(diary) end end def blog_category_cache_initialize cgi = CGI::new def cgi.referer; nil; end PStore.new(blog_category_cache).transaction do |db| db['blog_category'] = Hash.new @years.each do |y, ms| ms.each do |m| cgi.params['date'] = ["#{y}#{m}"] m = TDiaryMonth.new(cgi, '', @conf) m.diaries.each do |k, diary| diary.categories.each do |c| db['blog_category'][c] = Hash.new unless db['blog_category'][c] db['blog_category'][c][diary.date.strftime('%Y%m%d')] = diary.stripped_title end end end end end end add_conf_proc( 'blog_category', @blog_category_conf_label, 'basic' ) do if @mode == 'saveconf' and @cgi.valid?('blog_category_initialize') blog_category_cache_initialize end r = '' r << @blog_category_desc_label r end def blog_category_edit cache = blog_category_cache_restore return '' if cache.nil? ret = '' unless cache.keys.size == 0 then ret << %Q[ ] ret << '
' ret << "#{@blog_category_conf_label}:\n" cache.keys.sort.each do |cat| ret << %Q!| #{h cat}\n! end ret << "|\n
\n
\n" end end add_edit_proc do blog_category_edit end # Local Variables: # mode: ruby # indent-tabs-mode: t # tab-width: 3 # ruby-indent-level: 3 # End: # vim: ts=3