forked from RichardLitt/awesome-conferences
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
108 lines (95 loc) · 2.98 KB
/
Rakefile
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
require 'rubygems'
require 'rake'
require 'kramdown'
require 'yaml'
require 'time'
require 'nokogiri'
require 'open-uri'
require 'pry'
SOURCE = "."
CONFIG = {
'version' => "0.0.1",
'data-file' => File.join(SOURCE, 'conferences.yml'),
'readme' => File.join(SOURCE, 'README.md'),
'ignore-headers' => ['Resources',
'License',
'Other Lists',
'Call for Speakers',
'What makes a conference awesome?']
}
class Conference
attr_accessor :name, :website, :twitter, :location, :tags
def initialize(*args)
@name = args[0][:name] if args[0][:name]
@website = args[0][:website] if args[0][:website]
@twitter = args[0][:twitter] if args[0][:twitter]
@location = args[0][:location] if args[0][:location]
@tags = args[0][:tags] if args[0][:tags]
end
def to_yaml
{ name: @name,
website: @website,
twitter: @twitter,
location: @location,
tags: @tags }.to_yaml
end
def to_s
"#{@name}"
end
def ==(other)
# Is this the best way to check for equality?
# Maybe just the website?
@website == other.website && @name == other.name
end
def update_from_readme_content(conference)
@twitter = conference.twitter
@location = conference.location
end
def Conference.extract_from_yaml(yaml)
new(name: yaml[:name],
website: yaml[:website],
twitter: yaml[:twitter],
location: yaml[:location],
tags: yaml[:tags])
end
def Conference.extract_conference_from_readme_content(conf)
twitter = conf.css('a')[1].text if conf.css('a').count > 1
new(name: conf.css('a')[0].text,
website: conf.css('a')[0]['href'],
twitter: twitter || nil)
end
end
# Usage: rake transfer
desc "Transfer from readme.md to conferences.yml while saving the current data"
task :transfer do
conferences = YAML.load_stream(File.open(CONFIG['data-file'], 'a+'))
conferences = conferences.map do |conference|
Conference.extract_from_yaml conference
end
File.open(CONFIG['readme'],'r') do |file|
readme = file.read
readme_html = Kramdown::Document.new(readme).to_html
readme_parsed = Nokogiri::HTML(readme_html)
readme_parsed.css("h2, h3, h4").to_a.each do |item|
# ignore anything that's not already on there
unless CONFIG['ignore-headers'].include?(item.text)
location = item.text
item.next_element.css('li').to_a.each do |conf|
conference = Conference.extract_conference_from_readme_content conf
conference.location = location if location != "Conferences"
unless conferences.include?(conference)
conferences.push(conference)
else
ind = conferences.index(conference)
conferences[ind].update_from_readme_content conference
end
end
end
end
end
File.open(CONFIG['data-file'], 'w') do |file|
conferences.each do |conf|
file.write(conf.to_yaml)
end
end
end # task :transfer