File Coverage

blib/lib/XML/Tidy/Tiny.pm
Criterion Covered Total %
statement 56 63 88.8
branch 21 26 80.7
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 81 94 86.1


line stmt bran cond sub pod time code
1             package XML::Tidy::Tiny;
2 2     2   121464 use 5.008001;
  2         8  
  2         79  
3 2     2   10 use strict;
  2         4  
  2         74  
4 2     2   9 use Exporter 'import';
  2         9  
  2         1860  
5             # Items to export into callers namespace by default. Note: do not export
6             # names by default without a very good reason. Use EXPORT_OK instead.
7             # Do not simply export all your public functions/methods/constants.
8              
9             # This allows declaration use XML::Tidy::Tiny ':all';
10             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
11             # will save memory.
12             our %EXPORT_TAGS = ( 'all' => [ qw(
13             xml_tidy
14             ) ] );
15              
16             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
17              
18             our @EXPORT = qw(
19             xml_tidy
20             );
21             our $VERSION = '0.02';
22              
23             sub xml_tidy{
24 7     7 0 18 my $data = shift;
25 7         11 my $header ='';
26 7 100       29 $header = $1 if $data=~s/^(<\?.*?\?>)\s*//s;
27 7 100       17 $header .= "\n" if $header;
28              
29 7         104 my @data = grep length, split qr{(]+/?>)}, $data, -1;
30 7         20 my @pre_push ;
31             my @pre_number;
32 0         0 my @pre_level;
33 7         9 my $buff = '';
34 7         9 my $level = 0;
35              
36 7         8 my $accum;
37              
38 7         22 while (@data) {
39 28         120 my $item = shift @data;
40 28 50       59 next unless length $item;
41              
42 28         27 my $type;
43 28 100       69 if ( $item =~ m/^
44 18 100       52 if ( $item =~ m/^<\// ) { # TAG CLOSE
    100          
45 8         11 $type = -1;
46 8         9 my $indent;
47 8         9 --$level;
48 8         11 push @pre_push, $item;
49 8         14 $pre_push[0]=~s/^\s+//;
50 8         42 $buff .= $_ for "\n", ' ' x $level, @pre_push;
51 8         31 @pre_push = ();
52             }
53             elsif ( $item =~ m/\/>\z/ ) { # TAG ALONE
54 2         3 $type = 0;
55 2 50       5 if (@pre_push) {
56 2         6 push @pre_push, $item;
57             }
58             else {
59 0         0 $pre_push[0]=~s/^\s+//;
60 0         0 $buff .= $_ for "\n", ' ' x $level, @pre_push, $item;
61 0         0 @pre_push = ();
62             }
63             }
64             else {
65 8         9 $type = 1;
66 8 100       15 if (@pre_push) { # TAG OPEN
67 2         24 $pre_push[0]=~s/^\s+//;
68 2         11 $buff .= $_ for "\n", ' ' x ( $level - 1 ), shift @pre_push;
69 2         4 ++$level;
70 2 100       7 if ( @pre_push ){
71 1         3 $pre_push[0]=~s/^\s+//;
72 1         6 $buff .= $_ for "\n", ' ' x ( $level - 1), @pre_push;
73             }
74 2         7 @pre_push = $item;
75             }
76             else {
77 6         7 ++$level;
78 6         18 push @pre_push, $item;
79             }
80             }
81             }
82             else {
83 10 100       32 next if $item=~m/^\s+\z/;
84 8 100       17 if (@pre_push) {
85 7         24 push @pre_push, $item;
86             }
87             else {
88 1         2 push @pre_push, $item;
89 1         4 $pre_push[0]=~s/^\s+//;
90 1         9 $buff .= $_ for "\n", ' ' x $level, @pre_push;
91 1         4 @pre_push = ( );
92             }
93             }
94             }
95 7 50       15 if (@pre_push) {
96 0 0       0 $buff .= "\n" if length $buff;
97 0         0 $pre_push[0]=~s/^\s+//;
98 0         0 $buff .= $_ for ' ' x ( $level ), @pre_push;
99             }
100 7         25 $buff=~s/^\s+//;
101 7         15 $buff=~s/\s+\z//;
102 7         44 return $header . $buff;
103             }
104              
105              
106              
107             # Preloaded methods go here.
108              
109             1;
110             __END__