File Coverage

blib/lib/HTML/WebMake/Util.pm
Criterion Covered Total %
statement 21 105 20.0
branch 0 32 0.0
condition 0 12 0.0
subroutine 7 22 31.8
pod 1 13 7.6
total 29 184 15.7


line stmt bran cond sub pod time code
1             #
2              
3             package HTML::WebMake::Util;
4              
5              
6 1     1   6 use Carp;
  1         1  
  1         66  
7 1     1   5 use File::Basename;
  1         2  
  1         63  
8 1     1   6 use File::Path;
  1         1  
  1         58  
9 1     1   5 use File::Spec;
  1         9  
  1         23  
10 1     1   5 use Cwd;
  1         2  
  1         70  
11 1     1   7 use strict;
  1         1  
  1         41  
12              
13 1         2118 use vars qw{
14             @ISA
15 1     1   5 };
  1         2  
16              
17              
18              
19              
20             ###########################################################################
21              
22             sub new ($) {
23 0     0 0   my $class = shift;
24 0   0       $class = ref($class) || $class;
25              
26 0           my $self = {
27             'last_tag_text' => undef,
28             };
29              
30 0           bless ($self, $class);
31 0           $self;
32             }
33              
34 0     0 0   sub dbg { HTML::WebMake::Main::dbg (@_); }
35              
36             ###########################################################################
37              
38             sub glob_to_re ($$) {
39 0     0 0   my ($self, $patt) = @_;
40              
41 0 0         if (!defined $patt) { return $patt; }
  0            
42 0 0         if ($patt =~ s/^RE://) { return $patt; }
  0            
43              
44 0           $patt =~ s:([].+^\-\${}[|]):\\$1:g;
45 0           $patt =~ s/\\\.\\\.\\\./.*/g;
46 0           $patt =~ s/\*/[^\/]*/g;
47 0           $patt =~ s/\?/./g;
48 0           '^'.$patt.'$';
49             }
50              
51             ###########################################################################
52              
53             sub parse_boolean ($$) {
54 0     0 0   my ($self, $val) = @_;
55              
56 0 0 0       if (defined $val && $val =~ /^(?:true|yes|on|y|1)$/i) {
57 0           1;
58             } else {
59 0           0;
60             }
61             }
62              
63             ###########################################################################
64              
65             sub parse_xml_tag_attributes ($$$$$) {
66 0     0 0   my ($self, $tag, $origtxt, $filename, @reqd_attrs) = @_;
67 0           my $attrtxt = " ".$origtxt." ";
68 0           my $attrs = { };
69              
70             #dbg ("tag: <$tag$origtxt>");
71 0           while ($attrtxt =~ s{\s([A-Z0-9a-z_]+)\s*=\s*\"([^\"]*?)\"\s}{ }is) {
72             #dbg ("tag: <$tag$attrtxt>: $1=$2");
73 0           my ($atname, $atval) = ($1, $2); $atname =~ tr/A-Z/a-z/;
  0            
74 0           $attrs->{$atname} = $atval;
75             } # fix vim highlighting: "
76 0           while ($attrtxt =~ s{\s([A-Z0-9a-z_]+)\s*=\s*\'([^\']*?)\'\s}{ }is) {
77 0           my ($atname, $atval) = ($1, $2); $atname =~ tr/A-Z/a-z/;
  0            
78 0           $attrs->{$atname} = $atval;
79             } # fix vim highlighting: '
80 0           while ($attrtxt =~ s{\s([A-Z0-9a-z_]+)\s*=\s*(\S*)\s}{ }is) {
81 0           my ($atname, $atval) = ($1, $2); $atname =~ tr/A-Z/a-z/;
  0            
82 0           $attrs->{$atname} = $atval;
83             }
84              
85 0           foreach my $attr (@reqd_attrs) {
86 0 0         if (!defined $attrs->{$attr}) {
87 0           warn ($filename.": tag \"".$tag.
88             "\" is missing required attribute \"$attr\": <$tag $origtxt>\n");
89 0           return;
90             }
91             }
92              
93 0           return $attrs;
94             }
95              
96             ###########################################################################
97              
98             sub set_filename ($$) {
99 0     0 0   my ($self, $filename) = @_;
100 0           $self->{filename} = $filename;
101             }
102              
103             sub strip_tags ($$$$$@) {
104 0     0 0   my ($self, $file, $tag, $taghandler, $tagfn, @reqd_attrs) = @_;
105              
106 0 0         return unless $file =~ m{<${tag}\b}is;
107              
108 0           $file =~ s{<${tag}([^>]*?)/>}{
109 0           $self->_found_tag ($tag, $1, '', 1, \@reqd_attrs, $taghandler, $tagfn);
110             }gies;
111              
112 0           $file =~ s{<${tag}([^>]*?)>(.*?)<\/\s*${tag}\s*>}{
113 0           $self->_found_tag ($tag, $1, $2, 0, \@reqd_attrs, $taghandler, $tagfn);
114             }gies;
115              
116 0           $file;
117             }
118              
119             sub _strip_first_tag ($$$$$$@) {
120 0     0     my ($self, $paired, $textref, $tag, $taghandler, $tagfn, @reqd_attrs) = @_;
121              
122 0           $self->{last_tag_text} = $self->{last_tag_regexp} = undef;
123 0 0         return unless $$textref =~ m{^\s*<${tag}\b}is;
124              
125 0 0 0       if ($paired == 0 || $paired == 2) {
126 0 0         $$textref =~ s{^\s*<\S+([^>]*?)/>}{
127 0           $self->_found_tag ($tag, $1, '', 1, \@reqd_attrs, $taghandler, $tagfn);
128             }gies and return;
129             }
130              
131 0 0 0       if ($paired == 1 || $paired == 2) {
132 0 0         $$textref =~ s{^\s*<\S+([^>]*?)>(.*?)<\/\s*${tag}\s*>}{
133 0           $self->_found_tag ($tag, $1, $2, 0, \@reqd_attrs, $taghandler, $tagfn);
134             }gies and return;
135             }
136             }
137              
138             sub strip_first_tag ($$$$$@) {
139 0     0 0   return shift->_strip_first_tag (2, @_);
140             }
141             sub strip_first_lone_tag ($$$$$@) {
142 0     0 0   return shift->_strip_first_tag (0, @_);
143             }
144             sub strip_first_tag_block ($$$$$@) {
145 0     0 0   return shift->_strip_first_tag (1, @_);
146             }
147              
148             sub _found_tag ($$$$$$$) {
149 0     0     my ($self, $tag, $origtxt, $text, $isempty,
150             $reqd_attrs, $taghandler, $tagfn) = @_;
151              
152 0           $self->{last_tag_text} = '<'.$tag.$origtxt.'> ... ';
153              
154 0 0         if ($self->{generate_tag_regexps}) {
155 0 0         if ($isempty) {
156 0           $self->{last_tag_regexp} = qr/ \Q<${tag}${origtxt}\/>\E /isx;
157              
158             } else {
159 0           $self->{last_tag_regexp} = qr/ \Q<${tag}${origtxt}>\E
160             .*? <\/\s*\Q${tag}\E\s*> /isx;
161             }
162             }
163              
164 0           my $attrs = $self->parse_xml_tag_attributes ($tag, $origtxt,
165 0           $self->{filename}, @{$reqd_attrs});
166 0 0         if (!defined $attrs) { return; }
  0            
167              
168 0           &{$tagfn} ($taghandler, $tag, $attrs, $text);
  0            
169             }
170              
171             ###########################################################################
172              
173             =item @sorted = sort_by_score_title (@list);
174              
175             A sort function (see C) which sorts a list of content items in
176             order of their C metadata, with alphanumeric sorting by C used </td> </tr> <tr> <td class="h" > <a name="177">177</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> for items of the same score. </td> </tr> <tr> <td class="h" > <a name="178">178</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="179">179</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =cut </td> </tr> <tr> <td class="h" > <a name="180">180</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="181">181</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> sub sort_by_score_title { </td> </tr> <tr> <td class="h" > <a name="182">182</a> </td> <td class="c0" > 0 </td> <td >   </td> <td >   </td> <td class="c0" > <a href="blib-lib-HTML-WebMake-Util-pm--subroutine.html#182-1"> 0 </a> </td> <td class="c3" > <a href="blib-lib-HTML-WebMake-Util-pm--subroutine.html#182-1"> 1 </a> </td> <td >   </td> <td class="s"> my $cmp = $a->get_score() <=> $b->get_score(); </td> </tr> <tr> <td class="h" > <a name="183">183</a> </td> <td class="c0" > 0 </td> <td class="c0" > <a href="blib-lib-HTML-WebMake-Util-pm--branch.html#183-1"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> if ($cmp != 0) { return $cmp; } </td> </tr> <tr> <td class="h" > <a > </a> </td> <td class="c0" > 0 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="184">184</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="185">185</a> </td> <td class="c0" > 0 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $a->get_title() cmp $b->get_title(); </td> </tr> <tr> <td class="h" > <a name="186">186</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> } </td> </tr> <tr> <td class="h" > <a name="187">187</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="188">188</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> # a convenience function to do the sort for us, otherwise some package </td> </tr> <tr> <td class="h" > <a name="189">189</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> # twiddling is required (as $a and $b are set in the caller's pkg). </td> </tr> <tr> <td class="h" > <a name="190">190</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> # </td> </tr> <tr> <td class="h" > <a name="191">191</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> sub sort_list_by_score_title { </td> </tr> <tr> <td class="h" > <a name="192">192</a> </td> <td class="c0" > 0 </td> <td >   </td> <td >   </td> <td class="c0" > <a href="blib-lib-HTML-WebMake-Util-pm--subroutine.html#192-1"> 0 </a> </td> <td class="c0" > <a href="blib-lib-HTML-WebMake-Util-pm--subroutine.html#192-1"> 0 </a> </td> <td >   </td> <td class="s"> my ($self, @list) = @_; </td> </tr> <tr> <td class="h" > <a name="193">193</a> </td> <td class="c0" > 0 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> return sort sort_by_score_title @list; </td> </tr> <tr> <td class="h" > <a name="194">194</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> } </td> </tr> <tr> <td class="h" > <a name="195">195</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="196">196</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> ########################################################################### </td> </tr> <tr> <td class="h" > <a name="197">197</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="198">198</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> sub text_eol { </td> </tr> <tr> <td class="h" > <a name="199">199</a> </td> <td class="c0" > 0 </td> <td >   </td> <td >   </td> <td class="c0" > <a href="blib-lib-HTML-WebMake-Util-pm--subroutine.html#199-1"> 0 </a> </td> <td class="c0" > <a href="blib-lib-HTML-WebMake-Util-pm--subroutine.html#199-1"> 0 </a> </td> <td >   </td> <td class="s"> my ($self) = @_; </td> </tr> <tr> <td class="h" > <a name="200">200</a> </td> <td class="c0" > 0 </td> <td class="c0" > <a href="blib-lib-HTML-WebMake-Util-pm--branch.html#200-1"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> if ($^O =~ /(?:win|os2)/i) { </td> </tr> <tr> <td class="h" > <a > </a> </td> <td >   </td> <td class="c0" > <a href="blib-lib-HTML-WebMake-Util-pm--branch.html#-2"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="201">201</a> </td> <td class="c0" > 0 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> return "\r\n"; </td> </tr> <tr> <td class="h" > <a name="202">202</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> } elsif ($^O =~ /(?:mac)/i) { </td> </tr> <tr> <td class="h" > <a name="203">203</a> </td> <td class="c0" > 0 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> return "\r"; </td> </tr> <tr> <td class="h" > <a name="204">204</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> } else { </td> </tr> <tr> <td class="h" > <a name="205">205</a> </td> <td class="c0" > 0 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> return "\n"; </td> </tr> <tr> <td class="h" > <a name="206">206</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> } </td> </tr> <tr> <td class="h" > <a name="207">207</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> } </td> </tr> <tr> <td class="h" > <a name="208">208</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="209">209</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> ########################################################################### </td> </tr> <tr> <td class="h" > <a name="210">210</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="211">211</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> 1; </td> </tr> <tr> <td class="h" > <a name="212">212</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> </table> </body> </html>