make-html: add wilhelmtux color
[lugs.git] / make-html / make-html.pl
1 #!/usr/bin/perl
2 #
3 # Konvertiert die LUGS-Terminliste (im ASCII Format) in ein HTML-File
4 #
5 # (c) 1996-1998 Roland Alder
6 # (c) 2007-2008, 2011-2015, 2017 Steven Schubiger
7
8 use strict;
9 use warnings;
10 use lib qw(lib);
11
12 my $VERSION = '0.06';
13
14 #-----------------------
15 # Start of configuration
16 #-----------------------
17
18 #
19 # If you're looking for the template,
20 # it is contained below __DATA__ at the end
21 # of this script.
22 #
23
24 my $Config = {
25 data_source => './termine.txt',
26 html_file => './index.phtml',
27 colors => {
28 fremd => 'a2eeff', # blau/gruen
29 treff => '99ccff', # ex http://www.zuerich.ch/
30 seeland => 'ffffbb', # gelb
31 aargau => 'ffbbff', # violett
32 bern => 'a5f6bb', # gruen
33 spec => 'ff8a80', # rot
34 winti => 'd6d6ce', # ex http://www.stadt-winterthur.ch/
35 innerschweiz => '8abed7', # ex http://www.luzern.ch/
36 kreuzlingen => 'f9f9f9', # ehemals aargau (ex http://www.ag.ch/)
37 stgallen => 'e2b1a5', # wie heisst diese Farbe? :)
38 gnupingu => 'ffd133', # von http://www.gnupingu.ch/
39 debian => 'ffa500', # orange
40 hackerfunk => '99b2cd', # blau/grau
41 wilhelmtux => 'ffffbf', # from https://wilhelmtux.ch/
42 },
43 ical_dir => 'ical',
44 };
45
46 #---------------------
47 # End of configuration
48 #---------------------
49
50 #-------------------
51 # Start of internals
52 #-------------------
53
54 {
55 my $termine = LUGS::Termine::Liste->new;
56
57 $termine->init;
58
59 my ($html_before, $html_after) = $termine->extract_html;
60 $termine->parse_template;
61
62 my $fh = $termine->{fh}{out};
63
64 print {$fh} $html_before;
65 $termine->process_events;
66 print {$fh} $html_after;
67
68 $termine->cleanup;
69
70 $termine->finalize;
71 }
72
73 package LUGS::Termine::Liste;
74
75 use constant true => 1;
76
77 use File::Copy qw(copy);
78 use File::Temp qw(tempfile);
79 use LUGS::Events::Parser ();
80
81 # Return a new instance of our class.
82 sub new
83 {
84 my $class = shift;
85
86 return bless {};
87 }
88
89 # Open files and retrieve the modification time.
90 sub init
91 {
92 my $self = shift;
93
94 $self->{mtime} = scalar localtime +(stat($Config->{data_source}))[9];
95
96 open($self->{fh}{in}, '<', $Config->{html_file}) or die "Cannot open $Config->{html_file}: $!\n";
97 ($self->{fh}{out}, $self->{tmp_file}) = tempfile(UNLINK => true);
98 }
99
100 # Close file handles.
101 sub cleanup
102 {
103 my $self = shift;
104
105 foreach my $handle (qw(in out)) {
106 close($self->{fh}{$handle});
107 }
108 }
109
110 # Copy the temporary file to the HTML file's location.
111 sub finalize
112 {
113 my $self = shift;
114
115 copy($self->{tmp_file}, $Config->{html_file})
116 or die "Cannot copy $self->{tmp_file} to $Config->{html_file}: $!\n";
117 }
118
119 # Extract chunks before and after where the events get populated in.
120 sub extract_html
121 {
122 my $self = shift;
123
124 my $fh = $self->{fh}{in};
125 my $html = do { local $/; <$fh> };
126
127 my @regexes = (
128 qr/^ (.+? \n<!-- \s*? TERMINE_BEGIN \s*? --> \s*? \n)/sx,
129 qr/ \n(<!-- \s*? TERMINE_ENDE \s*? --> .*) $/sx,
130 );
131
132 my @chunks;
133 foreach my $regex (@regexes) {
134 push @chunks, $1 if $html =~ $regex;
135 }
136
137 return @chunks;
138 }
139
140 # Dump regular events formatted to the output handle.
141 sub process_events
142 {
143 my $self = shift;
144
145 my $parser = LUGS::Events::Parser->new($Config->{data_source});
146
147 my $i;
148 my %month_names = map { sprintf("%02d", ++$i) => $_ }
149 qw(Januar Februar M&auml;rz April Mai Juni Juli
150 August September Oktober November Dezember);
151
152 my $seen ||= '';
153 my $print_month = sub
154 {
155 my ($event) = @_;
156
157 my $year = $event->get_event_year;
158 my $month = $event->get_event_month;
159 my $day = $event->get_event_day;
160
161 if ($month ne $seen) {
162 $seen = $month;
163 $self->print_template('jahreszeit',
164 {
165 MONAT => $month_names{$month},
166 JAHR => $year,
167 });
168 }
169 };
170
171 $self->print_template('tabellenstart');
172 $self->print_template('kopfdaten');
173
174 while (my $event = $parser->next_event) {
175 $print_month->($event);
176
177 my $anchor = $event->get_event_anchor;
178
179 $self->print_template('farbe',
180 {
181 FARBE => $Config->{colors}->{$event->get_event_color}
182 });
183
184 $self->print_template('anker/wann',
185 {
186 ANKER => $anchor,
187 WOCHENTAG => $event->get_event_weekday,
188 TAG => $event->get_event_day,
189 });
190
191 $event->get_event_time
192 ? $self->print_template('zeit',
193 {
194 UHRZEIT => $event->get_event_time,
195 })
196 : $self->print_template('blank');
197
198 $event->get_event_responsible
199 ? $self->print_template('verantwortlich',
200 {
201 WER => $event->get_event_responsible,
202 })
203 : $self->print_template('blank');
204
205 $self->print_template('titel',
206 {
207 BEZEICHNUNG => $event->get_event_title,
208 });
209
210 $event->get_event_location
211 ? $self->print_template('standort',
212 {
213 STANDORT => $event->get_event_location,
214 })
215 : ();
216
217 $event->get_event_more
218 ? $self->print_template('infos',
219 {
220 INFORMATIONEN => $event->get_event_more,
221 })
222 : ();
223
224 my $ics_file = "$anchor.ics";
225 my $ics_link = join '/', ($Config->{ical_dir}, $ics_file);
226
227 $self->print_template('ical',
228 {
229 LINK => $ics_link,
230 });
231
232 $self->print_raw_html('</tr>');
233 }
234
235 $self->print_template('tabellenende');
236 $self->print_template('fussnoten',
237 {
238 AENDERUNG => $self->{mtime},
239 });
240 }
241
242 # Parse the template as outlined below __DATA__ and create
243 # a lookup map.
244 sub parse_template
245 {
246 my $self = shift;
247
248 my $template = do { local $/; <DATA> };
249
250 $self->{template} = [ map { s/\n{2,}$/\n/; $_ } # # description
251 grep /\S/, # -
252 split /\# \s+? .+? \s+? -\n/x,
253 $template ];
254 my @descriptions;
255 push @descriptions, $1 while $template =~ /\# \s+? (.+?) \s+? -\n/gx;
256
257 my $i;
258 $self->{lookup} = { map { $_ => $i++ } @descriptions };
259 }
260
261 # Look up the template item, substitute it with the data
262 # given and print it to the output handle.
263 sub print_template
264 {
265 my $self = shift;
266 my ($keyword, $data) = @_;
267
268 return unless exists $self->{lookup}->{$keyword};
269
270 my $item = $self->{template}->[$self->{lookup}->{$keyword}];
271
272 my %markers = (
273 begin => '[%',
274 end => '%]',
275 );
276 foreach my $marker ($markers{begin}, $markers{end}) {
277 $marker = qr/\Q$marker\E/;
278 }
279
280 foreach my $name (keys %$data) {
281 $item =~ s/$markers{begin}
282 \s*?
283 $name
284 \s*?
285 $markers{end}
286 /$data->{$name}/gx;
287 }
288
289 my $fh = $self->{fh}{out};
290 print {$fh} $item;
291 }
292
293 # Print raw HTML to the output handle.
294 sub print_raw_html
295 {
296 my $self = shift;
297 my ($html) = @_;
298
299 my $fh = $self->{fh}{out};
300 print {$fh} $html, "\n";
301 }
302
303 #-----------------
304 # End of internals
305 #-----------------
306
307 #
308 # Do not change the data descriptions within '# <name>' without
309 # adjusting the code accordingly; furthermore, the hyphen '-'
310 # is required and two trailing newlines at the end of the
311 # template item, too.
312 #
313
314 __DATA__
315
316 # tabellenstart
317 -
318 <table border=0 cellpadding=1 cellspacing=2>
319
320 # kopfdaten
321 -
322 <tr><td>&nbsp;</td></tr>
323 <tr><td colspan=4 align=left><h2>Definitive Daten</h2></td></tr>
324 <tr><th align=left>Tag</th><th align=left>Zeit</th><th align=left>Verantwortlich</th><th align=left>Anlass, Thema</th></tr>
325
326 # jahreszeit
327 -
328 <tr><th align=left colspan=3><br><font size="+1">[% MONAT %] [% JAHR %]</font></th></tr>
329
330 # anker
331 -
332 <a name="[% WERT %]"></a>
333
334 # farbe
335 -
336 <tr bgcolor="#[% FARBE %]">
337
338 # anker/wann
339 -
340 <td valign=top><a name="[% ANKER %]"></a>[% WOCHENTAG %], [% TAG %].</td>
341
342 # zeit
343 -
344 <td valign=top>[% UHRZEIT %]</td>
345
346 # verantwortlich
347 -
348 <td valign=top>[% WER %]</td>
349
350 # titel
351 -
352 <td valign=top><b>[% BEZEICHNUNG %]</b>
353
354 # standort
355 -
356 <br><font size=-1>[% STANDORT %]</font>
357
358 # infos
359 -
360 <br>[% INFORMATIONEN %]
361
362 # ical
363 -
364 <td valign=top><a href="[% LINK %]">iCal</a></td>
365
366 # tabellenende
367 -
368 </table>
369
370 # fussnoten
371 -
372 <p>
373 <font size="-1">Alle Angaben ohne Gew&auml;hr, letzte &Auml;nderung der Terminliste: [% AENDERUNG %]</font>
374
375 # blank
376 -
377 <td>&nbsp;</td>