1 | require "rss/utils"
|
---|
2 |
|
---|
3 | module RSS
|
---|
4 |
|
---|
5 | module XMLStyleSheetMixin
|
---|
6 | attr_accessor :xml_stylesheets
|
---|
7 | def initialize(*args)
|
---|
8 | super
|
---|
9 | @xml_stylesheets = []
|
---|
10 | end
|
---|
11 |
|
---|
12 | private
|
---|
13 | def xml_stylesheet_pi
|
---|
14 | xsss = @xml_stylesheets.collect do |xss|
|
---|
15 | pi = xss.to_s
|
---|
16 | pi = nil if /\A\s*\z/ =~ pi
|
---|
17 | pi
|
---|
18 | end.compact
|
---|
19 | xsss.push("") unless xsss.empty?
|
---|
20 | xsss.join("\n")
|
---|
21 | end
|
---|
22 | end
|
---|
23 |
|
---|
24 | class XMLStyleSheet
|
---|
25 |
|
---|
26 | include Utils
|
---|
27 |
|
---|
28 | ATTRIBUTES = %w(href type title media charset alternate)
|
---|
29 |
|
---|
30 | GUESS_TABLE = {
|
---|
31 | "xsl" => "text/xsl",
|
---|
32 | "css" => "text/css",
|
---|
33 | }
|
---|
34 |
|
---|
35 | attr_accessor(*ATTRIBUTES)
|
---|
36 | attr_accessor(:do_validate)
|
---|
37 | def initialize(*attrs)
|
---|
38 | @do_validate = true
|
---|
39 | ATTRIBUTES.each do |attr|
|
---|
40 | __send__("#{attr}=", nil)
|
---|
41 | end
|
---|
42 | vars = ATTRIBUTES.dup
|
---|
43 | vars.unshift(:do_validate)
|
---|
44 | attrs.each do |name, value|
|
---|
45 | if vars.include?(name.to_s)
|
---|
46 | __send__("#{name}=", value)
|
---|
47 | end
|
---|
48 | end
|
---|
49 | end
|
---|
50 |
|
---|
51 | def to_s
|
---|
52 | rv = ""
|
---|
53 | if @href
|
---|
54 | rv << %Q[<?xml-stylesheet]
|
---|
55 | ATTRIBUTES.each do |name|
|
---|
56 | if __send__(name)
|
---|
57 | rv << %Q[ #{name}="#{h __send__(name)}"]
|
---|
58 | end
|
---|
59 | end
|
---|
60 | rv << %Q[?>]
|
---|
61 | end
|
---|
62 | rv
|
---|
63 | end
|
---|
64 |
|
---|
65 | remove_method(:href=)
|
---|
66 | def href=(value)
|
---|
67 | @href = value
|
---|
68 | if @href and @type.nil?
|
---|
69 | @type = guess_type(@href)
|
---|
70 | end
|
---|
71 | @href
|
---|
72 | end
|
---|
73 |
|
---|
74 | remove_method(:alternate=)
|
---|
75 | def alternate=(value)
|
---|
76 | if value.nil? or /\A(?:yes|no)\z/ =~ value
|
---|
77 | @alternate = value
|
---|
78 | else
|
---|
79 | if @do_validate
|
---|
80 | args = ["?xml-stylesheet?", %Q[alternate="#{value}"]]
|
---|
81 | raise NotAvailableValueError.new(*args)
|
---|
82 | end
|
---|
83 | end
|
---|
84 | @alternate
|
---|
85 | end
|
---|
86 |
|
---|
87 | def setup_maker(maker)
|
---|
88 | xss = maker.xml_stylesheets.new_xml_stylesheet
|
---|
89 | ATTRIBUTES.each do |attr|
|
---|
90 | xss.__send__("#{attr}=", __send__(attr))
|
---|
91 | end
|
---|
92 | end
|
---|
93 |
|
---|
94 | private
|
---|
95 | def guess_type(filename)
|
---|
96 | /\.([^.]+)$/ =~ filename
|
---|
97 | GUESS_TABLE[$1]
|
---|
98 | end
|
---|
99 |
|
---|
100 | end
|
---|
101 | end
|
---|