forked from parisjs/parisjs-website
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add utils/meetups.js to parse and write the list of meetups.
- Loading branch information
1 parent
dfd7207
commit 1dac6cf
Showing
5 changed files
with
229 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "parisjs-website", | ||
"description": "The website of parisjs.org", | ||
"version": "0.0.1", | ||
"homepage": "http://parisjs.org/", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/parisjs/parisjs-website.git" | ||
}, | ||
"dependencies": { | ||
"jsdom": "", | ||
"underscore": "" | ||
}, | ||
"devDependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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() | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<li class="meetup"><a href="#"><%= title %></a></li> | ||
|
||
<li class="meetup-content"><% talks.forEach(function(talk) { %> | ||
|
||
<ul> | ||
<li class="avatar"><% if (talk.authors[0].avatar) { %><img alt="twitter avatar" src="<%= talk.authors[0].avatar %>"><% } %></li> | ||
<li class="titleTalk"><%= talk.title %></li> | ||
<li class="authorTalk">By: <% for (var i=0; i<talk.authors.length; i++) { var author = talk.authors[i]; var end = (i == talk.authors.length - 1); %><a href="<%= author.url %>"><%= author.name %></a><% if (!end) print(" and "); } %></li> | ||
<li class="descTalk">Here are the <% if (talk.projects.length == 1) { %><a href="<%= talk.projects[0] %>">project</a><% } %><% if (talk.slides.length == 1) { if (talk.slides[0] == "#") print('<s>'); %><a href="<%= talk.slides[0] %>">Slides</a><%if (talk.slides[0] == "#") print('</s>'); } %><% if (talk.videos.length > 0) { %> | ||
and the <% for (var j=0; j<talk.videos.length; j++) { %><a href="<%= talk.videos[j] %>">video<% if (talk.videos.length > 1) print(' part.'+ (j+1)); %></a><% } }%>!</li> | ||
</ul><% }) %> | ||
|
||
</li> | ||
|