Tentative design for showing sponsors; add UoCambridge
[exim-website.git] / script / eximhtml2txt.pl
1 #!/usr/bin/env perl
2 #
3 use strict;
4 use warnings;
5
6 use File::Spec;
7 use HTML::FormatText;
8 use HTML::TreeBuilder;
9
10 sub process_chapter {
11     my $fn = shift;
12
13     my $tree = HTML::TreeBuilder->new->parse_file($fn);
14     my ($chapter) = $tree->look_down( "_tag", "div", "class", "chapter", );
15     return '' unless ($chapter);
16     my $formatter = HTML::FormatText->new( leftmargin => 0, rightmargin => 72 );
17
18     my $text = $formatter->format($chapter);
19     $tree->delete;
20     return $text;
21 }
22
23 sub chapters_in_order {
24     my $dir = shift;
25
26     opendir DIR, $dir or die "opendir($dir) failed: $!\n";
27     my @numeric = sort grep {/^ch\d+\.html$/} readdir(DIR);
28     closedir(DIR) or die "closedir($dir) failed: $!\n";
29
30     my @results = map {
31         $_ = File::Spec->catfile($dir, $_);
32         if (-l $_) {
33             my $t;
34             eval { $t = readlink $_ };
35             $_ = File::Spec->rel2abs($t, $dir) if defined $t;
36         }
37         $_
38     } @numeric;
39     return @results;
40 }
41
42
43 my $dir = shift;
44 die "Need a directory\n" unless defined $dir;
45
46 foreach my $fn ( chapters_in_order($dir) ) {
47     print "=" x 72, "\n";
48     print $fn, "\n";
49     print "=" x 72, "\n";
50     print process_chapter($fn);
51     print "-" x 72, "\n";
52 }