line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::XML; |
2
|
1
|
|
|
1
|
|
18341
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
28
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
22
|
|
4
|
1
|
|
|
1
|
|
307
|
use Types; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use Text::Pretty qw(:all !text); |
6
|
|
|
|
|
|
|
use Exporter; |
7
|
|
|
|
|
|
|
use base qw/Exporter/; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.1'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub pptext ($) { Text::Pretty::text(shift) } |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our @EXPORT_OK = qw(elem ielem attr text comment cdata); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( all => \@EXPORT_OK ); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
newtype Text::XML::Attribute; |
18
|
|
|
|
|
|
|
newtype Text::XML::Element; |
19
|
|
|
|
|
|
|
newtype Text::XML::Text; |
20
|
|
|
|
|
|
|
newtype Text::XML::Comment, sub{ shift !~ qr{-->} }; |
21
|
|
|
|
|
|
|
newtype Text::XML::CData, sub{ shift !~ qr{]]>} }; |
22
|
|
|
|
|
|
|
newtype Text::XML::Name, sub{ shift =~ qr{^[\w:-]*$} }; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
uniontype Text::XML::XML, qw(Text::XML::Element |
25
|
|
|
|
|
|
|
Text::XML::Attribute |
26
|
|
|
|
|
|
|
Text::XML::Text |
27
|
|
|
|
|
|
|
Text::XML::Comment |
28
|
|
|
|
|
|
|
Text::XML::CData |
29
|
|
|
|
|
|
|
Text::XML::Name); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# element( name, [element|Text|CData|Comment] ) |
32
|
|
|
|
|
|
|
sub elem (*;$$) { Element( Name(shift), shift() || [], shift() || [] ) } |
33
|
|
|
|
|
|
|
# inline-element( name, [element|Text|CData|Comment] ) |
34
|
|
|
|
|
|
|
sub ielem (*;$$){ Element( Name(shift), shift() || [], shift() || [], 1 ) } |
35
|
|
|
|
|
|
|
sub attr ($$) { Attribute( Name(shift), shift ) } |
36
|
|
|
|
|
|
|
sub text ($) { Text(shift) } |
37
|
|
|
|
|
|
|
sub comment ($) { Comment(shift) } |
38
|
|
|
|
|
|
|
sub cdata ($) { CData(shift) } |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
instance Text::Pretty::Print, Text::XML::XML, |
41
|
|
|
|
|
|
|
pretty => sub |
42
|
|
|
|
|
|
|
{ my( $doc, %opts ) = @_ |
43
|
|
|
|
|
|
|
; $opts{encoding} = 'UTF-8' unless defined $opts{encoding} |
44
|
|
|
|
|
|
|
; $opts{indent} = 4 unless defined $opts{indent} |
45
|
|
|
|
|
|
|
; $doc = pretty_proc($doc, $opts{indent}) |
46
|
|
|
|
|
|
|
; $opts{doctype} |
47
|
|
|
|
|
|
|
? $doc = vcat [ hcat [ pptext '
|
48
|
|
|
|
|
|
|
, (nest 10, hsep [ (map {pptext $_} @{$opts{doctype}}) ]) |
49
|
|
|
|
|
|
|
, pptext '>' |
50
|
|
|
|
|
|
|
] |
51
|
|
|
|
|
|
|
, $doc |
52
|
|
|
|
|
|
|
] |
53
|
|
|
|
|
|
|
: undef |
54
|
|
|
|
|
|
|
; $opts{prolog} |
55
|
|
|
|
|
|
|
? $doc = vcat [ hcat [ pptext '
|
56
|
|
|
|
|
|
|
, (nest 6, hsep [ pretty_proc( attr( version => '1.0' ) |
57
|
|
|
|
|
|
|
, $opts{indent} ) |
58
|
|
|
|
|
|
|
, pretty_proc( attr( 'encoding' |
59
|
|
|
|
|
|
|
, $opts{encoding} ) |
60
|
|
|
|
|
|
|
, $opts{indent} ) |
61
|
|
|
|
|
|
|
]) |
62
|
|
|
|
|
|
|
, pptext '?>' |
63
|
|
|
|
|
|
|
] |
64
|
|
|
|
|
|
|
, $doc |
65
|
|
|
|
|
|
|
] |
66
|
|
|
|
|
|
|
: undef |
67
|
|
|
|
|
|
|
; $doc->pretty(%opts) |
68
|
|
|
|
|
|
|
}; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub pretty_proc ($$) |
71
|
|
|
|
|
|
|
{ no strict |
72
|
|
|
|
|
|
|
; my($doc,$i)=@_ |
73
|
|
|
|
|
|
|
; asserttype Text::XML::XML, $doc |
74
|
|
|
|
|
|
|
; match $doc |
75
|
|
|
|
|
|
|
=> Text::XML::Name |
76
|
|
|
|
|
|
|
=> sub{ pptext shift } |
77
|
|
|
|
|
|
|
=> Text::XML::Element |
78
|
|
|
|
|
|
|
=> sub{ my( $n, $as, $cs, $inline ) = @_ |
79
|
|
|
|
|
|
|
; $inline |
80
|
|
|
|
|
|
|
? hcat [ langle |
81
|
|
|
|
|
|
|
, pretty_proc($n,$i) |
82
|
|
|
|
|
|
|
, ( @$as ? ( space |
83
|
|
|
|
|
|
|
, hsep [map {onel pretty_proc($_,$i)} @$as] |
84
|
|
|
|
|
|
|
) |
85
|
|
|
|
|
|
|
: () ) |
86
|
|
|
|
|
|
|
, ( @$cs ? rangle |
87
|
|
|
|
|
|
|
: pptext ' />' ) |
88
|
|
|
|
|
|
|
, ( @$cs ? ( nest($i, hcat [(map {pretty_proc($_,$i)} @$cs) |
89
|
|
|
|
|
|
|
, pptext '' |
90
|
|
|
|
|
|
|
, pretty_proc($n,$i) |
91
|
|
|
|
|
|
|
, rangle |
92
|
|
|
|
|
|
|
]) |
93
|
|
|
|
|
|
|
) |
94
|
|
|
|
|
|
|
: () ) |
95
|
|
|
|
|
|
|
] |
96
|
|
|
|
|
|
|
: vcat [ hcat [ langle |
97
|
|
|
|
|
|
|
, pretty_proc($n,$i) |
98
|
|
|
|
|
|
|
, ( @$as ? ( space |
99
|
|
|
|
|
|
|
, (nest 2+length $n->[0] |
100
|
|
|
|
|
|
|
, hsep [map {pretty_proc($_,$i)} |
101
|
|
|
|
|
|
|
@$as ]) |
102
|
|
|
|
|
|
|
) |
103
|
|
|
|
|
|
|
: () ) |
104
|
|
|
|
|
|
|
, ( @$cs ? rangle |
105
|
|
|
|
|
|
|
: pptext ' />' ) |
106
|
|
|
|
|
|
|
] |
107
|
|
|
|
|
|
|
, ( @$cs ? ( nest($i, vcat [map {pretty_proc($_,$i)} @$cs]) |
108
|
|
|
|
|
|
|
, hcat [ pptext '' |
109
|
|
|
|
|
|
|
, pretty_proc($n,$i) |
110
|
|
|
|
|
|
|
, rangle |
111
|
|
|
|
|
|
|
] |
112
|
|
|
|
|
|
|
) |
113
|
|
|
|
|
|
|
: () ) |
114
|
|
|
|
|
|
|
] |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
=> Text::XML::Attribute => sub{ my($n,$v) = @_ |
117
|
|
|
|
|
|
|
; defined $v |
118
|
|
|
|
|
|
|
? do{ $v =~ s{&}{&}gsm |
119
|
|
|
|
|
|
|
; $v =~ s{"}{"}gsm |
120
|
|
|
|
|
|
|
; onel hcat [ pretty_proc($n,$i) |
121
|
|
|
|
|
|
|
, equals |
122
|
|
|
|
|
|
|
, qquotes pptext $v |
123
|
|
|
|
|
|
|
] |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
: pretty_proc($n,$i) |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
=> Text::XML::Text |
128
|
|
|
|
|
|
|
=> sub{ my $t = shift |
129
|
|
|
|
|
|
|
; $t =~ s{&}{&}gsm |
130
|
|
|
|
|
|
|
; $t =~ s{<}{<}gsm |
131
|
|
|
|
|
|
|
; $t =~ s{>}{>}gsm |
132
|
|
|
|
|
|
|
; words $t |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
=> Text::XML::Comment |
135
|
|
|
|
|
|
|
=> sub{ hsep [ pptext '' |
138
|
|
|
|
|
|
|
] |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
=> Text::XML::CData |
141
|
|
|
|
|
|
|
=> sub{ onel hcat [ pptext '
|
142
|
|
|
|
|
|
|
, pptext shift |
143
|
|
|
|
|
|
|
, pptext ']]>' |
144
|
|
|
|
|
|
|
] |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
1; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
__END__ |