lib/Text/SmartyPants.pm

filter: Statementstmt 42.2% 95 / 225 Branchbran 24.1% 29 / 120 Conditioncond 15.3% 4 / 26 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 52.9% 9 / 17 Podpod 100% 14 / 14 totaltotal 36.2% 152 / 419 CC 75 SCAR 52.2
CC
75
Cov
35%
SmartQuotes
306.0
process
247.2
SmartDashes
132.0
CRAP 185.6
↑ Summary lib/MojoMojo/Controller/Admin.pm »
1 package Text::SmartyPants;
2 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 27 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 $VERSION = "1.3";
5  
6 # Configurable variables:
7 27 my $smartypants_attr = "1";
8  
9 # 1 => "--" for em-dashes; no en-dash support
10 # 2 => "---" for em-dashes; "--" for en-dashes
11 # 3 => "--" for em-dashes; "---" for en-dashes
12 # See docs for more configuration options.
13  
14 # Globals:
15 27 my $tags_to_skip = qr!<(/?)(?:pre|code|kbd|script)[\s>]!;
16  
17 =head1 Methods
18  
19 =head2 process
20  
21 Do the bulk of the conversion work.
22  
23 =cut
24  
25 36 ▶sub process {
Subroutine process: called CC=38 SCAR=55.1
Pod documented
26 36 ▶ shift if ( $_[0] eq __PACKAGE__ ); # oops, called in OOP fashion.
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq __PACKAGE__ 36 0 36
27  
28 # Paramaters:
29 36 my $text = shift; # text to be parsed
30  
31 # value of the smart_quotes="" attribute. Default to 'everything on'
32 36 ▶ my $attr = shift || '1';
Condition50%1/2
shift() || '1'
l!l
036
MC/DC0%0/1
shift() || '1'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
33  
34 # Options to specify which transformations to make:
35 36 my ( $do_quotes, $do_backticks, $do_dashes, $do_ellipses, $do_stupefy );
36  
37 # should we translate &quot; entities into normal quotes?
38 36 my $convert_quot = 0;
39  
40 # Parse attributes:
41 # 0 : do nothing
42 # 1 : set all
43 # 2 : set all, using old school en- and em- dash shortcuts
44 # 3 : set all, using inverted old school en and em- dash shortcuts
45 #
46 # q : quotes
47 # b : backtick quotes (``double'' only)
48 # B : backtick quotes (``double'' and `single')
49 # d : dashes
50 # D : old school dashes
51 # i : inverted old school dashes
52 # e : ellipses
53 # w : convert &quot; entities to " for Dreamweaver users
54  
55 36 ▶ if ( $attr eq "0" ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($attr eq '0') { } 0 36 36
elsif ($attr eq '1') { } 36 0 36
elsif ($attr eq '2') { } 0 0 0
elsif ($attr eq '3') { } 0 0 0
elsif ($attr eq '-1') { } 0 0 0
56  
57 # Do nothing.
58 0 return $text;
59 }
60 elsif ( $attr eq "1" ) {
61  
62 # Do everything, turn all options on.
63 36 $do_quotes = 1;
64 36 $do_backticks = 1;
65 36 $do_dashes = 1;
66 36 $do_ellipses = 1;
67 }
68 elsif ( $attr eq "2" ) {
69  
70 # Do everything, turn all options on, use old school dash shorthand.
71 0 $do_quotes = 1;
72 0 $do_backticks = 1;
73 0 $do_dashes = 2;
74 0 $do_ellipses = 1;
75 }
76 elsif ( $attr eq "3" ) {
77  
78 # Do everything, turn all options on, use inverted old school dash shorthand.
79 0 $do_quotes = 1;
80 0 $do_backticks = 1;
81 0 $do_dashes = 3;
82 0 $do_ellipses = 1;
83 }
84 elsif ( $attr eq "-1" ) {
85  
86 # Special "stupefy" mode.
87 0 $do_stupefy = 1;
88 }
89 else {
90 0 my @chars = split( //, $attr );
91 0 foreach my $c (@chars) {
92 0 ▶ if ( $c eq "q" ) { $do_quotes = 1; }
Branch: 8 branches
BranchTrueFalseTotal
if ($c eq 'q') { } 0 0 0
elsif ($c eq 'b') { } 0 0 0
elsif ($c eq 'B') { } 0 0 0
elsif ($c eq 'd') { } 0 0 0
elsif ($c eq 'D') { } 0 0 0
elsif ($c eq 'i') { } 0 0 0
elsif ($c eq 'e') { } 0 0 0
elsif ($c eq 'w') { } 0 0 0
93 0 elsif ( $c eq "b" ) { $do_backticks = 1; }
94 0 elsif ( $c eq "B" ) { $do_backticks = 2; }
95 0 elsif ( $c eq "d" ) { $do_dashes = 1; }
96 0 elsif ( $c eq "D" ) { $do_dashes = 2; }
97 0 elsif ( $c eq "i" ) { $do_dashes = 3; }
98 0 elsif ( $c eq "e" ) { $do_ellipses = 1; }
99 0 elsif ( $c eq "w" ) { $convert_quot = 1; }
100 else {
101  
102 # Unknown attribute option, ignore.
103 }
104 }
105 }
106  
107 36 ▶ my $tokens ||= _tokenize($text);
Condition33%1/3
my $tokens ||= _tokenize($text)
l!l&&r!l&&!r
0360
MC/DC0%0/2
my $tokens ||= _tokenize($text)
my $tokens _tokenize($text)
Truth table33%1/3
A ||= B
A= my $tokensB= _tokenize($text)
ABresult
1X1
011
000
108 36 my $result = '';
109 36 my $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags.
110  
111 36 my $prev_token_last_char = ""; # This is a cheat, used to get some context
112 # for one-character tokens that consist of
113 # just a quote char. What we do is remember
114 # the last character of the previous text
115 # token, to use as context to curl single-
116 # character quote tokens correctly.
117  
118 36 foreach my $cur_token (@$tokens) {
119 400 ▶ if ( $cur_token->[0] eq "tag" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[0] eq 'tag') { } 244 156 400
120  
121 # Don't mess with quotes inside tags.
122 244 $result .= $cur_token->[1];
123 244 ▶ if ( $cur_token->[1] =~ m/$tags_to_skip/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[1] =~ /$tags_to_skip/) 39 205 244
124 39 ▶ $in_pre = defined $1 && $1 eq '/' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 && $1 eq '/' ? : 19 20 39
Condition66%2/3
defined $1 and $1 eq "/"
!ll&&!rl&&r
02019
MC/DC50%1/2
defined $1 and $1 eq "/"
defined $1 $1 eq "/"
Truth table66%2/3
A and B
A= defined $1B= $1 eq "/"
ABresult
0X0
100
111
125 }
126 }
127 else {
128 156 my $t = $cur_token->[1];
129 156 my $last_char = substr( $t, -1 ); # Remember last char of this token before processing.
130 156 ▶ if ( !$in_pre ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_pre) 92 64 156
131 92 $t = ProcessEscapes($t);
132  
133 92 ▶ if ($convert_quot) {
Branch: 1 branch
BranchTrueFalseTotal
if ($convert_quot) 0 92 92
134 0 $t =~ s/&quot;/"/g;
135 }
136  
137 92 ▶ if ($do_dashes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_dashes) 92 0 92
138 92 ▶ $t = EducateDashes($t) if ( $do_dashes == 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $do_dashes == 1 92 0 92
139 92 ▶ $t = EducateDashesOldSchool($t) if ( $do_dashes == 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $do_dashes == 2 0 92 92
140 92 ▶ $t = EducateDashesOldSchoolInverted($t) if ( $do_dashes == 3 );
Branch: 1 branch
BranchTrueFalseTotal
if $do_dashes == 3 0 92 92
141 }
142  
143 92 ▶ $t = EducateEllipses($t) if $do_ellipses;
Branch: 1 branch
BranchTrueFalseTotal
if $do_ellipses 92 0 92
144  
145 # Notes: backticks need to be processed before quotes.
146 92 ▶ if ($do_backticks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_backticks) 92 0 92
147 92 $t = EducateBackticks($t);
148 92 ▶ $t = EducateSingleBackticks($t) if ( $do_backticks == 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $do_backticks == 2 0 92 92
149 }
150  
151 92 ▶ if ($do_quotes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_quotes) 92 0 92
152 92 ▶ if ( $t eq q/'/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq q[']) { } 0 92 92
elsif ($t eq '"') { } 0 92 92
153  
154 # Special case: single-character ' token
155 0 ▶ if ( $prev_token_last_char =~ m/\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_token_last_char =~ /\S/) { } 0 0 0
156 0 $t = "&#8217;";
157 }
158 else {
159 0 $t = "&#8216;";
160 }
161 }
162 elsif ( $t eq q/"/ ) {
163  
164 # Special case: single-character " token
165 0 ▶ if ( $prev_token_last_char =~ m/\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_token_last_char =~ /\S/) { } 0 0 0
166 0 $t = "&#8221;";
167 }
168 else {
169 0 $t = "&#8220;";
170 }
171 }
172 else {
173  
174 # Normal case:
175 92 $t = EducateQuotes($t);
176 }
177 }
178  
179 92 ▶ $t = StupefyEntities($t) if $do_stupefy;
Branch: 1 branch
BranchTrueFalseTotal
if $do_stupefy 0 92 92
180 }
181 156 $prev_token_last_char = $last_char;
182 156 $result .= $t;
183 }
184 }
185  
186 36 return $result;
187 }
188  
189 =head2 SmartQuotes
190  
191 Quotes to entities.
192  
193 =cut
194  
195 0 ▶sub SmartQuotes {
Subroutine SmartQuotes: not called CC=17 SCAR=57.2
Pod documented
196  
197 # Paramaters:
198 0 my $text = shift; # text to be parsed
199 0 my $attr = shift; # value of the smart_quotes="" attribute
200  
201 0 my $do_backticks; # should we educate ``backticks'' -style quotes?
202  
203 0 ▶ if ( $attr == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr == 0) { } 0 0 0
elsif ($attr == 2) { } 0 0 0
204  
205 # do nothing;
206 0 return $text;
207 }
208 elsif ( $attr == 2 ) {
209  
210 # smarten ``backticks'' -style quotes
211 0 $do_backticks = 1;
212 }
213 else {
214 0 $do_backticks = 0;
215 }
216  
217 # Special case to handle quotes at the very end of $text when preceded by
218 # an HTML tag. Add a space to give the quote education algorithm a bit of
219 # context, so that it can guess correctly that it's a closing quote:
220 0 my $add_extra_space = 0;
221 0 ▶ if ( $text =~ m/>['"]\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ />['"]\z/) 0 0 0
222 0 $add_extra_space = 1; # Remember, so we can trim the extra space later.
223 0 $text .= " ";
224 }
225  
226 0 ▶ my $tokens ||= _tokenize($text);
Condition0%0/3
my $tokens ||= _tokenize($text)
l!l&&r!l&&!r
000
MC/DC0%0/2
my $tokens ||= _tokenize($text)
my $tokens _tokenize($text)
Truth table0%0/3
A ||= B
A= my $tokensB= _tokenize($text)
ABresult
1X1
011
000
227 0 my $result = '';
228 0 my $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags
229  
230 0 my $prev_token_last_char = ""; # This is a cheat, used to get some context
231 # for one-character tokens that consist of
232 # just a quote char. What we do is remember
233 # the last character of the previous text
234 # token, to use as context to curl single-
235 # character quote tokens correctly.
236  
237 0 foreach my $cur_token (@$tokens) {
238 0 ▶ if ( $cur_token->[0] eq "tag" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[0] eq 'tag') { } 0 0 0
239  
240 # Don't mess with quotes inside tags
241 0 $result .= $cur_token->[1];
242 0 ▶ if ( $cur_token->[1] =~ m/$tags_to_skip/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[1] =~ /$tags_to_skip/) 0 0 0
243 0 ▶ $in_pre = defined $1 && $1 eq '/' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 && $1 eq '/' ? : 0 0 0
Condition0%0/3
defined $1 and $1 eq "/"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $1 and $1 eq "/"
defined $1 $1 eq "/"
Truth table0%0/3
A and B
A= defined $1B= $1 eq "/"
ABresult
0X0
100
111
244 }
245 }
246 else {
247 0 my $t = $cur_token->[1];
248 0 my $last_char = substr( $t, -1 ); # Remember last char of this token before processing.
249 0 ▶ if ( !$in_pre ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_pre) 0 0 0
250 0 $t = ProcessEscapes($t);
251 0 ▶ if ($do_backticks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_backticks) 0 0 0
252 0 $t = EducateBackticks($t);
253 }
254  
255 0 ▶ if ( $t eq q/'/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq q[']) { } 0 0 0
elsif ($t eq '"') { } 0 0 0
256  
257 # Special case: single-character ' token
258 0 ▶ if ( $prev_token_last_char =~ m/\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_token_last_char =~ /\S/) { } 0 0 0
259 0 $t = "&#8217;";
260 }
261 else {
262 0 $t = "&#8216;";
263 }
264 }
265 elsif ( $t eq q/"/ ) {
266  
267 # Special case: single-character " token
268 0 ▶ if ( $prev_token_last_char =~ m/\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_token_last_char =~ /\S/) { } 0 0 0
269 0 $t = "&#8221;";
270 }
271 else {
272 0 $t = "&#8220;";
273 }
274 }
275 else {
276  
277 # Normal case:
278 0 $t = EducateQuotes($t);
279 }
280  
281 }
282 0 $prev_token_last_char = $last_char;
283 0 $result .= $t;
284 }
285 }
286  
287 0 ▶ if ($add_extra_space) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add_extra_space) 0 0 0
288 0 $result =~ s/ \z//; # Trim trailing space if we added one earlier.
289 }
290 0 return $result;
291 }
292  
293 =head2 SmartDashes
294  
295 Call the individual dash conversion to entities functions.
296  
297 =cut
298  
299 0 ▶sub SmartDashes {
Subroutine SmartDashes: not called CC=11 SCAR=48.8
Pod documented
300  
301 # Paramaters:
302 0 my $text = shift; # text to be parsed
303 0 my $attr = shift; # value of the smart_dashes="" attribute
304  
305 # reference to the subroutine to use for dash education, default to EducateDashes:
306 0 my $dash_sub_ref = \&EducateDashes;
307  
308 0 ▶ if ( $attr == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($attr == 0) { } 0 0 0
elsif ($attr == 2) { } 0 0 0
elsif ($attr == 3) { } 0 0 0
309  
310 # do nothing;
311 0 return $text;
312 }
313 elsif ( $attr == 2 ) {
314  
315 # use old smart dash shortcuts, "--" for en, "---" for em
316 0 $dash_sub_ref = \&EducateDashesOldSchool;
317 }
318 elsif ( $attr == 3 ) {
319  
320 # inverse of 2, "--" for em, "---" for en
321 0 $dash_sub_ref = \&EducateDashesOldSchoolInverted;
322 }
323  
324 0 my $tokens;
325 0 ▶ $tokens ||= _tokenize($text);
Condition0%0/3
$tokens ||= _tokenize($text)
l!l&&r!l&&!r
000
MC/DC0%0/2
$tokens ||= _tokenize($text)
$tokens _tokenize($text)
Truth table0%0/3
A ||= B
A= $tokensB= _tokenize($text)
ABresult
1X1
011
000
326  
327 0 my $result = '';
328 0 my $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags
329 0 foreach my $cur_token (@$tokens) {
330 0 ▶ if ( $cur_token->[0] eq "tag" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[0] eq 'tag') { } 0 0 0
331  
332 # Don't mess with quotes inside tags
333 0 $result .= $cur_token->[1];
334 0 ▶ if ( $cur_token->[1] =~ m/$tags_to_skip/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[1] =~ /$tags_to_skip/) 0 0 0
335 0 ▶ $in_pre = defined $1 && $1 eq '/' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 && $1 eq '/' ? : 0 0 0
Condition0%0/3
defined $1 and $1 eq "/"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $1 and $1 eq "/"
defined $1 $1 eq "/"
Truth table0%0/3
A and B
A= defined $1B= $1 eq "/"
ABresult
0X0
100
111
336 }
337 }
338 else {
339 0 my $t = $cur_token->[1];
340 0 ▶ if ( !$in_pre ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_pre) 0 0 0
341 0 $t = ProcessEscapes($t);
342 0 $t = $dash_sub_ref->($t);
343 }
344 0 $result .= $t;
345 }
346 }
347 0 return $result;
348 }
349  
350 =head2 SmartEllipses
351  
352 Call the individual ellipse conversion to entities functions.
353  
354 =cut
355  
356 0 ▶sub SmartEllipses {
Subroutine SmartEllipses: not called CC=9 SCAR=45.0
Pod documented
357  
358 # Paramaters:
359 0 my $text = shift; # text to be parsed
360 0 my $attr = shift; # value of the smart_ellipses="" attribute
361  
362 0 ▶ if ( $attr == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr == 0) 0 0 0
363  
364 # do nothing;
365 0 return $text;
366 }
367  
368 0 my $tokens;
369 0 ▶ $tokens ||= _tokenize($text);
Condition0%0/3
$tokens ||= _tokenize($text)
l!l&&r!l&&!r
000
MC/DC0%0/2
$tokens ||= _tokenize($text)
$tokens _tokenize($text)
Truth table0%0/3
A ||= B
A= $tokensB= _tokenize($text)
ABresult
1X1
011
000
370  
371 0 my $result = '';
372 0 my $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags
373 0 foreach my $cur_token (@$tokens) {
374 0 ▶ if ( $cur_token->[0] eq "tag" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[0] eq 'tag') { } 0 0 0
375  
376 # Don't mess with quotes inside tags
377 0 $result .= $cur_token->[1];
378 0 ▶ if ( $cur_token->[1] =~ m/$tags_to_skip/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[1] =~ /$tags_to_skip/) 0 0 0
379 0 ▶ $in_pre = defined $1 && $1 eq '/' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 && $1 eq '/' ? : 0 0 0
Condition0%0/3
defined $1 and $1 eq "/"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $1 and $1 eq "/"
defined $1 $1 eq "/"
Truth table0%0/3
A and B
A= defined $1B= $1 eq "/"
ABresult
0X0
100
111
380 }
381 }
382 else {
383 0 my $t = $cur_token->[1];
384 0 ▶ if ( !$in_pre ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_pre) 0 0 0
385 0 $t = ProcessEscapes($t);
386 0 $t = EducateEllipses($t);
387 }
388 0 $result .= $t;
389 }
390 }
391 0 return $result;
392 }
393  
394 =head2 EducateQuotes
395  
396 Parameter: String.
397  
398 Returns: The string, with "educated" curly quote HTML entities.
399  
400 Example input: "Isn't this fun?"
401 Example output: &#8220;Isn&#8217;t this fun?&#8221;
402  
403 =cut
404  
405 92 ▶sub EducateQuotes {
Subroutine EducateQuotes: called CC=1 SCAR=0.0
Pod documented
406 92 local $_ = shift;
407  
408 # Tell perl not to gripe when we use $1 in substitutions,
409 # even when it's undefined. Use $^W instead of "no warnings"
410 # for compatibility with Perl 5.005:
411 92 local $^W = 0;
412  
413 # Make our own "punctuation" character class, because the POSIX-style
414 # [:PUNCT:] is only available in Perl 5.6 or later:
415 92 my $punct_class = qr/[!"#\$\%'()*+,-.\/:;<=>?\@\[\\\]\^_`{|}~]/;
416  
417 # Special case if the very first character is a quote
418 # followed by punctuation at a non-word-break. Close the quotes by brute force:
419 92 s/^'(?=$punct_class\B)/&#8217;/;
420 92 s/^"(?=$punct_class\B)/&#8221;/;
421  
422 # Special case for double sets of quotes, e.g.:
423 # <p>He said, "'Quoted' words in a larger quote."</p>
424 92 s/"'(?=\w)/&#8220;&#8216;/g;
425 92 s/'"(?=\w)/&#8216;&#8220;/g;
426  
427 92 my $close_class = qr![^\ \t\r\n\[\{\(]!;
428  
429 # Single closing quotes:
430 92 s {
431 ($close_class)?
432 '
433 (?(1)| # If $1 captured, then do nothing;
434 (?=\s | s\b) # otherwise, positive lookahead for a whitespace
435 ) # char or an 's' at a word ending position. This
436 # is a special case to handle something like:
437 # "<i>Custer</i>'s Last Stand."
438 } {$1&#8217;}xgi;
439  
440 # Single opening quotes:
441 92 s/'/&#8216;/g;
442  
443 # Double closing quotes:
444 92 s {
445 ($close_class)?
446 "
447 (?(1)|(?=\s)) # If $1 captured, then do nothing;
448 # if not, then make sure the next char is whitespace.
449 } {$1&#8221;}xg;
450  
451 # Double opening quotes:
452 92 s/"/&#8220;/g;
453  
454 92 return $_;
455 }
456  
457 =head2 EducateBackticks
458  
459 Replace double (back)ticks w/ HTML entities.
460  
461 =cut
462  
463 92 ▶sub EducateBackticks {
Subroutine EducateBackticks: called CC=1 SCAR=0.0
Pod documented
464  
465 #
466 # Parameter: String.
467 # Returns: The string, with ``backticks'' -style double quotes
468 # translated into HTML curly quote entities.
469 #
470 # Example input: ``Isn't this fun?''
471 # Example output: &#8220;Isn't this fun?&#8221;
472 #
473  
474 92 local $_ = shift;
475 92 s/``/&#8220;/g;
476 92 s/''/&#8221;/g;
477 92 return $_;
478 }
479  
480 =head2 EducateSingleBackticks
481  
482 Replace single (back)ticks w/ HTML entities.
483  
484 =cut
485  
486 0 ▶sub EducateSingleBackticks {
Subroutine EducateSingleBackticks: not called CC=1 SCAR=6.9
Pod documented
487  
488 #
489 # Parameter: String.
490 # Returns: The string, with `backticks' -style single quotes
491 # translated into HTML curly quote entities.
492 #
493 # Example input: `Isn't this fun?'
494 # Example output: &#8216;Isn&#8217;t this fun?&#8217;
495 #
496  
497 0 local $_ = shift;
498 0 s/`/&#8216;/g;
499 0 s/'/&#8217;/g;
500 0 return $_;
501 }
502  
503 =head2 EducateDashes
504  
505 Dashes to HTML entity
506  
507 Parameter: String.
508  
509 Returns: The string, with each instance of "--" translated to
510 an em-dash HTML entity.
511  
512 =cut
513  
514 92 ▶sub EducateDashes {
Subroutine EducateDashes: called CC=1 SCAR=0.0
Pod documented
515  
516 92 local $_ = shift;
517 92 s/--/&#8212;/g;
518 92 return $_;
519 }
520  
521 =head2 EducateDashesOldSchool
522  
523 Dashes to entities.
524  
525  
526 Parameter: String.
527  
528 Returns: The string, with each instance of "--" translated to
529 an en-dash HTML entity, and each "---" translated to
530 an em-dash HTML entity.
531  
532  
533 =cut
534  
535 0 ▶sub EducateDashesOldSchool {
Subroutine EducateDashesOldSchool: not called CC=1 SCAR=6.9
Pod documented
536  
537 0 local $_ = shift;
538 0 s/---/&#8212;/g; # em
539 0 s/--/&#8211;/g; # en
540 0 return $_;
541 }
542  
543 =head2 EducateDashesOldSchoolInverted
544  
545  
546 Parameter: String.
547  
548 Returns: The string, with each instance of "--" translated to
549 an em-dash HTML entity, and each "---" translated to
550 an en-dash HTML entity. Two reasons why: First, unlike the
551 en- and em-dash syntax supported by
552 EducateDashesOldSchool(), it's compatible with existing
553 entries written before SmartyPants 1.1, back when "--" was
554 only used for em-dashes. Second, em-dashes are more
555 common than en-dashes, and so it sort of makes sense that
556 the shortcut should be shorter to type. (Thanks to Aaron
557 Swartz for the idea.)
558  
559
560 =cut
561  
562 0 ▶sub EducateDashesOldSchoolInverted {
Subroutine EducateDashesOldSchoolInverted: not called CC=1 SCAR=6.9
Pod documented
563  
564 0 local $_ = shift;
565 0 s/---/&#8211;/g; # en
566 0 s/--/&#8212;/g; # em
567 0 return $_;
568 }
569  
570 =head2 EducateEllipses
571  
572 Parameter: String.
573 Returns: The string, with each instance of "..." translated to
574 an ellipsis HTML entity.
575  
576 Example input: Huh...?
577 Example output: Huh&#8230;?
578  
579 =cut
580  
581 92 ▶sub EducateEllipses {
Subroutine EducateEllipses: called CC=1 SCAR=0.0
Pod documented
582  
583 92 local $_ = shift;
584 92 s/\.\.\./&#8230;/g;
585 92 return $_;
586 }
587  
588 =head2 StupefyEntities
589  
590 Parameter: String.
591 Returns: The string, with each SmartyPants HTML entity translated to
592 its ASCII counterpart.
593  
594 Example input: &#8220;Hello &#8212; world.&#8221;
595 Example output: "Hello -- world."
596  
597 =cut
598  
599 0 ▶sub StupefyEntities {
Subroutine StupefyEntities: not called CC=1 SCAR=6.9
Pod documented
600  
601 0 local $_ = shift;
602  
603 0 s/&#8211;/-/g; # en-dash
604 0 s/&#8212;/--/g; # em-dash
605  
606 0 s/&#8216;/'/g; # open single quote
607 0 s/&#8217;/'/g; # close single quote
608  
609 0 s/&#8220;/"/g; # open double quote
610 0 s/&#8221;/"/g; # close double quote
611  
612 0 s/&#8230;/.../g; # ellipsis
613  
614 0 return $_;
615 }
616  
617 =head2 SmartyPantsVersion
618  
619 Return the version of SmartyPants.
620  
621 =cut
622  
623 0 ▶sub SmartyPantsVersion {
Subroutine SmartyPantsVersion: not called CC=1 SCAR=6.9
Pod documented
624 0 return $VERSION;
625 }
626  
627 =head2 ProcessEscapes
628  
629 Parameter: String.
630 Returns: The string, with after processing the following backslash
631 escape sequences. This is useful if you want to force a "dumb"
632 quote or other character to appear.
633  
634 Escape Value
635 ------ -----
636 \\ &#92;
637 \" &#34;
638 \' &#39;
639 \. &#46;
640 \- &#45;
641 \` &#96;
642  
643 =cut
644  
645 92 ▶sub ProcessEscapes {
Subroutine ProcessEscapes: called CC=1 SCAR=0.0
Pod documented
646  
647 92 local $_ = shift;
648  
649 92 s! \\\\ !&#92;!gx;
650 92 s! \\" !&#34;!gx;
651 92 s! \\' !&#39;!gx;
652 92 s! \\\. !&#46;!gx;
653 92 s! \\- !&#45;!gx;
654 92 s! \\` !&#96;!gx;
655  
656 92 return $_;
657 }
658  
659 36 ▶sub _tokenize {
Subroutine _tokenize: called CC=4 SCAR=13.9
660  
661 #
662 # Parameter: String containing HTML markup.
663 # Returns: Reference to an array of the tokens comprising the input
664 # string. Each token is either a tag (possibly with nested,
665 # tags contained therein, such as <a href="<MTFoo>">, or a
666 # run of text between tags. Each element of the array is a
667 # two-element array; the first is either 'tag' or 'text';
668 # the second is the actual value.
669 #
670 #
671 # Based on the _tokenize() subroutine from Brad Choate's MTRegex plugin.
672 # <http://www.bradchoate.com/past/mtregex.php>
673 #
674  
675 36 my $str = shift;
676  
677 36 my $pos = 0;
678 36 my $len = length $str;
679 36 my @tokens;
680  
681 # pattern to match balanced nested <> pairs, up to two levels deep:
682 36 my $nested_angles = qr/<(?:[^<>]|<[^<>]*>)*>/;
683  
684 36 ▶ while ( $str =~ m/($nested_angles)/gs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /($nested_angles)/gs) 244 36 280
685 244 my $whole_tag = $1;
686 244 my $sec_start = pos $str;
687 244 my $tag_start = $sec_start - length $whole_tag;
688 244 ▶ if ( $pos < $tag_start ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $tag_start) 152 92 244
689 152 push @tokens, [ 'text', substr( $str, $pos, $tag_start - $pos ) ];
690 }
691 244 push @tokens, [ 'tag', $whole_tag ];
692 244 $pos = pos $str;
693 }
694 36 ▶ push @tokens, [ 'text', substr( $str, $pos, $len - $pos ) ] if $pos < $len;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < $len 4 32 36
695 36 \@tokens;
696 }
697  
698 27 1;
699 __END__
↑ Summary lib/MojoMojo/Controller/Admin.pm »