OSDN Git Service

eee13de91ef024d2df20c061e9b63c7288549271
[linuxjf/JF.git] / bin / sgml21html
1 #! /usr/bin/perl -w
2 # (if your perl5 lives in different path, plese edit the line above.)
3 #
4 # sgml21html - convert SGML into one HTML (using sgml2html)
5 #
6 # Copyright (C) 1999 Sadaaki Kato <skato@venus.dti.ne.jp>
7 #                    Taketoshi Sano <kgh12351@nifty.ne.jp>
8 #
9 #     All rights reserved.
10 #     This is free software with ABSOLUTELY NO WARRANTY.
11 #
12 # You can redistribute it and/or modify it under the terms of 
13 # the GNU General Public License version 2.
14 #
15 # $Date: 2000/05/07 15:22:35 $
16 #
17 # usage: sgml21html file[.sgml]
18 # Please read the Notes at the end of this script
19
20 require 5.004;
21 use strict;
22 use IO::File;
23
24 # Definition for Commands - Please edit to match your system
25 my $S2H = "sgml2html"; # used to convert SGML -> HTML initially
26
27 my @optS2H = (); # for jLinuxdoc-SGML (linuxdoc-sgml-ja) 1.5.1
28 # my @optS2H = ("-l", "ja", "-c", "nippon"); # for sgml-tools-ja 1.0.9
29
30 my $NKF = "nkf -j"; # used to convert JP code to jis
31 # my $NKF = "kcc -j"; # if you don't have nkf but have kcc, use this line.
32
33 my $DATE = "date"; # used to get date output
34
35 #
36 my $TMPF ="./sgml21html$$.tmp.html"; # temporary file name
37
38 # $HED defines the header form. 
39 # 'SGML_TITLE' will be replaced by the title strings.
40
41 my $DEFAULT_HEAD = <<END_OF_HEADER;
42 <HTML>
43 <HEAD>
44  <META http-equiv="Content-Type" content="text/html; charset=ISO-2022-JP">
45 <TITLE>
46 SGML_TITLE
47 </TITLE>
48 </HEAD>
49 <BODY BGCOLOR=white>
50 END_OF_HEADER
51
52 # $FOT defines the footer form.
53 # 'TODAY' will be replaced by the $DATE output.
54
55 my $DEFAULT_FOOT = <<END_OF_FOOTER;
56 <HR>
57 sgml21html conversion date: TODAY
58 </BODY>
59 </HTML>
60 END_OF_FOOTER
61
62 sub main() {
63
64         my $sgml = "";
65         my $name = "";
66         my $ret = 0;
67         my $title = "";
68         my @htmls = ();
69         my $html = "";
70         my @stack = ();
71         my $line = "";
72         my $date = "";
73         my $s2hvers = 0;
74
75         if (!defined $ARGV[0]) {
76                 print "usage: sgml21html SGML_file[.sgml]\n";
77                 exit 1;
78         }
79
80         if ($ARGV[0] eq "--JFindex") {
81           $DEFAULT_HEAD .= "<A HREF=\"INDEX-JF.html\">JF-INDEX (document list of JF Project)</A>";
82           shift @ARGV;
83         }
84
85         # check sgml-tools or jLinuxdoc-SGML(linuxdoc-sgml-ja)
86         $s2hvers = chkvers($S2H);
87         if($s2hvers != 0) {
88                 # for sgml-tools-ja 1.0.9
89                 @optS2H = ("-l", "ja", "-c", "nippon");
90         }
91
92         while (defined $ARGV[0]) {
93
94         # initialize / reset header & footer
95                 my $HED=$DEFAULT_HEAD;
96                 my $FOT=$DEFAULT_FOOT;
97
98         # get argument (SGML filename)
99                 $sgml = shift @ARGV;
100                 $_ = $sgml;
101                 s/\.sgml$//;
102                 s/^.*\///;
103                 $name = $_;
104                 print "NAME: " . $name . "\n";
105
106         # use sgml2html on given filename
107                 if(($ret = useS2H($sgml,$name)) != 0) {
108                         print "Can not exec sgml2html\n";
109                         exit 1;
110                 }
111
112         # get list of html files
113                 @htmls = gethtmls($name);
114                 if ( !defined $htmls[0] ) {
115                         print "Can not read html files\n";
116                         exit 1;
117                 }
118
119         # extract title
120                 if(($title = extitle($sgml)) eq "") {
121                         print "Can not extract <title>\n";
122                         exit 1;
123                 }
124
125         # make and printout the header
126                 my $fh = new IO::File "|$NKF > $TMPF";
127                 $HED =~ s/SGML_TITLE/$title/;
128                 if ( defined $fh ) {
129                         print $fh $HED;
130                 } else {
131                         print "Can not write temporary file\n";
132                         exit 1;
133                 }
134
135         # make text body
136                 foreach $html (@htmls) {
137                         @stack = maketxt($html, $name);
138                         if( defined $stack[0] ) {
139                                 foreach $line (@stack) {
140                                         print $fh $line;
141                                 }
142                         } else {
143                                 print "Can not make text. aborted\n";
144                                 $fh->close;
145                                 my @del = ($TMPF);
146                                 unlink @del;
147                                 exit 1;
148                         }
149                 }
150
151         # add footer
152                 $date = getdate($DATE);
153                 $FOT =~ s/TODAY/$date/;
154                 print $fh $FOT;
155
156         # close temporary file
157                 $fh->close;
158
159         # remove htmlfiles from sgml2html
160                 unlink @htmls;
161
162         # mv temporary file to target html file
163                 $html = $name . ".html";
164                 link $TMPF, $html;
165                 my @del = ($TMPF);
166                 unlink @del;
167
168         # do loop
169         }
170 }
171
172 main();
173
174 # use sgml2html 
175 sub useS2H($) {
176         my ($sgml,$name) = @_;
177         my $check = $sgml . ".sgml";
178         if( ( -r $name ) || ( -r $sgml ) || -r "$check" ) {
179                 my @args = ("$S2H", @optS2H, "$sgml");
180                 if(system(@args) != 0) {
181                         print "system @args failed: $?\n";
182                         return -1;
183                 }
184         } else {
185                 print "Invalid File specified\n";
186                 return 1;
187         }
188
189         return 0;
190 }
191
192 # extract Title
193 sub extitle($) {
194         my ($sgml) = @_;
195         my $title = "";
196
197         my $fh = new IO::File;
198
199         if ( -r "$sgml" ) {
200                 $fh->open("$sgml");
201                 while( <$fh> ) {
202                         if ( /<title>(.*)$/ ) {
203                              $title = $1;
204                              return $title;
205                         }
206                 }
207         } else {
208                 print "Can not read : " . $sgml . "\n";
209                 return "";
210         }
211
212         return $title;
213 }
214
215 # get the list of html files
216 sub gethtmls($) {
217         my ($name) = @_;
218         my @htmls = ();
219         my $type = ".html";
220
221         my $i = 0;
222         my $html = $name . $type;
223
224         while( -r "$html" ) {
225                 push @htmls, $html;
226                 $i++;
227                 $html = $name . "-" . $i . $type;
228         }
229
230         return @htmls;
231 }
232
233 # cutt headers and footers, and modify toc.
234 sub maketxt($$) {
235         my ($html, $name) = @_;
236         my $pname = $name;
237         my @temp = ();
238         my @stack = ();
239
240         my $fh = new IO::File;
241         my $cut = 1;
242         my $i = 0;
243         my $line = "";
244
245         if ( ! -r "$html" ) {
246                 print "Can not read $html\n";
247                 return @stack;
248         }
249
250 # open html
251         $fh->open("$html");
252         while( <$fh> ) {
253
254         # cut header
255                 if ( /^<HR>/ ) {
256                         $cut = 0;
257                 }
258
259         # push text into stack
260                 if ( $cut != 1 ) {
261                         push @temp, $_;
262                 }
263         }
264         $fh->close;
265
266 # cut footer
267         for ($i=0;$i<6;$i++) {
268                 pop @temp;
269         }
270
271 # modify toc
272         $_ = $pname;
273         s/\+/\\\+/g;
274         $pname = $_;
275         foreach $line (@temp) {
276                 $_ = $line;
277                 s/<A HREF=\"$pname-(\d+)\.html\"/<A HREF=\"\#s$1\"/;
278                 s/<A HREF=\"$pname-\d+\.html(\#.*)\"/<A HREF=\"$1\"/;
279                 s/<A NAME=\"s(\d+)\">(\d+\.)\s(.*)<\/A/<A NAME=\"s$1\">$2<\/A> <A HREF=\"#toc$1\">$3<\/A/;
280                 push @stack, $_;
281         }
282
283         return @stack;
284 }
285
286 sub getdate($) {
287         my ($datecmd) = @_;
288         my $date = "";
289
290         my $fh = new IO::File;
291
292         $fh->open("$datecmd |");
293         while( <$fh> ) {
294                 $date = $_;
295         }
296         $fh->close;
297
298         return $date;
299 }
300
301 sub chkvers($) {
302         my ($s2hcmd) = @_;
303         my $s2hvers = 0;
304         my $chkline = "";
305
306         my $fh = new IO::File;
307
308         $fh->open("$s2hcmd |");
309         while( <$fh> ) {
310                 $chkline = $_;
311                 if(/^SGML-Tools version 1.0.9$/) {
312                         $s2hvers = 1;
313                 }
314         }
315         $fh->close;
316
317         return $s2hvers;
318 }
319
320 # Note:
321 #
322 # $Log: sgml21html,v $
323 # Revision 1.17  2000/05/07 15:22:35  takes
324 # guidance-sgml.m4: update some description
325 # archives/sgml21html: add "--JFindex" option.
326 #
327 # Revision 1.16  2000/02/23 01:27:33  morimoto
328 #
329 # Wed Feb 23 10:26:22 2000  Jun Morimoto  <morimoto@xantia.citroen.org>
330 #
331 #       * workshop/archives/sgml21html: À¸À®¤µ¤ì¤ë [Back] ¤Î¥ê¥ó¥¯Àè¤ò
332 #         INDEX-JF.html ¤ËÊѹ¹
333 #
334 # Revision 1.15  2000/01/11 15:20:04  takes
335 # more modification to escape "+" in filename
336 #
337 # Revision 1.14  1999/12/05 03:28:02  sano
338 # modify "sub maketext($$)" to handle label/ref correctly
339 #
340 # Revision 1.13  1999/09/07 04:23:45  sano
341 # add "charset=ISO-2022-JP" as a META tag in default header
342 #
343 # Revision 1.12  1999/08/19 07:00:49  sano
344 # use "$name" variable to distinguish toc related & others
345 #
346 # Revision 1.11  1999/06/27 15:22:34  sano
347 # added one line to correct the <rabel> <ref id> tag handling
348 # (thanks to fukushima-san, and hayakawa-san)
349 #
350 # Revision 1.10  1999/05/24 04:31:38  sano
351 # update the bottomline Notation
352 #
353 # Revision 1.9  1999/05/24 04:27:49  sano
354 #  add version check rutine "chkvers" to make enable using
355 #  with ether jLinuxdoc-SGML (linuxdoc-sgml-ja) 1.5.1 or
356 #  sgml-tools-ja 1.0.9
357 #
358 # Revision 1.8  1999/05/24 03:49:27  sano
359 # add comments for use with jLinuxdoc-SGML 1.5.1,
360 # and use with kcc.
361 #
362 # Revision 1.7  1999/05/24 01:21:02  sano
363 #  update to be usable with sgml-tools-ja 1.0.9, and
364 #  fix bug in extitle (add return to avoid the repeating match)
365 #
366 # Revision 1.6  1999/04/30 12:54:02  sano
367 # change <toc> modify code to handle the name including "-"
368
369 # Revision 1.5  1999/03/20 18:43:06  sano
370 # add comments, and header and footer initialization
371
372 # v1.4 - remove absolute path for commands,
373 # v1.4 -  and replace die to return -1 in useS2H
374 # v1.4 -
375 # v1.3 - add and modify error recovery in main loop
376 # v1.3 -
377 # v1.2 - Fix main loop bug.
378 # v1.2 -
379 # v1.1 - Initial Version. announce to JF ML
380 # v1.1 -
381
382 # You need:
383 #
384 # - sgml-tools 1.0.9 or jLinuxdoc-SGML (linuxdoc-sgml-ja) 1.5.1
385 #    if you get trouble with automatic version checking,
386 #    use the fixed @optS2H line and comment out
387 #    the call for chkvers($S2H).
388 #
389 # - perl 5.004 (or later) as "/usr/bin/perl",
390 #    if you don't have it at that place, 
391 #    please change the first line to point the perl 5.004.
392 #
393 # - nkf (of kcc with the modification of this script)
394 #    if you use kcc, use commented out $NKF line.
395 #
396 # - date (this is standard command, so you should have it)
397 #