aboutsummaryrefslogtreecommitdiff
blob: 7ad24bb5038c98481c4d581dbd5db7e03dca7384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require_relative 'lib/weightedrandomizer'

module Gentoo
  module Tags
    class AdsTag < Liquid::Tag
      # When changing this, you must recalculate the columns in the HTML below as well!
      AD_COUNT = 4

      def render(context)
        ads = context.registers[:site].data['ads']['active']

        ad_html = ''
        first_ad = true
        raw_weighted_ads = Hash[ads.map { |ad| [ad, ad['weight'] || 0] }]
        ads_wr = WeightedRandomizer.new(raw_weighted_ads)
        ads_wr.sample(AD_COUNT * 10).uniq.slice(0, AD_COUNT).each do |ad|
          if first_ad
            ad_html += '<div class="col-xs-12 col-md-2 col-md-offset-2 sponsorlogo">'
            first_ad = false
          else
            ad_html += '<div class="col-xs-12 col-md-2 sponsorlogo">'
          end

          ad_html += '<!-- sponsor{name:%s,weight:%d} -->' % [ad['name'], ad['weight']]

          if ad.has_key? 'img'
            ad_html += "<a href=\"%s\" title=\"%s\"><img src=\"/assets/img/sponsors/ads/%s\" alt=\"%s\"></a>" %
              [ad['link'], ad['alt'], ad['img'], ad['alt']]
          else
            ad_html += "<span class=\"text-ad\"><span class=\"text-ad-content\">%s</span></span>" % ad['blurb']
          end

          ad_html += "</div>\n"
        end

        ad_html
      end
    end
  end
end

Liquid::Template.register_tag('render_ads', Gentoo::Tags::AdsTag)