# -*- coding: utf-8 -*- module Jekyll class ReviewsGenerator < Generator safe true priority :low LOCALE = :ru def localize(input, format=nil) load_translations format = (format =~ /^:(\w+)/) ? $1.to_sym : format I18n.l input, :format => format end def load_translations unless I18n::backend.instance_variable_get(:@translations) I18n.backend.load_translations Dir[File.join(File.dirname(__FILE__), '../_locales/*.yml')] I18n.locale = LOCALE end end # Generates the reviews file def generate(site) @buffer = '' # Create the destination folder if necessary reviews_dest = File.join(site.dest, 'reviews') FileUtils.mkdir_p(reviews_dest) unless File.directory?(reviews_dest) # Write the contents of reviews file File.open(File.join(reviews_dest, 'index.html'), 'w') do |f| generate_header generate_content site f.write @buffer f.close end # Add the reviews file, so it won't be cleaned site.pages << Page.new(site, site.dest, 'reviews', 'index.html') end # Add the YAML header def generate_header @buffer += "---\nlayout: page\ntitle: Обзоры\n---\n" end # Add a string containing the review entries def generate_content(site) # Creates a hash mapping paths to tournaments tournaments = site.data['tournaments'] path = tournaments.map do |entry| entry['events'].map { |trn| entry['year'].to_s + "/" + trn['dir'].sub(/\/$/, '') } end.flatten trn = tournaments.map { |entry| entry['events'] }.flatten @trn_hash = Hash[ path.zip(trn) ] # Creates review entries site.posts.docs.reverse_each do |post| review_entry(site, post) unless post.data['pgn'].nil? end end # Creates a review entry from the given post def review_entry(site, post) # Adds the link to post @buffer += "\n
" @buffer += "#{post.data["title"]}" @buffer += "
\n" @buffer += "