update method/attribute parsing
[gitmo/MooseX-AutoDoc.git] / lib / MooseX / AutoDoc / View / TT.pm
1 package MooseX::AutoDoc::View::TT;
2
3 #TODO:
4 #    Versions
5 #    POD block for RT / Bugs / etc
6 #    MooseX::Methods support
7
8 use Moose;
9 use Scalar::Util qw/blessed/;
10 use Template;
11
12 extends 'MooseX::AutoDoc::View';
13
14 has _tt     => (is => 'ro', isa => 'Template', lazy_build => 1);
15 has '+args' => (isa => 'HashRef');
16
17 sub _build__tt {
18   my $self = shift;
19   Template->new($self->has_args ? $self->args : {})
20     || confess $Template::ERROR;
21 }
22
23 has role_template   => (is => 'rw', isa => 'Str', lazy_build => 1);
24 has class_template  => (is => 'rw', isa => 'Str', lazy_build => 1);
25
26 has role_template_blocks  => (is => 'rw', isa => 'HashRef', lazy_build => 1);
27 has class_template_blocks => (is => 'rw', isa => 'HashRef', lazy_build => 1);
28
29 sub _build_role_template  { "[% USE wrap; PROCESS role_block;  %]" }
30 sub _build_class_template { "[% USE wrap; PROCESS class_block; %]" }
31
32 sub render_role {
33   my ($self, $vars, $options) = @_;
34   my $tt = $self->_tt;
35   my $output;
36   my $template = $self->role_template. " ".$self->_role_blocks;
37   $tt->process(\ $template, $vars, \ $output, %{ $options || {}})
38     || confess $tt->error;
39   return $output;
40 }
41
42 sub render_class {
43   my ($self, $vars, $options) = @_;
44   my $tt = $self->_tt;
45   my $output;
46   my $template = $self->class_template. " ".$self->_class_blocks;
47   $tt->process(\ $template, $vars, \ $output, %{ $options || {}})
48     || confess $tt->error;
49   return $output;
50 }
51
52
53 sub _class_blocks {
54   my $self= shift;
55   my $blocks = $self->class_template_blocks;
56   return join "",
57     map { " [%- BLOCK ${_} %] ".$blocks->{$_}." [% END; -%] "}
58       keys %$blocks;
59 }
60
61 sub _role_blocks {
62   my $self= shift;
63   my $blocks = $self->role_template_blocks;
64   return join "",
65     map { " [%- BLOCK ${_} %] ".$blocks->{$_}." [% END; -%] "}
66       keys %$blocks;
67 }
68
69 1;
70
71 sub _build_class_template_blocks{
72   my $blocks = {};
73   $blocks->{name_block} = q^
74 =head1 NAME
75
76 [% class.name %]
77 ^;
78
79   $blocks->{synopsys_block} = q^
80 =head1 SYNOPSIS
81
82     use [% class.name %];
83     #TODO
84     [% class.name %]->new();
85 ^;
86
87   $blocks->{description_block} = q^
88 =head1 DESCRIPTION
89
90 [%- IF class.superclasses.size == 1 %]
91 [% 'This class is a subclass of L<' _ class.superclasses.first.name _'>' _
92 ' and inherits all it\'s methods and attributes.' FILTER wrap(80,'','') %]
93
94 [%- ELSIF class.superclasses.size > 1 %]
95 This class is a subclass of the following classes and inherits all their
96 methods and attributes;
97
98 =over 4
99 [%- FOREACH superclass = class.superclasses %]
100
101 =item L<[% superclass.name %]>
102 [%- END %]
103
104 =back
105
106 [%- END -%]
107 ^;
108
109   $blocks->{roles_consumed_block} = q^
110 [%- IF class.roles.size %]
111 =head1 ROLES CONSUMED
112
113 The following roles are consumed by this class. Unless otherwise indicated, all
114 methods, modifiers and attributes present in those roles are applied to this
115 class.
116
117 =over 4
118 [% FOREACH role_consumed = class.roles;
119      PROCESS role_consumed_block;
120    END %]
121 =back
122 [% END -%]
123 ^;
124
125   $blocks->{role_consumed_block} = q^
126 =item L<[% role_consumed.name %]>
127 ^;
128
129   $blocks->{attributes_block} = q^
130 [%- IF class.attributes.size %]
131 =head1 ATTRIBUTES
132
133 Unless noted otherwise, you may set any of these attributes at C<new> time by
134 passing key / value pairs to C<new> where the key is the name of the attribute
135 you wish to set. Unless noted otherwise accessor methods for attributes also
136 share the same name as the attribute.
137 [%
138   FOREACH attribute = class.attributes;
139     PROCESS attribute_block;
140   END;
141 END;
142 -%]
143 ^;
144
145  $blocks->{attribute_block} = q^
146 =head2 [% attribute.name %]
147 [%- IF attribute.info.size %]
148
149 =over 4
150 [% FOREACH pair IN attribute.info.pairs %]
151 =item B<[% pair.key %]> - [% pair.value %]
152 [% END %]
153 =back
154 [%- END; %]
155
156 [% attribute.description FILTER wrap(80, '',''); %]
157 ^;
158
159  $blocks->{methods_block} = q^
160 =head1 METHODS
161
162 =head2 new key => $value
163
164 Instantiate a new object. Please refer to L</"ATTRIBUTES"> for a list of valid
165 key options.
166 [%
167   FOREACH method = class.methods;
168     PROCESS method_block;
169   END;
170 %]
171 =head2 meta
172
173 Retrieve the metaclass instance. Please see L<Moose::Meta::Class> and
174 L<Class::MOP::Class> for more information.
175 ^;
176
177   $blocks->{method_block} = q^
178 =head2 [% method.name %]
179
180 Description of [% method.name %]
181 ^;
182
183   $blocks->{authors_block} = q^
184 =head1 AUTHORS
185 [%
186   FOREACH author = authors;
187     PROCESS author_block;
188   END;
189 -%]
190 ^;
191
192   $blocks->{author_block} = q^
193 [%
194 IF author.name.length;         author.name _ ' ';  END;
195 IF author.handle.length; '(' _ author.name _ ') '; END;
196 IF author.email.length;  '<' _ author.email _ '>'; END;
197 %]
198 ^;
199
200   $blocks->{license_block} = q^
201 =head1 COPYRIGHT AND LICENSE
202
203 [% license FILTER wrap(80, '', '') %]
204 ^;
205
206   $blocks->{class_block} = q^
207 [%
208 PROCESS name_block;
209 PROCESS synopsys_block;
210 PROCESS description_block;
211 PROCESS roles_consumed_block;
212 PROCESS attributes_block;
213 PROCESS methods_block;
214 PROCESS authors_block;
215 PROCESS license_block;
216 %]
217 =cut
218 ^;
219
220   return $blocks;
221 }
222
223 sub _build_role_template_blocks{
224   my $blocks = {};
225
226   $blocks->{name_block} = q^
227 =head1 NAME
228
229 [% role.name %]
230 ^;
231
232   $blocks->{synopsys_block} = q^
233 =head1 SYNOPSIS
234
235     use Moose;
236     with '[% role.name %]';
237 ^;
238
239   $blocks->{description_block} = q^
240 =head1 DESCRIPTION
241
242 When consumed, this role will apply to the consuming class all the methods,
243 method modifiers, and attributes it is composed of.
244 ^;
245
246   $blocks->{roles_consumed_block} = q^
247 [%- IF role.roles.size %]
248 =head1 ROLES CONSUMED
249
250 The following roles are consumed by this role. Unless otherwise indicated, all
251 methods, modifiers and attributes present in those roles will also be applied
252 to any class or role consuming this role.
253
254 =over 4
255 [% FOREACH role_consumed = role.roles;
256      PROCESS role_consumed_block;
257    END %]
258 =back
259 [% END -%]
260 ^;
261
262   $blocks->{role_consumed_block} = q^
263 =item L<[% role_consumed.name %]>
264 ^;
265
266   $blocks->{attributes_block} = q^
267 [%- IF role.attributes.size %]
268 =head1 ATTRIBUTES
269
270 Unless noted otherwise, you may set any of these attributes on consuming
271 classes at C<new()> time by passing key / value pairs to C<new> where the key
272 is the name of the attribute you wish to set. Unless noted otherwise accessor
273 methods for attributes also share the same name as the attribute.
274 [%
275   FOREACH attribute = role.attributes;
276     PROCESS attribute_block;
277   END;
278 END;
279 -%]
280 ^;
281
282  $blocks->{attribute_block} = q^
283 =head2 [% attribute.name %]
284 [%- IF attribute.info.size %]
285
286 =over 4
287 [% FOREACH pair IN attribute.info.pairs %]
288 =item B<[% pair.key %]> -   [% pair.value %]
289 [% END %]
290 =back
291 [%- END; %]
292
293 [% attribute.description FILTER wrap(80, '',''); %]
294 ^;
295
296  $blocks->{methods_block} = q^
297 =head1 METHODS
298 [%
299   FOREACH method = role.methods;
300     PROCESS method_block;
301   END;
302 %]
303 =head2 meta
304
305 Retrieve the role metaclass instance. Please see L<Moose::Meta::Role>;
306 ^;
307
308   $blocks->{method_block} = q^
309 =head2 [% method.name %]
310
311 Description of [% method.name %]
312 ^;
313
314   $blocks->{authors_block} = q^
315 =head1 AUTHORS
316 [%
317   FOREACH author = authors;
318     PROCESS author_block;
319   END;
320 -%]
321 ^;
322
323   $blocks->{author_block} = q^
324 [%
325 IF author.name.length; author.name _ ' '; END;
326 IF author.handle.length; '(' _ author.name _ ') '; END;
327 IF author.email.length; '<' _ author.email _ '> '; END;
328 %]
329 ^;
330
331   $blocks->{license_block} = q^
332 =head1 COPYRIGHT AND LICENSE
333
334 [% license FILTER wrap(80, '', '') %]
335 ^;
336
337   $blocks->{role_block} = q^
338 [%
339 PROCESS name_block;
340 PROCESS synopsys_block;
341 PROCESS description_block;
342 PROCESS roles_consumed_block;
343 PROCESS attributes_block;
344 PROCESS methods_block;
345 PROCESS authors_block;
346 PROCESS license_block;
347 %]
348 =cut
349 ^;
350
351   return $blocks;
352 }
353
354 1;
355
356 __END__;