2014-07-02 13:22:52 +04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
module Jekyll
|
|
|
|
class ReviewsGenerator < Generator
|
|
|
|
safe true
|
|
|
|
priority :low
|
|
|
|
|
2018-10-28 20:51:58 +04:00
|
|
|
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
|
2014-07-02 14:12:57 +04:00
|
|
|
|
2014-07-02 13:22:52 +04:00
|
|
|
# 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
|
2014-07-02 22:01:17 +04:00
|
|
|
@buffer += "---\nlayout: page\ntitle: Обзоры\n---\n"
|
2014-07-02 13:22:52 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Add a string containing the review entries
|
|
|
|
def generate_content(site)
|
2014-07-02 16:21:58 +04:00
|
|
|
# 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
|
2018-10-28 17:59:15 +04:00
|
|
|
site.posts.docs.reverse_each do |post|
|
2014-07-02 13:22:52 +04:00
|
|
|
review_entry(site, post) unless post.data['pgn'].nil?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a review entry from the given post
|
|
|
|
def review_entry(site, post)
|
2014-07-02 16:21:58 +04:00
|
|
|
# Adds the link to post
|
2014-07-02 13:22:52 +04:00
|
|
|
@buffer += "\n<p class=\"lead\" style=\"margin-bottom:0\">"
|
2018-10-28 17:59:15 +04:00
|
|
|
@buffer += "<a href=\"#{site.baseurl}#{post.url}\">#{post.data["title"]}</a>"
|
2014-07-02 13:22:52 +04:00
|
|
|
@buffer += "</p>\n"
|
|
|
|
|
|
|
|
@buffer += "<dl class=\"dl-horizontal\">\n"
|
|
|
|
|
2014-07-02 16:21:58 +04:00
|
|
|
# Adds the date of post
|
2014-07-02 14:12:57 +04:00
|
|
|
post_date = localize(post.date, "%d %B %Y")
|
2014-07-02 13:22:52 +04:00
|
|
|
@buffer += "<dt>Дата:</dt><dd>#{post_date}</dd>\n"
|
|
|
|
|
|
|
|
pgn = post.data['pgn']
|
|
|
|
if pgn =~ /^\//
|
|
|
|
none, year, tournament, basename = pgn.split '/'
|
|
|
|
else
|
|
|
|
year, tournament, basename = pgn.split '/'
|
|
|
|
end
|
|
|
|
|
2014-07-02 16:21:58 +04:00
|
|
|
# Adds the tournament of the game
|
|
|
|
trn = @trn_hash[ year + "/" + tournament ]
|
|
|
|
trn_link = "<a href=\"" + trn['url'] + "\">" + trn['title'] + "</a>"
|
|
|
|
@buffer += "<dt>Турнир:</dt><dd>#{trn_link}</dd>\n"
|
2014-07-02 13:22:52 +04:00
|
|
|
|
2014-07-02 16:21:58 +04:00
|
|
|
# Adds the game info
|
2014-07-02 13:22:52 +04:00
|
|
|
white, black = basename[11..-1].sub(/-with-comments\.pgn$/, '').split('-vs-')
|
|
|
|
white_link = "<a href=\"https://www.linux.org.ru/people/#{white}/profile\">#{white}</a>"
|
|
|
|
black_link = "<a href=\"https://www.linux.org.ru/people/#{black}/profile\">#{black}</a>"
|
|
|
|
game_date_str = basename[0..9]
|
2014-07-02 14:12:57 +04:00
|
|
|
game_date = localize(Time.parse(game_date_str), "%d %b %Y")
|
2014-07-02 13:22:52 +04:00
|
|
|
@buffer += "<dt>Игра:</dt><dd>#{white_link} vs. #{black_link} (#{game_date})</dd>\n"
|
|
|
|
|
|
|
|
@buffer += "</dl>\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|