doc/ja/prehtml
2000-10-02 09:16:24 +00:00

127 lines
3.3 KiB
Perl
Executable file

#!/usr/bin/perl -w
#
# The FreeBSD Japanese Documentation Project
#
# This is a preprocessor for HTML docs.
#
# usage: prehtml [-revcheck <localtop> <relative to localtop>] <SGML filename>
# (ex. % prehtml -revcheck ../.. news/1996 index.sgml)
#
# $FreeBSD$
my $revcheck;
my $topdir;
my $reldir;
my %file;
my %rev;
### parse options ....................................................
###
sub sOPT {1};
sub sARG {2};
my $opt_state = sOPT;
my $argv;
while(defined($_ = $ARGV[0])) {
if ($opt_state eq sOPT) {
# option expected
if(/^-(.+)/) {
shift @ARGV; # discard option itself;
local $_ = $1;
/revcheck/ and do {
$topdir = shift @ARGV;
$reldir = shift @ARGV;
if($reldir ne '' and $reldir !~ /\/$/) {
$reldir .= "/";
}
$revcheck = 1;
next;
};
} else {
# this is not a option but an argument
$opt_state = sARG;
next;
}
} elsif ($opt_state eq sARG) {
$file{target} = $_;
if($revcheck) {
s/.sgml$//;
my $basename = $_;
$file{cvsweb} = "www/en/${reldir}${basename}.sgml";
$file{orgbase} = "${topdir}/../en/${reldir}${basename}";
$rev{org} = get_rev_org($file{orgbase}.".sgml");
}
last;
}
die "internal error: option parsing abnormally terminated.\n";
}
### add and replace entities .........................................
###
open TARGET,"<$file{target}" or die "cannot open a target file: $!\n";
my $pos_date;
### first, get date string and rev_target
while(defined($_ = <TARGET>)) {
if(/<!ENTITY\s+date\s+\"\$Free[B]SD: [^\s]+ [.0-9]+ ([\/0-9]+)[^\$]*\$\">/) {
$pos_date = tell(TARGET);
$date = "<!ENTITY date \"Last modified: $1\">";
}
if($revcheck) {
if(/[Oo]riginal [Rr]evision:[ \t]*([0-9.]+)/) {
$rev{target} = $1;
}
last if($revcheck and $rev{target} and $date);
} else {
last if($date);
}
}
$rev{target} ||= "TARGET revision not found";
$rev{org} ||= "ORG revision not found";
### next, put lines and replace the line with
### $date + entity difinitions for revcheck
seek TARGET,0,0;
while(defined($_ = <TARGET>)) {
if(tell(TARGET) == $pos_date) {
print $date,"\n";
if($revcheck) {
#print STDERR "$rev{org} -> $rev{target}\n";
print "<!ENTITY base CDATA '$topdir'>\n";
print "<!ENTITY file.cvsweb CDATA '$file{cvsweb}'>\n";
print "<!ENTITY file.orgbase CDATA '$file{orgbase}'>\n";
print "<!ENTITY rev.latest CDATA '$rev{org}'>\n";
print "<!ENTITY rev.target CDATA '$rev{target}'>\n";
printf "<!ENTITY %% rev.diff '%s'>\n", ($rev{org} eq $rev{target}) ? "IGNORE" : "INCLUDE";
}
} else {
# for backward compatibility
s/<!ENTITY \% rev.incl SYSTEM "[-a-zA-Z0-9]+.revinc"> \%rev.incl;//;
print;
}
}
close TARGET;
exit 0;
sub get_rev_org
{
my $infile = shift @_;
my $rev_org;
open ORG,"<$infile" or return undef;
while(defined($_ = <ORG>)) {
if(/\$Free[B]SD: [^\s]+ ([.0-9]+) [\/0-9]+[^\$]*\$/) {
$rev_org = $1;
last;
}
}
close ORG;
return $rev_org;
}
__END__