From b0c622da7c0a8bdabb33d63fd0c2d7c2b9df02ef Mon Sep 17 00:00:00 2001 From: Alex Legler Date: Thu, 2 Apr 2015 11:09:31 +0200 Subject: Initial version --- _plugins/tags.rb | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 _plugins/tags.rb (limited to '_plugins/tags.rb') diff --git a/_plugins/tags.rb b/_plugins/tags.rb new file mode 100644 index 0000000..5175cde --- /dev/null +++ b/_plugins/tags.rb @@ -0,0 +1,41 @@ +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 += '