#!/usr/bin/perl
#replace_selected
use strict;
use Encode qw(from_to);
my $inText = $ARGV[1];
&from_to( $inText, 'utf-8', 'euc-jp' ); #'shiftjis' 'utf-8'
my @lines = split( /\r/, $inText );
my @trArray = ();
my $lineIndex = -1;
foreach my $line ( @lines )
{
$lineIndex++;
&addTRItem( \@trArray, $line, $lineIndex );
}
my @tableArray = ();
push( @tableArray, '
' );
foreach my $itemArrayRef ( @trArray )
{
foreach my $item ( @$itemArrayRef )
{
$item = "\t$item";
}
push( @tableArray, "" );
push( @tableArray, @$itemArrayRef );
push( @tableArray, "
" );
}
push( @tableArray, "
" );
my $outText = join( "\n", @tableArray );
&from_to( $outText, 'euc-jp', 'shiftjis' );
print $outText;
#-------
sub addTRItem
{
my $inTRItemArrayRef = shift;
my $inLine = shift;
my $inLineIndex = shift;
my @resultArray = ( );
my @itemArray = split( "\t", $inLine );
my $itemIndex = -1;
foreach my $item ( @itemArray )
{
$itemIndex++;
my $tag = "td";
if( $item eq '同上' )
{
&addRowspan( $inTRItemArrayRef, $itemIndex );
push( @resultArray, "" );
next;
}
elsif( $item eq '同左' )
{
&addColspan( \@resultArray );
push( @resultArray, "" );
next;
}
elsif( $item =~ /^\*(.+)/ )
{
$tag = "th";
$item = $1;
}
push( @resultArray, "<$tag>$item$tag>" );
}
push( @$inTRItemArrayRef, \@resultArray );
}
sub addRowspan
{
my $inTRArrayRef = shift;
my $inItemIndex = shift;
my $theTRIndex;
for( $theTRIndex = $#$inTRArrayRef ; $theTRIndex > 0 ; $theTRIndex )
{
my $arrayRef = $$inTRArrayRef[ $theTRIndex ];
unless( $$arrayRef[ $inItemIndex ] =~ m/^