- Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathtranslation_status.rb
134 lines (105 loc) · 3.12 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# frozen_string_literal: true
moduleJekyll
# Provides a tag that generates an overview of all news posts
# and their translations.
#
# Outputs HTML.
moduleTranslationStatus
LANGS=%w[enbgdeesfriditjakoplptrutrvizh_cnzh_tw].freeze
START_DATE="2013-04-01"
OK_CHAR="✓"
MISSING_CHAR=""# "✗"
POST_DISPLAY_LENGTH=50
TEMPLATE=<<~ERB
<p>
Posts with missing translations: <%=posts.size.to_s%><br>
Start date: <%=START_DATE%><br>
Ignored languages: <%=ignored%>
</p>
<table>
<colgroup>
<col>
<%LANGS.eachdo |lang| -%>
<colclass="<%=lang%>">
<%end-%>
</colgroup>
<thead>
<%=table_header%>
</thead>
<tbody>
<%posts.eachdo |post| -%>
<%=table_row(post)%>
<%end-%>
</tbody>
</table>
ERB
SET_OF_LANGS=Set.new(LANGS)
classPost
attr_reader:name
attr_accessor:translations,:security
definitialize(name)
@name=name
@security=false
@translations=Set.new
end
defcompleted?
SET_OF_LANGS == translations
end
defshort_name
ifname.size > POST_DISPLAY_LENGTH
"#{name[0...POST_DISPLAY_LENGTH - 3]}..."
else
name
end
end
defshort_name_in_red
%Q(<span style="color:red">#{short_name}</span>)
end
defrow_data(langs)
display_name=(security ? short_name_in_red : short_name)
[display_name] + langs.mapdo |lang|
iftranslations.include?(lang)
%Q(<a href="/#{lang}/news/#{name}">#{OK_CHAR}</a>)
else
MISSING_CHAR
end
end
end
end
classTag < Liquid::Tag
definitialize(tag_name,path,tokens)
super
@posts=Hash.new{|posts,name| posts[name]=Post.new(name)}
end
defremove_completed_posts
@posts.delete_if{|_name,post| post.completed?}
end
deftoo_old(date)
date.strftime("%Y-%m-%d") < START_DATE
end
deftable_header
"<tr><th>News Post</th><th>#{LANGS.join('</th><th>')}</th></tr>"
end
deftable_row(post)
"<tr><td>#{post.row_data(LANGS).join('</td><td>')}</td></tr>"
end
defrender(context)
categories=context.registers[:site].categories
ignored_langs=categories.keys - LANGS - ["news"]
LANGS.eachdo |lang|
categories[lang].eachdo |post|
nextiftoo_old(post.date)
name=post.url.gsub(%r{\A/#{lang}/news/},"")
@posts[name].translations << lang
@posts[name].security=trueifpost.data["tags"].include?("security")
end
end
remove_completed_posts
ignored=ignored_langs.empty? ? "none" : ignored_langs.sort.join(", ")
posts=@posts.sort.reverse.map{|_name,post| post}
ERB.new(TEMPLATE,trim_mode: "-").result(binding)
end
end
end
end
Liquid::Template.register_tag("translation_status",Jekyll::TranslationStatus::Tag)