blib/lib/Text/Typography.pm

filter: Statementstmt 6.6% 16 / 239 Branchbran 0.0% 0 / 120 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 21.0% 4 / 19 Podpod 7.1% 1 / 14 totaltotal 4.8% 21 / 432 CC 74 SCAR 64.0
CC
74
Cov
4%
SmartyPants
1332.0
SmartQuotes
306.0
SmartDashes
132.0
CRAP 603.6
↑ Summary
1 package Text::Typography;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use vars qw($VERSION @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 $VERSION = '0.01';
10 1 @EXPORT_OK = qw(typography);
11  
12 =head1 NAME
13  
14 Text::Typography - Markup ASCII text with correct typography for HTML
15  
16 =head1 SYNOPSIS
17  
18 use Text::Typography qw(typography);
19
20 print typography($text);
21  
22 =head1 DESCRIPTION
23  
24 This module is a thin wrapper for John Gruber's SmartyPants plugin for various
25 CMSs.
26  
27 SmartyPants is a web publishing utility that translates plain ASCII
28 punctuation characters into "smart" typographic punctuation HTML
29 entities. SmartyPants can perform the following transformations:
30  
31 =over 4
32  
33 =item *
34  
35 Straight quotes ( " and ' ) into "curly" quote HTML entities
36  
37 =item *
38  
39 Backticks-style quotes (``like this'') into "curly" quote HTML entities
40  
41 =item *
42  
43 Dashes (C<--> and C<--->) into en- and em-dash entities
44  
45 =item *
46  
47 Three consecutive dots (C<...>) into an ellipsis entity
48  
49 =back
50  
51 SmartyPants does not modify characters within C<< <pre> >>, C<< <code> >>,
52 C<< <kbd> >>, C<< <script> >>, or C<< <math> >> tag blocks.
53 Typically, these tags are used to display text where smart quotes and
54 other "smart punctuation" would not be appropriate, such as source code
55 or example markup.
56  
57  
58 =head2 typography($text[, $attributes])
59  
60 Returns a string marked up with the proper HTML entities for proper
61 typography.
62  
63 For fine grain control over what gets converted, use the C<$attributes>
64 option. The default value is 3.
65  
66 The following numeric values set a group of options:
67  
68 0 : do nothing
69 1 : set all
70 2 : set all, using old school en- and em- dash shortcuts (-- and ---)
71 3 : set all, using inverted old school en- and em- dash shortcuts (--- and --)
72  
73 For even finer control, specify a string of one or more of the
74 following characters:
75  
76 q : quotes
77 b : backtick quotes (``double'' only)
78 B : backtick quotes (``double'' and `single')
79 d : dashes
80 D : old school dashes
81 i : inverted old school dashes
82 e : ellipses
83 w : convert &quot; entities to " for Dreamweaver users
84  
85 =head2 Backslash Escapes
86  
87 If you need to use literal straight quotes (or plain hyphens and
88 periods), SmartyPants accepts the following backslash escape sequences
89 to force non-smart punctuation. It does so by transforming the escape
90 sequence into a decimal-encoded HTML entity:
91  
92 Escape Value Character
93 ------ ----- ---------
94 \\ &#92; \
95 \" &#34; "
96 \' &#39; '
97 \. &#46; .
98 \- &#45; -
99 \` &#96; `
100  
101 This is useful, for example, when you want to use straight quotes as
102 foot and inch marks: 6'2" tall; a 17" iMac.
103  
104 =head2 Algorithmic Shortcomings
105  
106 One situation in which quotes will get curled the wrong way is when
107 apostrophes are used at the start of leading contractions. For example:
108  
109 'Twas the night before Christmas.
110  
111 In the case above, SmartyPants will turn the apostrophe into an opening
112 single-quote, when in fact it should be a closing one. I don't think
113 this problem can be solved in the general case -- every word processor
114 I've tried gets this wrong as well. In such cases, it's best to use the
115 proper HTML entity for closing single-quotes (C<&#8217;>) by hand.
116  
117 =head1 AUTHOR
118  
119 Thomas Sibley created this module using the code from the SmartyPants
120 CMS plugin by John Gruber (L<http://daringfireball.net/projects/smartypants/>).
121  
122 =head1 COPYRIGHT AND LICENSE
123  
124 Copyright (c) 2003 John Gruber
125 (http://daringfireball.net/)
126 All rights reserved.
127  
128 Redistribution and use in source and binary forms, with or without
129 modification, are permitted provided that the following conditions are met:
130  
131 * Redistributions of source code must retain the above copyright
132 notice, this list of conditions and the following disclaimer.
133  
134 * Redistributions in binary form must reproduce the above copyright
135 notice, this list of conditions and the following disclaimer in the
136 documentation and/or other materials provided with the distribution.
137  
138 * Neither the name "SmartyPants" nor the names of its contributors may
139 be used to endorse or promote products derived from this software
140 without specific prior written permission.
141  
142 This software is provided by the copyright holders and contributors "as is"
143 and any express or implied warranties, including, but not limited to, the
144 implied warranties of merchantability and fitness for a particular purpose
145 are disclaimed. In no event shall the copyright owner or contributors be
146 liable for any direct, indirect, incidental, special, exemplary, or
147 consequential damages (including, but not limited to, procurement of
148 substitute goods or services; loss of use, data, or profits; or business
149 interruption) however caused and on any theory of liability, whether in
150 contract, strict liability, or tort (including negligence or otherwise)
151 arising in any way out of the use of this software, even if advised of the
152 possibility of such damage.
153  
154 =cut
155  
156 0 ▶sub typography {
Subroutine typography: not called CC=2 SCAR=17.9
Pod documented
157 0 my $text = shift;
158 0 ▶ my $attr = defined $_[0] ? shift : 3;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
159 0 return SmartyPants($text, $attr);
160 }
161  
162 #
163 # Beginning of slightly modified selected code from SmartyPants
164 #
165  
166 # Globals:
167 1 my $tags_to_skip = qr!<(/?)(?:pre|code|kbd|script|math)[\s>]!; # a / for vim
168  
169 0 ▶sub SmartyPants {
Subroutine SmartyPants: not called CC=36 SCAR=71.9
Pod undocumented
170 # Paramaters:
171 0 my $text = shift; # text to be parsed
172 0 my $attr = shift; # value of the smart_quotes="" attribute
173  
174 # Options to specify which transformations to make:
175 0 my ($do_quotes, $do_backticks, $do_dashes, $do_ellipses, $do_stupefy);
176 0 my $convert_quot = 0; # should we translate &quot; entities into normal quotes?
177  
178 # Parse attributes:
179 # 0 : do nothing
180 # 1 : set all
181 # 2 : set all, using old school en- and em- dash shortcuts
182 # 3 : set all, using inverted old school en and em- dash shortcuts
183 #
184 # q : quotes
185 # b : backtick quotes (``double'' only)
186 # B : backtick quotes (``double'' and `single')
187 # d : dashes
188 # D : old school dashes
189 # i : inverted old school dashes
190 # e : ellipses
191 # w : convert &quot; entities to " for Dreamweaver users
192  
193 0 ▶ if ($attr eq "0") {
Branch: 5 branches
BranchTrueFalseTotal
if ($attr eq '0') { } 0 0 0
elsif ($attr eq '1') { } 0 0 0
elsif ($attr eq '2') { } 0 0 0
elsif ($attr eq '3') { } 0 0 0
elsif ($attr eq '-1') { } 0 0 0
194 # Do nothing.
195 0 return $text;
196 }
197 elsif ($attr eq "1") {
198 # Do everything, turn all options on.
199 0 $do_quotes = 1;
200 0 $do_backticks = 1;
201 0 $do_dashes = 1;
202 0 $do_ellipses = 1;
203 }
204 elsif ($attr eq "2") {
205 # Do everything, turn all options on, use old school dash shorthand.
206 0 $do_quotes = 1;
207 0 $do_backticks = 1;
208 0 $do_dashes = 2;
209 0 $do_ellipses = 1;
210 }
211 elsif ($attr eq "3") {
212 # Do everything, turn all options on, use inverted old school dash shorthand.
213 0 $do_quotes = 1;
214 0 $do_backticks = 1;
215 0 $do_dashes = 3;
216 0 $do_ellipses = 1;
217 }
218 elsif ($attr eq "-1") {
219 # Special "stupefy" mode.
220 0 $do_stupefy = 1;
221 }
222 else {
223 0 my @chars = split(//, $attr);
224 0 foreach my $c (@chars) {
225 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
226 0 elsif ($c eq "b") { $do_backticks = 1; }
227 0 elsif ($c eq "B") { $do_backticks = 2; }
228 0 elsif ($c eq "d") { $do_dashes = 1; }
229 0 elsif ($c eq "D") { $do_dashes = 2; }
230 0 elsif ($c eq "i") { $do_dashes = 3; }
231 0 elsif ($c eq "e") { $do_ellipses = 1; }
232 0 elsif ($c eq "w") { $convert_quot = 1; }
233 else {
234 # Unknown attribute option, ignore.
235 }
236 }
237 }
238  
239 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
240 0 my $result = '';
241 0 my $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags.
242  
243 0 my $prev_token_last_char = ""; # This is a cheat, used to get some context
244 # for one-character tokens that consist of
245 # just a quote char. What we do is remember
246 # the last character of the previous text
247 # token, to use as context to curl single-
248 # character quote tokens correctly.
249  
250 0 foreach my $cur_token (@$tokens) {
251 0 ▶ if ($cur_token->[0] eq "tag") {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[0] eq 'tag') { } 0 0 0
252 # Don't mess with quotes inside tags.
253 0 $result .= $cur_token->[1];
254 0 ▶ if ($cur_token->[1] =~ m/$tags_to_skip/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[1] =~ /$tags_to_skip/) 0 0 0
255 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
256 }
257 } else {
258 0 my $t = $cur_token->[1];
259 0 my $last_char = substr($t, -1); # Remember last char of this token before processing.
260 0 ▶ if (! $in_pre) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_pre) 0 0 0
261 0 $t = ProcessEscapes($t);
262  
263 0 ▶ if ($convert_quot) {
Branch: 1 branch
BranchTrueFalseTotal
if ($convert_quot) 0 0 0
264 0 $t =~ s/&quot;/"/g;
265 }
266  
267 0 ▶ if ($do_dashes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_dashes) 0 0 0
268 0 ▶ $t = EducateDashes($t) if ($do_dashes == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $do_dashes == 1 0 0 0
269 0 ▶ $t = EducateDashesOldSchool($t) if ($do_dashes == 2);
Branch: 1 branch
BranchTrueFalseTotal
if $do_dashes == 2 0 0 0
270 0 ▶ $t = EducateDashesOldSchoolInverted($t) if ($do_dashes == 3);
Branch: 1 branch
BranchTrueFalseTotal
if $do_dashes == 3 0 0 0
271 }
272  
273 0 ▶ $t = EducateEllipses($t) if $do_ellipses;
Branch: 1 branch
BranchTrueFalseTotal
if $do_ellipses 0 0 0
274  
275 # Note: backticks need to be processed before quotes.
276 0 ▶ if ($do_backticks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_backticks) 0 0 0
277 0 $t = EducateBackticks($t);
278 0 ▶ $t = EducateSingleBackticks($t) if ($do_backticks == 2);
Branch: 1 branch
BranchTrueFalseTotal
if $do_backticks == 2 0 0 0
279 }
280  
281 0 ▶ if ($do_quotes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_quotes) 0 0 0
282 0 ▶ if ($t eq q/'/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq q[']) { } 0 0 0
elsif ($t eq '"') { } 0 0 0
283 # Special case: single-character ' token
284 0 ▶ if ($prev_token_last_char =~ m/\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_token_last_char =~ /\S/) { } 0 0 0
285 0 $t = "&#8217;";
286 }
287 else {
288 0 $t = "&#8216;";
289 }
290 }
291 elsif ($t eq q/"/) {
292 # Special case: single-character " token
293 0 ▶ if ($prev_token_last_char =~ m/\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_token_last_char =~ /\S/) { } 0 0 0
294 0 $t = "&#8221;";
295 }
296 else {
297 0 $t = "&#8220;";
298 }
299 }
300 else {
301 # Normal case:
302 0 $t = EducateQuotes($t);
303 }
304 }
305  
306 0 ▶ $t = StupefyEntities($t) if $do_stupefy;
Branch: 1 branch
BranchTrueFalseTotal
if $do_stupefy 0 0 0
307 }
308 0 $prev_token_last_char = $last_char;
309 0 $result .= $t;
310 }
311 }
312  
313 0 return $result;
314 }
315  
316  
317 0 ▶sub SmartQuotes {
Subroutine SmartQuotes: not called CC=17 SCAR=57.2
Pod undocumented
318 # Paramaters:
319 0 my $text = shift; # text to be parsed
320 0 my $attr = shift; # value of the smart_quotes="" attribute
321  
322 0 my $do_backticks; # should we educate ``backticks'' -style quotes?
323  
324 0 ▶ if ($attr == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr == 0) { } 0 0 0
elsif ($attr == 2) { } 0 0 0
325 # do nothing;
326 0 return $text;
327 }
328 elsif ($attr == 2) {
329 # smarten ``backticks'' -style quotes
330 0 $do_backticks = 1;
331 }
332 else {
333 0 $do_backticks = 0;
334 }
335  
336 # Special case to handle quotes at the very end of $text when preceded by
337 # an HTML tag. Add a space to give the quote education algorithm a bit of
338 # context, so that it can guess correctly that it's a closing quote:
339 0 my $add_extra_space = 0;
340 0 ▶ if ($text =~ m/>['"]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ />['"]\z/) 0 0 0
341 0 $add_extra_space = 1; # Remember, so we can trim the extra space later.
342 0 $text .= " ";
343 }
344  
345 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
346 0 my $result = '';
347 0 my $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags
348  
349 0 my $prev_token_last_char = ""; # This is a cheat, used to get some context
350 # for one-character tokens that consist of
351 # just a quote char. What we do is remember
352 # the last character of the previous text
353 # token, to use as context to curl single-
354 # character quote tokens correctly.
355  
356 0 foreach my $cur_token (@$tokens) {
357 0 ▶ if ($cur_token->[0] eq "tag") {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[0] eq 'tag') { } 0 0 0
358 # Don't mess with quotes inside tags
359 0 $result .= $cur_token->[1];
360 0 ▶ if ($cur_token->[1] =~ m/$tags_to_skip/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[1] =~ /$tags_to_skip/) 0 0 0
361 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
362 }
363 } else {
364 0 my $t = $cur_token->[1];
365 0 my $last_char = substr($t, -1); # Remember last char of this token before processing.
366 0 ▶ if (! $in_pre) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_pre) 0 0 0
367 0 $t = ProcessEscapes($t);
368 0 ▶ if ($do_backticks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_backticks) 0 0 0
369 0 $t = EducateBackticks($t);
370 }
371  
372 0 ▶ if ($t eq q/'/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq q[']) { } 0 0 0
elsif ($t eq '"') { } 0 0 0
373 # Special case: single-character ' token
374 0 ▶ if ($prev_token_last_char =~ m/\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_token_last_char =~ /\S/) { } 0 0 0
375 0 $t = "&#8217;";
376 }
377 else {
378 0 $t = "&#8216;";
379 }
380 }
381 elsif ($t eq q/"/) {
382 # Special case: single-character " token
383 0 ▶ if ($prev_token_last_char =~ m/\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_token_last_char =~ /\S/) { } 0 0 0
384 0 $t = "&#8221;";
385 }
386 else {
387 0 $t = "&#8220;";
388 }
389 }
390 else {
391 # Normal case:
392 0 $t = EducateQuotes($t);
393 }
394  
395 }
396 0 $prev_token_last_char = $last_char;
397 0 $result .= $t;
398 }
399 }
400  
401 0 ▶ if ($add_extra_space) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add_extra_space) 0 0 0
402 0 $result =~ s/ \z//; # Trim trailing space if we added one earlier.
403 }
404 0 return $result;
405 }
406  
407  
408 0 ▶sub SmartDashes {
Subroutine SmartDashes: not called CC=11 SCAR=48.8
Pod undocumented
409 # Paramaters:
410 0 my $text = shift; # text to be parsed
411 0 my $attr = shift; # value of the smart_dashes="" attribute
412  
413 # reference to the subroutine to use for dash education, default to EducateDashes:
414 0 my $dash_sub_ref = \&EducateDashes;
415  
416 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
417 # do nothing;
418 0 return $text;
419 }
420 elsif ($attr == 2) {
421 # use old smart dash shortcuts, "--" for en, "---" for em
422 0 $dash_sub_ref = \&EducateDashesOldSchool;
423 }
424 elsif ($attr == 3) {
425 # inverse of 2, "--" for em, "---" for en
426 0 $dash_sub_ref = \&EducateDashesOldSchoolInverted;
427 }
428  
429 0 my $tokens;
430 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
431  
432 0 my $result = '';
433 0 my $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags
434 0 foreach my $cur_token (@$tokens) {
435 0 ▶ if ($cur_token->[0] eq "tag") {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[0] eq 'tag') { } 0 0 0
436 # Don't mess with quotes inside tags
437 0 $result .= $cur_token->[1];
438 0 ▶ if ($cur_token->[1] =~ m/$tags_to_skip/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[1] =~ /$tags_to_skip/) 0 0 0
439 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
440 }
441 } else {
442 0 my $t = $cur_token->[1];
443 0 ▶ if (! $in_pre) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_pre) 0 0 0
444 0 $t = ProcessEscapes($t);
445 0 $t = $dash_sub_ref->($t);
446 }
447 0 $result .= $t;
448 }
449 }
450 0 return $result;
451 }
452  
453  
454 0 ▶sub SmartEllipses {
Subroutine SmartEllipses: not called CC=9 SCAR=45.0
Pod undocumented
455 # Paramaters:
456 0 my $text = shift; # text to be parsed
457 0 my $attr = shift; # value of the smart_ellipses="" attribute
458  
459 0 ▶ if ($attr == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr == 0) 0 0 0
460 # do nothing;
461 0 return $text;
462 }
463  
464 0 my $tokens;
465 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
466  
467 0 my $result = '';
468 0 my $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags
469 0 foreach my $cur_token (@$tokens) {
470 0 ▶ if ($cur_token->[0] eq "tag") {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[0] eq 'tag') { } 0 0 0
471 # Don't mess with quotes inside tags
472 0 $result .= $cur_token->[1];
473 0 ▶ if ($cur_token->[1] =~ m/$tags_to_skip/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_token->[1] =~ /$tags_to_skip/) 0 0 0
474 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
475 }
476 } else {
477 0 my $t = $cur_token->[1];
478 0 ▶ if (! $in_pre) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_pre) 0 0 0
479 0 $t = ProcessEscapes($t);
480 0 $t = EducateEllipses($t);
481 }
482 0 $result .= $t;
483 }
484 }
485 0 return $result;
486 }
487  
488  
489 0 ▶sub EducateQuotes {
Subroutine EducateQuotes: not called CC=1 SCAR=6.9
Pod undocumented
490 #
491 # Parameter: String.
492 #
493 # Returns: The string, with "educated" curly quote HTML entities.
494 #
495 # Example input: "Isn't this fun?"
496 # Example output: &#8220;Isn&#8217;t this fun?&#8221;
497 #
498  
499 0 local $_ = shift;
500  
501 # Tell perl not to gripe when we use $1 in substitutions,
502 # even when it's undefined. Use $^W instead of "no warnings"
503 # for compatibility with Perl 5.005:
504 0 local $^W = 0;
505  
506  
507 # Make our own "punctuation" character class, because the POSIX-style
508 # [:PUNCT:] is only available in Perl 5.6 or later:
509 0 my $punct_class = qr/[!"#\$\%'()*+,-.\/:;<=>?\@\[\\\]\^_`{|}~]/;
510  
511 # Special case if the very first character is a quote
512 # followed by punctuation at a non-word-break. Close the quotes by brute force:
513 0 s/^'(?=$punct_class\B)/&#8217;/;
514 0 s/^"(?=$punct_class\B)/&#8221;/;
515  
516  
517 # Special case for double sets of quotes, e.g.:
518 # <p>He said, "'Quoted' words in a larger quote."</p>
519 0 s/"'(?=\w)/&#8220;&#8216;/g;
520 0 s/'"(?=\w)/&#8216;&#8220;/g;
521  
522 # Special case for decade abbreviations (the '80s):
523 0 s/'(?=\d{2}s)/&#8217;/g;
524  
525 0 my $close_class = qr![^\ \t\r\n\[\{\(\-]!;
526 0 my $dec_dashes = qr/&#8211;|&#8212;/;
527  
528 # Get most opening single quotes:
529 0 s {
530 (
531 \s | # a whitespace char, or
532 &nbsp; | # a non-breaking space entity, or
533 -- | # dashes, or
534 &[mn]dash; | # named dash entities
535 $dec_dashes | # or decimal entities
536 &\#x201[34]; # or hex
537 )
538 ' # the quote
539 (?=\w) # followed by a word character
540 } {$1&#8216;}xg;
541 # Single closing quotes:
542 0 s {
543 ($close_class)?
544 '
545 (?(1)| # If $1 captured, then do nothing;
546 (?=\s | s\b) # otherwise, positive lookahead for a whitespace
547 ) # char or an 's' at a word ending position. This
548 # is a special case to handle something like:
549 # "<i>Custer</i>'s Last Stand."
550 } {$1&#8217;}xgi;
551  
552 # Any remaining single quotes should be opening ones:
553 0 s/'/&#8216;/g;
554  
555  
556 # Get most opening double quotes:
557 0 s {
558 (
559 \s | # a whitespace char, or
560 &nbsp; | # a non-breaking space entity, or
561 -- | # dashes, or
562 &[mn]dash; | # named dash entities
563 $dec_dashes | # or decimal entities
564 &\#x201[34]; # or hex
565 )
566 " # the quote
567 (?=\w) # followed by a word character
568 } {$1&#8220;}xg;
569  
570 # Double closing quotes:
571 0 s {
572 ($close_class)?
573 "
574 (?(1)|(?=\s)) # If $1 captured, then do nothing;
575 # if not, then make sure the next char is whitespace.
576 } {$1&#8221;}xg;
577  
578 # Any remaining quotes should be opening ones.
579 0 s/"/&#8220;/g;
580  
581 0 return $_;
582 }
583  
584  
585 0 ▶sub EducateBackticks {
Subroutine EducateBackticks: not called CC=1 SCAR=6.9
Pod undocumented
586 #
587 # Parameter: String.
588 # Returns: The string, with ``backticks'' -style double quotes
589 # translated into HTML curly quote entities.
590 #
591 # Example input: ``Isn't this fun?''
592 # Example output: &#8220;Isn't this fun?&#8221;
593 #
594  
595 0 local $_ = shift;
596 0 s/``/&#8220;/g;
597 0 s/''/&#8221;/g;
598 0 return $_;
599 }
600  
601  
602 0 ▶sub EducateSingleBackticks {
Subroutine EducateSingleBackticks: not called CC=1 SCAR=6.9
Pod undocumented
603 #
604 # Parameter: String.
605 # Returns: The string, with `backticks' -style single quotes
606 # translated into HTML curly quote entities.
607 #
608 # Example input: `Isn't this fun?'
609 # Example output: &#8216;Isn&#8217;t this fun?&#8217;
610 #
611  
612 0 local $_ = shift;
613 0 s/`/&#8216;/g;
614 0 s/'/&#8217;/g;
615 0 return $_;
616 }
617  
618  
619 0 ▶sub EducateDashes {
Subroutine EducateDashes: not called CC=1 SCAR=6.9
Pod undocumented
620 #
621 # Parameter: String.
622 #
623 # Returns: The string, with each instance of "--" translated to
624 # an em-dash HTML entity.
625 #
626  
627 0 local $_ = shift;
628 0 s/--/&#8212;/g;
629 0 return $_;
630 }
631  
632  
633 0 ▶sub EducateDashesOldSchool {
Subroutine EducateDashesOldSchool: not called CC=1 SCAR=6.9
Pod undocumented
634 #
635 # Parameter: String.
636 #
637 # Returns: The string, with each instance of "--" translated to
638 # an en-dash HTML entity, and each "---" translated to
639 # an em-dash HTML entity.
640 #
641  
642 0 local $_ = shift;
643 0 s/---/&#8212;/g; # em
644 0 s/--/&#8211;/g; # en
645 0 return $_;
646 }
647  
648  
649 0 ▶sub EducateDashesOldSchoolInverted {
Subroutine EducateDashesOldSchoolInverted: not called CC=1 SCAR=6.9
Pod undocumented
650 #
651 # Parameter: String.
652 #
653 # Returns: The string, with each instance of "--" translated to
654 # an em-dash HTML entity, and each "---" translated to
655 # an en-dash HTML entity. Two reasons why: First, unlike the
656 # en- and em-dash syntax supported by
657 # EducateDashesOldSchool(), it's compatible with existing
658 # entries written before SmartyPants 1.1, back when "--" was
659 # only used for em-dashes. Second, em-dashes are more
660 # common than en-dashes, and so it sort of makes sense that
661 # the shortcut should be shorter to type. (Thanks to Aaron
662 # Swartz for the idea.)
663 #
664  
665 0 local $_ = shift;
666 0 s/---/&#8211;/g; # en
667 0 s/--/&#8212;/g; # em
668 0 return $_;
669 }
670  
671  
672 0 ▶sub EducateEllipses {
Subroutine EducateEllipses: not called CC=1 SCAR=6.9
Pod undocumented
673 #
674 # Parameter: String.
675 # Returns: The string, with each instance of "..." translated to
676 # an ellipsis HTML entity. Also converts the case where
677 # there are spaces between the dots.
678 #
679 # Example input: Huh...?
680 # Example output: Huh&#8230;?
681 #
682  
683 0 local $_ = shift;
684 0 s/\.\.\./&#8230;/g;
685 0 s/\. \. \./&#8230;/g;
686 0 return $_;
687 }
688  
689  
690 0 ▶sub StupefyEntities {
Subroutine StupefyEntities: not called CC=1 SCAR=6.9
Pod undocumented
691 #
692 # Parameter: String.
693 # Returns: The string, with each SmartyPants HTML entity translated to
694 # its ASCII counterpart.
695 #
696 # Example input: &#8220;Hello &#8212; world.&#8221;
697 # Example output: "Hello -- world."
698 #
699  
700 0 local $_ = shift;
701  
702 0 s/&#8211;/-/g; # en-dash
703 0 s/&#8212;/--/g; # em-dash
704  
705 0 s/&#8216;/'/g; # open single quote
706 0 s/&#8217;/'/g; # close single quote
707  
708 0 s/&#8220;/"/g; # open double quote
709 0 s/&#8221;/"/g; # close double quote
710  
711 0 s/&#8230;/.../g; # ellipsis
712  
713 0 return $_;
714 }
715  
716 0 ▶sub ProcessEscapes {
Subroutine ProcessEscapes: not called CC=1 SCAR=6.9
Pod undocumented
717 #
718 # Parameter: String.
719 # Returns: The string, with after processing the following backslash
720 # escape sequences. This is useful if you want to force a "dumb"
721 # quote or other character to appear.
722 #
723 # Escape Value
724 # ------ -----
725 # \\ &#92;
726 # \" &#34;
727 # \' &#39;
728 # \. &#46;
729 # \- &#45;
730 # \` &#96;
731 #
732 0 local $_ = shift;
733  
734 0 s! \\\\ !&#92;!gx;
735 0 s! \\" !&#34;!gx;
736 0 s! \\' !&#39;!gx;
737 0 s! \\\. !&#46;!gx;
738 0 s! \\- !&#45;!gx;
739 0 s! \\` !&#96;!gx;
740  
741 0 return $_;
742 }
743  
744  
745 0 ▶sub _tokenize {
Subroutine _tokenize: not called CC=4 SCAR=30.0
746 #
747 # Parameter: String containing HTML markup.
748 # Returns: Reference to an array of the tokens comprising the input
749 # string. Each token is either a tag (possibly with nested,
750 # tags contained therein, such as <a href="<MTFoo>">, or a
751 # run of text between tags. Each element of the array is a
752 # two-element array; the first is either 'tag' or 'text';
753 # the second is the actual value.
754 #
755 #
756 # Based on the _tokenize() subroutine from Brad Choate's MTRegex plugin.
757 # <http://www.bradchoate.com/past/mtregex.php>
758 #
759  
760 0 my $str = shift;
761 0 my $pos = 0;
762 0 my $len = length $str;
763 0 my @tokens;
764  
765 0 my $depth = 6;
766 0 my $nested_tags = join('|', ('(?:<(?:[^<>]') x $depth) . (')*>)' x $depth);
767 0 my $match = qr/(?s: <! ( -- .*? -- \s* )+ > ) | # comment
768 (?s: <\? .*? \?> ) | # processing instruction
769 $nested_tags/x; # nested tags
770  
771 0 ▶ while ($str =~ m/($match)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /($match)/g) 0 0 0
772 0 my $whole_tag = $1;
773 0 my $sec_start = pos $str;
774 0 my $tag_start = $sec_start - length $whole_tag;
775 0 ▶ if ($pos < $tag_start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $tag_start) 0 0 0
776 0 push @tokens, ['text', substr($str, $pos, $tag_start - $pos)];
777 }
778 0 push @tokens, ['tag', $whole_tag];
779 0 $pos = pos $str;
780 }
781 0 ▶ push @tokens, ['text', substr($str, $pos, $len - $pos)] if $pos < $len;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < $len 0 0 0
782 0 \@tokens;
783 }
784  
785 1 42;
↑ Summary