forked from parisjs/parisjs-website
/
meetups.js
executable file
·177 lines (161 loc) · 5.58 KB
/
meetups.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env node
/**
* You don't want to manage the list of events/talks/people by hands? This script can help you.
*
* It's a two way parser:
* - read index.html and extract the list of events/talks/people and output JSON on stdout
* - read a JSON from stdin and update index.html
*
* Why JSON?
* Because there is no good yaml parser in javascript, and it was out of the scope for now
*
* How to use it?
* node utils/meetups.js parse > meetup.json
* node utils/meetups.js update < meetup.json
*/
var jsdom = require('jsdom')
, fs = require('fs')
, _ = require('underscore')
;
if (process.argv.length == 3) {
if (process.argv[2] == 'parse') {
return parseMeetups();
} else if (process.argv[2] == 'update') {
return updateMeetups();
}
}
console.log('usage');
console.log('node utils/meetups.js parse');
console.log('node utils/meetups.js update');
return;
function readStdin(callback) {
var data = "";
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) {
data += chunk;
});
process.stdin.on('end', function () {
callback(data);
});
}
function updateMeetups() {
readStdin(function(data) {
var meetups = JSON.parse(data);
var html = generateHTMLFor(meetups);
var website = fs.readFileSync(__dirname + '/../index.html', 'utf8');
var jquery = 'http://code.jquery.com/jquery-1.5.min.js';
jsdom.env(website, [jquery],
function(errors, window) {
if (errors) {
return console.error('jsdom error', errors)
}
var $ = window.$;
var $meetups = $('#meetups > ul').empty().append("\n ").append($(html)).append("\n ");
var document = window.document;
// remove the jquery inserted by jsdom
$('script:last', document).remove();
var output = document.doctype + document.innerHTML;
console.log(output.trim());
});
});
}
function generateHTMLFor(meetups) {
var template = _.template(fs.readFileSync(__dirname +'/template_meetup.html', 'utf8'));
var html = meetups.map(function(meetup) {
return template(meetup);
});
return "\n"+ html.join('')+"\n\n";
}
//
function parseMeetups() {
var website = fs.readFileSync(__dirname + '/../index.html', 'utf8');
var jquery = 'http://code.jquery.com/jquery-1.5.min.js';
jsdom.env(website, [jquery],
function(errors, window) {
if (errors) {
return console.error('jsdom error', errors)
}
var $ = window.$;
var meetups = extractMeetups($);
console.log(JSON.stringify(meetups));
//var normalization = normalizeMeetups(meetups);
//console.log(JSON.stringify(normalization));
});
}
/**
* The hard part
* find same authors to have a ref to each ones
*/
function normalizeMeetups(meetups) {
var authors = {};
// don't worry, this is only the first level of indentation
meetups.forEach(function(meetup) {
// don't worry, this is only the second level of indentation
meetup.talks.forEach(function(talk) {
// don't worry, this is only the last level of indentation
talk.authors = talk.authors.map(function(author) {
if (authors[author.name]) {
var previous = authors[author.name];
['url', 'avatar'].forEach(function(property) {
if (author[property] != previous[property])
console.error('grrrr! '+ property +' mismatch for '+ author.name);
});
}
authors[author.name] = author;
return author.name;
});
})
});
return {
meetups: meetups,
authors: authors
}
}
/**
* Return a raw object of all events and talks
*/
function extractMeetups($) {
var $meetups = $('#meetups li.meetup');
var meetups = [];
$meetups.each(function() {
var $meetup = $(this);
var meetup = {
title: $(this).text(),
talks: []
};
var $talks = $meetup.next('.meetup-content').find('> ul');
meetup.talks = extractTalks($, $talks);
meetups.push(meetup);
});
return meetups;
}
function extractTalks($, $talks) {
return $talks.toArray().map(function(talk) {
var $talk = $(talk);
function getLinks(regexp) {
return $talk.find('.descTalk a').filter(function() {
return $(this).text().match(regexp);
}).map(function() {
return $(this).attr('href');
}).toArray();
}
var slides = getLinks(/slide/i);
var videos = getLinks(/(video|part)/i);
var projects = getLinks(/project/i);
return {
title: $talk.find('.titleTalk').text().trim(),
slides: slides,
videos: videos,
projects: projects,
authors: $talk.find('.authorTalk a').map(function() {
return {
name: $(this).text(),
url: $(this).attr('href'),
// sorry boy, in case of multiple authors, we have only one avatar
avatar: $talk.find('.avatar img').attr('src')
}
}).toArray()
}
});
}