]> git.pond.sub.org Git - empserver/blob - info/subj2html.pl
Use strict & warnings. Fix several minor bugs uncovered by it.
[empserver] / info / subj2html.pl
1 #!/usr/local/bin/perl
2
3 use strict;
4 use warnings;
5
6 my $dome = 0;
7 my (@Fld, $str, @a);
8
9 line: while (<>) {
10     chop;       # strip record separator
11     @Fld = split(' ', $_, 9999);
12
13 procline:
14     if (/^\.TH/) {
15         $str=$Fld[2];
16         for (my $i=3; $i <= $#Fld; $i++) {
17             $str .= " " . $Fld[$i];
18         }
19         $str = &htmlify($str);
20         printf("<title>%s : %s</title><h1>%s : %s</h1>\n",
21                $Fld[1], $str, $Fld[1], $str);
22         next line;
23     }
24
25     if (/^\.SY../) {
26 #       $i = $_ =~ "\"" && ($RLENGTH = length($&), $RSTART = length($`)+1);
27 #       $str = substr($_, $i + 1, length($_) - $i - 1);
28         $str = substr($_,5);
29         $str = &htmlify($str);
30         printf "<samp>[##:##] </samp><KBD>%s</KBD><p>\n", $str;
31         next line;
32     }
33
34     if (/^\.EX../) {
35         $str = substr($_, 5);
36         printf "<br><samp>[##:##] </samp><kbd>%s</kbd><p>\n", &htmlify($str);
37         next line;
38     }
39
40     if (/^\.L/) {
41         @a = split('[: ",.]+');
42         $str = &anchor($a[2]);
43         $str = ("$str $a[3]") if defined $a[3];
44         printf("<br>%s\n", $str);
45         next line;
46     }
47
48     if (/^\.s3/) { printf (("<p>\n"));  next line; }
49     if (/^\.s1/) {printf (("<hr> \n")); next line; }
50     if (/^\.br/) { printf "<br>\n"; next line; }
51
52     if (/^\.(FI|fi)/) { printf (("</pre><p>\n")); $dome = 0; next line;   }
53     if (/^\.(NF|nf)/) { printf (("<p><pre>\n")); $dome = 1; next line;    }
54
55     if ($dome == 1) {
56         @a = split('[: ",.]+');
57
58         for (my $i = 1; $i <= $#a ; ($i)++) {       
59             printf("%s",&anchor($a[$i]));
60             for (my $j = 0; $j < 20 - length($a[$i]); $j++) {
61                 printf(" ");
62             }
63         }
64         printf("\n");
65         while (<>) {
66             chop;       # strip record separator
67             @a = split('[: ,.]+');
68             @Fld = split(' ', $_, 9999);
69             if (/^\./) { goto procline; }
70             for (my $i = 1; $i <= $#a ; ($i)++) {       
71                 printf("%s",&anchor($a[$i]));
72                 for (my $j = 0; $j < 20 - length($a[$i]); $j++) {
73                     printf(" ");
74                 }
75             }
76             printf("\n");
77         }
78         next line;
79     }
80
81     if (/^\.SA/) {
82         @a = split('[: ",.]+');
83
84         printf("See also : %s\n",&anchor($a[2]) );
85         for (my $i = 3; $i <= $#a ; ($i)++) {       
86             printf(", %s\n",&anchor($a[$i]));
87         }
88
89         while (<>) {
90             chop;       # strip record separator
91             @a = split('[: ,.]+');
92             @Fld = split(' ', $_, 9999);
93             if (/^\./) { goto procline; }
94             for (my $i = 0; $i <= $#a ; ($i)++) {       
95                 printf(", %s\n",&anchor($a[$i]));
96             }
97         }
98             
99         exit(0);
100     }
101             
102     if (/^\./) {        next line; }
103
104     if (/^(See also|See Also|see also)/) {
105         @a = split('[: ,.]+');
106
107         printf("See also : %s\n",&anchor($a[2]) );
108         for (my $i = 3; $i <= $#a ; ($i)++) {       
109             printf(", %s\n",&anchor($a[$i]));
110         }
111
112         while (<>) {
113             chop;       # strip record separator
114             @a = split('[: ,.]+');
115             @Fld = split(' ', $_, 9999);
116             if (/^\./) { goto procline; }
117             for (my $i = 0; $i <= $#a ; ($i)++) {       
118                 printf(", %s\n",&anchor($a[$i]));
119             }
120         }
121             
122         exit(0);
123     }
124             
125     printf "%s\n", &htmlify($_);
126 }
127
128 #sub anchor {
129 #    local($_) = @_;
130 #    $file = $_ . ".t";
131 #    if (-r $file) {
132 #       return ("<a href=" . $_ . ".html" . ">" . $_ . "</a>");
133 #    } else {
134 #       return ( " " . $_ . " ");
135 #    }
136 #}
137
138 sub anchor {
139     local ($_) = @_;
140     my (@file, $file);
141     $file = $_ . ".t";
142 #    if (-r $file) {
143     if (1) {
144       $file =~ s/.t$/.html/;
145       return ("<a href=\"$file\">$_</a>");
146     } else {
147         @file = <$_*t>;
148         if (@file) {
149           warn "Expanding $_ to $file[0]\n";
150           $file[0] =~ s/.t$/.html/;
151           return ("<a href=\"$file[0]\">$_</a>");
152         } else {
153           warn "Unable to link $_\n";
154           return ( "<em>$_</em>");
155         }
156     }
157 }
158
159     
160 # Translate HTML special characters into escape sequences
161 sub htmlify {
162         local($_) = @_;
163         s/^\"(.*)\"$/$1/;
164         s/\\&//g;               # a nothing character
165         s/\&/&amp;/g;
166         s/\</&lt;/g;
167         s/\>/&gt;/g;
168         while (@a = /(\\\*Q)([A-Za-z0-9\-\.]+)(\\\*U)/) {
169             /(\\\*Q)([A-Za-z\-]+)(\\\*U)/;
170             $_ = $` . &anchor($a[1]) . $';
171         }
172         while (@a = /(\\\*Q)(\"info )([A-Za-z0-9\-\.]+)(\\\*U)/) {
173             /(\\\*Q)(\"info )([\w\-\.]+)(\\\*U)/;
174             $_ = $` . "\"info " . &anchor($a[2]) . $';
175         }
176         while (@a = /(\"info )([A-Za-z0-9\-\.]+)/) {
177             /(\"info )([\w\-\.]+)/;
178             $_ = $` . "\"info " . &anchor($a[1]) . $';
179         }
180         s/\\\*Q/<em>/g;
181         s/\\\*U/<\/em>/g;
182         s/\\fI/<em>/g;
183         s/\\fR/<\/em><\/em>/g;
184         s/\\fB/<strong>/g;
185         s/\\fP/<\/strong><\/em>/g;
186         s/\\\*\(bF/<strong>/g;  # bold font
187         s/\\\*\(pF/<\/strong><\/em>/g; # pica font
188         s/\\\*\(nF/<\/strong><\/em>/g; # normal font
189         s/\\\*\(iF/<em>/g;      # italic font
190         s/\\\(mu/x/g;           # multiply symbol
191         s/\\ / /g;              # should be non breaking space
192         return $_;
193 }
194
195 #sub htmlify {
196 #    local($str) = @_;
197 #    $str;
198 #}
199
200