forked from knode/meetups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
186 lines (142 loc) · 3.31 KB
/
index.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
178
179
180
181
182
183
184
185
186
module.exports = main
var path = require('path')
, fs = require('fs')
var geocoder = require('omnigeo')
, through = require('through')
, marked = require('marked')
, ls = require('ls-stream')
if(require.main === module) {
main()
.pipe(require('JSONStream').stringify())
.pipe(process.stdout)
}
function main() {
return ls(__dirname)
.pipe(filter())
.pipe(annotateContent())
.pipe(toMetadata())
.pipe(addGeolocation())
}
// only emit entries that look like meetup markdown files.
function filter() {
var stream = through(write)
return stream
function write(entry) {
if(!/^(.+)\-(.+)(\-(.+))?/g.test(entry.path.replace(__dirname))) {
entry.ignore()
return
}
if(entry.stat.isFile() && path.extname(entry.path) === '.md') {
stream.queue(entry)
}
}
}
// staple the file contents onto the meetup markdown files.
function annotateContent() {
var stream = through(write, end)
, pending = 0
, ended
return stream
function write(entry) {
++pending
fs.readFile(entry.path, 'utf8', onread)
function onread(err, data) {
if(err) {
return stream.emit('error', err)
}
stream.queue({
entry: entry
, data: data
})
--pending
check()
}
}
function end() {
ended = true
check()
}
function check() {
if(ended && !pending) {
stream.queue(null)
}
}
}
// turn the meetup {data: filedata, entry: entry} data into
// metadata about the meetups.
function toMetadata() {
var stream = through(write)
return stream
function write(data) {
var lexed = marked.lexer(data.data)
, plural = {
'urls': true
, 'organizers': true
}
, formats = []
, format
, meta
meta = [lexed[1].header].concat(lexed[1].cells).reduce(function(lhs, rhs) {
var key = slugify(rhs[0].slice(2, -3))
lhs[key] = plural[key] ? rhs[1].split(/,\s*/) : rhs[1]
return lhs
}, {})
meta.name = lexed[0].text
while(lexed[0].type !== 'hr') {
lexed.shift()
}
lexed = lexed.slice(2)
// nom formats
while(lexed.length && lexed[0].type === 'heading' && lexed[0].depth !== 2) {
format = {}
format.name = lexed[0].text
format.info = []
lexed.shift()
while(lexed.length && lexed[0].type !== 'heading' && lexed[0].depth !== 3) {
format.info.push(lexed.shift())
}
formats.push(format)
}
meta.formats = formats
lexed.shift()
meta.process = lexed.map(function(xs) {
return xs.text || ''
}).join('\n')
stream.queue(meta)
}
}
// add geolocation
function addGeolocation() {
var stream = through(write, end)
, pending = 0
, ended
return stream
function write(meta) {
++pending
geocoder({service: 'nominatim'}).geocode(meta.location, function(res) {
meta.coords = res
stream.queue(meta)
--pending
check()
})
}
function end() {
ended = true
check()
}
function check() {
if(ended && !pending) {
stream.queue(null)
}
}
}
// simple in this case:
function slugify(input) {
input = input.toString()
return input
.replace(/[^\w\s\d\-]/g, '')
.replace(/^\s*/, '')
.replace(/\s*$/, '')
.replace(/[\-\s]+/g, '-')
.toLowerCase()
}