diff options
Diffstat (limited to '_plugins')
-rw-r--r-- | _plugins/devaway.rb | 15 | ||||
-rw-r--r-- | _plugins/downloads.rb | 44 | ||||
-rw-r--r-- | _plugins/filters.rb | 23 | ||||
-rw-r--r-- | _plugins/lib/weightedrandomizer.rb | 60 | ||||
-rw-r--r-- | _plugins/mirrors.rb | 52 | ||||
-rw-r--r-- | _plugins/navigation.rb | 97 | ||||
-rw-r--r-- | _plugins/news.rb | 58 | ||||
-rw-r--r-- | _plugins/packages.rb | 37 | ||||
-rw-r--r-- | _plugins/planet.rb | 34 | ||||
-rw-r--r-- | _plugins/tags.rb | 41 | ||||
-rw-r--r-- | _plugins/wiki.rb | 30 |
11 files changed, 491 insertions, 0 deletions
diff --git a/_plugins/devaway.rb b/_plugins/devaway.rb new file mode 100644 index 0000000..5d411a0 --- /dev/null +++ b/_plugins/devaway.rb @@ -0,0 +1,15 @@ +module Gentoo + class DevawayGenerator < Jekyll::Generator + DEVAWAY_XML = '_data/devaway.xml' + + def generate(site) + data = Nokogiri::XML(File.open(DEVAWAY_XML)) + + site.data['devaway'] ||= { } + + data.xpath('/devaway/dev').each do |dev| + site.data['devaway'][dev['nick']] = dev.xpath('./reason/text()').first.content + end + end + end +end
\ No newline at end of file diff --git a/_plugins/downloads.rb b/_plugins/downloads.rb new file mode 100644 index 0000000..69720e3 --- /dev/null +++ b/_plugins/downloads.rb @@ -0,0 +1,44 @@ +require 'date' + +module Gentoo + class DownloadGenerator < Jekyll::Generator + DIR = '_data/downloads/' + + def generate(site) + site.data['downloads'] ||= {} + + Dir.glob(DIR + '*') do |raw_arch| + arch = File.basename(raw_arch) + site.data['downloads'][arch] = {} + + File.readlines(raw_arch + '/iso.txt').each do |line| + #puts line + next if line.start_with? '#' + if line =~ /^(\d{8})\/(\S+) (\d+)$/ + date = Date.parse("%s-%s-%s" % [$1[0..3], $1[4..5], $1[6..7]]) + site.data['downloads'][arch]['iso'] ||= {} + site.data['downloads'][arch]['iso']['minimal'] = { 'date' => date, 'filename' => "%s/%s" % [$1, $2], 'size' => $3 } + end + end + + File.readlines(raw_arch + '/stage3.txt').each do |line| + # puts line + next if line.start_with? '#' + + if line =~ /^(\d{8})\/(\w+\/)?stage3-(.*)-\d{8}.tar.bz2 (\d+)$/ + date = Date.parse("%s-%s-%s" % [$1[0..3], $1[4..5], $1[6..7]]) + + site.data['downloads'][arch]['stage3'] ||= {} + site.data['downloads'][arch]['stage3'][$3] = { + 'name' => $3, + 'date' => date, + 'filename' => "%s/%sstage3-%s-%s.tar.bz2" % [$1, $2, $3, $1], + 'subdir' => $2, + 'size' => $4 + } + end + end + end + end + end +end
\ No newline at end of file diff --git a/_plugins/filters.rb b/_plugins/filters.rb new file mode 100644 index 0000000..1a4f3c1 --- /dev/null +++ b/_plugins/filters.rb @@ -0,0 +1,23 @@ +module Gentoo + module Filters + UNITS = %W(B KiB MiB GiB TiB).freeze + + def nice_filesize(input) + number = input.to_i + if number < 1024 + exponent = 0 + else + max_exp = UNITS.size - 1 + + exponent = (Math.log(number) / Math.log(1024)).to_i + exponent = max_exp if exponent > max_exp + + number /= 1024 ** exponent + end + + "#{number} #{UNITS[exponent]}" + end + end +end + +Liquid::Template.register_filter(Gentoo::Filters)
\ No newline at end of file diff --git a/_plugins/lib/weightedrandomizer.rb b/_plugins/lib/weightedrandomizer.rb new file mode 100644 index 0000000..6d36b92 --- /dev/null +++ b/_plugins/lib/weightedrandomizer.rb @@ -0,0 +1,60 @@ +# Implements weighted randomization for a group of weighted items. +# This class expects a hash of key -> value pairs where the value is +# the weight for the item. +# +# @example Usage +# wr = WeightedRandomizer.new('queue1' => 25, 'queue2' => 100, 'queue3' => 2) +# puts "Using queue #{wr.sample}" +# +# @note Mostly adapted from recipe 5.11 from the Ruby Cookbook. +class WeightedRandomizer + VERSION = '0.1.2' + + # Creates a new instance. + # + # @param [Hash] items the weighted items (key item, value weight) + # @return [WeightedRandomizer] + def initialize(items) + @items = normalize(items) + end + + # Returns one or more weighted random values. + # + # @param [Integer] num the number of samples to return + # @return [Object, Array<Object>] one or more sampled items + def sample(num = nil) + return _sample unless num + Array.new(num) { _sample } + end + + private + + # Returns a single weighted random value. + # + # @return [Object] the weighted item + def _sample + pick = rand + @items.each do |key, weight| + return key if pick <= weight + pick -= weight + end + nil + end + + # Normalizes the weights to float values so that + # arbitrary integer/float weights can be specified. + # + # @param [Hash] items the weighted items (key item, value weight) + # @return [Hash] the items with their weights normalized + def normalize(items) + normalized = {} + sum = items.values.inject(0.0, :+) + items.each do |key, weight| + normalized[key] = weight / sum + end + + normalized + end +end + + diff --git a/_plugins/mirrors.rb b/_plugins/mirrors.rb new file mode 100644 index 0000000..1d30238 --- /dev/null +++ b/_plugins/mirrors.rb @@ -0,0 +1,52 @@ +module Gentoo + class StaticMirrorDataGenerator < Jekyll::Generator + DISTFILES_XML = '_data/mirrors-distfiles.xml' + RSYNC_XML = '_data/mirrors-rsync.xml' + + def generate(site) + site.data['mirrors'] ||= { 'rsync' => {}, 'distfiles' => {} } + + load_mirrors(site, DISTFILES_XML, 'distfiles') + load_mirrors(site, RSYNC_XML, 'rsync') + end + + def load_mirrors(site, xml, key) + mirrorinfo = Nokogiri::XML(File.open(xml)) + + mirrorinfo.xpath('/mirrors/mirrorgroup').each do |mirrorgroup| + region = mirrorgroup['region'] + country = mirrorgroup['countryname'] + country_code = mirrorgroup['country'] + + site.data['mirrors'][key][region] ||= {} + site.data['mirrors'][key][region][country_code] ||= { 'name' => country, 'mirrors' => [] } + + mirrorgroup.children.each do |mirror| + mirror_data = { 'uris' => [] } + next unless mirror.name == 'mirror' + + mirror.children.each do |tag| + + case tag.name + when 'name' + mirror_data['name'] = tag.text + when 'uri' + uri = { + 'protocol' => tag['protocol'], + 'ipv4' => tag['ipv4'], + 'ipv6' => tag['ipv6'], + 'partial' => tag['partial'], + 'uri' => tag.text + } + + mirror_data['uris'] << uri + end + end + + site.data['mirrors'][key][region][country_code]['mirrors'] << mirror_data + end + end + + end + end +end
\ No newline at end of file diff --git a/_plugins/navigation.rb b/_plugins/navigation.rb new file mode 100644 index 0000000..0df1404 --- /dev/null +++ b/_plugins/navigation.rb @@ -0,0 +1,97 @@ +module Gentoo + module Tags + class NavigationTag < Liquid::Tag + def initialize(tag_name, level, tokens) + super + @level = level.strip + end + + def render(context) + all_pages = context.registers[:site].pages + current_page = context.registers[:page] + + if @level == 'primary' + primary(all_pages, current_page) + elsif @level == 'secondary' + secondary(all_pages, current_page) + elsif @level == 'tertiary' + tertiary(all_pages, current_page) + elsif @level =~ /^sitemap:(.*)$/ + sitemap(all_pages, $1) + else + 'unknown navigation level' + end + end + + private + def primary(all_pages, current_page) + pages = all_pages.select {|page| page.data['nav1-show'] == true } + + generate(pages, current_page, '1') + end + + def secondary(all_pages, current_page) + pages = all_pages.select {|page| page.data['nav1'] == current_page['nav1'] and page.data['nav2-show'] == true } + + generate(pages, current_page, '2') + end + + def tertiary(all_pages, current_page) + pages = all_pages.select {|page| page.data['nav1'] == current_page['nav1'] and page.data['nav2'] == current_page['nav2'] and page.data['nav3-show'] == true } + + generate(pages, current_page, '3') + end + + def sitemap(all_pages, nav1) + pages = all_pages.select {|page| page.data['nav1'] == nav1 and page.data['nav2-show'] == true } + + generate(pages, {}, '2') + end + + def generate(all_pages, current_page, level) + level_show = "nav%s-show" % level + level_weight = "nav%s-weight" % level + level_str = "nav%s" % level + + pages = all_pages.select {|page| page.data[level_show] == true } + pages.sort! do |a, b| + if a.data.has_key? level_weight and b.data.has_key? level_weight + a.data[level_weight] <=> b.data[level_weight] + else + a_title = a.data['navtitle'] || a.data['title'] + b_title = b.data['navtitle'] || b.data['title'] + + a_title <=> b_title + end + end + + html = '' + pages.each do |page| + css_class = current_page[level_str] == page.data[level_str] ? 'active' : '' + + html += "<li class=\"%s\">" % css_class + + if page.data.has_key? 'redirect' + html += '<a href="' + page.data['redirect'] + '">' + else + html += '<a href="' + page.url.gsub('index.html', '') + '">' + end + + if page.data.has_key? 'navtitle' + html += page.data['navtitle'] + else + html += page.data['title'] + end + + html += "</a></li>\n" + end + + html + end + + + end + end +end + +Liquid::Template.register_tag('navigation', Gentoo::Tags::NavigationTag)
\ No newline at end of file diff --git a/_plugins/news.rb b/_plugins/news.rb new file mode 100644 index 0000000..0b3d2dc --- /dev/null +++ b/_plugins/news.rb @@ -0,0 +1,58 @@ +module Gentoo + class NewsGenerator < Jekyll::Generator + NEWS_DIR = '_data/news/' + + def generate(site) + site.data['newsitems'] ||= [] + + Dir.chdir(NEWS_DIR) do + Dir.glob('[0-9][0-9][0-9][0-9]').reverse.each do |year| + Dir.glob("#{year}/[0-9][0-9][0-9][0-9]*").reverse.each do |path| + begin + name = path.gsub(/#{year}\//, '') + + site.pages << NewsPage.new(site, path, name) + rescue + # fail them silently + end + end + end + end + end + end + + class NewsPage < Jekyll::Page + def initialize(site, path, name) + @site = site + @base = @site.source + @dir = NewsGenerator::NEWS_DIR + @name = "#{name}.html" + + self.process(@name) + self.read_yaml(File.join(@base, NewsGenerator::NEWS_DIR, path), "#{name}.en.txt") + + self.data['permalink'] = "/support/news-items/#{name}.html" + self.data['layout'] = 'page-pre' + self.data['nav1'] = 'support' + self.data['nav2'] = 'news-items' + + File.readlines(File.join(@base, NewsGenerator::NEWS_DIR, path, "#{name}.en.txt")).each do |line| + if line =~ /Title: (.*)$/ + @title = $1 + end + + if line =~ /Author: (.*)$/ + @author = $1 + end + + if line =~ /Posted: (.*)$/ + @date = $1 + end + end + + site.data['newsitems'] << { 'title' => @title, 'author' => @author, 'date' => @date, 'url' => self.data['permalink'] } + + self.data['title'] = @title + end + end +end
\ No newline at end of file diff --git a/_plugins/packages.rb b/_plugins/packages.rb new file mode 100644 index 0000000..eab8168 --- /dev/null +++ b/_plugins/packages.rb @@ -0,0 +1,37 @@ +module Gentoo + class PackagesGenerator < Jekyll::Generator + XML = '_data/packages.xml' + + def generate(site) + xml = Nokogiri::XML(File.open(XML)) + # atom is mainly one namespace + xml.remove_namespaces! + + site.data['packages'] ||= { 'updates' => [] } + + xml.xpath('/feed/entry').each do |item| + item_data = {} + + item.children.each do |tag| + case tag.name + when 'title' + tag.css('span').each do |span| + if span['class'] == 'cpvstr' + item_data['atom'] = span.text.strip + item_data['atom_c'], pv = item_data['atom'].split('/', 2) + item_data['atom_p'], item_data['atom_v'] = pv.rpartition('-') + elsif span['class'] == 'description' + item_data['description'] = span.text.strip + end + end + when 'link' + item_data['uri'] = tag['href'] + end + end + + site.data['packages']['updates'] << item_data + end + + end + end +end
\ No newline at end of file diff --git a/_plugins/planet.rb b/_plugins/planet.rb new file mode 100644 index 0000000..4c9df0d --- /dev/null +++ b/_plugins/planet.rb @@ -0,0 +1,34 @@ +module Gentoo + class PlanetGenerator < Jekyll::Generator + PLANET_XML = '_data/planet.xml' + + def generate(site) + planetinfo = Nokogiri::XML(File.open(PLANET_XML)) + # author is the only thing taken from the dublin core, we don't need namespaces for that + planetinfo.remove_namespaces! + + site.data['planet'] ||= { 'posts' => [] } + + planetinfo.xpath('/rss/channel/item').each do |item| + item_data = {} + + item.children.each do |tag| + case tag.name + when 'title' + item_data['title'] = tag.text.partition(':')[2].strip + when 'link' + item_data['uri'] = tag.text + when 'creator' + if tag.text =~ /^(.*) \(([^)]+)\)$/ + item_data['author'] = $1 + item_data['nick'] = $2 + end + end + end + + site.data['planet']['posts'] << item_data + end + + end + end +end
\ No newline at end of file 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 += '<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) diff --git a/_plugins/wiki.rb b/_plugins/wiki.rb new file mode 100644 index 0000000..ee2c27c --- /dev/null +++ b/_plugins/wiki.rb @@ -0,0 +1,30 @@ +module Gentoo + class WikiGenerator < Jekyll::Generator + WIKI_XML = '_data/wiki.xml' + + def generate(site) + wikiinfo = Nokogiri::XML(File.open(WIKI_XML)) + # author is the only thing taken from the dublin core, we don't need namespaces for that + wikiinfo.remove_namespaces! + + site.data['wiki'] ||= { 'updates' => [] } + + wikiinfo.xpath('/rss/channel/item').each do |item| + item_data = {} + + item.children.each do |tag| + case tag.name + when 'title' + item_data['title'] = tag.text + when 'link' + item_data['uri'] = tag.text + when 'creator' + item_data['author'] = tag.text + end + end + + site.data['wiki']['updates'] << item_data + end + end + end +end
\ No newline at end of file |