Class: TD2Planet::Formatter

Inherits:
Object
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/td2planet/formatter.rb

Direct Known Subclasses

DefaultFormatter, SampleFormatter

Constant Summary collapse

ERB_METHODS =
[]
TukkomiLinkRe =
/^<p><a href="(.+)">ツッコミを入れる<\/a><\/p>$/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Formatter

Returns a new instance of Formatter.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/td2planet/formatter.rb', line 50

def initialize(config)
  @config = config
  @config['title'] ||= '(no title Planet)'
  @config['tdiary_theme_path'] ||= '/tdiary/theme'
  @config['tdiary_theme'] ||= 'default'
  @config['date_strftime_format'] ||= '%Y-%m-%d'
  @config['sanchor_strftime_format'] ||= '%H:%M:%S'
  @base_uri = URI.parse(@config['base_uri'])
  @config['templates_path'] ||= []
  @config['templates_path'].push(default_templates_dir)
  ERB_METHODS.each do |method_name, basename|
    @config['templates_path'].find do |dir|
      fname = File.expand_path(basename, dir)
      if File.exist?(fname)
        puts "use template #{basename}: #{fname}"
        erb = ERB.new(File.read(fname), nil, '-')
        eval("def self.#{method_name}\n#{erb.src}\nend\n", binding, fname, 0)
        true
      else
        false
      end
    end
  end
end

Class Method Details

.def_erb_method(method_name, fname = nil) ⇒ Object



16
17
18
19
20
21
# File 'lib/td2planet/formatter.rb', line 16

def self.def_erb_method(method_name, fname=nil)
  if /\A\w+/ =~ method_name
    fname ||= "#{$&}.rhtml"
  end
  ERB_METHODS << [method_name, fname]
end

Instance Method Details

#date_format(item) ⇒ Object



75
76
77
78
# File 'lib/td2planet/formatter.rb', line 75

def date_format(item)
  return "" unless item.respond_to?(:date) && item.date
  item.date.localtime.strftime(@config['date_strftime_format'])
end

#default_templates_dirObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/td2planet/formatter.rb', line 35

def default_templates_dir
  basename = 'layout.rhtml'
  dir = File.expand_path('../../data/td2planet/templates', File.dirname(__FILE__))
  if File.exist?(File.join(dir, basename))
    return dir
  end

  require 'rbconfig'
  dir = File.expand_path('td2planet/templates', Config::CONFIG['datadir'])
  if File.exist?(File.join(dir, basename))
   return dir
  end
  raise "not found templates"
end

#hk(s) ⇒ Object



31
32
33
# File 'lib/td2planet/formatter.rb', line 31

def hk(s)
  h(k(s))
end

#k(s) ⇒ Object



28
29
30
# File 'lib/td2planet/formatter.rb', line 28

def k(s)
  NKF.nkf('-wm0', s.to_s)
end


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/td2planet/formatter.rb', line 184

def move_tukkomi_link(html)
  if TukkomiLinkRe =~ html
    tukkomi_link = $&
    tukkomi_uri = $1
    re = Regexp.new(Regexp.quote(tukkomi_link))
    tukkomi_moved_html = html.gsub(re, '')
    re = Regexp.new(Regexp.quote('<!--<div class="caption"></div>-->'))
    tukkomi_moved_html.sub!(re) { %Q|<div class="caption">[<a href="#{tukkomi_uri}">ツッコミを入れる</a>]</div>| }
    # other day tukkomi_link found
    if TukkomiLinkRe =~ tukkomi_moved_html
      html.gsub!(TukkomiLinkRe) { %Q|<div class="caption">[<a href="#{$1}">ツッコミを入れる</a>]</div>| }
    else
      html = tukkomi_moved_html
    end
  end
  html
end

#relative_path_to_absolute_uri(attr_value, base_uri) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/td2planet/formatter.rb', line 123

def relative_path_to_absolute_uri(attr_value, base_uri)
  uri = URI.parse(attr_value)
  if uri.scheme.nil?
    URI.parse(base_uri) + uri
  else
    uri
  end
rescue URI::InvalidURIError
  attr_value
end

#sanchor_format(item) ⇒ Object



79
80
81
82
# File 'lib/td2planet/formatter.rb', line 79

def sanchor_format(item)
  return "" unless item.respond_to?(:date) && item.date
  item.date.localtime.strftime(@config['sanchor_strftime_format'])
end

#skip?(item) ⇒ Boolean

override

Returns:

  • (Boolean)


90
91
92
# File 'lib/td2planet/formatter.rb', line 90

def skip?(item)
  false
end

#tag_attr_relative_path_to_absolute_uri(tag, attr_name, base_uri) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/td2planet/formatter.rb', line 134

def tag_attr_relative_path_to_absolute_uri(tag, attr_name, base_uri)
  tag.gsub!(/#{Regexp.quote(attr_name)}=([\"\'])([^\"\']*)\1/i) do
    %Q!#{attr_name}=#{$1}#{relative_path_to_absolute_uri($2, base_uri)}#{$1}!
  end or tag.gsub!(/#{Regexp.quote(attr_name)}=(\S+)/) do
    %Q!#{attr_name}=#{relative_path_to_absolute_uri($1, base_uri)}!
  end
  tag
end

#to_author(item) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/td2planet/formatter.rb', line 175

def to_author(item)
  if item.respond_to?(:dc_creator) && item.dc_creator
    " (#{hk(item.dc_creator)})"
  else
    ""
  end
end

#to_categories(item) ⇒ Object



170
171
172
173
# File 'lib/td2planet/formatter.rb', line 170

def to_categories(item)
  return "" unless item.respond_to?(:dc_subjects)
  h(item.dc_subjects.collect{|s|"[#{k(s.content)}]" if /./ =~ s.content})
end

#to_html(rss_list) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/td2planet/formatter.rb', line 94

def to_html(rss_list)
  @rss_list = rss_list
  day_rss = {}
  rss_list.each do |rss|
    next unless rss.items
    rss.items.each do |item|
      next if skip?(item)
      day = (day_rss[[date_format(item), rss]] ||= Array.new)
      day.push(item)
    end
  end
  days = []
  day_rss.keys.sort_by do |date, rss|
    date
  end.reverse_each do |key|
    date, rss = key
    items = day_rss[key]
    items = items.sort_by do |item|
      # tdiary makerss plugin generates same time entries
      item.date.to_s + item.link
    end
    days << {:items => items, :rss => rss}
  end
  days = days.sort_by do |day|
    -day[:items].collect{|item| item.date.to_i}.max
  end
  layout(days)
end

#to_rss(rss_list, version = '1.0', basename = 'rss.xml') ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/td2planet/formatter.rb', line 205

def to_rss(rss_list, version='1.0', basename='rss.xml')
  RSS::Maker.make(version) do |maker|
    maker.channel.about = @base_uri + basename
    maker.channel.title = @config['title']
    maker.channel.link = @base_uri
    maker.channel.description = "#{@base_uri} - #{@config['title']}"

    maker.items.do_sort = true

    rss_list.each do |rss|
      rss.items.each do |item|
        next if skip?(item)
        new_item = maker.items.new_item
        %w"link title date".each do |attr|
          value = item.__send__(attr)
          value = k(value) if value.is_a?(String)
          new_item.__send__("#{attr}=", value)
        end
      end
    end
  end
end

#to_sanchor(item) ⇒ Object



166
167
168
# File 'lib/td2planet/formatter.rb', line 166

def to_sanchor(item)
  %Q!<a href="#{hk item.link}"><span class="sanchor">#{h sanchor_format(item)}</span></a> !
end

#to_section_body(item) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/td2planet/formatter.rb', line 143

def to_section_body(item)
  if item.respond_to?(:content_encoded) && item.content_encoded
    k(item.content_encoded).gsub(/<([aA]\b[\s\S]+?)>/) do
      a = tag_attr_relative_path_to_absolute_uri($1, "href", item.link)
      %Q!<#{a} rel="nofollow">!
    #end.gsub(/<img\b[\s\S]+?>/i) do
    #  tag_attr_relative_path_to_absolute_uri($&, "src", item.link)
    end.gsub(/<img\b[\s\S]+?>/i) do
      img = $&
      case img
      when /alt=([\"\'])(.+?)\1/
        $2
      when /alt=(\S+?)/
        $1
      else
        "[img]"
      end
    end
  else
    '<p>' + h(k(item.description)).gsub(/\r?\n/, '<br>') + '</p>'
  end
end

#too_old?(item, sec = 7*24*60*60) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/td2planet/formatter.rb', line 84

def too_old?(item, sec=7*24*60*60)
  return false unless item.respond_to?(:date) && item.date
  item.date < Time.now - sec
end