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 += '