Gentoo Archives: gentoo-commits

From: Alex Legler <a3li@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] sites/www:master commit in: _plugins/
Date: Mon, 30 Nov 2015 19:48:37
Message-Id: 1448912902.a100faf515c3cfe8c2d1d8fe4bc6c0fb48f7429d.a3li@gentoo
1 commit: a100faf515c3cfe8c2d1d8fe4bc6c0fb48f7429d
2 Author: Alex Legler <alex <AT> a3li <DOT> li>
3 AuthorDate: Mon Nov 30 19:48:22 2015 +0000
4 Commit: Alex Legler <a3li <AT> gentoo <DOT> org>
5 CommitDate: Mon Nov 30 19:48:22 2015 +0000
6 URL: https://gitweb.gentoo.org/sites/www.git/commit/?id=a100faf5
7
8 Ruby code cleanup
9
10 _plugins/devaway.rb | 4 ++--
11 _plugins/filters.rb | 6 +++---
12 _plugins/herds.rb | 6 +++---
13 _plugins/mirrors.rb | 4 +---
14 _plugins/navigation.rb | 31 +++++++++++++++++--------------
15 _plugins/news.rb | 38 +++++++++++++++-----------------------
16 _plugins/planet.rb | 3 +--
17 _plugins/tags.rb | 9 +++++----
18 _plugins/wiki.rb | 2 +-
19 9 files changed, 48 insertions(+), 55 deletions(-)
20
21 diff --git a/_plugins/devaway.rb b/_plugins/devaway.rb
22 index 5d411a0..9ba5018 100644
23 --- a/_plugins/devaway.rb
24 +++ b/_plugins/devaway.rb
25 @@ -5,11 +5,11 @@ module Gentoo
26 def generate(site)
27 data = Nokogiri::XML(File.open(DEVAWAY_XML))
28
29 - site.data['devaway'] ||= { }
30 + site.data['devaway'] ||= {}
31
32 data.xpath('/devaway/dev').each do |dev|
33 site.data['devaway'][dev['nick']] = dev.xpath('./reason/text()').first.content
34 end
35 end
36 end
37 -end
38 \ No newline at end of file
39 +end
40
41 diff --git a/_plugins/filters.rb b/_plugins/filters.rb
42 index 34ad123..df5b4f7 100644
43 --- a/_plugins/filters.rb
44 +++ b/_plugins/filters.rb
45 @@ -1,6 +1,6 @@
46 module Gentoo
47 module Filters
48 - UNITS = %W(B KiB MiB GiB TiB).freeze
49 + UNITS = %w(B KiB MiB GiB TiB).freeze
50
51 def nice_filesize(input)
52 number = input.to_i
53 @@ -12,7 +12,7 @@ module Gentoo
54 exponent = (Math.log(number) / Math.log(1024)).to_i
55 exponent = max_exp if exponent > max_exp
56
57 - number /= 1024 ** exponent
58 + number /= 1024**exponent
59 end
60
61 "#{number} #{UNITS[exponent]}"
62 @@ -25,4 +25,4 @@ module Gentoo
63 end
64 end
65
66 -Liquid::Template.register_filter(Gentoo::Filters)
67 \ No newline at end of file
68 +Liquid::Template.register_filter(Gentoo::Filters)
69
70 diff --git a/_plugins/herds.rb b/_plugins/herds.rb
71 index 2f357c7..971f1e2 100644
72 --- a/_plugins/herds.rb
73 +++ b/_plugins/herds.rb
74 @@ -4,7 +4,7 @@ module Gentoo
75
76 def generate(site)
77 xml = Nokogiri::XML(File.open(XML))
78 -
79 +
80 site.data['herds'] ||= {}
81
82 xml.xpath('/herds/herd').each do |item|
83 @@ -21,7 +21,7 @@ module Gentoo
84 maint_data = {
85 'email' => maint.xpath('./email/text()').first.text,
86 'name' => nil,
87 - 'role' => nil,
88 + 'role' => nil
89 }
90
91 ns_name = maint.xpath('./name/text()')
92 @@ -40,4 +40,4 @@ module Gentoo
93 end
94 end
95 end
96 -end
97 \ No newline at end of file
98 +end
99
100 diff --git a/_plugins/mirrors.rb b/_plugins/mirrors.rb
101 index 1d30238..c1bf17b 100644
102 --- a/_plugins/mirrors.rb
103 +++ b/_plugins/mirrors.rb
104 @@ -26,7 +26,6 @@ module Gentoo
105 next unless mirror.name == 'mirror'
106
107 mirror.children.each do |tag|
108 -
109 case tag.name
110 when 'name'
111 mirror_data['name'] = tag.text
112 @@ -46,7 +45,6 @@ module Gentoo
113 site.data['mirrors'][key][region][country_code]['mirrors'] << mirror_data
114 end
115 end
116 -
117 end
118 end
119 -end
120 \ No newline at end of file
121 +end
122
123 diff --git a/_plugins/navigation.rb b/_plugins/navigation.rb
124 index 0df1404..66bf9a3 100644
125 --- a/_plugins/navigation.rb
126 +++ b/_plugins/navigation.rb
127 @@ -24,38 +24,43 @@ module Gentoo
128 end
129
130 private
131 +
132 def primary(all_pages, current_page)
133 - pages = all_pages.select {|page| page.data['nav1-show'] == true }
134 + pages = all_pages.select { |page| page.data['nav1-show'] == true }
135
136 generate(pages, current_page, '1')
137 end
138
139 def secondary(all_pages, current_page)
140 - pages = all_pages.select {|page| page.data['nav1'] == current_page['nav1'] and page.data['nav2-show'] == true }
141 + pages = all_pages.select { |page| page.data['nav1'] == current_page['nav1'] && page.data['nav2-show'] == true }
142
143 generate(pages, current_page, '2')
144 end
145
146 def tertiary(all_pages, current_page)
147 - pages = all_pages.select {|page| page.data['nav1'] == current_page['nav1'] and page.data['nav2'] == current_page['nav2'] and page.data['nav3-show'] == true }
148 + pages = all_pages.select do |page|
149 + page.data['nav1'] == current_page['nav1'] &&
150 + page.data['nav2'] == current_page['nav2'] &&
151 + page.data['nav3-show'] == true
152 + end
153
154 generate(pages, current_page, '3')
155 end
156
157 def sitemap(all_pages, nav1)
158 - pages = all_pages.select {|page| page.data['nav1'] == nav1 and page.data['nav2-show'] == true }
159 + pages = all_pages.select { |page| page.data['nav1'] == nav1 && page.data['nav2-show'] == true }
160
161 generate(pages, {}, '2')
162 end
163
164 def generate(all_pages, current_page, level)
165 - level_show = "nav%s-show" % level
166 - level_weight = "nav%s-weight" % level
167 - level_str = "nav%s" % level
168 + level_show = 'nav%s-show' % level
169 + level_weight = 'nav%s-weight' % level
170 + level_str = 'nav%s' % level
171
172 - pages = all_pages.select {|page| page.data[level_show] == true }
173 + pages = all_pages.select { |page| page.data[level_show] == true }
174 pages.sort! do |a, b|
175 - if a.data.has_key? level_weight and b.data.has_key? level_weight
176 + if a.data.key?(level_weight) && b.data.key?(level_weight)
177 a.data[level_weight] <=> b.data[level_weight]
178 else
179 a_title = a.data['navtitle'] || a.data['title']
180 @@ -71,13 +76,13 @@ module Gentoo
181
182 html += "<li class=\"%s\">" % css_class
183
184 - if page.data.has_key? 'redirect'
185 + if page.data.key? 'redirect'
186 html += '<a href="' + page.data['redirect'] + '">'
187 else
188 html += '<a href="' + page.url.gsub('index.html', '') + '">'
189 end
190
191 - if page.data.has_key? 'navtitle'
192 + if page.data.key? 'navtitle'
193 html += page.data['navtitle']
194 else
195 html += page.data['title']
196 @@ -88,10 +93,8 @@ module Gentoo
197
198 html
199 end
200 -
201 -
202 end
203 end
204 end
205
206 -Liquid::Template.register_tag('navigation', Gentoo::Tags::NavigationTag)
207 \ No newline at end of file
208 +Liquid::Template.register_tag('navigation', Gentoo::Tags::NavigationTag)
209
210 diff --git a/_plugins/news.rb b/_plugins/news.rb
211 index a93aa06..a90c435 100644
212 --- a/_plugins/news.rb
213 +++ b/_plugins/news.rb
214 @@ -1,12 +1,12 @@
215 module Gentoo
216 class NewsGenerator < Jekyll::Generator
217 - NEWS_DIR = '_data/news/'
218 + NEWS_DIR = '_data/news/'
219
220 def generate(site)
221 site.data['newsitems'] ||= []
222
223 Dir.chdir(NEWS_DIR) do
224 - Dir.glob("[0-9][0-9][0-9][0-9]*").reverse.each do |path|
225 + Dir.glob('[0-9][0-9][0-9][0-9]*').reverse_each do |path|
226 begin
227 name = path
228
229 @@ -17,7 +17,7 @@ module Gentoo
230 end
231 end
232
233 - site.data['newsitems'] = site.data['newsitems'].sort {|a,b| b['date'] <=> a['date'] }
234 + site.data['newsitems'] = site.data['newsitems'].sort { |a, b| b['date'] <=> a['date'] }
235 end
236 end
237
238 @@ -28,31 +28,23 @@ module Gentoo
239 @dir = NewsGenerator::NEWS_DIR
240 @name = "#{name}.html"
241
242 - self.process(@name)
243 - self.read_yaml(File.join(@base, NewsGenerator::NEWS_DIR, path), "#{name}.en.txt")
244 + process(@name)
245 + read_yaml(File.join(@base, NewsGenerator::NEWS_DIR, path), "#{name}.en.txt")
246
247 - self.data['permalink'] = "/support/news-items/#{name}.html"
248 - self.data['layout'] = 'page-pre'
249 - self.data['nav1'] = 'support'
250 - self.data['nav2'] = 'news-items'
251 + data['permalink'] = "/support/news-items/#{name}.html"
252 + data['layout'] = 'page-pre'
253 + data['nav1'] = 'support'
254 + data['nav2'] = 'news-items'
255
256 File.readlines(File.join(@base, NewsGenerator::NEWS_DIR, path, "#{name}.en.txt")).each do |line|
257 - if line =~ /Title: (.*)$/
258 - @title = $1
259 - end
260 -
261 - if line =~ /Author: (.*)$/
262 - @author = $1
263 - end
264 -
265 - if line =~ /Posted: (.*)$/
266 - @date = $1
267 - end
268 + @title = $1 if line =~ /Title: (.*)$/
269 + @author = $1 if line =~ /Author: (.*)$/
270 + @date = $1 if line =~ /Posted: (.*)$/
271 end
272
273 - site.data['newsitems'] << { 'title' => @title, 'author' => @author, 'date' => @date, 'url' => self.data['permalink'] }
274 + site.data['newsitems'] << { 'title' => @title, 'author' => @author, 'date' => @date, 'url' => data['permalink'] }
275
276 - self.data['title'] = @title
277 + data['title'] = @title
278 end
279 end
280 -end
281 \ No newline at end of file
282 +end
283
284 diff --git a/_plugins/planet.rb b/_plugins/planet.rb
285 index 265e703..cca9122 100644
286 --- a/_plugins/planet.rb
287 +++ b/_plugins/planet.rb
288 @@ -32,7 +32,6 @@ module Gentoo
289
290 site.data['planet']['posts'] << item_data unless ignore
291 end
292 -
293 end
294 end
295 -end
296 \ No newline at end of file
297 +end
298
299 diff --git a/_plugins/tags.rb b/_plugins/tags.rb
300 index 5175cde..7ad24bb 100644
301 --- a/_plugins/tags.rb
302 +++ b/_plugins/tags.rb
303 @@ -1,4 +1,5 @@
304 require_relative 'lib/weightedrandomizer'
305 +
306 module Gentoo
307 module Tags
308 class AdsTag < Liquid::Tag
309 @@ -8,18 +9,19 @@ module Gentoo
310 def render(context)
311 ads = context.registers[:site].data['ads']['active']
312
313 - ad_html = ""
314 + ad_html = ''
315 first_ad = true
316 raw_weighted_ads = Hash[ads.map { |ad| [ad, ad['weight'] || 0] }]
317 ads_wr = WeightedRandomizer.new(raw_weighted_ads)
318 - ads_wr.sample(AD_COUNT*10).uniq.slice(0, AD_COUNT).each do |ad|
319 + ads_wr.sample(AD_COUNT * 10).uniq.slice(0, AD_COUNT).each do |ad|
320 if first_ad
321 ad_html += '<div class="col-xs-12 col-md-2 col-md-offset-2 sponsorlogo">'
322 first_ad = false
323 else
324 ad_html += '<div class="col-xs-12 col-md-2 sponsorlogo">'
325 end
326 - ad_html += '<!-- sponsor{name:%s,weight:%d} -->' % [ad['name'], ad['weight']]
327 +
328 + ad_html += '<!-- sponsor{name:%s,weight:%d} -->' % [ad['name'], ad['weight']]
329
330 if ad.has_key? 'img'
331 ad_html += "<a href=\"%s\" title=\"%s\"><img src=\"/assets/img/sponsors/ads/%s\" alt=\"%s\"></a>" %
332 @@ -34,7 +36,6 @@ module Gentoo
333 ad_html
334 end
335 end
336 -
337 end
338 end
339
340
341 diff --git a/_plugins/wiki.rb b/_plugins/wiki.rb
342 index ee2c27c..5d25716 100644
343 --- a/_plugins/wiki.rb
344 +++ b/_plugins/wiki.rb
345 @@ -27,4 +27,4 @@ module Gentoo
346 end
347 end
348 end
349 -end
350 \ No newline at end of file
351 +end