App-diff2vba

 view release on metacpan or  search on metacpan

lib/App/diff2vba.pm  view on Meta::CPAN

    has maxlen    => "   =i " , default => 250;
    has adjust    => "   =i " , default => 2;
    has identical => "   !  " ;
    has reverse   => "   !  " ;
    has help      => "      " ;
    has version   => "      " ;

    has '+help' => sub {
	pod2usage
	    -verbose  => 99,
	    -sections => [ qw(SYNOPSIS VERSION) ];
    };

    has '+version' => sub {
	print "Version: $VERSION\n";
	exit;
    };

    Getopt::EX::Hashed->configure( DEFAULT => [ is => 'rw' ] );

    has SCRIPT    => ;
    has TABLE     => ;
    has QUOTES    => default => { '“' => "Chr(&H8167)", '”' => "Chr(&H8168)" };
    has QUOTES_RE => ;

} no Getopt::EX::Hashed;

sub run {
    my $app = shift;
    local @ARGV = map { utf8::is_utf8($_) ? $_ : decode('utf8', $_) } @_;

    use Getopt::EX::Long qw(GetOptions Configure ExConfigure);
    ExConfigure BASECLASS => [ __PACKAGE__, "Getopt::EX" ];
    Configure qw(bundling no_getopt_compat);
    $app->getopt || pod2usage();

    $app->initialize;

    for my $file (@ARGV ? @ARGV : '-') {
	print $app->reset->load($file)->vba->script;
    }

    return 0;
}

sub load {
    my $app = shift;
    my $file = shift;

    open my $fh, $file or die "$file: $!\n";

    $app->TABLE(my $fromto = []);
    while (<$fh>) {
	#
	# diff --combined (generic)
	#
	if (m{^
	       (?<command>
	       (?<mark> \@{2,} ) [ ]
	       (?<lines> (?: [-+]\d+(?:,\d+)? [ ] ){2,} )
	       \g{mark}
	       (?s:.*)
	       )
	       }x) {
	    my($command, $lines) = @+{qw(command lines)};
	    my $column = length $+{mark};
	    my @lines = map {
		$_ eq ' ' ? 1 : int $_
	    } $lines =~ /\d+(?|,(\d+)|( ))/g;

	    warn $_ if $app->debug;

	    next if @lines != $column;
	    next if $column != 2;

	    push @$fromto, $app->read_diff($fh, @lines);
	}
    }
    $app;
}

sub vba {
    (my $app = shift)
	->prologue()
	->substitute()
	->epilogue();
}

sub prologue {
    my $app = shift;
    if (my $name = $app->subname) {
	$app->append(text => "Sub $name()\n");
    }
    $app->append(section => "setup.vba");
    $app;
}

sub epilogue {
    my $app = shift;
    if (my $name = $app->subname) {
	$app->append(text => "End Sub\n");
    }
    $app;
}

sub substitute {
    my $app = shift;
    my $template = sprintf "subst_%s.vba", $app->format;
    my $max = $app->maxlen;

    my @fromto = do {
	if ($app->reverse) {
	    map { [ $_->[1], $_->[0] ] } @{$app->TABLE};
	} else {
	    @{$app->TABLE};
	}
    };
    while (my($i, $fromto) = each @fromto) {
	my $fromto = $fromto[$i];
	use integer;
	chomp @$fromto;



( run in 0.877 second using v1.01-cache-2.11-cpan-39bf76dae61 )