blib/lib/PYX/GraphViz.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package PYX::GraphViz;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use GraphViz;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use PYX::Parser;
10  
11 our $VERSION = 0.05;
12  
13 # Constructor.
14 sub new {
15 my ($class, @params) = @_;
16  
17 # Object.
18 my $self = bless {}, $class;
19  
20 # Colors.
21 $self->{'colors'} = {
22 'a' => 'blue',
23 'blockquote' => 'orange',
24 'br' => 'orange',
25 'div' => 'green',
26 'form' => 'yellow',
27 'html' => 'black',
28 'img' => 'violet',
29 'input' => 'yellow',
30 'option' => 'yellow',
31 'p' => 'orange',
32 'select' => 'yellow',
33 'table' => 'red',
34 'td' => 'red',
35 'textarea' => 'yellow',
36 'tr' => 'red',
37 '*' => 'grey',
38 };
39  
40 # Layout.
41 $self->{'layout'} = 'neato';
42  
43 # Height and width.
44 $self->{'height'} = 10;
45 $self->{'width'} = 10;
46  
47 # Node height.
48 $self->{'node_height'} = 0.3;
49  
50 # Output handler.
51 $self->{'output_handler'} = \*STDOUT;
52  
53 # Process params.
54 set_params($self, @params);
55  
56 # GraphViz object.
57 $self->{'_g'} = GraphViz->new(
58 'layout' => $self->{'layout'},
59 'overlap' => 'scale',
60 'height' => $self->{'height'},
61 'width' => $self->{'width'},
62 );
63  
64 # Check to '*' color.
65 if (! exists $self->{'colors'}->{'*'}) {
66 err "Bad color define for '*' elements.";
67 }
68  
69 # PYX::Parser object.
70 $self->{'_pyx_parser'} = PYX::Parser->new(
71 'callbacks' => {
72 'end_element' => \&_end_element,
73 'final' => \&_final,
74 'start_element' => \&_start_element,
75 },
76 'non_parser_options' => {
77 'colors' => $self->{'colors'},
78 'g' => $self->{'_g'},
79 'node_height' => $self->{'node_height'},
80 'num' => 0,
81 'stack' => [],
82 },
83 'output_handler' => $self->{'output_handler'},
84 );
85  
86 # Object.
87 return $self;
88 }
89  
90 # Parse pyx text or array of pyx text.
91 sub parse {
92 my ($self, $pyx, $out) = @_;
93 $self->{'_pyx_parser'}->parse($pyx, $out);
94 return;
95 }
96  
97 # Parse file with pyx text.
98 sub parse_file {
99 my ($self, $file, $out) = @_;
100 $self->{'_pyx_parser'}->parse_file($file, $out);
101 return;
102 }
103  
104 # Parse from handler.
105 sub parse_handler {
106 my ($self, $input_file_handler, $out) = @_;
107 $self->{'_pyx_parser'}->parse_handler($input_file_handler, $out);
108 return;
109 }
110  
111 # Process element.
112 sub _start_element {
113 my ($pyx_parser_obj, $elem) = @_;
114 $pyx_parser_obj->{'non_parser_options'}->{'num'}++;
115 my $num = $pyx_parser_obj->{'non_parser_options'}->{'num'};
116 my $colors = $pyx_parser_obj->{'non_parser_options'}->{'colors'};
117 my $color;
118 if (exists $colors->{$elem}) {
119 $color = $colors->{$elem};
120 } else {
121 $color = $colors->{'*'};
122 }
123 my $g = $pyx_parser_obj->{'non_parser_options'}->{'g'};
124 my $node_height = $pyx_parser_obj->{'non_parser_options'}
125 ->{'node_height'};
126 $g->add_node($num,
127 'color' => $color,
128 'height' => $node_height,
129 'shape' => 'point'
130 );
131 my $stack_ar = $pyx_parser_obj->{'non_parser_options'}->{'stack'};
132 if (@{$stack_ar}) {
133 $g->add_edge(
134 $num=> $stack_ar->[-1]->[1],
135 'arrowhead' => 'none',
136 'weight' => 2,
137 );
138 }
139 push @{$stack_ar}, [$elem, $num];
140 return;
141 }
142  
143 # Process elements.
144 sub _end_element {
145 my ($pyx_parser_obj, $elem) = @_;
146 my $stack_ar = $pyx_parser_obj->{'non_parser_options'}->{'stack'};
147 if ($stack_ar->[-1]->[0] eq $elem) {
148 pop @{$stack_ar};
149 }
150 return;
151 }
152  
153 # Final.
154 sub _final {
155 my $pyx_parser_obj = shift;
156 my $g = $pyx_parser_obj->{'non_parser_options'}->{'g'};
157 my $out = $pyx_parser_obj->{'output_handler'};
158 $g->as_png($out);
159 return;
160 }
161  
162 1;
163  
164 __END__
↑ Summary