#!/usr/bin/perl
#=======================================================================
# Copyright (C) 2003 Daniele Giacomini daniele@swlibero.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#=======================================================================
# program < OLD_ALML_SOURCE > NEW_ALML_SOURCE
#
# Convert listings.
#=======================================================================

# Record letto.
$riga = "";


# Salta blocco.
sub SaltaBlocco
{
    local ($conclusione) = $_[0];

    # Cerca di saltare il blocco.
    while ($riga !~ m|$conclusione|i)
      {
	# Trasferisce il record letto, e legge il successivo.
	print STDOUT "$riga";
	$riga = <STDIN>;
      }

    # Trasferisce anche l'ultimo record letto.
    print STDOUT "$riga";
}    

# Inizio del programma.

# Scandisce le righe.
while ($riga = <STDIN>)
  {
    # Se si incontra un commento deve essere saltato.
    if ($riga =~ m|<!--|)
      {
	&SaltaBlocco( "-->" );
      }
    elsif ($riga =~ m|<verbatimpre|)
      {
	&SaltaBlocco( "</verbatimpre>" );
      }
    elsif ($riga =~ m|<pre|)
      {
	&SaltaBlocco( "</pre>" );
      }
    elsif ($riga =~ m|<listing.*sep="([^"]+)".*>|i)
      {
	$sep = $1;
	$sep =~ m|^(.*)LISTING\.SEP(.*)$|;
	$sep = $1 . "PRE.BORDER" . $2;
        print STDOUT ("$riga");
	$riga = <STDIN>;
        if ($riga =~ m|^<verbatimpre(.*)>$|)
	  {
	    $riga = "<verbatimpre border=\"$sep\"$1>\n";
    	    print STDOUT ("$riga");
	    $riga = <STDIN>;
	    &SaltaBlocco( "</verbatimpre>" );
	  }
	elsif ($riga =~ m|^<pre(.*)>$|)
	  {
	    $riga = "<pre border=\"$sep\"$1>\n";
    	    print STDOUT ("$riga");
	    $riga = <STDIN>;
	    &SaltaBlocco( "</pre>" );
	  }
	else
	  {
    	    print STDOUT ("$riga");
	  }
      }
    else
      {
        print STDOUT ("$riga");
      }
  }

#======================================================================

