-
Notifications
You must be signed in to change notification settings - Fork 3
/
XML.pm
430 lines (250 loc) · 8.21 KB
/
XML.pm
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package Builder::XML;
use strict;
use warnings;
use Carp;
use Builder::Utils;
use Builder::XML::Utils;
our $VERSION = '0.02';
our $AUTOLOAD;
sub __new__ {
my ( $class ) = shift;
my %args = Builder::XML::Utils::get_args( @_ );
bless {
%args,
block_id => $args{ _block_id },
stack => $args{ _stack },
context => Builder::XML::Utils::build_context(),
}, $class;
}
sub AUTOLOAD {
my ( $self ) = shift;
my @args = @_;
if ( $AUTOLOAD =~ /.*::(.*)/ ) {
my $elt = $1;
my $attr = undef;
# sub args get resent as callback
if ( wantarray ) {
return sub { $self->$elt( @args ) };
}
# if first arg is hasharray then its attributes!
$attr = shift @args if ref $args[0] eq 'HASH';
if ( ref $args[0] eq 'CODE' ) {
$self->__element__( context => 'start', element => $elt, attr => $attr );
for my $inner ( @args ) {
if ( ref $inner eq 'CODE' ) { $inner->() }
else { $self->__push__( sub { $inner } ) }
}
$self->__element__( context => 'end', element => $elt );
return;
}
# bog standard element
$self->__element__( element => $elt, attr => $attr, text => "@args" );
}
$self;
}
######################################################
# methods
sub __render__ {
my $self = shift;
my $render;
# render subs just for this block
my @this_block = Builder::Utils::yank { $_->[0] == $self->{block_id} } @{ $self->{stack} };
while ( my $block = shift @this_block ) {
my ( $block_id, $code ) = @$block;
$render.= $code->();
}
return $render;
}
sub __element__ {
my ( $self, %param ) = @_;
$param{ text } ||= '';
$param{ context } ||= 'element';
$self->{ context }->{ $param{ context } }->( $self, \%param );
return;
}
sub __cdata__ {
my $self = shift;
return $_[0] if $self->{ cdata } == 1;
return $self->__cdatax__( $_[0] );
}
sub __cdatax__ {
my $self = shift;
return q{<![CDATA[} . $_[0] . q{]]>};
}
sub __say__ {
my ( $self, @say ) = @_;
for my $said ( @say ) { $self->__push__( sub { $said } ) }
$self;
}
sub __push__ {
my ( $self, $code ) = @_;
# straight to output stream if provided
if ( $self->{ _output } ) {
print { $self->{ _output } } $code->();
return;
}
# else add to stack
push @{ $self->{ stack } }, [ $self->{ block_id }, $code ];
}
sub __inc__ { $_[0]->{ _inc }->() }
sub __dec__ { $_[0]->{ _dec }->() }
sub __level__ { $_[0]->{ _level }->() }
sub __tab__ {
my $self = shift;
my $tab = q{};
$tab = q{ } x $self->{ pre_indent } if $self->{ pre_indent };
$tab.= q{ } x ( $self->{ indent } * $self->__level__ ) if $self->{ indent };
return $tab;
}
sub __start_tab__ { $_[0]->__tab__ }
sub __end_tab__ {
my $self = shift;
return q{} if $self->{ newline } && ! $self->{ open_newline };
$self->__tab__;
}
sub __open_newline__ {
my $self = shift;
return $self->{cr} if $self->{ open_newline };
return q{};
}
sub __close_newline__ {
my $self = shift;
return $self->{cr} if $self->{ close_newline };
return q{};
}
sub DESTROY {
my $self = shift;
$self = undef;
}
1;
__END__
=head1 NAME
Builder::XML - Building block for XML
=head1 VERSION
Version 0.02
=cut
=head1 SYNOPSIS
Please look at L<Builder> docs. This currently contains the necessary synopsis & description for Builder::XML.
At some point in future it will be moved here and Builder docs will be replaced with something more generic.
=head1 DESCRIPTION
See above.
=head2 So how does it work? (in more detail!)
Here are Builder::XML parameter contexts....
no parameters => produces a closed tag
$xm->br;
# => <br />
first parameter is a hashref => attributes
$xm->span( { id => 'mydiv', class => 'thisClass' }, 'some content' );
# => <span class="thisClass" id="mydiv">some content</span>
parameter(s) are a anon sub or code ref --> callback
$xm->ul( { class => 'list' }, sub {
for my $numb qw/one two three/ {
$xm->li( $numb );
}
});
# => <ul class="list"><li>one</li><li>two</li><li>three</li></ul>
parameter(s) are content => element text
$xm->p( 'one', 'two', 'and three' );
# => <p>one two and three</p>
parameter(s) are Builder blocks or content => nesting
$xm->p( 'one', $xm->span( 'two' ), 'and three' );
# => <p>one <span>two</span> and three</p>
# NB. THIS DOESN'T WORK YET... unless first param is an object
# Workaround - use __say__ method around text like so...
#
# $xm->p( $xm->__say__('one'), $xm->span( 'two' ), 'and three' );
#
# This needs "fixing" for HTML usage
parameter(s) are Builder blocks within builder blocks => nesting ad-infinitum
$xm->div(
$xm->div(
xm->span( 'hi there'),
),
);
# => <div><div><span>hi there</span></div></div>
=head2 Gotchas?
TODO: XML entities not implemented
TODO: invalid method calls... $xm->flip-flop, $xm->DESTROY, $xm->AUTOLOAD
TODO: Fix / workaround for attribute ordering
=head1 EXPORT
None.
=head1 METHODS
All methods are prefix/postfixed with __ so that ambigious method calls wont clash
and can be turned successfully into XML elements.
Below is a complete list of defined methods in Builder::XML.
NB. Most of these are private methods and only listing here for reference.
=head2 __new__
Private.
This is the contructor called by the Builder object when creating a block...
$xm = $builder->block( 'Builder::XML' );
All arguments are passed from Builder->block method straight to Builder::XML->__new__
=head2 __render__
Will immediately render the building block.
Can be useful in some cases...
# provide example here of it working
# and then provide example of what can go wrong!
...but recommend $builder->render for best practise.
=head2 __element__
Private
=head2 __cdata__
Wraps content in <![CDATA[ ]]> element.
Useful for quick ditties like....
$xm->span( $xm->__cdata__( 'yada yada' ) );
# => <span><!CDATA[yada yada]]></span>
But for best practise you probably still find building a block more useful in the long run...
my $xm = $builder->block( 'Builder::XML', { cdata => 1 } );
$xm->span( 'yada yada' );
=head2 __cdatax__
PRIVATE - used with __cdata__
=head2 __say__
Really a Private method but as mentioned in I<Gotchas> it can be useful for working around some implementation issues.
=head2 __push__
Private
=head2 __inc__
Private
=head2 __dec__
Private
=head2 __level__
Private
=head2 __tab__
Private
=head2 __start_tab__
Private
=head2 __end_tab__
Private
=head2 __open_newline__
Private
=head2 __close_newline__
Private
=head2 AUTOLOAD
Used in method to XML element resolution.
Therefore at present AUTOLOAD cannot be used as a XML element.
=head2 DESTROY
Standard POOP!
Therefore at present DESTROY cannot be used as a XML element.
=head1 AUTHOR
Barry Walsh C<< <draegtun at cpan.org> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-builder at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Builder>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Builder::XML
You can also look for information at: L<Builder>
=over 4
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Builder>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Builder>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Builder>
=item * Search CPAN
L<http://search.cpan.org/dist/Builder/>
=back
=head1 ACKNOWLEDGEMENTS
=head1 COPYRIGHT & LICENSE
Copyright 2008,2009 Barry Walsh (Draegtun Systems Ltd), all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut