scripts/niceXML
author insilmaril
Tue, 06 Apr 2010 13:30:07 +0000
changeset 843 2d36a7bb0867
parent 192 96e7cd53125e
permissions -rwxr-xr-x
(Very) minor changes for debugging output
insilmaril@192
     1
#!/usr/bin/perl 
insilmaril@192
     2
#
insilmaril@192
     3
# Hack to make single-line XML file  easier to read by using indention
insilmaril@192
     4
#
insilmaril@192
     5
# (c) Uwe Drechsel
insilmaril@192
     6
#
insilmaril@192
     7
# License: GPL 
insilmaril@192
     8
insilmaril@192
     9
my $filename =shift;
insilmaril@192
    10
my $s;
insilmaril@192
    11
open (INFILE, "<$filename") ||
insilmaril@192
    12
	die "Could not read $filename.";
insilmaril@192
    13
$s=join("\n",<INFILE>);
insilmaril@192
    14
insilmaril@192
    15
$s=~s/>/>\n/gm;
insilmaril@192
    16
insilmaril@192
    17
my @lines=split ("\n",$s);
insilmaril@192
    18
my $i=0;
insilmaril@192
    19
my $is="";
insilmaril@192
    20
insilmaril@192
    21
foreach (@lines)
insilmaril@192
    22
{
insilmaril@192
    23
	if (!/<.*?\/>/)
insilmaril@192
    24
	{
insilmaril@192
    25
		if (/<\//)
insilmaril@192
    26
		{
insilmaril@192
    27
			# Closing tag
insilmaril@192
    28
			$i--;
insilmaril@192
    29
			if ($i<0) {$i=0};
insilmaril@192
    30
			$is=indent($i);
insilmaril@192
    31
			print "$is$_\n";
insilmaril@192
    32
		} else
insilmaril@192
    33
		{
insilmaril@192
    34
			if (/<(?!\?)/)  # ignore <? ... ?>
insilmaril@192
    35
			{
insilmaril@192
    36
				# Opening tag
insilmaril@192
    37
				print "$is$_\n";
insilmaril@192
    38
				$i++;
insilmaril@192
    39
				$is=indent($i);
insilmaril@192
    40
			} else 
insilmaril@192
    41
			{ 
insilmaril@192
    42
				# empty lines etc
insilmaril@192
    43
				print "$is$_\n"; 
insilmaril@192
    44
			}
insilmaril@192
    45
		}
insilmaril@192
    46
	} else 
insilmaril@192
    47
	{ 
insilmaril@192
    48
		# Ignor single tags <../>
insilmaril@192
    49
		print "$is$_\n"; 
insilmaril@192
    50
	}
insilmaril@192
    51
}
insilmaril@192
    52
print "\n";
insilmaril@192
    53
exit;
insilmaril@192
    54
insilmaril@192
    55
sub indent()
insilmaril@192
    56
{
insilmaril@192
    57
	my $size=shift;
insilmaril@192
    58
	my $s="";
insilmaril@192
    59
	for ($i=0; $i<$size; $i++)
insilmaril@192
    60
	{
insilmaril@192
    61
		$s=$s."  ";
insilmaril@192
    62
	}
insilmaril@192
    63
	return $s;
insilmaril@192
    64
}