-
Notifications
You must be signed in to change notification settings - Fork 1
/
formatxml.pl
executable file
·99 lines (76 loc) · 1.9 KB
/
formatxml.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env perl
# SPDX-License-Identifier: Apache-2.0
use strict;
use warnings;
use POSIX;
my $indent_size = 2;
my $indent_char = ' ';
my $max_line_length = 100;
sub trim {
my $s = shift;
$s =~ s/^\s+|\s+$//g;
return $s;
}
# Expects two parameters: string to analyze and stack's depth.
sub pretty_print {
my $s = trim(shift(@_));
my $total_indent_size = $indent_size * shift(@_);
if (length($s) <= $max_line_length) {
print($indent_char x ($total_indent_size));
print("$s\n");
return;
}
# Compared to /\s+/, ' ' also removes any leading whitespace.
my @attrs = split(' ', $s);
$s = $indent_char x ($total_indent_size);
# Append '<NodeName' and update size.
$s .= shift(@attrs);
$total_indent_size = length($s);
for my $a (@attrs) {
if (length($s) + length($a) >= $max_line_length) {
print("$s\n");
$s = $indent_char x ($total_indent_size);
}
$s .= " $a";
}
if (length($s) > $total_indent_size) {
print("$s\n");
}
}
my ($infilename, $outfilename) = @ARGV;
my ($INFILE, $OUTFILE);
die "Input filename missing\n" if (!$infilename);
open($INFILE, '<', $infilename) || die $!;
if ($outfilename) {
open($OUTFILE, '>', ".$outfilename.tmp") || die $!;
select($OUTFILE);
}
my $current_node = "";
my $depth = 0;
while (<$INFILE>) {
my $line = trim($_);
next if !length($line);
if ($line =~ /^<\//) {
$depth--;
die "Unbalanded node at line $.:\n$line\n" if ($depth < 0);
pretty_print($line, $depth);
next;
}
$current_node = join(' ', $current_node, $line);
# If current node ends here, print it.
if ($current_node =~ />$/) {
pretty_print($current_node, $depth);
# Push stack only when '>' is on its own.
# TODO: mixed '<node> <!-- comment -->' not supported.
if ($current_node !~ /<\/|[-|\/|\?]>$/) {
$depth++;
}
$current_node = "";
}
}
close($INFILE);
if ($outfilename) {
select(STDOUT);
close($OUTFILE);
}
rename(".$outfilename.tmp", $outfilename) || die $!;