text_filter.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:3k
源码类别:

Ajax

开发平台:

Others

  1. require 'net/http'
  2. class TextFilter < ActiveRecord::Base
  3.   serialize :filters
  4.   serialize :params
  5.   @text_helper = ContentTextHelpers.new
  6.   def sanitize(*args,&blk)
  7.     self.class.sanitize(*args,&blk)
  8.   end
  9.   def self.available_filters
  10.     TextFilterPlugin.filter_map.values
  11.   end
  12.   TYPEMAP={TextFilterPlugin::Markup => "markup",
  13.            TextFilterPlugin::MacroPre => "macropre",
  14.            TextFilterPlugin::MacroPost => "macropost",
  15.            TextFilterPlugin::PostProcess => "postprocess",
  16.            TextFilterPlugin => "other"}
  17.   def self.available_filter_types
  18.     filters=available_filters
  19.     @cached_filter_types ||= {}
  20.     unless @cached_filter_types[filters]
  21.       types={"macropre" => [],
  22.              "macropost" => [],
  23.              "markup" => [],
  24.              "postprocess" => [],
  25.              "other" => []}
  26.       filters.each { |filter| types[TYPEMAP[filter.superclass]].push(filter) }
  27.       @cached_filter_types[filters] = types
  28.     end
  29.     @cached_filter_types[filters]
  30.   end
  31.   def self.filters_map
  32.     TextFilterPlugin.filter_map
  33.   end
  34.   def self.filter_text(blog, text, content, filters, filterparams={})
  35.     map=TextFilter.filters_map
  36.     filters.each do |filter|
  37.       next if filter == nil
  38.       begin
  39.         filter_class = map[filter.to_s]
  40.         next unless filter_class
  41.         text = filter_class.filtertext(blog, content, text, :filterparams => filterparams)
  42.       rescue => err
  43.         logger.error "Filter #{filter} failed: #{err}"
  44.       end
  45.     end
  46.     text
  47.   end
  48.   def self.filter_text_by_name(blog, text, filtername)
  49.     f = TextFilter.find_by_name(filtername)
  50.     f.filter_text_for_content blog, text, nil
  51.   end
  52.   def filter_text_for_content(blog, text, content)
  53.     self.class.filter_text(blog, text, content,
  54.       [:macropre, markup, :macropost, filters].flatten, params)
  55.   end
  56.   def help
  57.     filter_map = TextFilter.filters_map
  58.     filter_types = TextFilter.available_filter_types
  59.     help = []
  60.     help.push(filter_map[markup])
  61.     filter_types['macropre'].sort_by {|f| f.short_name}.each { |f| help.push f }
  62.     filter_types['macropost'].sort_by {|f| f.short_name}.each { |f| help.push f }
  63.     filters.each { |f| help.push(filter_map[f.to_s]) }
  64.     help_text = help.collect do |f|
  65.       f.help_text.blank? ? '' : "<h3>#{f.display_name}</h3>n#{BlueCloth.new(f.help_text).to_html}n"
  66.     end
  67.     help_text.join("n")
  68.   end
  69.   def commenthelp
  70.     filter_map = TextFilter.filters_map
  71.     help = [filter_map[markup]]
  72.     filters.each { |f| help.push(filter_map[f.to_s]) }
  73.     help_text = help.collect do |f|
  74.       f.help_text.blank? ? '' : "#{BlueCloth.new(f.help_text).to_html}n"
  75.     end.join("n")
  76.     return help_text
  77.   end
  78.   def to_s; self.name; end
  79.   def to_text_filter; self; end
  80. end