#!/usr/bin/perl
#=======================================================================
# Copyright (c) 2002 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 verbatim pictures to asciiart.
#=======================================================================

# Record letto.
$riga = "";

$it_is_a_figure = 0;


# 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>;
      }
}    

# Inizio del programma.

# Scandisce le righe.
while ($riga = <STDIN>)
  {
    # Se si incontra un commento deve essere saltato.
    if ($riga =~ m|<!--|)
      {
	&SaltaBlocco( "-->" );
      }
    elsif ($riga =~ m|<figure\b|i)
      {
	$it_is_a_figure = 1;
      }
    elsif ($riga =~ m|</figure\b|i)
      {
	$it_is_a_figure = 0;
      }
    elsif ($riga =~ m|(<verbatimpre\b)(.*)$|i)
      {
	if ($it_is_a_figure)
	  {
	    $riga = "<asciiart" . $2 . "\n";
	  }
      }
    elsif ($riga =~ m|(</verbatimpre\b)(.*)$|i)
      {
	if ($it_is_a_figure)
	  {
	    $riga = "</asciiart" . $2 . "\n";
	  }
      }
    print STDOUT ("$riga");
  }

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

