#!/usr/bin/perl # # ldp_mk - create all various output forms needed for the LDP from SGML file # # gferg@sgi.com / Ferg # # LOG: # 02Aug2000 - added cmd-line options and index creation (gferg) # if($ARGV[0] eq '') { &usage(); exit(0); } my($_toolroot) = $ENV{'SGML_TOOLROOT'} || '/export/sunsite/users/gferg/toolroot'; my($_jade) = 'jade-1.2.1'; my($dtd) = 'SGML'; my($dcl) = ''; my($linuxdoc) = 1; my($create_index) = 1; my($cmd, $fname, $fname_wo_ext, $txt_filter, $style, $s, $db_v, $x) = ''; my(@flines) = (); while(1) { if ($ARGV[0] eq "-style") { shift(@ARGV); $style = $ARGV[0]; shift(@ARGV); } elsif ($ARGV[0] eq "-txt") { shift(@ARGV); $txt_filter = $ARGV[0]; shift(@ARGV); } elsif ($ARGV[0] eq "-no_index") { shift(@ARGV); $create_index = 0; } else { last; } } $fname = $ARGV[(@ARGV + 0) - 1]; $fname_wo_ext = $fname; if( $fname =~ /\.[\w]+ml$/ ) { $fname_wo_ext =~ s/\.[\w]+$//; } else { if( -e "$fname.sgml" ) { $fname .= "\.sgml"; } else { $fname .= "\.xml"; } } if( !(-e "$fname") ) { print "\nldp_mk: ERROR - cannot find/read $fname\n"; &usage(); exit(0); } if( $style eq '' ) { $style = "$_toolroot/dsssl/docbook/html/ldp.dsl#html"; } # determine DTD # open(FP_IN, "head -50 $fname | grep -i '\!doctype' |") || die "\nldp_mk: ERROR - cannot determine DTD for $fname\n"; $s = ; close(FP_IN); if( $s eq '' ) { die "\nldp_mk: cannot determine DTD for $fname\n"; } if( $s =~ /linuxdoc/i ) { $linuxdoc = 1; print "\nldp_mk: $fname is LinuxDoc SGML\n"; if( $txt_filter eq '' ) { $txt_filter = 'lynx'; } } else { # determine docbook version # ($db_v) = ($s =~ /\s+V(\d)\./i); # do not override any various SGML catalog file settings # if( $s =~ /\ xml\ /i ) { $dtd = 'XML'; $x = 'x'; $dcl = "${_toolroot}/${_jade}/pubtext/xml.dcl"; } if( $ENV{'SGML_CATALOG_FILES'} eq '' ) { $s = ($db_v =~ /3/ ? "${_toolroot}/dtd/docbook${x}_31/catalog:" : "${_toolroot}/dtd/docbook${x}_41/catalog:") . "${_toolroot}/dsssl/docbook/catalog:" . "${_toolroot}/${_jade}/dsssl/catalog"; $ENV{'SGML_CATALOG_FILES'} = $s; } if( !(-d "$fname_wo_ext") ) { mkdir("$fname_wo_ext", 0755); } $linuxdoc = 0; print "\nldp_mk: $fname is DocBook (vers. $db_v) $dtd\n"; if( $txt_filter eq '' ) { $txt_filter = 'w3m'; } } # create the index... # if( $linuxdoc == 0 && $create_index == 1 ) { print "\nldp_mk: creating index from $fname...\n"; $cmd = "$_toolroot/mkindex/collateindex.pl -N -o index.sgml; " . "$_toolroot/$_jade/jade/jade -t sgml -V html-index -d $style $dcl $fname ; " . "$_toolroot/mkindex/collateindex.pl -g -t Index -i doc-index " . "-o index.sgml HTML.index;" . "rm -f HTML.index"; system($cmd); } # create HTML version # print "\nldp_mk: creating HTML from $fname...\n"; if( $linuxdoc == 1 ) { $cmd = "$_toolroot/sgml-tools/bin/sgml2html -c latin $fname"; } else { $cmd = "$_toolroot/$_jade/jade/jade -t sgml -i html -d $style $dcl $fname; " . "mv -f \*.html $fname_wo_ext/;" . "$_toolroot/$_jade/jade/jade -t sgml -i html -V nochunks " . "-d $style $dcl $fname > 00_${fname_wo_ext}.html"; } system($cmd); if( ($linuxdoc == 1 && !(-e "$fname_wo_ext.html")) || ($linuxdoc == 0 && !(-e "$fname_wo_ext/index.html")) ) { print "\nldp_mk: WARNING - could not create HTML: $fname_wo_ext\n"; } # create PLAIN TEXT version # print "\nldp_mk: creating plain text from $fname...\n"; if( $linuxdoc == 1 ) { $cmd = "$_toolroot/sgml-tools/bin/sgml2txt -c latin -f $fname"; } else { open(F_IN, "00_${fname_wo_ext}.html"); $s = join('', ); close(F_IN); # fix to place URLs in-line for text variant # open(F_OUT, "> TXT_${fname_wo_ext}.html"); $s =~ s/(.*?)<\/A\s*>/[$1]<\/TT>\ $3/gm; print F_OUT $s; close(F_OUT); if( $txt_filter =~ /lynx/i ) { $cmd = "lynx -dump "; } elsif( $txt_filter =~ /w3m/i ) { $cmd = "$_toolroot/w3m/bin/w3m -S -cols 78 -dump "; } elsif( $txt_filter =~ /html2text/ ) { $cmd = "$_toolroot/html2text/bin/html2text -style pretty -nobs "; } else { print "\nldp_mk: txt_filter($txt_filter) unrecognized, using lynx\n"; $cmd = "lynx -dump "; } $cmd .= "TXT_${fname_wo_ext}.html > $fname_wo_ext.txt;" . "rm -f TXT_${fname_wo_ext}.html"; } system($cmd); # patch the text file; especially needed w/lynx # if( -e "$fname_wo_ext.txt" ) { my($def_blanks)= 3; my($count) = 0; @flines = (); open(CPIPE, "pwd |"); $s = ; chop($s); my($pat) = 'file://localhost' . $s . '/'; $pat =~ s/\+/\\+/g; my($pat2) = $fname_wo_ext; $pat2 =~ s/\+/\\+/g; close(CPIPE); open(HFILE, "+< $fname_wo_ext.txt") || die "\nldp_mk: cannot open: $fname_wo_ext.txt"; while() { if (length($_) == 1) { $count++; } else { $count = 0; } if ($count <= $def_blanks) { $_ =~ s/$pat//g; $_ =~ s/00_$pat2/$pat2/g; push(@flines, $_); } } seek(HFILE,0,0); print HFILE @flines; truncate(HFILE, tell(HFILE)); close(HFILE); } else { print "\nldp_mk: WARNING - could not create TXT: $fname_wo_ext.txt\n"; } # create PDF/PS versions # # Note that we use the single-page HTML variant # print "\nldp_mk: creating PDF from $fname...\n"; my($print_str) = ''; if( $linuxdoc == 1 ) { # patch the linuxdoc-source single HTML file # system("$_toolroot/sgml_ld_1html $fname"); $print_str = "00_${fname_wo_ext}.html"; } else { # create new files from DocBook-source single HTML file to use for print # require "$_toolroot/lib/fix_print_html.lib"; &fix_print_html("00_${fname_wo_ext}.html", 'body.html', 'title.html'); $print_str = "--titlefile title.html body.html"; } if( -e "00_$fname_wo_ext.html" ) { $cmd = "$_toolroot/htmldoc/bin/htmldoc --size universal -t pdf -f " . "$fname_wo_ext.pdf $print_str; " . "$_toolroot/htmldoc/bin/htmldoc --size universal -t ps -f " . "$fname_wo_ext.ps $print_str"; system($cmd); if( !(-e "$fname_wo_ext.pdf") ) { print "\nldp_mk: WARNING - could not create $fname_wo_ext.pdf\n"; } if( !(-e "$fname_wo_ext.ps") ) { print "\nldp_mk: WARNING - could not create $fname_wo_ext.ps\n"; } else { $cmd = "gzip -f $fname_wo_ext.ps"; system($cmd); } } else { print "\nldp_mk: WARNING - HTML file 00_$fname_wo_ext.html not found; ", "skipping PDF/PS creation\n"; } # # perform the packaging steps # print "\nldp_mk: creating HTML package...\n"; if( $linuxdoc == 1 ) { $cmd = "tar -cvf $fname_wo_ext-html.tar $fname_wo_ext\*.html; " . "gzip -f $fname_wo_ext-html.tar"; } else { $cmd = "tar -cvf $fname_wo_ext-html.tar $fname_wo_ext/\*; " . "gzip -f $fname_wo_ext-html.tar"; } system($cmd); if( !(-e "$fname_wo_ext-html.tar.gz") ) { print "\nldp_mk: WARNING - could not create $fname_wo_ext-html.tar.gz\n"; } print "\nldp_mk: creating SGML package...\n"; $cmd = "cp $fname ldp_mk_tmp; gzip -f ldp_mk_tmp; " . "mv -f ldp_mk_tmp.gz $fname_wo_ext." . lc($dtd) . ".gz"; system($cmd); # if LinuxDoc, create DocBook SGML... # if( $linuxdoc == 1 ) { print "\nldp_mk: creating DocBook from LinuxDoc...\n"; $ENV{'SGML_CATALOG_FILES'} = "$_toolroot/dtd/docbook.cat:$_toolroot/$_jade/dsssl/catalog"; $cmd = "$_toolroot/$_jade/sgmlnorm/sgmlnorm " . "$_toolroot/ld2db/docbook.dcl $fname > EX_$fname ;" . "$_toolroot/$_jade/jade/jade -t sgml " . "-c $_toolroot/ld2db/catalog " . "-d $_toolroot/ld2db/ld2db.dsl#db EX_$fname > DB_$fname ;" . "rm -f EX_$fname"; system($cmd); if( !(-e "DB_$fname") ) { print "\nldp_mk: WARNING - could not create DocBook: DB_$fname\n"; } else { $cmd = "gzip -f DB_$fname"; system($cmd); } } # cleanup system("rm -f index.sgml body.html title.html"); print "\nldp_mk: completed...\n"; exit(0); sub usage { print "\n\n", "usage: ldp_mk ", "-style -txt -no_index .sgml\n\n", " where:\n", " - fullpath to a DSSSL stylesheet\n", " - one of the following: lynx, w3m, html2text\n"; }