Wiki/Txt.pm

filter: Statementstmt 76.0% 54 / 71 Branchbran 66.6% 32 / 48 Conditioncond 33.3% 4 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 66.1% 92 / 139 CC 30 SCAR 40.6
CC
30
Cov
69%
_from_txt
58.2
CRAP 58.2
↑ Summary Wiki/Node.pm »
1 #############################################################################
2 # (c) by Tels 2004. Part of Convert::Wiki
3 #
4 # contains the from_txt() fuctionality for Convert::Wiki
5 #############################################################################
6  
7 package Convert::Wiki;
8  
9 sub _from_txt
10 7 ▶ {
Subroutine _from_txt: called CC=30 SCAR=40.6
11 7 my ($self,$txt) = @_;
12  
13 7 $self->clear();
14  
15 #########################################################################
16 # Stage 0: global normalization
17  
18 # convert "\n \n" to "\n\n"
19 7 $txt =~ s/\n\s+\n/\n\n/;
20  
21 # convert "foo:\nbah" to "foo:\n\nbah" (but not headlines ending in ":")
22 7 $txt =~ s/:\s*\n([^=_-])/:\n\n$1/g;
23  
24 # remove leading \n:
25 7 $txt =~ s/^\n+//g;
26  
27 # remove leading lines:
28 7 $txt =~ s/^\s*[=_-]+\n+//;
29  
30 # take the text, recognize parts at it's beginning until we no longer have
31 # anything left
32 7 my ($opt);
33 7 my $tries = 0;
34 7 my $node_nr = 0;
35  
36 7 my $last_node = $self->{nodes};
37  
38 7 ▶ while ((length($txt) > 0) && ($tries++ < 16))
Branch: 1 branch
BranchTrueFalseTotal
if (length $txt > 0 and $tries++ < 16) 47 7 54
Condition66%2/3
length $txt > 0 and $tries++ < 16
!ll&&!rl&&r
7047
MC/DC50%1/2
length $txt > 0 and $tries++ < 16
length $txt > 0 $tries++ < 16
Truth table66%2/3
A and B
A= length $txt > 0B= $tries++ < 16
ABresult
0X0
100
111
39 {
40  
41 #########################################################################
42 # Stage 1: local normalization
43
44 # remove superflous newlines
45 47 $txt =~ s/^\n+//g;
46
47 # remove "=========" and similiar stray delimiters
48 47 $txt =~ s/^[=]+\n+//;
49
50 #########################################################################
51 # Stage 2: conversion to internal format
52  
53 47 $opt = undef; # reset
54  
55 47 ▶ if ($self->{debug})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 47 47
56 {
57 0 $txt =~ /^(.*)\n(.*)\n(.*)/;
58 0 ▶ my $a = $1 || '';
Condition0%0/2
$1 || ''
l!l
00
MC/DC0%0/1
$1 || ''
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
59 0 ▶ my $b = $2 || '';
Condition0%0/2
$2 || ''
l!l
00
MC/DC0%0/1
$2 || ''
$2
Truth table0%0/2
A
A= $2
Aresult
11
00
60 0 ▶ my $c = $3 || '';
Condition0%0/2
$3 || ''
l!l
00
MC/DC0%0/1
$3 || ''
$3
Truth table0%0/2
A
A= $3
Aresult
11
00
61 0 print STDERR "# at node $node_nr\n";
62 0 print STDERR "# Text start is now:\n# '$a'\n# '$b'\n# '$c'\n";
63 }
64
65 # "Foo\n===" looks like a headline
66 47 ▶ if ($txt =~ s/^([^=_-].+)\n[=_-]+\n+//)
Branch: 9 branches
BranchTrueFalseTotal
if ($txt =~ s/^([^=_-].+)\n[=_-]+\n+//) { } 13 34 47
elsif ($txt =~ s/^[-_]+\n//) { } 2 32 34
elsif ($txt =~ s/^([\d\.]+) (.+)\n//) { } 0 32 32
elsif ($txt =~ s/^(\s*[*+-](\s+(.|\n)+?))(\n\n|\n\s*[*+-])/$4/) { } 7 25 32
elsif ($txt =~ s/^\s+(([^\s].+\n){2,})//) { } 2 23 25
elsif ($txt =~ s/^\s+(((.+)\n\n))//) { } 2 21 23
elsif ($txt =~ s/^(([ \t]+[^\s\n*+=-].+\n){2,})//) { } 0 21 21
elsif ($txt =~ s/^([\$\#])\s+(((.+)\n){1,})//) { } 1 20 21
elsif ($txt =~ s/^([^\s](([^*+\n-].+)\n){1,})//) { } 18 2 20
67 {
68 13 ▶ print STDERR "# case 1\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 13 13
69 13 $opt = { txt => $1, type => 'head1' };
70 }
71 # '-----' or '_____' to rulers
72 elsif ($txt =~ s/^[-_]+\n//)
73 {
74 2 ▶ print STDERR "# case 2\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 2 2
75 2 $opt = { type => 'line' };
76 }
77 # "1. Foo\n" looks like a bullet
78 elsif ($txt =~ s/^([\d\.]+) (.+)\n//)
79 {
80 0 ▶ print STDERR "# case 3\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
81 0 $opt = { txt => $2, name => $1, type => 'item' };
82 }
83 # "* Foo\n" looks like a bullet
84 elsif ($txt =~ s/^(\s*[*+-](\s+(.|\n)+?))(\n\n|\n\s*[*+-])/$4/)
85 {
86 7 ▶ print STDERR "# case 4\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 7 7
87 7 my $t = $1;
88 7 $t =~ s/^\s*[*+-]\s+//; # "- Foo" => "Foo"
89 7 $t =~ s/\n\s+/\n/g; # "\n Boo" => "\nBoo"
90  
91 7 $t =~ s/\s+\z//g; # remove trailing space
92 7 $t =~ s/\n/ /g; # remove newlines entirely
93  
94 7 $opt = { txt => $t, type => 'item' };
95 }
96 # " some text\nmore text" is one paragraph and not monospaced
97 elsif ($txt =~ s/^\s+(([^\s].+\n){2,})//)
98 {
99 2 ▶ print STDERR "# case 5\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 2 2
100 2 $opt = { txt => $1, type => 'para' };
101 }
102 # " some text\n\n" is one monospaced line
103 elsif ($txt =~ s/^\s+(((.+)\n\n))//)
104 {
105 2 ▶ print STDERR "# case 6\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 2 2
106 2 my $t = $1;
107 2 $t =~ s/\n\s+/\n/g; # "\n Boo" => "\nBoo"
108 2 $t =~ s/\n\z//g; # remove trailing \n
109  
110 2 $opt = { txt => $t, type => 'mono' };
111 }
112 # " Foo\n" looks like a monospaced text
113 elsif ($txt =~ s/^(([ \t]+[^\s\n*+=-].+\n){2,})//)
114 {
115 0 ▶ print STDERR "# case 7 '$1'\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
116 0 my $t = $1;
117  
118 0 $t =~ s/^\s+//g; # " Boo" => "Boo"
119 0 $t =~ s/\n\s+/\n/g; # "\n Boo" => "\nBoo"
120 0 $t =~ s/\n\z//g; # remove trailing \n
121  
122 0 $opt = { txt => $t, type => 'mono' };
123 }
124 # Also: "$ Foo\n" and "# bah" look like a monospaced text
125 elsif ($txt =~ s/^([\$\#])\s+(((.+)\n){1,})//)
126 {
127 1 ▶ print STDERR "# case 8\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
128 1 my $t = "$1 $2";
129 1 $t =~ s/\n\s+/\n/g; # "\n Boo" => "\nBoo"
130 1 $t =~ s/\n\z//g; # remove trailing \n
131  
132 1 $opt = { txt => $t, type => 'mono' };
133 }
134 # "Foo\n" looks like a text
135 elsif ($txt =~ s/^([^\s](([^*+\n-].+)\n){1,})//)
136 {
137 18 ▶ print STDERR "# case 9\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 18 18
138 18 $opt = { txt => $1, type => 'para' };
139 }
140
141 47 ▶ if (defined $opt)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt) 45 2 47
142 {
143 45 $tries = 0;
144 45 ▶ if ($self->{debug})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 45 45
145 {
146 0 require Data::Dumper;
147 0 print STDERR Data::Dumper::Dumper($opt);
148 }
149 45 $opt->{interlink} = $self->{interlink};
150 45 my $node = Convert::Wiki::Node->new( $opt );
151 45 ▶ if ($last_node)
Branch: 1 branch
BranchTrueFalseTotal
if ($last_node) { } 38 7 45
152 {
153 # link node to last node
154 38 $last_node->link( $node );
155 }
156 else
157 {
158 7 $self->{nodes} = $node;
159 }
160 45 $last_node = $node;
161 45 $node_nr++;
162 }
163 }
164 7 ▶ if ($tries > 0 && length($txt) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($tries > 0 and length $txt > 0) 0 7 7
Condition66%2/3
$tries > 0 and length $txt > 0
!ll&&!rl&&r
520
MC/DC0%0/2
$tries > 0 and length $txt > 0
$tries > 0 length $txt > 0
Truth table66%2/3
A and B
A= $tries > 0B= length $txt > 0
ABresult
0X0
100
111
165 {
166 # something was left over
167 0 $self->error( "Cannot recognize text ahead of me. Giving up." );
168 }
169
170 7 $self;
171 }
172  
173 2 1;
174 __END__
↑ Summary Wiki/Node.pm »