lib/MKDoc/XML/Tagger/Preserve.pm

filter: Statementstmt 79.6% 47 / 59 Branchbran 62.5% 5 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 2 totaltotal 72.2% 60 / 83 CC 8 SCAR 14.0
CC
8
Cov
74%
process_file
12.0
_preserve_encode
4.0
_compute_unique_string
2.0
CRAP 4.1
« lib/MKDoc/XML/TreeBuilder.pm ↑ Summary lib/MKDoc/XML/Token.pm »
1 # -------------------------------------------------------------------------------------
2 # MKDoc::XML::Tagger::Preserve
3 # -------------------------------------------------------------------------------------
4 # Author : Jean-Michel Hiver.
5 # Copyright : (c) MKDoc Holdings Ltd, 2003
6 #
7 # This module uses MKDoc::XML::Tagger, except it preserves specific tags to prevent
8 # them from being tagged twice. At the moment the module uses regexes to do that so it
9 # might not be very generic but it should at least work for XHTML <a> tags.
10 # -------------------------------------------------------------------------------------
11 package MKDoc::XML::Tagger::Preserve;
12 5 ▶use MKDoc::XML::Tagger;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 our @Preserve = ();
18  
19  
20 ##
21 # $class->process_data ($xml, @expressions);
22 # ------------------------------------------
23 # Tags $xml with @expressions, where expression is a list of hashes.
24 #
25 # For example:
26 #
27 # MKDoc::XML::Tagger::Preserve->process (
28 # [ 'i_will_be_preserved', 'a' ],
29 # 'I like oranges and bananas',
30 # { _expr => 'oranges', _tag => 'a', href => 'http://www.google.com?q=oranges' },
31 # { _expr => 'bananas', _tag => 'a', href => 'http://www.google.com?q=bananas' },
32 #
33 # Will return
34 #
35 # 'I like <a href="http://www.google.com?q=oranges">oranges</a> and \
36 # <a href="http://www.google.com?q=bananas">bananas</a>.
37 ##
38 sub process_data
39 2 ▶{
Subroutine process_data: called CC=1 SCAR=0.0
Pod undocumented
40 2 my $class = shift;
41 2 local @Preserve = @{shift()};
42 2 my $text = shift;
43 2 my @list = ();
44  
45  
46 2 ($text, @list) = _preserve_encode ($text);
47 2 $text = MKDoc::XML::Tagger->process_data ($text, @_);
48 2 $text = _preserve_decode ($text, @list);
49  
50 2 return $text;
51 }
52  
53  
54 sub process_file
55 0 ▶{
Subroutine process_file: not called CC=3 SCAR=24.8
Pod undocumented
56 0 my $class = shift;
57 0 my $file = shift;
58 0 ▶ open FP, "<$file" || do {
Condition0%0/3
"<$file" || do { warn "Cannot read-open $file"; return [] }
l!l&&r!l&&!r
000
MC/DC0%0/2
"<$file" || do { warn "Cannot read-open $file"; return [] }
"<$file" do { warn "Cannot read-open $file"; return [] }
Truth table0%0/3
A || B
A= "<$file"B= do { warn "Cannot read-open $file"; return [] }
ABresult
1X1
011
000
59 0 warn "Cannot read-open $file";
60 0 return [];
61 };
62
63 0 my $data = '';
64 0 ▶ while (<FP>) { $data .= $_ }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FP)) 0 0 0
65 0 close FP;
66
67 0 return $class->process_data ($data);
68 }
69  
70  
71 sub _preserve_encode
72 3 ▶{
Subroutine _preserve_encode: called CC=4 SCAR=13.9
73 3 my $text = shift;
74 3 my @list = ();
75 3 for my $tag (@Preserve)
76 {
77 3 my @tags = $text =~ /(<$tag\s.*?<\/$tag>)/gs;
78 3 ▶ for my $tag (@tags) { while ($text =~ s/\Q$tag\E/_compute_unique_string ($text, $tag, \@list)/e) {} }
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/\Q$tag\E/_compute_unique_string($text, $tag, \@list);/e) 2 2 4
79 }
80
81 3 return $text, @list;
82 }
83  
84  
85 sub _preserve_decode
86 2 ▶{
Subroutine _preserve_decode: called CC=2 SCAR=6.9
87 2 my $text = shift;
88 2 my @tsil = reverse (@_);
89
90 2 ▶ while (@tsil)
Branch: 1 branch
BranchTrueFalseTotal
if (@tsil) 1 2 3
91 {
92 1 my $val = shift (@tsil);
93 1 my $id = shift (@tsil);
94 1 $text =~ s/$id/$val/;
95 }
96
97 2 return $text;
98 }
99  
100  
101 sub _compute_unique_string
102 3 ▶{
Subroutine _compute_unique_string: called CC=2 SCAR=7.2
103 3 my $text = shift;
104 3 my $str = shift;
105 3 my $list = shift;
106 3 my $id = join '', map { chr (ord ('a') + int (rand (26))) } 1..10;
107 3 ▶ while ($text =~ /\Q$id\E/)
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\Q$id\E/) 0 3 3
108 {
109 0 $id = join '', map { chr (ord ('a') + int (rand (26))) } 1..10;
110 }
111
112 3 push @{$list}, $id => $str;
113 3 return $id;
114 }
115  
116  
117 5 1;
118  
119  
120 __END__
« lib/MKDoc/XML/TreeBuilder.pm ↑ Summary lib/MKDoc/XML/Token.pm »