User:Dr Scofield/logtransform

From Second Life Wiki
Jump to navigation Jump to search

SLlog2wiki

below is a small script that transforms SL chat logs into wiki tables for posting onto this wiki. you need to invoke it with the full name of the note taker; for example, assuming i was the note taker:

sllog2wiki --taker="Dr Scofield" < raw-log > wiki-log

you can also create a config file for this little script and place your fullname in there, like this:

# $HOME/.sllog2wikirc file
taker = Dr Scofield

and here's the script, copy & paste into a perl script file:

#!/usr/bin/perl
# Placed into the public domain by IBM Research.

use strict;
use English;
use Getopt::Long;

sub eject_header ( );
sub eject_line ( $$$$ );
sub eject_footer ( );
sub load_cfg ( );

my $you = undef;
load_cfg;
GetOptions("taker=s" => \$you);
if (!$you) {
    printf STDERR "you need to specify who you are via --taker=\"First Last\"\n";
    exit 1;
}

eject_header;
while (<>) {
    if (m/(                         # $1: captures whole lead
           \[(?:\d{4}\/\d{2}\/\d{2}\s+)?(\d{1,2}:\d{2})\]\s+?  # $2: captures optional timestamp [13:00]
           ((?:you|\S+\s+[^\s:]+))  # $3: captures Firstname Lastname
           ([\s:])                  # $4: captures (:| )
          )
          \s*                       # gobble up white space
          (.*)                      # $5 captures actual text
         /xi) {
	my $timestamp = $2 ? '[' . $2 . ']' : " ";
	my $who = $3;
	my $sep = $4;
	my $text = $5;

	if ($who =~ m/you/i) {
	    $who = $you;
	}
	eject_line $timestamp, $who, $sep, $text;
	next;
    }
    print "??? ", $_;
}
eject_footer;

exit 0;

sub eject_header() {
    printf STDOUT "{|\n";
}

my $last_who = undef;
my $cidx = 0;

sub eject_line($$$$) {
    my $timestamp = shift;
    my $who = shift;
    my $sep = shift;
    my $text = shift;

    my $bg = "";

    if (defined $last_who and $last_who ne $who) {
	$cidx = ($cidx + 1) % 2;
    }
    $last_who = $who;

    if (0 == $cidx) {
	$bg="background-color:#FFFFFF;";
    } else {
	$bg="background-color:#F0F0F0;";
    }

    $text =~ s/%/%%/g;
    $who =~ s/\s+/ /g;

    printf STDOUT "|- style=\"vertical-align:top;$bg\"\n";
    printf STDOUT "| $timestamp\n";
    if ($sep =~ /:/) {
	printf STDOUT "| style=\"white-space:nowrap;\"|[[User:$who|$who]]:\n";
	printf STDOUT "| style=\"white-space:normal;\"|" . $text . "\n";
    } else {
	printf STDOUT "| colspan=\"2\"|''$who " . $text . "''\n";
    }
}

sub eject_footer() {
    printf STDOUT "|}";
}

sub load_cfg() {
    my $cfg = $ENV{'HOME'} . "/.sllog2wikirc";
    return unless -e $cfg and -r $cfg;
    
    open CFG, "<$cfg" or die "cannot open config file $cfg ($!)";
    my @cfg = <CFG>;
    close CFG;

    foreach my $l (@cfg) {
	next unless $l =~ m/^\s*([^=]+?)\s*=\s*(.+)\s*$/;
	if (! grep(/$1=/, @ARGV)) {
	    push @ARGV, '--' . $1 . '=' . $2;
	}
    }
}