/
translation_status.rb
117 lines (89 loc) · 2.61 KB
/
translation_status.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-
module Jekyll
# Provides a tag that generates an overview of all news posts
# and their translations.
#
# Outputs HTML.
module TranslationStatus
LANGS = %w{en de es fr id it ja ko pl ru vi zh_cn zh_tw}
START_DATE = '2013-04-01'
OK_CHAR = '✓'
MISSING_CHAR = '' # '✗'
POST_DISPLAY_LENGTH = 50
TEMPLATE =<<-EOF.gsub(/^ /, '')
<p>
Posts with missing translations: <%= posts.size.to_s %><br>
Start date: <%= START_DATE %><br>
Ignored languages: <%= ignored %>
</p>
<table>
<%= table_header %>
<% posts.each do |post| -%>
<%= table_row(post) %>
<% end -%>
</table>
EOF
SET_OF_LANGS = Set.new(LANGS)
class Post
attr_reader :name
attr_accessor :translations
def initialize(name)
@name = name
@translations = Set.new
end
def completed?
SET_OF_LANGS == translations
end
def short_name
if name.size > POST_DISPLAY_LENGTH
name[0...POST_DISPLAY_LENGTH - 3] + '...'
else
name
end
end
def row_data(langs)
[short_name] + langs.map do |lang|
if translations.include?(lang)
%Q(<a href="/#{lang}/news/#{name}">#{OK_CHAR}</a>)
else
MISSING_CHAR
end
end
end
end
class Tag < Liquid::Tag
def initialize(tag_name, path, tokens)
super
@posts = Hash.new {|posts, name| posts[name] = Post.new(name) }
end
def remove_completed_posts
@posts.delete_if {|name, post| post.completed? }
end
def too_old(date)
date.strftime("%Y-%m-%d") < START_DATE
end
def table_header
"<tr><th>News Post</th><th>#{LANGS.join('</th><th>')}</th></tr>"
end
def table_row(post)
"<tr><td>#{post.row_data(LANGS).join('</td><td>')}</td></tr>"
end
def render(context)
categories = context.registers[:site].categories
ignored_langs = categories.keys - LANGS - ['news']
LANGS.each do |lang|
categories[lang].each do |post|
next if too_old(post.date)
name = post.url.gsub(%r(\A/#{lang}/news/), '')
@posts[name].translations << lang
end
end
remove_completed_posts
ignored = ignored_langs.sort.join(', ')
posts = @posts.sort.reverse.map {|name, post| post }
ERB.new(TEMPLATE, nil, '-').result(binding)
end
end
end
end
Liquid::Template.register_tag('translation_status', Jekyll::TranslationStatus::Tag)