#!/usr/bin/perl -i

use English;


## get input
undef $INPUT_RECORD_SEPARATOR;
$file = <>;

## transform it

my $notEqualsDoubleBackslashNotAmpersandRE = '(?:(?!\\\\\\\\)[^=&\n])*';
    
my $lineWithOneEqualsRE = $notEqualsDoubleBackslashNotAmpersandRE . '=' . $notEqualsDoubleBackslashNotAmpersandRE . '\n';

$file =~ s/(?:^|^\n|\n\n)($lineWithOneEqualsRE(?:$lineWithOneEqualsRE)+)(?:\n|$)/doBlock($1)/eg;





print $file; 


sub doBlock {
    my ($block) = @_;
    $block =~ s/=/&=&/g;
    $block =~ s/(=.*\n)/$1\\\\/g;
    $block = substr($block, 0,-2); # remove trailing \\
    $block = "\n\n".'\\begin{align*}' . "\n" . $block . '\\end{align*}' . "\n";
    return $block;
}





