2004-03-21 12:20:47 +03:00
|
|
|
require "rss/utils"
|
|
|
|
|
|
|
|
module RSS
|
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
module XMLStyleSheetMixin
|
|
|
|
attr_accessor :xml_stylesheets
|
|
|
|
def initialize(*args)
|
|
|
|
super
|
|
|
|
@xml_stylesheets = []
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def xml_stylesheet_pi
|
|
|
|
xsss = @xml_stylesheets.collect do |xss|
|
|
|
|
pi = xss.to_s
|
|
|
|
pi = nil if /\A\s*\z/ =~ pi
|
|
|
|
pi
|
|
|
|
end.compact
|
|
|
|
xsss.push("") unless xsss.empty?
|
|
|
|
xsss.join("\n")
|
|
|
|
end
|
|
|
|
end
|
2004-03-21 12:20:47 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
class XMLStyleSheet
|
2004-03-21 12:20:47 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
include Utils
|
2004-03-21 12:20:47 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
ATTRIBUTES = %w(href type title media charset alternate)
|
2004-03-21 12:20:47 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
GUESS_TABLE = {
|
|
|
|
"xsl" => "text/xsl",
|
|
|
|
"css" => "text/css",
|
|
|
|
}
|
2004-03-21 12:20:47 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
attr_accessor(*ATTRIBUTES)
|
|
|
|
attr_accessor(:do_validate)
|
|
|
|
def initialize(*attrs)
|
2007-02-11 16:12:16 +03:00
|
|
|
if attrs.size == 1 and
|
|
|
|
(attrs.first.is_a?(Hash) or attrs.first.is_a?(Array))
|
|
|
|
attrs = attrs.first
|
|
|
|
end
|
2004-10-16 08:51:15 +04:00
|
|
|
@do_validate = true
|
|
|
|
ATTRIBUTES.each do |attr|
|
2005-11-23 12:49:02 +03:00
|
|
|
__send__("#{attr}=", nil)
|
2004-10-16 08:51:15 +04:00
|
|
|
end
|
|
|
|
vars = ATTRIBUTES.dup
|
|
|
|
vars.unshift(:do_validate)
|
|
|
|
attrs.each do |name, value|
|
|
|
|
if vars.include?(name.to_s)
|
2005-11-23 12:49:02 +03:00
|
|
|
__send__("#{name}=", value)
|
2004-10-16 08:51:15 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2004-03-21 12:20:47 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
def to_s
|
|
|
|
rv = ""
|
|
|
|
if @href
|
|
|
|
rv << %Q[<?xml-stylesheet]
|
|
|
|
ATTRIBUTES.each do |name|
|
2005-11-23 12:49:02 +03:00
|
|
|
if __send__(name)
|
|
|
|
rv << %Q[ #{name}="#{h __send__(name)}"]
|
2004-10-16 08:51:15 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
rv << %Q[?>]
|
|
|
|
end
|
|
|
|
rv
|
|
|
|
end
|
2004-03-21 12:20:47 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
remove_method(:href=)
|
|
|
|
def href=(value)
|
|
|
|
@href = value
|
|
|
|
if @href and @type.nil?
|
|
|
|
@type = guess_type(@href)
|
|
|
|
end
|
|
|
|
@href
|
|
|
|
end
|
2004-03-21 12:20:47 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
remove_method(:alternate=)
|
|
|
|
def alternate=(value)
|
|
|
|
if value.nil? or /\A(?:yes|no)\z/ =~ value
|
|
|
|
@alternate = value
|
|
|
|
else
|
|
|
|
if @do_validate
|
|
|
|
args = ["?xml-stylesheet?", %Q[alternate="#{value}"]]
|
|
|
|
raise NotAvailableValueError.new(*args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@alternate
|
|
|
|
end
|
2004-03-21 12:20:47 +03:00
|
|
|
|
2004-11-27 11:47:28 +03:00
|
|
|
def setup_maker(maker)
|
|
|
|
xss = maker.xml_stylesheets.new_xml_stylesheet
|
|
|
|
ATTRIBUTES.each do |attr|
|
|
|
|
xss.__send__("#{attr}=", __send__(attr))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
private
|
|
|
|
def guess_type(filename)
|
2004-11-01 16:30:08 +03:00
|
|
|
/\.([^.]+)$/ =~ filename
|
2004-10-16 08:51:15 +04:00
|
|
|
GUESS_TABLE[$1]
|
|
|
|
end
|
2004-03-21 12:20:47 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
end
|
2004-03-21 12:20:47 +03:00
|
|
|
end
|