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