Debian Codegraph @

winfo/Newsletters/mkindex.pl

raw
1 #!/usr/local/bin/perl -wT
2
3 #########
4 # author: rmp for edgrif
5 #
6 # This script was originally written by Roger Pettet for Ed Griffiths. It's purpose
7 # is trawl through all the newsletters in one or more YearNNNN directories and prepare
8 # an index.shtml file for each directory. The index file contains nested lists of links
9 # to the major sections within the newsletter. This makes it a bit easier to just eyeball
10 # the newsletters and see what's in the latest one.
11 #
12 # N.B. We rely on the format of the newsletters being just-so and in particular we rely on
13 # the format of the anchors and headers being:
14 #
15 # <a name="header_abbreviation"></a><hN>The actual header</hN>
16 #
17 # The script picks out the text of the anchor flag and the text of the actual header.
18 #
19 #
20 #
21
22
23 use strict;
24 my $WEB_PATH = '.';
25 my $FILE_SUFFIX = 'html';
26 my $DEFAULT_ANCHOR = 'Top' ;
27
28 for my $dir (@ARGV) {
29 if(! -d $dir ) {
30 print STDERR qq($dir is not a directory\n);
31 next;
32
33 } else {
34
35 #########
36 # untaint $dir
37 # regexp out any nasty characters here
38 # unfortunately file & directories can have virtually anything in
39 # so we do a naughty, useless untaint:
40 #
41 $dir =~ m/(.*)/s;
42 my $cleandir = $1;
43
44 my $content = "";
45 &munge_files(\$content, $cleandir);
46 &print_index(\$content, $cleandir);
47 }
48 }
49
50 1;
51
52
53 sub munge_files {
54 my ($content_ref, $dir) = @_;
55
56 my @valid_files = qw(January February March April May June July August September October November December);
57
58 for my $file_head (@valid_files) {
59 my $file = qq($dir/$file_head.$FILE_SUFFIX);
60
61 if(! -e qq($file) ) {
62 print STDERR qq($file not found\n);
63 next;
64
65 } else {
66
67 eval {
68 open(FIN, $file) || die;
69 };
70
71 if($@) {
72 print STDERR qq(Could not open $file\n);
73 next;
74 }
75
76 #########
77 # nuke input record separator
78 #
79 my $irs = $/;
80 undef($/);
81
82 my $file_contents = <FIN>;
83
84
85 #########
86 # be nice. reset record separator
87 #
88 $/ = $irs;
89
90 close(FIN);
91
92
93 my (@headers) = $file_contents =~ /<a.*?><h.>.*?<\/h.>/mig;
94 print STDERR qq(Found ), scalar @headers, qq( headers in $file\n);
95
96
97 if(scalar @headers > 0) {
98 my $htype = "" ;
99
100
101 # we could insert an extra link here to get to the overview in one
102 # go...
103
104
105 $$content_ref .= qq(\n<h3>$file_head</h3>\n<ul>\n);
106
107 for my $header (@headers) {
108
109 if (($header !~ /Next User Group Meeting/)
110 && ($header !~ /monthly build now available/)
111 && ($header !~ /ACEDB User Group Newsletter/)) {
112
113 #########
114 # remove any extra html tags in here just to clean up a little
115 #
116 my $link = $header;
117 $link =~ s/<.*?>//mig;
118
119 # extract the anchor name from the header, if there isn't one
120 # set it to the default.
121 my $aname = $header ;
122 $aname =~ s/<a name="(.*?)">.*/$1/i;
123 $aname ||= $DEFAULT_ANCHOR ;
124
125 # check what level header it is, <h2> will start a new section
126 # and all others will be subheaders until the next <h2>
127 if($header =~ /<h2>/ig) {
128 $htype = "h2" ;
129 } else {
130 $htype = "" ;
131 }
132
133 if(!$htype) {
134 $$content_ref .= qq( <ul>\n );
135 }
136
137 my $year = $dir;
138 $year =~ s/^.*\/(.*?)/$1/ig;
139
140 #########
141 # append the link to our content
142 #
143 $$content_ref .= qq( <li><a href="$WEB_PATH/$file_head.$FILE_SUFFIX#$aname">$link</a></li>\n);
144
145 if(!$htype) {
146 $$content_ref .= qq( </ul>\n);
147 }
148 }
149
150 }
151
152 $$content_ref .= qq(</ul><br />);
153 }
154 }
155 }
156 }
157
158 sub print_index {
159 my($content_ref, $dir) = @_;
160
161 my $year = $dir;
162 $year =~ s/^.*\/(.*?)$/$1/ig;
163
164 open(FOUT, ">$dir/index.shtml") || die(qq(Could not open index file));
165
166 print FOUT qq(<html>\n) ;
167
168 # warn user that file is machine generated...
169 print FOUT qq(\n<!-- -->\n) ;
170 print FOUT qq(<!-- This file was machine generated by mkindex.pl -->\n) ;
171 print FOUT qq(<!-- You should not hand edit this file, alter mkindex.pl instead. -->\n) ;
172 print FOUT qq(<!-- -->\n\n) ;
173
174 print FOUT qq(<head>
175 <title>Acedb Newsletters for $year</title>
176 <link rel="stylesheet" type="text/css" href="../newsletter.css" TITLE="Style sheet for Newsletters">
177 </head>
178 <body>
179 <h1 align="center">Acedb Newsletters for $year</h1>\n\n);
180
181 print FOUT $$content_ref;
182
183 print FOUT qq(\n\n<HR>\n);
184
185 print FOUT qq(<ADDRESS><A HREF="mailto:acedb\@sanger.ac.uk">acedb\@sanger.ac.uk</A></ADDRESS>\n);
186
187 print FOUT qq(\n</body>\n</html>\n);
188
189 close(FOUT);
190
191 print STDERR qq(Generated index $dir/index.shtml\n);
192 }