Skip to content

Commit

Permalink
Merge pull request #73 from lewisnyman/commits-per-country
Browse files Browse the repository at this point in the history
Added a countries page
  • Loading branch information
lauriii committed Oct 11, 2015
2 parents fe251f5 + bb681c9 commit ab89a2c
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 4 deletions.
160 changes: 160 additions & 0 deletions app/bin/countries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#!/usr/bin/env ruby

Encoding.default_external = Encoding::UTF_8
require 'erb'
require 'yaml'
require 'nokogiri'
require 'open_uri_redirections'
require 'time'
require 'json'

COMPANY_NOT_FOUND='not_found'
COMPANY_NOT_DEFINED='not_defined'
UPDATE_NONE=0
UPDATE_NOT_FOUND=1
UPDATE_ALL=2

name_variants = Hash.new(0)
if File.exists? ('../data/company_infos.yml')
companies_info = YAML::load_file('../data/company_infos.yml')
else
companies_info = Hash.new(0)
end
if File.exists? ('../data/country_mapping.yml')
company_mapping = YAML::load_file('../data/country_mapping.yml') || Hash.new(0)
else
company_mapping = Hash.new(0)
end
update=UPDATE_NONE
if ARGV.length == 1
if ARGV[0] == '--update-all'
update=UPDATE_ALL
else
if ARGV[0] == '--update-not-found'
update=UPDATE_NOT_FOUND
end
end
end

i = 1;
lastOrder = -1;
lastMentions = 0;
file = file = File.read('../../dist/data.json')
data = JSON.parse(file)
contributors = data['contributors']
companies = Hash.new(0)

def ensure_company(companies, companies_info, key, title, link)
unless companies.key? key
companies[key] = Hash.new(0)
companies[key]['contributors'] = Hash.new(0)
if companies_info.key? key
companies[key]['title'] = companies_info[key]['title']
companies[key]['link'] = companies_info[key]['link']
else
companies[key]['title'] = title
companies[key]['link'] = link
end
end
end

contributors.sort_by {|k, v| v }.reverse.each do |name,mentions|
if company_mapping.key? name
if update == UPDATE_NONE or (update == UPDATE_NOT_FOUND and company_mapping[name] != COMPANY_NOT_FOUND)
ensure_company(companies, companies_info, company_mapping[name], 'should be filled via company infos', 'should be filled via company infos')
companies[company_mapping[name]]['mentions'] += mentions
companies[company_mapping[name]]['contributors'][name] = mentions
next
end
end
if name_variants.key? name
urlname = name_variants[name].gsub ' ', '-';
else
urlname = name.gsub ' ', '-';
end
url = "https://www.drupal.org/u/#{urlname}"
url = URI::encode(url)
begin
html = open(url, :allow_redirections => :safe)
doc = Nokogiri::HTML(html)
rescue
next
end
found = true
doc.css('title').each do |title|
if title.text == 'Page not found | Drupal.org'
found = false
end
end
unless found
ensure_company(companies, companies_info, COMPANY_NOT_FOUND, 'Users not found', 'Users not found')
companies[COMPANY_NOT_FOUND]['mentions'] += mentions
companies[COMPANY_NOT_FOUND]['contributors'][name] = mentions
end
if found
found = false
if company_wrapper = doc.at_css('.field-name-field-country')
if company_wrapper.at_css('img')
company = company_wrapper.at_css('img')['alt']
else
company = company_wrapper.text
end
if company_wrapper.at_css('a')
link = company_wrapper.at_css('a')
link['href'] = 'https://drupal.org' + link['href']
# If we still don't have the company name, follow the link to the page.
unless company
html = open(link['href'], :allow_redirections => :safe)
company_page = Nokogiri::HTML(html)
if company_title = company_page.at_css('#page-subtitle')
company = company_title.text
end
end
else
# If there is no link, use the company name instead.
link = company
end
company = company.strip
company_key = company.downcase
ensure_company(companies, companies_info, company_key, company, link.to_s)
companies[company_key]['mentions'] += mentions
companies[company_key]['contributors'][name] = mentions
found = true
end
unless found
ensure_company(companies, companies_info, COMPANY_NOT_DEFINED, 'Not specified', 'Not specified')
companies[COMPANY_NOT_DEFINED]['mentions'] += mentions
companies[COMPANY_NOT_DEFINED]['contributors'][name] = mentions
end
end
end

companies = companies.sort_by {|k, v| v['mentions'] }.reverse
companies.each do |k, values|
unless companies_info.key? k
companies_info[k] = Hash.new(0)
companies_info[k]['title'] = values['title']
companies_info[k]['link'] = values['link']
end
values['contributors'].each do |name, mentions|
company_mapping[name] = k
end
if values['contributors'].length == 0
companies_info.delete(k)
end
end
File.open('../data/company_infos.yml', 'w') { |f| YAML.dump(companies_info, f) }
File.open('../data/country_mapping.yml', 'w') { |f| YAML.dump(company_mapping, f) }

sum = contributors.values.reduce(:+).to_f
puts ERB.new(DATA.readlines.join, 0, '>').result

time = Time.now()
description = "A simple table of all contributors to Drupal 8 core"
header = ERB.new(File.new("../templates/partials/header.html.erb").read).result(binding)
footer = ERB.new(File.new("../templates/partials/footer.html.erb").read).result(binding)
companies_template = File.open("../templates/countries.html.erb", 'r').read
renderer = ERB.new(companies_template)
puts output = renderer.result()

__END__
3 changes: 2 additions & 1 deletion app/templates/companies.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ul>
<li><a href="index.html">List Contributors</a></li>
<li><a href="companies.html">List Companies</a></li>
<li><a href="countries.html">List Countries</a></li>
</ul>
</div>

Expand Down Expand Up @@ -57,4 +58,4 @@
</table>
</section>
</div>
<%= footer %>
<%= footer %>
51 changes: 51 additions & 0 deletions app/templates/countries.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<%= header %>
<div class="main-content-wrap" class="outer">
<section class="main-content inner">
<div class="table-filter">
Total: <%= companies.length %> countries listed
<ul>
<li><a href="index.html">List Contributors</a></li>
<li><a href="companies.html">List Companies</a></li>
<li><a href="countries.html">List Countries</a></li>
</ul>
</div>

<p class="updated">Last updated <span class="timeago" title="<%= time.iso8601(10) %>"><%= time %></span></p>
<table cellpadding="4" style="border: 1px solid #000000; border-collapse: collapse;" border="1" class="companies">
<col width="5%">
<col width="50%">
<col width="15%">
<col width="15%">
<col width="15%">
<tr>
<th>#</th>
<th>Country</th>
<th>Contributors</th>
<th>Mentions</th>
<th>Percent</th>
</tr>
<% companies.each do |name, values| %>
<tr id="<%= name %>">
<td id="<%= name %>"><%= (lastMentions == values['mentions']) ? lastOrder : i %></td>
<td><%= values['link'] %> <img src="images/icon_info.png" alt="Info" title="Toggle employees" class="toggle">
<table class="employees" style="display: none">
<% values['contributors'].each do |contributor, mentions| %>
<tr><td><a href="https://www.drupal.org/u/<%= contributor.gsub ' ', '-' %>"><%= contributor %></a></td><td><%= mentions %></td></tr>
<% end %>
</table>
</td>
<td><%= values['contributors'].length %></td>
<td><%= values['mentions'] %> (~<%= values['mentions'] / values['contributors'].length %>)</td>
<td><%= ((values['mentions']/sum)*100).round(4) %>%</td>
<% if lastMentions != values['mentions'] %>
<% lastOrder = i %>
<% end %>
<% i += 1 %>
<% lastMentions = values['mentions'] %>
</tr>
<% end %>

</table>
</section>
</div>
<%= footer %>
1 change: 1 addition & 0 deletions app/templates/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ul>
<li><a href="index.html">List Contributors</a></li>
<li><a href="companies.html">List Companies</a></li>
<li><a href="countries.html">List Countries</a></li>
</ul>
</div>

Expand Down
8 changes: 7 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ gulp.task('companyinfo', function () {
.pipe(shell(['./companies.rb --update-all'], { 'cwd': './app/bin'}));
});

// Build countries page
gulp.task('buildcountries', ['buildjson'], function () {
return gulp.src('')
.pipe(shell(['./countries.rb > ../../dist/countries.html'], { 'cwd': './app/bin'}));
});

// Build json data
gulp.task('buildjson', function () {
return gulp.src('')
Expand Down Expand Up @@ -123,7 +129,7 @@ gulp.task('minifyhtml', function() {
// The whole shebang
gulp.task('default', function(callback) {
runSequence(['clean', 'bower', 'drupalcore'],
['buildcontributors', 'buildcompanies', 'buildjson', 'javascripts', 'images', 'sass'],
['buildcontributors', 'buildcompanies', 'buildcountries', 'javascripts', 'images', 'sass'],
'usemin',
'minifyhtml',
callback);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
"gulp-if": "^1.2.5",
"gulp-imagemin": "^2.0.0",
"gulp-jshint": "^1.9.0",
"gulp-minify-css": "^0.3.11",
"gulp-minify-css": "0.3.11",
"gulp-minify-html": "0.1.7",
"gulp-sass": "^1.1.0",
"gulp-shell": "^0.2.11",
"gulp-uglify": "^1.0.1",
"gulp-uncss": "^0.5.1",
"gulp-usemin": "^0.3.8",
"gulp-usemin": "0.3.8",
"jscs": "*",
"jshint": "*",
"jshint-stylish": "^1.0.0",
Expand Down

0 comments on commit ab89a2c

Please sign in to comment.