#!/usr/bin/perl -w
# debug_html.pl - Don Yang (uguu.org)
#
# Parse HTML output from Homura and convert it to CSV for debugging.
#
# 06/26/11
use strict;
# Load input
my $text = join '', <>;
# Extract string table and event list
unless( $text =~ /string_table=\n(.*)\nvar events=\n(.*)\nevents\.push/s )
{
die "Missing data arrays\n";
}
my ($raw_strings, $raw_events) = ($1, $2);
# Parse string table
print "index,string\n";
my @string_table = ();
foreach my $i ($raw_strings =~ /"((?:[^"\\]|\\.)*)"/g)
{
push @string_table, $i;
print $#string_table, ",\"$i\"\n";
}
# Parse event list and output CSV
print "t0,t1,t_offset,duration,type,slot,y|index,x|length,data\n";
my $last_timestamp = 0;
my @event = ();
foreach my $i ($raw_events =~ /(\d+)[,\]]/g)
{
push @event, $i;
next if (scalar @event) < 6;
my ($offset, $duration, $type, $slot, $y, $x) = @event;
@event = ();
# Output timestamp
my $t0 = $last_timestamp + $offset;
my $t1 = ($duration == 0 ? "inf" : $t0 + $duration);
print "$t0,$t1,$offset,$duration,";
$last_timestamp = $t0;
if( $type == 0 )
{
print "CURSOR,$slot,$y,$x,\n";
}
elsif( $type == 1 || $type == 2 )
{
print (($type == 1 ? "REPLACE" : "APPEND"), ",$slot,$y,$x,");
if( $y <= $#string_table )
{
my $s = $string_table[$y] . " ";
my $end = 0;
for(my $length = 0; $length < $x; $length++)
{
if( substr($s, $end, 2) eq "\\u" ) { $end += 6; }
elsif( substr($s, $end, 1) eq "\\" ) { $end += 2; }
else { $end++; }
}
$s = substr($s, 0, $end);
print "\"$s\"\n";
}
else
{
print "ERROR\n";
}
}
elsif( $type == 3 )
{
print "DELETE,$slot,$y,$x,\n";
}
else
{
print "SLOW_CURSOR,$slot,$y,$x,", $type - 3, "\n";
}
}