class TD2Planet::Formatter

Constants

ERB_METHODS
TukkomiLinkRe

Public Class Methods

def_erb_method(method_name, fname=nil) click to toggle source
   # File lib/td2planet/formatter.rb
15 def self.def_erb_method(method_name, fname=nil)
16   if /\A\w+/ =~ method_name
17     fname ||= "#{$&}.rhtml"
18   end
19   ERB_METHODS << [method_name, fname]
20 end
new(config) click to toggle source
   # File lib/td2planet/formatter.rb
49 def initialize(config)
50   @config = config
51   @config['title'] ||= '(no title Planet)'
52   @config['tdiary_theme_path'] ||= '/tdiary/theme'
53   @config['tdiary_theme'] ||= 'default'
54   @config['date_strftime_format'] ||= '%Y-%m-%d'
55   @config['sanchor_strftime_format'] ||= '%H:%M:%S'
56   @base_uri = URI.parse(@config['base_uri'])
57   @config['templates_path'] ||= []
58   @config['templates_path'].push(default_templates_dir)
59   ERB_METHODS.each do |method_name, basename|
60     @config['templates_path'].find do |dir|
61       fname = File.expand_path(basename, dir)
62       if File.exist?(fname)
63         puts "use template #{basename}: #{fname}"
64         erb = ERB.new(File.read(fname), nil, '-')
65         eval("def self.#{method_name}\n#{erb.src}\nend\n", binding, fname, 0)
66         true
67       else
68         false
69       end
70     end
71   end
72 end

Public Instance Methods

date_format(item) click to toggle source
   # File lib/td2planet/formatter.rb
74 def date_format(item)
75   return "" unless item.respond_to?(:date) && item.date
76   item.date.localtime.strftime(@config['date_strftime_format'])
77 end
default_templates_dir() click to toggle source
   # File lib/td2planet/formatter.rb
34 def default_templates_dir
35   basename = 'layout.rhtml'
36   dir = File.expand_path('../../data/td2planet/templates', File.dirname(__FILE__))
37   if File.exist?(File.join(dir, basename))
38     return dir
39   end
40 
41   require 'rbconfig'
42   dir = File.expand_path('td2planet/templates', Config::CONFIG['datadir'])
43   if File.exist?(File.join(dir, basename))
44    return dir
45   end
46   raise "not found templates"
47 end
hk(s) click to toggle source
   # File lib/td2planet/formatter.rb
30 def hk(s)
31   h(k(s))
32 end
k(s) click to toggle source
   # File lib/td2planet/formatter.rb
27 def k(s)
28   NKF.nkf('-wm0', s.to_s)
29 end
relative_path_to_absolute_uri(attr_value, base_uri) click to toggle source
    # File lib/td2planet/formatter.rb
122 def relative_path_to_absolute_uri(attr_value, base_uri)
123   uri = URI.parse(attr_value)
124   if uri.scheme.nil?
125     URI.parse(base_uri) + uri
126   else
127     uri
128   end
129 rescue URI::InvalidURIError
130   attr_value
131 end
sanchor_format(item) click to toggle source
   # File lib/td2planet/formatter.rb
78 def sanchor_format(item)
79   return "" unless item.respond_to?(:date) && item.date
80   item.date.localtime.strftime(@config['sanchor_strftime_format'])
81 end
skip?(item) click to toggle source

override

   # File lib/td2planet/formatter.rb
89 def skip?(item)
90   false
91 end
tag_attr_relative_path_to_absolute_uri(tag, attr_name, base_uri) click to toggle source
    # File lib/td2planet/formatter.rb
133 def tag_attr_relative_path_to_absolute_uri(tag, attr_name, base_uri)
134   tag.gsub!(/#{Regexp.quote(attr_name)}=([\"\'])([^\"\']*)\1/i) do
135     %Q!#{attr_name}=#{$1}#{relative_path_to_absolute_uri($2, base_uri)}#{$1}!
136   end or tag.gsub!(/#{Regexp.quote(attr_name)}=(\S+)/) do
137     %Q!#{attr_name}=#{relative_path_to_absolute_uri($1, base_uri)}!
138   end
139   tag
140 end
to_author(item) click to toggle source
    # File lib/td2planet/formatter.rb
174 def to_author(item)
175   if item.respond_to?(:dc_creator) && item.dc_creator
176     " (#{hk(item.dc_creator)})"
177   else
178     ""
179   end
180 end
to_categories(item) click to toggle source
    # File lib/td2planet/formatter.rb
169 def to_categories(item)
170   return "" unless item.respond_to?(:dc_subjects)
171   h(item.dc_subjects.collect{|s|"[#{k(s.content)}]" if /./ =~ s.content})
172 end
to_html(rss_list) click to toggle source
    # File lib/td2planet/formatter.rb
 93 def to_html(rss_list)
 94   @rss_list = rss_list
 95   day_rss = {}
 96   rss_list.each do |rss|
 97     next unless rss.items
 98     rss.items.each do |item|
 99       next if skip?(item)
100       day = (day_rss[[date_format(item), rss]] ||= Array.new)
101       day.push(item)
102     end
103   end
104   days = []
105   day_rss.keys.sort_by do |date, rss|
106     date
107   end.reverse_each do |key|
108     date, rss = key
109     items = day_rss[key]
110     items = items.sort_by do |item|
111       # tdiary makerss plugin generates same time entries
112       item.date.to_s + item.link
113     end
114     days << {:items => items, :rss => rss}
115   end
116   days = days.sort_by do |day|
117     -day[:items].collect{|item| item.date.to_i}.max
118   end
119   layout(days)
120 end
to_rss(rss_list, version='1.0', basename='rss.xml') click to toggle source
    # File lib/td2planet/formatter.rb
204 def to_rss(rss_list, version='1.0', basename='rss.xml')
205   RSS::Maker.make(version) do |maker|
206     maker.channel.about = @base_uri + basename
207     maker.channel.title = @config['title']
208     maker.channel.link = @base_uri
209     maker.channel.description = "#{@base_uri} - #{@config['title']}"
210 
211     maker.items.do_sort = true
212 
213     rss_list.each do |rss|
214       rss.items.each do |item|
215         next if skip?(item)
216         new_item = maker.items.new_item
217         %w"link title date".each do |attr|
218           value = item.__send__(attr)
219           value = k(value) if value.is_a?(String)
220           new_item.__send__("#{attr}=", value)
221         end
222       end
223     end
224   end
225 end
to_sanchor(item) click to toggle source
    # File lib/td2planet/formatter.rb
165 def to_sanchor(item)
166   %Q!<a href="#{hk item.link}"><span class="sanchor">#{h sanchor_format(item)}</span></a> !
167 end
to_section_body(item) click to toggle source
    # File lib/td2planet/formatter.rb
142 def to_section_body(item)
143   if item.respond_to?(:content_encoded) && item.content_encoded
144     k(item.content_encoded).gsub(/<([aA]\b[\s\S]+?)>/) do
145       a = tag_attr_relative_path_to_absolute_uri($1, "href", item.link)
146       %Q!<#{a} rel="nofollow">!
147     #end.gsub(/<img\b[\s\S]+?>/i) do
148     #  tag_attr_relative_path_to_absolute_uri($&, "src", item.link)
149     end.gsub(/<img\b[\s\S]+?>/i) do
150       img = $&
151       case img
152       when /alt=([\"\'])(.+?)\1/
153         $2
154       when /alt=(\S+?)/
155         $1
156       else
157         "[img]"
158       end
159     end
160   else
161     '<p>' + h(k(item.description)).gsub(/\r?\n/, '<br>') + '</p>'
162   end
163 end
too_old?(item, sec=7*24*60*60) click to toggle source
   # File lib/td2planet/formatter.rb
83 def too_old?(item, sec=7*24*60*60)
84   return false unless item.respond_to?(:date) && item.date
85   item.date < Time.now - sec
86 end