/
PEG.pm
211 lines (170 loc) · 5.36 KB
/
PEG.pm
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package PEG;
use Dancer ':syntax';
use Encode qw(decode);
use XML::RSS;
use DateTime;
use Data::ICal;
use Data::ICal::Entry::Event;
use DateTime::Format::ICal;
our $VERSION = '0.1';
my %content = (
data => {},
src => {
news => 'news.yml',
events => 'events.yml',
earlier_events => 'events.yml',
},
);
sub _read_file {
my $name = shift;
my $now = DateTime->now->ymd; # YYYY-MM-DD
$now =~ s/-/./g;
my $file = path config->{appdir}, 'data', $content{src}{$name};
my $current_stamp = (stat($file))[9];
my $sub = $name eq 'news' ? 'news' : 'events';
if (not $content{data}{$name} or $content{stamp}{$name} < $current_stamp) {
my $data = YAML::LoadFile($file);
if ($name eq 'news') {
$content{data}{$name}{$sub} = $data;
} elsif ($name eq 'events') {
$content{data}{$name}{$sub} = [ grep {$_->{date} ge $now} @$data ];
} elsif ($name eq 'earlier_events') {
$content{data}{$name}{$sub} = [ reverse grep {$_->{date} lt $now } @$data ];
}
$content{stamp}{$name} = $current_stamp;
}
return;
}
sub _content {
foreach my $src (keys %{$content{src}}) {
_read_file($src);
}
return $content{data};
}
get qr{^ / (?: index \. html )? $}x => sub {
template 'index' => _content()->{'index'};
};
my @pages = qw{
what why who sponsors members contact
membership benefits about earlier_events mailing_lists
news
news/grants-to-invite-speakers-to-non-perl-events
news/announcement-and-public-discussion-lists
events/fosdem_2011
events/plat_forms_2011
};
get qr{^ / ([\w/-]+) $ }x => sub {
# get page
my ($page) = splat;
# we have the page or we pass up on it
grep { $page eq $_ } @pages or return pass;
# render it
template $page => _content->{$page};
};
get '/events' => sub {
my @events = map {$_->{text} = _event_text($_); $_} @{ _content->{events}{events} };
template 'events' => {events => \@events};
};
get '/calendar' => sub {
my $calendar = Data::ICal->new();
foreach my $n (@{ _content()->{events}{events} }) {
my $url = '';
if ($n->{url}) {
$url = $n->{url};
}
my $wiki = '';
if ($n->{wiki}) {
$n->{wiki} =~ s/ /_/g;
$wiki = "http://perlfoundation.org/perl5/$n->{wiki}";
}
my ($year, $month, $day) = split /\./, $n->{date};
my $dt = DateTime->new(year => $year, month => $month, day => $day);
my $duration = DateTime::Duration->new(days => $n->{days});
my $vevent = Data::ICal::Entry::Event->new();
$vevent->add_properties(
summary => decode( 'utf-8', $n->{title} ),
description => "",
dtstart => DateTime::Format::ICal->format_datetime($dt),
location => ($n->{address} || ''),
url => ($wiki || $url || ''),
duration => DateTime::Format::ICal->format_duration($duration),
);
$calendar->add_entry($vevent);
}
return $calendar->as_string;
};
# for historical reasons:
get '/rss' => sub {
return _rss('news');
};
get '/rss/news' => sub {
return _rss('news');
};
get '/rss/events' => sub {
return _rss('events');
};
sub _rss {
my $name = shift;
my $rss = XML::RSS->new( version => '1.0' );
my $year = 1900 + (localtime)[5];
my $base = "http://perl-ecosystem.org";
my $url = "$base/$name";
$rss->channel(
title => "Perl Ecosystem Group $name",
link => $url,
description => 'Bridging the gap between business and the open source Perl community',
dc => {
language => 'en-us',
publisher => 'gabor@perl-ecosystem.org',
rights => "Copyright 2010-$year, Perl Ecosystem Group",
},
syn => {
updatePeriod => "hourly",
updateFrequency => "1",
updateBase => "1901-01-01T00:00+00:00",
}
);
foreach my $n (@{ _content()->{$name}{$name} }) {
my $text = $n->{text} || '';
$text =~ s{"/}{"$base/}g;
if ($name eq 'events') {
$text = _event_text($n);
}
$rss->add_item(
title => decode( 'utf-8', $n->{title} ),
link => $url . ( $n->{permalink} || '' ),
description => decode( 'utf-8', $text ),
dc => {
creator => $n->{author},
date => $n->{date},
subject => (
$n->{tags} and ref( $n->{tags} ) eq 'ARRAY' ?
join( ', ', @{ $n->{tags} } ) :
''
),
},
);
}
return $rss->as_string;
};
sub _event_text {
my ($n) = @_;
my $text = '';
if ($n->{days} == 1) {
$text = "On $n->{date}";
} else {
$text = "starting on $n->{date} for $n->{days} days";
}
$text .= "<br />";
if ($n->{url}) {
$text .= qq{ <a href="$n->{url}">$n->{title}</a><br /> };
}
if ($n->{wiki}) {
$n->{wiki} =~ s/ /_/g;
my $wiki = "http://perlfoundation.org/perl5/$n->{wiki}";
$text .= qq{ Presence is being organized on the <a href="$wiki">wiki</a><br /> };
}
$text .= " Location: $n->{address}<br />";
return $text;
}
true;