first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / scripts / text / list_to_html.pl
1 #!/usr/bin/perl
2
3 ###############################################################################
4 #                                                                             #
5 #  Name   : list_to_html                                                      #
6 #  Author : Chris Koeritz                                                     #
7 #  Rights : Copyright (C) 1996-$now by Author                                 #
8 #                                                                             #
9 #  Purpose:                                                                   #
10 #                                                                             #
11 #    Munges multiple files together into one output file with html formatting #
12 #  that makes it appropriate for a numbered list.                             #
13 #                                                                             #
14 ###############################################################################
15 #  This program is free software; you can redistribute it and/or modify it    #
16 #  under the terms of the GNU General Public License as published by the Free #
17 #  Software Foundation; either version 2 of the License or (at your option)   #
18 #  any later version.  See: "http://www.gruntose.com/Info/GNU/GPL.html" for a #
19 #  version of the License.  Please send any updates to "fred@gruntose.com".   #
20 ###############################################################################
21
22 require "filename_helper.pl";
23
24 # this is our default output filename for the html list.
25 local($output_file) = "text_listing.html";
26
27 # make sure they gave us some files to process.
28 if ($#ARGV < 0) {
29   &instructions;
30   exit 0;
31 }
32
33 # clear and initialize our output file before doing anything else.
34 open(O_FILE, ">" . $output_file);
35
36 # add in the boilerplate for an html document and our initial nesting level.
37 print O_FILE "\
38 <!DOCTYPE html PUBLIC \"-//w3c//dtd html 4.0 transitional//en\">\
39 <html>\
40 <head>\
41 <title>generated html from list_to_html script</title>\
42 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\
43 <meta name=\"author\" content=\"Fred T. Hamster\">\
44 </head>\
45 <body>\
46 <ol>\n";
47
48 close O_FILE;
49
50 # now iterate over the list of files and dump them to our output file.
51 local($filename);
52 foreach $filename (&glob_list(@ARGV)) {
53   $chewed_filename = &basename($filename);
54   $chewed_filename =~ s/_/ /g;
55   $chewed_filename =~ s/\.[a-zA-Z][a-zA-Z][a-zA-Z]$//;
56   $chewed_filename =~ s/\.[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z]$//;
57 #print "new filename is $chewed_filename\n";
58   &do_dump($filename, $chewed_filename, $output_file);
59 }
60
61 open(O_FILE, ">>" . $output_file);
62
63 # return from first nesting and close out the document.
64 print O_FILE "\n</ol>\
65 </body>\
66 </html>\n";
67
68 close O_FILE;
69
70 exit 0;
71
72 ############################################################################
73
74 # prints help out on using this amazing program.
75
76 sub instructions {
77   print "
78
79 list_to_html: this program needs a set of filenames to print out in html
80 list format.  the resulting information will be placed in a file called
81 \"$output_file\" in the current directory.
82
83 ";
84 }
85
86 ############################################################################
87
88 # do_dump: prints the contents of the first parameter's file out by
89 # concatenating it onto the second parameter's file.  since it's an append,
90 # if you mess up, the file will at least not be overwritten.
91
92 sub do_dump {
93
94   ($to_dump, $chewed_name, $output_file) = @_;
95
96 #  print "dumpfile=$to_dump and chewed=$chewed_name and outfile=$output_file\n";
97
98   if (&basename($to_dump) eq &basename($output_file)) {
99     # we don't ever dump to the same file as we're outputting to.  that would
100     # be quite silly and would make the file pretty useless, as well as
101     # rendering the original file corrupted possibly.
102     return;
103   }
104
105   $header = "
106 <li>%1</li>
107 <ol>
108 ";
109
110   $footer = "</ol>
111 ";
112
113   $header_copy = $header;
114   $header_copy =~ s/%1/$chewed_name/;
115
116 ##print $header_copy;
117
118   open(TO_DUMP, "<$to_dump");
119   open(OUT, ">>$output_file");
120
121   print OUT $header_copy;
122
123   while (<TO_DUMP>) {
124     local($line) = $_;
125 #local($i);
126 #for ($i = 0; $i < length($line); $i++) {
127 #$curr_char = substr($line, $i, 1);
128 #print "[$curr_char]";
129 #}
130 #print "\n";
131     local($curr_char) = 0;
132 #print "start len=" . length($line) . "\n";
133     do {
134 #print "chopping\n";
135       chop $line;  # remove end of line char(s).
136       $curr_char = substr($line, length($line) - 1, 1);
137         # get new last char in string.
138     } while ( ($curr_char eq "\r") || ($curr_char eq "\n") );
139     local($do_print) = 1;
140     if (length($line) == 0) {
141       #continue;  # skip the first blank lines.
142       $do_print = 0;  #no continue statement??
143     } else {
144       $just_started = 0;
145         # a non-blank line has been seen.  now we reset our flag so we stop
146         # checking for blank lines.
147     }
148     if ($do_print) { print OUT "<li>" . $line . "</li>\n"; }
149   }
150
151   print OUT $footer;
152   close OUT;
153 }
154