| 1 | package Tenjin::Util; | ||||||||||||
| 2 | |||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 8 | ▶ | use HTML::Entities; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 8 | our $VERSION = "1.000000"; | |||||||||||
| 8 | 8 | $VERSION = eval $VERSION; | |||||||||||
| 9 | |||||||||||||
| 10 | =head1 NAME | ||||||||||||
| 11 | |||||||||||||
| 12 | Tenjin::Util - Utility methods for Tenjin. | ||||||||||||
| 13 | |||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||
| 15 | |||||||||||||
| 16 | # in your templates: | ||||||||||||
| 17 | |||||||||||||
| 18 | # encode a URL | ||||||||||||
| 19 | [== encode_url('http://www.google.com/search?q=tenjin&ie=utf-8&oe=utf-8&aq=t') =] | ||||||||||||
| 20 | # returns http%3A//www.google.com/search%3Fq%3Dtenjin%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt | ||||||||||||
| 21 | |||||||||||||
| 22 | # escape a string of lines of HTML code | ||||||||||||
| 23 | <?pl my $string = '<h1>You & Me</h1>\n<h2>Me & You</h2>'; ?> | ||||||||||||
| 24 | [== text2html($string) =] | ||||||||||||
| 25 | # returns <h1>You & Me</h1><br />\n<h2>Me & You</h2> | ||||||||||||
| 26 | |||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||
| 28 | |||||||||||||
| 29 | This module provides a few utility functions which can be used in your | ||||||||||||
| 30 | templates for your convenience. These include functions to (un)escape | ||||||||||||
| 31 | and (en/de)code URLs. | ||||||||||||
| 32 | |||||||||||||
| 33 | =head1 METHODS | ||||||||||||
| 34 | |||||||||||||
| 35 | =head2 expand_tabs( $str, [$tabwidth] ) | ||||||||||||
| 36 | |||||||||||||
| 37 | Receives a string that might contain tabs in it, and replaces those | ||||||||||||
| 38 | tabs with spaces, each tab with the number of spaces defined by C<$tabwidth>, | ||||||||||||
| 39 | or, if C<$tabwidth> was not passed, with 8 spaces. | ||||||||||||
| 40 | |||||||||||||
| 41 | =cut | ||||||||||||
| 42 | |||||||||||||
| 43 | sub expand_tabs { | ||||||||||||
| 44 | 0 | ▶ | my ($str, $tabwidth) = @_; | ||||||||||
|
Subroutine
expand_tabs: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 45 | |||||||||||||
| 46 | 0 | ▶ | $tabwidth ||= 8; | ||||||||||
|
Condition0%0/2
$tabwidth ||= 8
MC/DC0%0/1
$tabwidth ||= 8
$tabwidth
Truth table0%0/2
A
A= $tabwidth
| |||||||||||||
| 47 | 0 | my $s = ''; | |||||||||||
| 48 | 0 | my $pos = 0; | |||||||||||
| 49 | 0 | ▶ | while ($str =~ /.*?\t/sg) { # /(.*?)\t/ may be slow | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | 0 | my $end = $+[0]; | |||||||||||
| 51 | 0 | my $text = substr($str, $pos, $end - 1 - $pos); | |||||||||||
| 52 | 0 | my $n = rindex($text, "\n"); | |||||||||||
| 53 | 0 | ▶ | my $col = $n >= 0 ? length($text) - $n - 1 : length($text); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | 0 | $s .= $text; | |||||||||||
| 55 | 0 | $s .= ' ' x ($tabwidth - $col % $tabwidth); | |||||||||||
| 56 | 0 | $pos = $end; | |||||||||||
| 57 | } | ||||||||||||
| 58 | 0 | my $rest = substr($str, $pos); | |||||||||||
| 59 | 0 | return $s; | |||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | =head2 escape_xml( $str ) | ||||||||||||
| 63 | |||||||||||||
| 64 | Receives a string of XML (or (x)HTML) code and converts the characters | ||||||||||||
| 65 | <>&\' to HTML entities. This is the method that is invoked when you use | ||||||||||||
| 66 | [= $expression =] in your templates. | ||||||||||||
| 67 | |||||||||||||
| 68 | =cut | ||||||||||||
| 69 | |||||||||||||
| 70 | sub escape_xml { | ||||||||||||
| 71 | 1 | ▶ | encode_entities($_[0], '<>&"\''); | ||||||||||
|
Subroutine
escape_xml: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | =head2 unescape_xml( $str ) | ||||||||||||
| 75 | |||||||||||||
| 76 | Receives a string of escaped XML (or (x)HTML) code (for example, a string | ||||||||||||
| 77 | that was escaped with the L<escape_xml()|escape_xml( $str )> function, | ||||||||||||
| 78 | and 'unescapes' all HTML entities back to their actual characters. | ||||||||||||
| 79 | |||||||||||||
| 80 | =cut | ||||||||||||
| 81 | |||||||||||||
| 82 | sub unescape_xml { | ||||||||||||
| 83 | 1 | ▶ | decode_entities($_[0]); | ||||||||||
|
Subroutine
unescape_xml: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | =head2 encode_url( $url ) | ||||||||||||
| 87 | |||||||||||||
| 88 | Receives a URL and encodes it by escaping 'non-standard' characters. | ||||||||||||
| 89 | |||||||||||||
| 90 | =cut | ||||||||||||
| 91 | |||||||||||||
| 92 | sub encode_url { | ||||||||||||
| 93 | 1 | ▶ | my $url = shift; | ||||||||||
|
Subroutine
encode_url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 94 | |||||||||||||
| 95 | 1 | $url =~ s/([^-A-Za-z0-9_.\/])/sprintf("%%%02X", ord($1))/sge; | |||||||||||
| 96 | 1 | $url =~ tr/ /+/; | |||||||||||
| 97 | 1 | return $url; | |||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | =head2 decode_url( $url ) | ||||||||||||
| 101 | |||||||||||||
| 102 | Does the opposite of L<encode_url()|encode_url( $url )>. | ||||||||||||
| 103 | |||||||||||||
| 104 | =cut | ||||||||||||
| 105 | |||||||||||||
| 106 | sub decode_url { | ||||||||||||
| 107 | 0 | ▶ | my $url = shift; | ||||||||||
|
Subroutine
decode_url: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 108 | |||||||||||||
| 109 | 0 | $url =~ s/\%([a-fA-F0-9][a-fA-F0-9])/pack('C', hex($1))/sge; | |||||||||||
| 110 | 0 | return $url; | |||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | =head2 checked( $val ) | ||||||||||||
| 114 | |||||||||||||
| 115 | Receives a value of some sort, and if it is a true value, returns the string | ||||||||||||
| 116 | ' checked="checked"' which can be appended to HTML checkboxes. | ||||||||||||
| 117 | |||||||||||||
| 118 | =cut | ||||||||||||
| 119 | |||||||||||||
| 120 | sub checked { | ||||||||||||
| 121 | 0 | ▶ | $_[0] ? ' checked="checked"' : ''; | ||||||||||
|
Subroutine
checked: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||
| 122 | } | ||||||||||||
| 123 | |||||||||||||
| 124 | =head2 selected( $val ) | ||||||||||||
| 125 | |||||||||||||
| 126 | Receives a value of some sort, and if it is a true value, returns the string | ||||||||||||
| 127 | ' selected="selected"' which can be used in an option in an HTML select box. | ||||||||||||
| 128 | |||||||||||||
| 129 | =cut | ||||||||||||
| 130 | |||||||||||||
| 131 | sub selected { | ||||||||||||
| 132 | 0 | ▶ | $_[0] ? ' selected="selected"' : ''; | ||||||||||
|
Subroutine
selected: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||
| 133 | } | ||||||||||||
| 134 | |||||||||||||
| 135 | =head2 disabled( $val ) | ||||||||||||
| 136 | |||||||||||||
| 137 | Receives a value of some sort, and if it is a true value, returns the string | ||||||||||||
| 138 | ' disabled="disabled"' which can be used in an HTML input. | ||||||||||||
| 139 | |||||||||||||
| 140 | =cut | ||||||||||||
| 141 | |||||||||||||
| 142 | sub disabled { | ||||||||||||
| 143 | 0 | ▶ | $_[0] ? ' disabled="disabled"' : ''; | ||||||||||
|
Subroutine
disabled: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||
| 144 | } | ||||||||||||
| 145 | |||||||||||||
| 146 | =head2 nl2br( $text ) | ||||||||||||
| 147 | |||||||||||||
| 148 | Receives a string of text containing lines delimited by newline characters | ||||||||||||
| 149 | (\n, or possibly \r\n) and appends an HTML line break (<br />) to every | ||||||||||||
| 150 | line (the newline character is left untouched). | ||||||||||||
| 151 | |||||||||||||
| 152 | =cut | ||||||||||||
| 153 | |||||||||||||
| 154 | sub nl2br { | ||||||||||||
| 155 | 0 | ▶ | my $text = shift; | ||||||||||
|
Subroutine
nl2br: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 156 | |||||||||||||
| 157 | 0 | $text =~ s/(\r?\n)/<br \/>$1/g; | |||||||||||
| 158 | 0 | return $text; | |||||||||||
| 159 | } | ||||||||||||
| 160 | |||||||||||||
| 161 | =head2 text2html( $text ) | ||||||||||||
| 162 | |||||||||||||
| 163 | Receives a string of text containing lines delimited by newline characters, | ||||||||||||
| 164 | and possibly some XML (or (x)HTML) code, escapes that code with | ||||||||||||
| 165 | L<escape_xml()|escape_xml( $str )> and then appends an HTML line break | ||||||||||||
| 166 | to every line with L<nl2br()|nl2br( $text )>. | ||||||||||||
| 167 | |||||||||||||
| 168 | =cut | ||||||||||||
| 169 | |||||||||||||
| 170 | sub text2html { | ||||||||||||
| 171 | 0 | ▶ | nl2br(escape_xml($_[0])); | ||||||||||
|
Subroutine
text2html: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 172 | } | ||||||||||||
| 173 | |||||||||||||
| 174 | =head2 tagattr( $name, $expr, [$value] ) | ||||||||||||
| 175 | |||||||||||||
| 176 | =cut | ||||||||||||
| 177 | |||||||||||||
| 178 | sub tagattr { | ||||||||||||
| 179 | 0 | ▶ | my ($name, $expr, $value) = @_; | ||||||||||
|
Subroutine
tagattr: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 180 | |||||||||||||
| 181 | 0 | ▶ | return '' unless $expr; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 182 | 0 | ▶ | $value = $expr unless defined $value; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 183 | 0 | return " $name=\"$value\""; | |||||||||||
| 184 | } | ||||||||||||
| 185 | |||||||||||||
| 186 | =head2 tagattrs( %attrs ) | ||||||||||||
| 187 | |||||||||||||
| 188 | =cut | ||||||||||||
| 189 | |||||||||||||
| 190 | sub tagattrs { | ||||||||||||
| 191 | 0 | ▶ | my (%attrs) = @_; | ||||||||||
|
Subroutine
tagattrs: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 192 | |||||||||||||
| 193 | 0 | my $s = ''; | |||||||||||
| 194 | 0 | ▶ | while (my ($k, $v) = each %attrs) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 195 | 0 | ▶ | $s .= " $k=\"".escape_xml($v)."\"" if defined $v; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 196 | } | ||||||||||||
| 197 | 0 | return $s; | |||||||||||
| 198 | } | ||||||||||||
| 199 | |||||||||||||
| 200 | =head2 new_cycle( @items ) | ||||||||||||
| 201 | |||||||||||||
| 202 | Creates a subroutine reference that can be used for cycling through the | ||||||||||||
| 203 | items of the C<@items> array. So, for example, you can: | ||||||||||||
| 204 | |||||||||||||
| 205 | my $cycle = new_cycle(qw/red green blue/); | ||||||||||||
| 206 | print $cycle->(); # prints 'red' | ||||||||||||
| 207 | print $cycle->(); # prints 'green' | ||||||||||||
| 208 | print $cycle->(); # prints 'blue' | ||||||||||||
| 209 | print $cycle->(); # prints 'red' again | ||||||||||||
| 210 | |||||||||||||
| 211 | =cut | ||||||||||||
| 212 | |||||||||||||
| 213 | sub new_cycle { | ||||||||||||
| 214 | 0 | ▶ | my $i = 0; | ||||||||||
|
Subroutine
new_cycle: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 215 | 0 | ▶ | sub { $_[$i++ % scalar @_] }; # returns | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 216 | } | ||||||||||||
| 217 | |||||||||||||
| 218 | =head1 INTERNAL(?) METHODS | ||||||||||||
| 219 | |||||||||||||
| 220 | =head2 _p( $expression ) | ||||||||||||
| 221 | |||||||||||||
| 222 | Wraps a Perl expression in a customized wrapper which will be processed | ||||||||||||
| 223 | by the Tenjin preprocessor and replaced with the standard [== $expression =]. | ||||||||||||
| 224 | |||||||||||||
| 225 | =cut | ||||||||||||
| 226 | |||||||||||||
| 227 | sub _p { | ||||||||||||
| 228 | 1 | ▶ | "<`\#$_[0]\#`>"; | ||||||||||
|
Subroutine
_p: called CC=1 SCAR=0.0
| |||||||||||||
| 229 | } | ||||||||||||
| 230 | |||||||||||||
| 231 | =head2 _P( $expression ) | ||||||||||||
| 232 | |||||||||||||
| 233 | Wrap a Perl expression in a customized wrapper which will be processed | ||||||||||||
| 234 | by the Tenjin preprocessor and replaced with the standard [= $expression =], | ||||||||||||
| 235 | which means the expression will be escaped. | ||||||||||||
| 236 | |||||||||||||
| 237 | =cut | ||||||||||||
| 238 | |||||||||||||
| 239 | sub _P { | ||||||||||||
| 240 | 1 | ▶ | "<`\$$_[0]\$`>"; | ||||||||||
|
Subroutine
_P: called CC=1 SCAR=0.0
| |||||||||||||
| 241 | } | ||||||||||||
| 242 | |||||||||||||
| 243 | =head2 _decode_params( $s ) | ||||||||||||
| 244 | |||||||||||||
| 245 | =cut | ||||||||||||
| 246 | |||||||||||||
| 247 | sub _decode_params { | ||||||||||||
| 248 | 0 | ▶ | my $s = shift; | ||||||||||
|
Subroutine
_decode_params: not called CC=2 SCAR=17.9
| |||||||||||||
| 249 | |||||||||||||
| 250 | 0 | ▶ | return '' unless $s; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 251 | |||||||||||||
| 252 | 0 | $s =~ s/%3C%60%23(.*?)%23%60%3E/'[=='.decode_url($1).'=]'/ge; | |||||||||||
| 253 | 0 | $s =~ s/%3C%60%24(.*?)%24%60%3E/'[='.decode_url($1).'=]'/ge; | |||||||||||
| 254 | 0 | $s =~ s/<`\#(.*?)\#`>/'[=='.unescape_xml($1).'=]'/ge; | |||||||||||
| 255 | 0 | $s =~ s/<`\$(.*?)\$`>/'[='.unescape_xml($1).'=]'/ge; | |||||||||||
| 256 | 0 | $s =~ s/<`\#(.*?)\#`>/[==$1=]/g; | |||||||||||
| 257 | 0 | $s =~ s/<`\$(.*?)\$`>/[=$1=]/g; | |||||||||||
| 258 | |||||||||||||
| 259 | 0 | return $s; | |||||||||||
| 260 | } | ||||||||||||
| 261 | |||||||||||||
| 262 | 8 | 1; | |||||||||||
| 263 | |||||||||||||
| 264 | =head1 SEE ALSO | ||||||||||||
| 265 | |||||||||||||
| 266 | L<Tenjin>, L<Tenjin::Template>, L<Tenjin::Context>. | ||||||||||||
| 267 | |||||||||||||
| 268 | =head1 AUTHOR, LICENSE AND COPYRIGHT | ||||||||||||
| 269 | |||||||||||||
| 270 | See L<Tenjin>. | ||||||||||||
| 271 | |||||||||||||
| 272 | =cut | ||||||||||||