画像が見つからないときにテキストリンクを表示する(true)か noimage画像を表示する(false)かの設定。
class="amazon"の時に画像の横に表示されるテキストと、画像の代替テキストなど(alt, title)は区別して、altや titleは常に設定されるようにして欲しい。
この辺のことです。
if with_image and @conf['amazon.hidename'] || pos != 'amazon' then
label = ''
elsif not label
label = %Q|#{amazon_title( item )}#{author}|
end
両方とも自分でなんとかしてみる。
def amazon_image( item )
image = {}
begin
size = case @conf['amazon.imgsize']
when 0; 'Large'
when 2; 'Small'
else; 'Medium'
end
image[:src] = item.elements.to_a( "#{size}Image/URL" )[0].text
image[:height] = item.elements.to_a( "#{size}Image/Height" )[0].text
image[:width] = item.elements.to_a( "#{size}Image/Width" )[0].text
rescue
image[:src] = image[:height] = image[:width] = nil
end
image
end
def amazon_default_image
image = {}
base = @conf['amazon.default_image_base'] || 'http://www.tdiary.org/images/amazondefaults/'
case @conf['amazon.imgsize']
when 0
image[:src] = "#{base}large.png"
image[:height] = 500
image[:width] = 380
when 2
image[:src] = "#{base}small.png"
image[:height] = 75
image[:width] = 57
else
image[:src] = "#{base}medium.png"
image[:height] = 160
image[:width] = 122
end
image
end
def amazon_to_html( item, with_image = true, label = nil, pos = 'amazon' )
with_image = false if @mode == 'categoryview'
author = amazon_author( item )
author = "(#{author})" unless author.empty?
label = %Q|#{amazon_title( item )}#{author}| unless label
if with_image
image = amazon_image( item )
image = amazon_default_image if not image[:src] and not @conf['amazon.nodefault']
if image[:src] then
img = <<-HTML
<img class="#{h pos}" src="#{h image[:src]}"
height="#{h image[:height]}" width="#{h image[:width]}"
alt="#{h label}" title="#{h label}">
HTML
img.gsub!( /\t/, '' )
label = '' if @conf['amazon.hidename'] || pos != 'amazon'
else
img = ''
end
end
url = amazon_url( item )
%Q|<a href="#{h url}">#{img}#{h label}</a>|
end
動いているようだ。labelという変数に二つの役割が与えられているのが微かに気になるが。
def amazon_get( asin, with_image = true, label = nil, pos = 'amazon' )
……(省略)……
rescue Timeout::Error
asin
rescue NoMethodError
if item == nil then
message = doc.elements.to_a( 'Items/Request/Errors/Error/Message' )[0].text
"#{label ? label : asin}<!--#{h @conf.to_native( message, 'utf-8' )}-->"
else
"#{label ? label : asin}<!--#{h $!}\n#{h $@.join( ' / ' )}-->"
end
end
labelは他の場所で HTMLエスケープ前の文字列として扱われているのでエスケープするのがよい。asinだって中身が英数字に違いなかろうがなんだろうがエスケープすればいい。
コメント(<!---->)の中身は h(ERB::Util.html_escape) する必要はなく .gsub(/--+/, '-') した方が良い、とまで言うとパラノイア・原理主義者っぽい気がするのは何故? コメントに起因する不具合に遭ったことがないから?