File Coverage

blib/lib/HTML/WebMake/PerlCodeLibrary.pm
Criterion Covered Total %
statement 6 152 3.9
branch 0 34 0.0
condition n/a
subroutine 2 60 3.3
pod 0 29 0.0
total 8 275 2.9


line stmt bran cond sub pod time code
1             # Library of perl functions for use by WebMake scripts.
2              
3             =head1 NAME
4              
5             PerlCodeLibrary - a selection of functions for use by perl code embedded in a
6             WebMake file.
7              
8             =head1 SYNOPSIS
9              
10             <{perl
11              
12             $foo = get_content ($bar);
13             [... etc.]
14              
15             # or:
16              
17             $foo = $self->get_content ($bar);
18             [... etc.]
19              
20             }>
21              
22             =head1 DESCRIPTION
23              
24             These functions allow code embedded in a <{perl}> or <{perlout}> section of a
25             WebMake file to be used to script the generation of content.
26              
27             Each of these functions is defined both as a standalone function, or as a
28             function on the PerlCode object. Code in one of the <{perl*}> sections can
29             access this PerlCode object as the C<$self> variable. If you plan to use
30             WebMake from mod_perl or in a threaded environment, be sure to call them as
31             methods on C<$self>.
32              
33             =head1 METHODS
34              
35             =over 4
36              
37             =cut
38              
39             package HTML::WebMake::PerlCode;
40              
41 1     1   7 use Carp;
  1         3  
  1         68  
42 1     1   6 use strict;
  1         3  
  1         2781  
43              
44             ###########################################################################
45              
46             =item $expandedtext = expand ($text);
47              
48             Expand a block of text, interpreting any references, user tags, or
49             any other WebMake markup contained within.
50              
51             =cut
52              
53             sub expand {
54 0     0 0   my ($self, $text) = @_;
55 0           return $self->{main}->fileless_subst ($HTML::WebMake::Main::SUBST_EVAL, $text);
56             }
57              
58             # -------------------------------------------------------------------------
59              
60             =item @names = content_matching ($pattern);
61              
62             Find all items of content that match the glob pattern C<$pattern>. If
63             C<$pattern> begins with the prefix B, it is treated as a regular
64             expression. The list of items returned is not in any logical order.
65              
66             =cut
67              
68             sub content_matching {
69 0     0 0   my ($self, $patt) = @_;
70              
71 0           $patt = $self->{main}->{util}->glob_to_re ($patt);
72 0           my @ret = grep (m/${patt}/, $self->{main}->get_all_content_names());
73 0           @ret;
74             }
75              
76             # -------------------------------------------------------------------------
77              
78             =item @objs = content_names_to_objects (@names);
79              
80             Given a list of content names, convert to the corresponding list of content
81             objects, ie. objects of type C.
82              
83             =cut
84              
85             sub content_names_to_objects {
86 0     0 0   my ($self, @namelist) = @_;
87 0           my @list = ();
88 0           foreach my $elem (@namelist) {
89 0           my $contobj = $self->{main}->get_content_obj($elem);
90 0 0         if (!defined $contobj) {
91 0           warn "content_names_to_objects: not a item: $elem\n";
92 0           next;
93             }
94 0           push (@list, $contobj);
95             }
96 0           @list;
97             }
98              
99             # -------------------------------------------------------------------------
100              
101             =item $obj = get_content_object ($name);
102              
103             Given a content name, convert to the corresponding content object, ie. objects
104             of type C.
105              
106             =cut
107              
108             sub get_content_object {
109 0     0 0   my ($self, $name) = @_;
110 0           my $contobj = $self->{main}->get_content_obj($name);
111 0 0         if (!defined $contobj) {
112 0           warn "get_content_object: not a item: $name\n";
113             }
114 0           $contobj;
115             }
116              
117             # -------------------------------------------------------------------------
118              
119             =item @names = content_objects_to_names (@objs);
120              
121             Given a list of objects of type C, convert to
122             the corresponding list of content name strings.
123              
124             =cut
125              
126             sub content_objects_to_names {
127 0     0 0   my ($self, @objlist) = @_;
128 0           local ($_);
129 0           map { $_->get_name() } @objlist;
  0            
130             }
131              
132             # -------------------------------------------------------------------------
133              
134             =item @sortedobjs = sort_content_objects ($sortstring, @objs);
135              
136             Sort a list of content objects by the sort string C<$sortstring>.
137             See ''sorting.html'' in the WebMake documentation for details on
138             sort strings.
139              
140             =cut
141              
142             sub sort_content_objects {
143 0     0 0   my ($self, $sortstr, @list) = @_;
144 0           my $sortsub = $self->get_sort_sub($sortstr);
145 0           sort $sortsub (@list);
146             }
147              
148             # -------------------------------------------------------------------------
149              
150             =item @names = sorted_content_matching ($sortstring, $pattern);
151              
152             Find all items of content that match the glob-style pattern C<$pattern>. The
153             list of items returned is ordered according to the sort string C<$sortstring>.
154             If C<$pattern> begins with the prefix B, it is treated as a regular
155             expression.
156              
157             See ''sorting.html'' in the WebMake documentation for details on sort strings.
158              
159             This, by the way, is essentially implemented as follows:
160              
161             my @list = $self->content_matching ($pattern);
162             @list = $self->content_names_to_objects (@list);
163             @list = $self->sort_content_objects ($sortstring, @list);
164             return $self->content_objects_to_names (@list);
165              
166             =cut
167              
168             sub sorted_content_matching {
169 0     0 0   my ($self, $string, $patt) = @_;
170              
171 0           my @list = $self->content_matching ($patt);
172 0           @list = $self->content_names_to_objects (@list);
173 0           @list = $self->sort_content_objects ($string, @list);
174 0           return $self->content_objects_to_names (@list);
175             }
176              
177             # -------------------------------------------------------------------------
178              
179             =item $str = get_content ($name);
180              
181             Get the item of content named C<$name>. Equivalent to a $ {content_reference}.
182              
183             =cut
184              
185             sub get_content {
186 0     0 0   my ($self, $key) = @_;
187 0 0         if (!defined $key) { croak ("get_content with undef key"); }
  0            
188 0           my $str = $self->{main}->curly_or_meta_subst ($HTML::WebMake::Main::SUBST_EVAL, $key);
189 0           $str;
190             }
191              
192             =item @list = get_list ($name);
193              
194             Get the item of content named, but in Perl list format. It is assumed that the
195             list is stored in the content item in whitespace-separated format.
196              
197             =cut
198              
199             sub get_list {
200 0     0 0   my ($self, $key) = @_;
201 0 0         if (!defined $key) { croak ("get_list with undef key"); }
  0            
202 0           my $str = $self->{main}->curly_or_meta_subst ($HTML::WebMake::Main::SUBST_EVAL, $key);
203 0           split (' ', $str);
204             }
205              
206             =item set_content ($name, $value);
207              
208             Set a content chunk to the value provided. This content will not appear in a
209             sitemap, and navigation links will never point to it.
210              
211             Returns the content object created.
212              
213             =cut
214              
215             sub set_content {
216 0     0 0   my ($self, $key, $val) = @_;
217 0 0         if (!defined $key) { croak ("set_content with undef key"); }
  0            
218 0 0         if (!defined $val) { croak ("set_content with undef val"); }
  0            
219 0           return $self->{main}->set_unmapped_content ($key, $val);
220             }
221              
222             =item set_list ($name, @values);
223              
224             Set a content chunk to a list containing the values provided, separated by
225             spaces. This content will not appear in a sitemap, and navigation links will
226             never point to it.
227              
228             Returns the content object created.
229              
230             =cut
231              
232             sub set_list {
233 0     0 0   my ($self, $key, @vals) = @_;
234 0 0         if (!defined $key) { croak ("set_list with undef key"); }
  0            
235 0           return $self->{main}->set_unmapped_content ($key,
236             join (' ', @vals));
237             }
238              
239             =item set_mapped_content ($name, $value, $upname);
240              
241             Set a content chunk to the value provided. This content will appear in a
242             sitemap and the navigation hierarchy. C<$upname> should be the name of it's
243             parent content item. This item must not be metadata, or other
244             dynamically-generated content; only first-class mapped content can be used.
245              
246             Returns the content object created.
247              
248             =cut
249              
250             sub set_mapped_content {
251 0     0 0   my ($self, $key, $val, $upname) = @_;
252 0 0         if (!defined $key) { croak ("set_mapped_content with undef key"); }
  0            
253 0 0         if (!defined $val) { croak ("set_mapped_content with undef val"); }
  0            
254 0 0         if (!defined $upname) { croak ("set_mapped_content with undef upname"); }
  0            
255 0           return $self->{main}->set_mapped_content ($key, $val, $upname);
256             }
257              
258             =item del_content ($name);
259              
260             Delete a named content chunk.
261              
262             =cut
263              
264             sub del_content {
265 0     0 0   my ($self, $key) = @_;
266 0 0         if (!defined $key) { croak ("del_content with undef key"); }
  0            
267 0           $self->{main}->del_content ($key);
268             }
269              
270             # -------------------------------------------------------------------------
271              
272             =item @names = url_matching ($pattern);
273              
274             Find all URLs (from and tags) whose name matches the glob-style
275             pattern C<$pattern>. The names of the URLs, not the URLs themselves, are
276             returned. If C<$pattern> begins with the prefix B, it is treated as a
277             regular expression.
278              
279             =cut
280              
281             sub url_matching {
282 0     0 0   my ($self, $patt) = @_;
283              
284 0           $patt = $self->{main}->{util}->glob_to_re ($patt);
285 0           my @ret = grep (m/${patt}/, $self->{main}->get_all_url_names());
286 0           @ret;
287             }
288              
289             =item $url = get_url ($name);
290              
291             Get a named URL. Equivalent to an $ (url_reference).
292              
293             =cut
294              
295             sub get_url {
296 0     0 0   my ($self, $key) = @_;
297 0 0         if (!defined $key) { croak ("get_url with undef key"); }
  0            
298 0           $self->{main}->round_subst ($HTML::WebMake::Main::SUBST_EVAL, $key);
299             }
300              
301             =item set_url ($name, $url);
302              
303             Set an URL to the value provided.
304              
305             =cut
306              
307             sub set_url {
308 0     0 0   my ($self, $key, $val) = @_;
309 0 0         if (!defined $key) { croak ("get_url with undef key"); }
  0            
310 0 0         if (!defined $val) { croak ("get_url with undef val"); }
  0            
311 0           $self->{main}->add_url ($key, $val);
312             }
313              
314             =item del_url ($name);
315              
316             Delete an URL.
317              
318             =cut
319              
320             sub del_url {
321 0     0 0   my ($self, $key) = @_;
322 0 0         if (!defined $key) { croak ("del_url with undef key"); }
  0            
323 0           $self->{main}->del_url ($key);
324             }
325              
326             # -------------------------------------------------------------------------
327              
328             =item $listtext = make_list ($itemname, @namelist);
329              
330             Generate a list by iterating through the C<@namelist>, setting the content item
331             C to the current name, and interpreting the content chunk named
332             C<$itemname>. This content chunk should refer to C<${item}> appropriately.
333              
334             Each resulting block of content is appended to a $listtext, which is finally
335             returned.
336              
337             See the C sample site for an example of this in use.
338              
339             =cut
340              
341             sub make_list {
342 0     0 0   my ($self, $list_item_name, @story_list) = @_;
343              
344 0           my @listtext = ();
345 0           foreach my $story (@story_list) {
346 0           $self->set_content ("item", $story);
347 0           push (@listtext, $self->get_content ($list_item_name));
348             }
349 0           join ('', @listtext);
350             }
351              
352             # -------------------------------------------------------------------------
353              
354             sub _make_sitemap {
355 0     0     my ($self, $topname, $map_generated_content, $contname) = @_;
356 0           my $top = undef;
357              
358 0 0         if (defined $topname) {
359 0           $top = $self->{main}->get_content_obj($topname);
360 0 0         if (!defined $top) {
361 0           warn "make_sitemap: item not found: $topname\n";
362 0           return "";
363             }
364             }
365              
366 0           $self->{main}->getmapper()->map_site ($top,
367             $map_generated_content, $contname);
368             }
369              
370             sub make_sitemap {
371 0     0 0   my ($self, $topname, $contname) = @_;
372 0           $self->_make_sitemap ($topname, 0, $contname);
373             }
374              
375             sub make_contentmap {
376 0     0 0   my ($self, $topname, $contname) = @_;
377 0           $self->_make_sitemap ($topname, 1, $contname);
378             }
379              
380             # -------------------------------------------------------------------------
381              
382             =item define_tag ($tagname, \&handlerfn, @required_attributes);
383              
384             Define a tag for use in content items. Any occurrences of this tag, with at
385             least the set of attributes defined in @required_attributes, will cause the
386             handler function referred to by handlerfn to be called.
387              
388             Handler functions are called as fcllows:
389              
390             handler ($tagname, $attrs, $text, $perlcode);
391              
392             Where $tagname is the name of the tag, $attrs is a reference to a hash
393             containing the attribute names and the values used in the tag, and $text is the
394             text between the start and end tags.
395              
396             $perlcode is the PerlCode object, allowing you to write proper object-oriented
397             code that can be run in a threaded environment or from mod_perl. This can be
398             ignored if you like.
399              
400             This function returns an empty string.
401              
402             =cut
403              
404             sub define_tag {
405 0     0 0   my ($self, $name, $fnname, @reqdattrs) = @_;
406 0           $self->{main}->getusertags()->def_tag (0,0,0, $name, $fnname, @reqdattrs);
407             }
408              
409             =item define_empty_tag ($tagname, \&handlerfn, @required_attributes);
410              
411             Define a tag for use in content items. This is identical to define_tag above,
412             but is intended for use to define ''empty'' tags, ie. tags which occur alone,
413             not as part of a start and end tag pair.
414              
415             The handler in this case is called with an empty string for the $text
416             argument.
417              
418             =cut
419              
420             sub define_empty_tag {
421 0     0 0   my ($self, $name, $fnname, @reqdattrs) = @_;
422 0           $self->{main}->getusertags()->def_tag (1,0,0, $name, $fnname, @reqdattrs);
423             }
424              
425             # -------------------------------------------------------------------------
426              
427             =item define_preformat_tag ($tagname, \&handlerfn, @required_attributes);
428              
429             Identical to L, above, with one difference; these tags will
430             be interpreted B the content undergoes any format conversion.
431              
432             =cut
433              
434             sub define_preformat_tag {
435 0     0 0   my ($self, $name, $fnname, @reqdattrs) = @_;
436 0           $self->{main}->getusertags()->def_tag (0,0,1, $name, $fnname, @reqdattrs);
437             }
438              
439             =item define_empty_preformat_tag ($tagname, \&handlerfn, @required_attributes);
440              
441             Identical to L, above, with one difference; these tags will
442             be interpreted B the content undergoes any format conversion.
443              
444             =cut
445              
446             sub define_empty_preformat_tag {
447 0     0 0   my ($self, $name, $fnname, @reqdattrs) = @_;
448 0           $self->{main}->getusertags()->def_tag (1,0,1, $name, $fnname, @reqdattrs);
449             }
450              
451             # -------------------------------------------------------------------------
452              
453             =item define_wmk_tag ($tagname, \&handlerfn, @required_attributes);
454              
455             Define a tag for use in the WebMake file.
456              
457             Aside from operating on the WebMake file instead of inside content items, this
458             is otherwise identical to define_tag above,
459              
460             =cut
461              
462             sub define_wmk_tag {
463 0     0 0   my ($self, $name, $fnname, @reqdattrs) = @_;
464 0           $self->{main}->getusertags()->def_tag (0,1,0, $name, $fnname, @reqdattrs);
465             }
466              
467             =item define_empty_wmk_tag ($tagname, \&handlerfn, @required_attributes);
468              
469             Define an empty, aka. standalone, tag for use in the WebMake file.
470              
471             Aside from operating on the WebMake file instead of inside content items, this
472             is otherwise identical to define_tag above,
473              
474             =cut
475              
476             sub define_empty_wmk_tag {
477 0     0 0   my ($self, $name, $fnname, @reqdattrs) = @_;
478 0           $self->{main}->getusertags()->def_tag (1,1,0, $name, $fnname, @reqdattrs);
479             }
480              
481             # -------------------------------------------------------------------------
482              
483             =item $obj = get_root_content_object();
484              
485             Get the content object representing the ''root'' of the site map. Returns
486             undef if no root object exists, or the WebMake file does not contain a
487             <sitemap> command.
488              
489             =cut
490              
491             sub get_root_content_object {
492 0     0 0   my ($self) = @_;
493 0           return $self->{main}->getmapper()->get_root();
494             }
495              
496             # -------------------------------------------------------------------------
497              
498             =item $name = get_current_main_content();
499              
500             Get the ''main'' content on the current output page. The ''main'' content is
501             defined as the most recently referenced content item which (a) is not generated
502             content (perl code, sitemaps, breadcrumb trails etc.), and (b) has its
503             C attribute set to "true".
504              
505             Note that this API should only be called from a deferred content reference;
506             otherwise the ''main'' content item may not have been referenced by the time
507             this API is called.
508              
509             C is returned if no main content item has been referenced.
510              
511             =cut
512              
513             sub get_current_main_content {
514 0     0 0   my ($self) = @_;
515 0           $self->{main}->curly_subst ($HTML::WebMake::Main::SUBST_EVAL, "__MainContentName");
516             }
517              
518             # -------------------------------------------------------------------------
519              
520             =item $main = get_webmake_main_object();
521              
522             Get the current WebMake interpreter's instance of C
523             object. Virtually all of WebMake's functionality and internals can be accessed
524             through this.
525              
526             =cut
527              
528             sub get_webmake_main_object {
529 0     0 0   my ($self) = @_;
530 0           $self->{main};
531             }
532              
533             # -------------------------------------------------------------------------
534             # Glue functions -- these allow calls from perl code without using the
535             # $self->function_name() OO mode.
536              
537             package main;
538              
539             sub content_matching {
540 0     0     $HTML::WebMake::PerlCode::GlobalSelf->content_matching(@_);
541             }
542             sub sorted_content_matching {
543 0     0     $HTML::WebMake::PerlCode::GlobalSelf->sorted_content_matching(@_);
544             }
545             sub sort_content_list {
546 0     0     $HTML::WebMake::PerlCode::GlobalSelf->sort_content_list(@_);
547             }
548             sub set_mapped_content {
549 0     0     $HTML::WebMake::PerlCode::GlobalSelf->set_mapped_content(@_);
550             }
551              
552 0     0     sub get_content { $HTML::WebMake::PerlCode::GlobalSelf->get_content(@_); }
553 0     0     sub get_list { $HTML::WebMake::PerlCode::GlobalSelf->get_list(@_); }
554 0     0     sub set_content { $HTML::WebMake::PerlCode::GlobalSelf->set_content(@_); }
555 0     0     sub set_list { $HTML::WebMake::PerlCode::GlobalSelf->set_list(@_); }
556 0     0     sub del_content { $HTML::WebMake::PerlCode::GlobalSelf->del_content(@_); }
557 0     0     sub url_matching { $HTML::WebMake::PerlCode::GlobalSelf->url_matching(@_); }
558 0     0     sub get_url { $HTML::WebMake::PerlCode::GlobalSelf->get_url(@_); }
559 0     0     sub set_url { $HTML::WebMake::PerlCode::GlobalSelf->set_url(@_); }
560 0     0     sub del_url { $HTML::WebMake::PerlCode::GlobalSelf->del_url(@_); }
561 0     0     sub make_list { $HTML::WebMake::PerlCode::GlobalSelf->make_list(@_); }
562             sub content_names_to_objects
563 0     0     { $HTML::WebMake::PerlCode::GlobalSelf->content_names_to_objects(@_); }
564             sub get_content_object
565 0     0     { $HTML::WebMake::PerlCode::GlobalSelf->get_content_object(@_); }
566             sub sort_content_objects
567 0     0     { $HTML::WebMake::PerlCode::GlobalSelf->sort_content_objects(@_); }
568             sub content_objects_to_names
569 0     0     { $HTML::WebMake::PerlCode::GlobalSelf->content_objects_to_names(@_); }
570             sub define_tag
571 0     0     { $HTML::WebMake::PerlCode::GlobalSelf->define_tag(@_); }
572             sub define_empty_tag
573 0     0     { $HTML::WebMake::PerlCode::GlobalSelf->define_empty_tag(@_); }
574             sub define_preformat_tag
575 0     0     { $HTML::WebMake::PerlCode::GlobalSelf->define_preformat_tag(@_); }
576             sub define_empty_preformat_tag
577 0     0     { $HTML::WebMake::PerlCode::GlobalSelf->define_empty_preformat_tag(@_); }
578             sub define_wmk_tag
579 0     0     { $HTML::WebMake::PerlCode::GlobalSelf->define_wmk_tag(@_); }
580             sub define_empty_wmk_tag
581 0     0     { $HTML::WebMake::PerlCode::GlobalSelf->define_empty_wmk_tag(@_); }
582             sub get_root_content_object
583 0     0     { $HTML::WebMake::PerlCode::GlobalSelf->get_root_content_object(@_); }
584             sub get_current_main_content
585 0     0     { $HTML::WebMake::PerlCode::GlobalSelf->get_current_main_content(@_); }
586             sub get_webmake_main_object
587 0     0     { $HTML::WebMake::PerlCode::GlobalSelf->get_webmake_main_object(@_); }
588             sub expand
589 0     0     { $HTML::WebMake::PerlCode::GlobalSelf->expand(@_); }
590              
591             1;