#!/usr/bin/perl -w # log2art: translate LOG file into weblog entries. my $rcsid = '$Id: log2art,v 1.3 2007/10/16 19:12:27 vogelke Exp $'; use Getopt::Long; use Pod::Usage; use File::Basename; use Regexp::Common qw /URI/; use Carp; use strict; use diagnostics; use subs qw/prep usage version/; $ENV{'PATH'} = join ":", qw(/bin /usr/bin /usr/local/bin); my $myname = basename($0); $myname =~ s/\.\w*$//; # strip any extension # # Command-line options. # my %options; my @getopt_args = ( 'h|?', # print usage 'm', # print manpage 'v', # print version 'u=s', # output format ); Getopt::Long::config("noignorecase", "bundling"); usage() unless GetOptions(\%options, @getopt_args); manpage() if $options{'m'}; version() if $options{'v'}; usage() if $options{'h'} || !@ARGV; # # Real work starts here. # my $preformat = 0; # found a
line?
my $date = ''; # from each dated entry.
my $section = ''; # from each dated entry.
my $flgtitle = 0;
# FIXME: read this stuff somewhere.
my $fullname = "Karl Vogel";
my $email = "vogelke\@pobox.com";
TOP: while (<>) {
chomp;
# save beginning as title
/^BEGINNING OF LOG FOR\s+(.*)\s+==*$/ and do {
prtitle($1, $fullname);
$flgtitle = 1;
next TOP;
};
# begin dated entry; optional tab means skip date
# multiple spaces means kill everything but date
/^[0-9A-Z]/ and do {
die "No title found" unless $flgtitle;
if (/\t/) {
($date, $section) = split(/\t/);
} elsif (/(.*?) /) {
$section = $1;
} else {
$section = $_;
}
print "== $section ==\n";
next TOP;
};
# preformatted text; leave it alone except
# for command line prompts "me%" and "root#".
/---------S$/ and do {
die "No title found" unless $flgtitle;
print "```\n";
while (<>) {
chomp;
$_ = prep ($_);
s!^ !!;
last if /---------E$/;
print "$_\n";
}
print "```\n";
next TOP;
};
/^$/ and do {
die "No title found" unless $flgtitle;
print "\n";
next TOP;
};
# Handle URIs, etc.
$_ = prep ($_);
print "$_\n";
}
prfooter($email);
exit (0);
#---------------------------------------------------------------------
# Print title.
sub prtitle {
my ($title, $name) = @_;
print <<"EndTitle";
$title
$name
\%\%mtime(\%a, \%d \%b \%Y \%T)
= Introduction =
Your intro goes here.
EndTitle
}
#---------------------------------------------------------------------
# Print footer.
sub prfooter {
my ($email) = @_;
my $rev = '//$' . 'Revision: 1.1 $' . '//';
print <<"EndFooter";
= Feedback =
Feel free to send [comments mailto:$email].
-----------------------
//Generated from [\%\%infile \%\%infile] by//
//[txt2tags http://txt2tags.sourceforge.net]//{br}
$rev
EndFooter
}
#---------------------------------------------------------------------
# Print a usage message from the comment header and exit.
sub usage {
my ($emsg) = @_;
require Pod::Usage;
import Pod::Usage qw(pod2usage);
warn "$emsg\n" if defined $emsg;
pod2usage(-verbose => 1);
}
sub manpage {
require Pod::Usage;
import Pod::Usage qw(pod2usage);
pod2usage(-exitstatus => 0, -verbose => 2);
}
#---------------------------------------------------------------------
# Print the current version and exit.
sub version {
$_ = $rcsid;
s/,v / /;
@_ = split;
print "$myname v$_[2] $_[3] $_[4]\n";
exit(0);
}
#---------------------------------------------------------------------
# Prepare string by expanding tabs, etc.
sub prep
{
local($_) = shift;
1 while s/\t+/' ' x (length($&) * 5 - length($`) % 5)/e;
# no leading spaces needed.
s!^\s*!!g;
return $_;
}
#---------------------------------------------------------------------
__END__
=head1 NAME
log2art - translate LOG file into a txt2tags-style article
=head1 SYNOPSIS
log2art [-hmv] logfile
=head1 OPTIONS
=over 4
=item B<-h>
Print a brief help message and exit.
=item B<-m>
Print the manual page and exit.
=item B<-v>
Prints the version and exits.
=back
=head1 DESCRIPTION
B will read the LOGfile and write the contents to stdout
in txt2tags format. The first line will be used for the title, each
date-line will be written as an H2 header entry, and preformatted text
will be preserved.
Manual touchup will almost certainly be needed.
=head1 AUTHOR
Karl Vogel
Sumaria Systems, Inc.
=cut