SAC/Writer.pm untested

filter: Statementstmt 0.0% 0 / 189 Branchbran 0.0% 0 / 104 Conditioncond 0.0% 0 / 104 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 497 CC 53 SCAR 50.4
CC
53
Cov
0%
stringify_condition
462.0
stringify_selector
156.0
stringify_lexical_unit
72.0
CRAP 154.4
1
2 ###
3 # SAC Writer - a writer handler for SAC
4 # Robin Berjon <robin@knowscape.com>
5 # 23/04/2001 - documentation, misc details
6 # 19/03/2001 - second version
7 ###
8
9 package CSS::SAC::Writer;
10 use strict;
11 use vars qw($VERSION);
12 $VERSION = '0.03';
13
14 use CSS::SAC::Selector qw(:constants);
15 use CSS::SAC::Condition qw(:constants);
16 use CSS::SAC::LexicalUnit qw(:constants);
17
18
19 #---------------------------------------------------------------------#
20 # build the fields for an array based object
21 #---------------------------------------------------------------------#
22 use Class::ArrayObjects define => {
23 fields => [qw(
24 _out_
25 _write_to_
26 _nsmap_
27 )],
28 };
29 #---------------------------------------------------------------------#
30
31
32
33 ### Constructor #######################################################
34 # #
35 # #
36
37
38 #---------------------------------------------------------------------#
39 # CSS::SAC::Writer->new(\%options)
40 # creates a new sac doc handler
41 #---------------------------------------------------------------------#
42 sub new {
43 my $class = ref($_[0])?ref(shift):shift;
44 my $options = shift;
45
46 # munge the options
47 my ($mode,$write_to);
48 if ($options->{string}) {
49 $mode = 'string';
50 $write_to = $options->{string};
51 die "option 'string' must be a scalar reference" unless ref $options->{string};
52 }
53 elsif ($options->{ioref}) {
54 $mode = 'fh';
55 $write_to = $options->{ioref};
56 }
57 elsif ($options->{filename}) {
58 $mode = 'fh';
59 open my($write_to), "$options->{filename}" or die $!;
60 }
61 else {
62 return undef;
63 }
64
65 # prepare the object and the namespace map
66 my $self = [];
67 $self->[_nsmap_] = {};
68
69 # set the right closure to write
70 if ($mode eq 'string') {
71 $self->[_out_] = \&write_string;
72 }
73 else {
74 $self->[_out_] = \&write_fh;
75 }
76 $self->[_write_to_] = $write_to;
77
78 return bless $self, $class;
79 }
80 #---------------------------------------------------------------------#
81
82
83 #---------------------------------------------------------------------#
84 # write_string($self,$content)
85 # the coderef we use to write to a string
86 #---------------------------------------------------------------------#
87 sub write_string {
88 ${$_[0]->[_write_to_]} .= $_[1];
89 }
90 #---------------------------------------------------------------------#
91
92
93 #---------------------------------------------------------------------#
94 # write_fh($self,$content)
95 # the coderef we use to write to a filehandle
96 #---------------------------------------------------------------------#
97 sub write_fh {
98 my $fh = $_[0]->[_write_to_];
99 print $fh $_[1];
100 }
101 #---------------------------------------------------------------------#
102
103
104 # #
105 # #
106 ### Constructor #######################################################
107
108
109
110 ### Callbacks #########################################################
111 # #
112 # #
113
114
115 #---------------------------------------------------------------------#
116 # start_document
117 #---------------------------------------------------------------------#
118 sub start_document {
119 my $dh = shift;
120 # do nothing
121 }
122 #---------------------------------------------------------------------#
123
124
125 #---------------------------------------------------------------------#
126 # end_document
127 #---------------------------------------------------------------------#
128 sub end_document {
129 my $dh = shift;
130 # do nothing
131 }
132 #---------------------------------------------------------------------#
133
134
135 #---------------------------------------------------------------------#
136 # start_selector($sel_list)
137 #---------------------------------------------------------------------#
138 sub start_selector {
139 my $dh = shift;
140 my $sel_list = shift;
141
142 my @sel_strings;
143 for my $sel (@$sel_list) {
144 push @sel_strings, $dh->stringify_selector($sel);
145 }
146 $dh->[_out_]->($dh, "\n" . join(', ', @sel_strings) . ' {');
147 }
148 #---------------------------------------------------------------------#
149
150
151 #---------------------------------------------------------------------#
152 # end_selector($sel_list)
153 #---------------------------------------------------------------------#
154 sub end_selector {
155 my $dh = shift;
156 my $sel_list = shift;
157 $dh->[_out_]->($dh, "\n}\n");
158 }
159 #---------------------------------------------------------------------#
160
161
162 #---------------------------------------------------------------------#
163 # property($name,$lu,$important)
164 #---------------------------------------------------------------------#
165 sub property {
166 my $dh = shift;
167 my $name = shift;
168 my $lu = shift;
169 my $important = shift;
170
171 $dh->[_out_]->($dh, "\n\t$name:\t");
172 while (@$lu) {
173 my $val = shift @$lu;
174 $dh->[_out_]->($dh, $dh->stringify_lexical_unit($val));
175
176 if ($lu->[0]) {
177 if ($lu->[0]->is_type(OPERATOR_COMMA)) {
178 shift @$lu;
179 $dh->[_out_]->($dh, ', ');
180 }
181 elsif ($lu->[0]->is_type(OPERATOR_SLASH)) {
182 shift @$lu;
183 $dh->[_out_]->($dh, '/');
184 }
185 else {
186 $dh->[_out_]->($dh, ' ');
187 }
188 }
189 }
190 $dh->[_out_]->($dh, (($important)?' !important':'') . ';');
191 }
192 #---------------------------------------------------------------------#
193
194
195 #---------------------------------------------------------------------#
196 # ignorable_at_rule($at_rule)
197 #---------------------------------------------------------------------#
198 sub ignorable_at_rule {
199 my $dh = shift;
200 my $at_rule = shift;
201
202 $dh->[_out_]->($dh, "\n/* ignorable: $at_rule */\n");
203 }
204 #---------------------------------------------------------------------#
205
206
207 #---------------------------------------------------------------------#
208 # import_style($uri,\@media)
209 #---------------------------------------------------------------------#
210 sub import_style {
211 my $dh = shift;
212 my $uri = shift;
213 my $media = shift;
214
215 $dh->[_out_]->($dh, "\n\@import url($uri) " . join(', ', @$media) . ";\n");
216 }
217 #---------------------------------------------------------------------#
218
219
220 #---------------------------------------------------------------------#
221 # namespace_declaration($prefix,$uri)
222 #---------------------------------------------------------------------#
223 sub namespace_declaration {
224 my $dh = shift;
225 my $prefix = shift;
226 my $uri = shift;
227
228 # we need to provide a global ns map here
229 if (defined $prefix) {
230 $dh->[_nsmap_]->{$uri} = $prefix;
231 }
232
233 $dh->[_out_]->($dh, "\n\@namespace" . ((defined $prefix)?" $prefix ":' ') . "url($uri);\n");
234 }
235 #---------------------------------------------------------------------#
236
237
238 #---------------------------------------------------------------------#
239 # start_media(\@media)
240 #---------------------------------------------------------------------#
241 sub start_media {
242 my $dh = shift;
243 my $media = shift;
244
245 $dh->[_out_]->($dh, "\n\@media " . join(', ', @$media) . " {\n");
246 }
247 #---------------------------------------------------------------------#
248
249
250 #---------------------------------------------------------------------#
251 # end_media(\@media)
252 #---------------------------------------------------------------------#
253 sub end_media {
254 my $dh = shift;
255 my $media = shift;
256
257 $dh->[_out_]->($dh, "\n}\n");
258 }
259 #---------------------------------------------------------------------#
260
261
262 #---------------------------------------------------------------------#
263 # comment($comment)
264 #---------------------------------------------------------------------#
265 sub comment {
266 my $dh = shift;
267 my $comment = shift;
268
269 $dh->[_out_]->($dh, "/* $comment */");
270 }
271 #---------------------------------------------------------------------#
272
273
274 #---------------------------------------------------------------------#
275 # charset($charset)
276 #---------------------------------------------------------------------#
277 sub charset {
278 my $dh = shift;
279 my $charset = shift;
280
281 $dh->[_out_]->($dh, "\@charset '$charset';\n");
282 }
283 #---------------------------------------------------------------------#
284
285
286 #---------------------------------------------------------------------#
287 # start_font_face
288 #---------------------------------------------------------------------#
289 sub start_font_face {
290 my $dh = shift;
291
292 $dh->[_out_]->($dh, "\n\@font-face {\n");
293 }
294 #---------------------------------------------------------------------#
295
296
297 #---------------------------------------------------------------------#
298 # end_font_face
299 #---------------------------------------------------------------------#
300 sub end_font_face {
301 my $dh = shift;
302 $dh->[_out_]->($dh, "\n}\n");
303 }
304 #---------------------------------------------------------------------#
305
306
307 #---------------------------------------------------------------------#
308 # start_page($name,$pseudo_page)
309 #---------------------------------------------------------------------#
310 sub start_page {
311 my $dh = shift;
312 my $name = shift;
313 my $pseudo_page = shift;
314
315 $dh->[_out_]->($dh, "\n\@page " . ((defined $name)?"$name ":'') .
316 ((defined $pseudo_page)?":$pseudo_page ":'') . "{\n");
317 }
318 #---------------------------------------------------------------------#
319
320
321 #---------------------------------------------------------------------#
322 # end_page($name,$pseudo_page)
323 #---------------------------------------------------------------------#
324 sub end_page {
325 my $dh = shift;
326 my $name = shift;
327 my $pseudo_page = shift;
328
329 $dh->[_out_]->($dh, "\n}\n");
330 }
331 #---------------------------------------------------------------------#
332
333
334 # #
335 # #
336 ### Callbacks #########################################################
337
338
339
340 ### Helpers ###########################################################
341 # #
342 # #
343
344
345 #---------------------------------------------------------------------#
346 # stringify_selector($sel)
347 # returns a string of that selector
348 #---------------------------------------------------------------------#
349 sub stringify_selector {
350 my $dh = shift;
351 my $sel = shift;
352
353 # child
354 if ($sel->is_type(CHILD_SELECTOR)) {
355 return $dh->stringify_selector($sel->AncestorSelector)
356 . ' > ' .
357 $dh->stringify_selector($sel->SimpleSelector);
358 }
359
360 # descendant
361 elsif ($sel->is_type(DESCENDANT_SELECTOR)) {
362 return $dh->stringify_selector($sel->AncestorSelector)
363 . ' ' .
364 $dh->stringify_selector($sel->SimpleSelector);
365 }
366
367 # direct adjacent
368 elsif ($sel->is_type(DIRECT_ADJACENT_SELECTOR)) {
369 return $dh->stringify_selector($sel->Selector)
370 . ' + ' .
371 $dh->stringify_selector($sel->SiblingSelector);
372 }
373
374 # indirect adjacent
375 elsif ($sel->is_type(INDIRECT_ADJACENT_SELECTOR)) {
376 return $dh->stringify_selector($sel->Selector)
377 . ' ~ ' .
378 $dh->stringify_selector($sel->SiblingSelector);
379 }
380
381 # conditional
382 elsif ($sel->is_type(CONDITIONAL_SELECTOR)) {
383 return $dh->stringify_selector($sel->SimpleSelector)
384 .
385 $dh->stringify_condition($sel->Condition);
386 }
387
388 # negative
389 elsif ($sel->is_type(NEGATIVE_SELECTOR)) {
390 return ':not(' . $dh->stringify_selector($sel->SimpleSelector) . ')';
391 }
392
393 # element
394 elsif ($sel->is_type(ELEMENT_NODE_SELECTOR)) {
395 my $string;
396 if (defined $sel->NamespaceURI) {
397 if (length $sel->NamespaceURI) {
398 $string = $dh->[_nsmap_]->{$sel->NamespaceURI} . '|';
399 } # else we don't put anything and it's in the default ns
400 }
401 else {
402 $string = '*|';
403 }
404 $string .= (defined $sel->LocalName)?$sel->LocalName:'*';
405
406 return $string;
407 }
408
409 # pseudo element
410 elsif ($sel->is_type(PSEUDO_ELEMENT_SELECTOR)) {
411 return '::' . $sel->LocalName;
412 }
413
414 # error ?
415 else {
416 warn "unknown selector type";
417 }
418 }
419 #---------------------------------------------------------------------#
420
421
422
423 #---------------------------------------------------------------------#
424 # stringify_condition($sel)
425 # returns a string of that condition
426 #---------------------------------------------------------------------#
427 sub stringify_condition {
428 my $dh = shift;
429 my $cond = shift;
430
431 # and
432 if ($cond->is_type(AND_CONDITION)) {
433 return $dh->stringify_condition($cond->FirstCondition)
434 .
435 $dh->stringify_condition($cond->SecondCondition);
436 }
437
438 # attr
439 elsif (
440 $cond->is_type(ATTRIBUTE_CONDITION) or
441 $cond->is_type(BEGIN_HYPHEN_ATTRIBUTE_CONDITION) or
442 $cond->is_type(ONE_OF_ATTRIBUTE_CONDITION) or
443 $cond->is_type(STARTS_WITH_ATTRIBUTE_CONDITION) or
444 $cond->is_type(ENDS_WITH_ATTRIBUTE_CONDITION) or
445 $cond->is_type(CONTAINS_ATTRIBUTE_CONDITION)
446 ) {
447 my $string = '[';
448
449 # the name
450 if (defined $cond->NamespaceURI) {
451 if (length $cond->NamespaceURI) {
452 $string .= $dh->[_nsmap_]->{$cond->NamespaceURI} . '|';
453 }
454 }
455 else {
456 $string .= '*|';
457 }
458 $string .= (defined $cond->LocalName)?$cond->LocalName:'*';
459
460 # the value
461 if ($cond->Specified) {
462 my $op = '=';
463 $cond->is_type(BEGIN_HYPHEN_ATTRIBUTE_CONDITION) and $op = '|=';
464 $cond->is_type(ONE_OF_ATTRIBUTE_CONDITION) and $op = '~=';
465 $cond->is_type(STARTS_WITH_ATTRIBUTE_CONDITION) and $op = '^=';
466 $cond->is_type(ENDS_WITH_ATTRIBUTE_CONDITION) and $op = '$=';
467 $cond->is_type(CONTAINS_ATTRIBUTE_CONDITION) and $op = '*=';
468
469 # find the right op depending on the attr type
470
471 $string .= "$op'" . $cond->Value . "'";
472 }
473
474 $string .= ']';
475
476 return $string;
477 }
478
479 # class
480 elsif ($cond->is_type(CLASS_CONDITION)) {
481 return '.' . $cond->Value;
482 }
483
484 # content
485 elsif ($cond->is_type(CONTENT_CONDITION)) {
486 return ":contains('" . $cond->Data . "')";
487 }
488
489 # id
490 elsif ($cond->is_type(ID_CONDITION)) {
491 return '#' . $cond->Value;
492 }
493
494 # lang
495 elsif ($cond->is_type(LANG_CONDITION)) {
496 return ":lang(" . $cond->Lang . ")";
497 }
498
499 # negative
500 elsif ($cond->is_type(NEGATIVE_CONDITION)) {
501 return ":not(" . $dh->stringify_condition($cond->Condition) . ")";
502 }
503
504 # only child
505 elsif ($cond->is_type(ONLY_CHILD_CONDITION)) {
506 return ':only-child';
507 }
508
509 # only of type
510 elsif ($cond->is_type(ONLY_TYPE_CONDITION)) {
511 return ':only-of-type';
512 }
513
514 # root
515 elsif ($cond->is_type(IS_ROOT_CONDITION)) {
516 return ':root';
517 }
518
519 # empty
520 elsif ($cond->is_type(IS_EMPTY_CONDITION)) {
521 return ':empty';
522 }
523
524 # pseudo-class
525 elsif ($cond->is_type(PSEUDO_CLASS_CONDITION)) {
526 return ':' . $cond->Value;
527 }
528
529 # positional
530 elsif ($cond->is_type(POSITIONAL_CONDITION)) {
531 my $string;
532
533 # the second part right
534 if ($cond->Type) {
535 $string = 'of-type';
536 }
537 else {
538 $string = 'child';
539 }
540
541 # get the first part right
542 if ($cond->Position == 1) {
543 return ':first-' . $string;
544 }
545 elsif ($cond->Position == -1) {
546 return ':last-' . $string;
547 }
548 else {
549 $string = ':nth-' . $string;
550 }
551
552 # add the expression
553 $string .= '(' . $cond->Position . ')';
554
555 return $string;
556 }
557 }
558 #---------------------------------------------------------------------#
559
560
561 #---------------------------------------------------------------------#
562 # stringify_lexical_unit($sel)
563 # returns a string of that lexical unit
564 #---------------------------------------------------------------------#
565 sub stringify_lexical_unit {
566 my $dh = shift;
567 my $lu = shift;
568
569 # dimensions
570 if (
571 $lu->is_type(CENTIMETER) or $lu->is_type(DEGREE) or
572 $lu->is_type(DIMENSION) or $lu->is_type(EM) or
573 $lu->is_type(EX) or $lu->is_type(GRADIAN) or
574 $lu->is_type(HERTZ) or $lu->is_type(INCH) or
575 $lu->is_type(KILOHERTZ) or $lu->is_type(MILLIMETER) or
576 $lu->is_type(MILLISECOND) or $lu->is_type(PERCENTAGE) or
577 $lu->is_type(PICA) or $lu->is_type(PIXEL) or
578 $lu->is_type(POINT) or $lu->is_type(RADIAN) or
579 $lu->is_type(SECOND)
580 ) {
581 return $lu->Value . $lu->DimensionUnitText;
582 }
583
584 # functions
585 elsif (
586 $lu->is_type(ATTR) or $lu->is_type(COUNTER_FUNCTION) or
587 $lu->is_type(URI) or $lu->is_type(COUNTERS_FUNCTION) or
588 $lu->is_type(FUNCTION) or $lu->is_type(RECT_FUNCTION)
589 ) {
590 return $lu->FunctionName . '(' . $lu->Value . ')';
591 }
592
593 # inherit
594 elsif ($lu->is_type(INHERIT)) {
595 return 'inherit';
596 }
597
598 # ident, number, unicoderange
599 elsif ($lu->is_type(IDENT) or $lu->is_type(INTEGER) or
600 $lu->is_type(REAL) or $lu->is_type(UNICODERANGE)) {
601 return $lu->Value;
602 }
603
604 # string
605 elsif ($lu->is_type(STRING_VALUE)) {
606 return "'" . $lu->Value . "'";
607 }
608
609 # rgbcolor
610 elsif ($lu->is_type(RGBCOLOR)) {
611 if ($lu->FunctionName eq 'rgb') {
612 return 'rgb(' . $lu->Value . ')';
613 }
614 else {
615 return '#' . $lu->Value;
616 }
617 }
618 }
619 #---------------------------------------------------------------------#
620
621
622 # #
623 # #
624 ### Helpers ###########################################################
625
626
627
628 ### Error Callbacks ###################################################
629 # #
630 # #
631
632
633 #---------------------------------------------------------------------#
634 # warning($warning)
635 #---------------------------------------------------------------------#
636 sub warning {
637 my $eh = shift;
638 my $warning = shift;
639
640 warn "[WARN] $warning\n";
641 }
642 #---------------------------------------------------------------------#
643
644
645 #---------------------------------------------------------------------#
646 # error($error)
647 #---------------------------------------------------------------------#
648 sub error {
649 my $eh = shift;
650 my $error = shift;
651
652 warn "[ERROR] $error\n";
653 }
654 #---------------------------------------------------------------------#
655
656
657 #---------------------------------------------------------------------#
658 # fatal_error($error)
659 #---------------------------------------------------------------------#
660 sub fatal_error {
661 my $eh = shift;
662 my $error = shift;
663
664 die "[FATAL] $error\n";
665 }
666 #---------------------------------------------------------------------#
667
668
669 # #
670 # #
671 ### Error Callbacks ###################################################
672
673 1;
674
675 =pod
676
677 =head1 SYNOPSIS
678
679 use CSS::SAC qw();
680 use CSS::SAC::Writer ();
681
682 ### create a doc handler using the writer
683 # options can also be ioref and string (given a stringref) in which
684 # case it'll write to the filehandle or to the string.
685 # Yes, it also works as an ErrorHandler (though not a good one)
686
687 my $doc_h = CSS::SAC::Writer->new({ filename => 'out.css' });
688 my $sac = CSS::SAC->new({
689 DocumentHandler => $doc_h,
690 ErrorHandler => $doc_h,
691 });
692
693 # generate a stream of events
694 $sac->parse({ filename => 'foo.css' });
695
696 =head1 DESCRIPTION
697
698 This is a simplistic SAC handler that demonstrates how one may use
699 CSS::SAC. More useful ones will follow. Obviously, it isn't documented
700 much, given that its value resides mostly in the source code :)
701
702 You can of course still use it as a way to write CSS from a SAC stream.
703
704 =head1 AUTHOR
705
706 Robin Berjon <robin@knowscape.com>
707
708 This module is licensed under the same terms as Perl itself.
709
710 =cut
711