Wiki/Text.pm

filter: Statementstmt 20.7% 37 / 178 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 52.6% 10 / 19 Podpod 100% 6 / 6 totaltotal 19.3% 53 / 274 CC 38 SCAR 46.1
CC
38
Cov
15%
Wiki2HTML
210.0
WikiLink
182.0
CommonMarkup
30.0
CRAP 100.0
↑ Summary Wiki.pm »
1 package Labyrinth::Plugin::Wiki::Text;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 $VERSION = '1.05';
8  
9 =head1 NAME
10  
11 Labyrinth::Plugin::Wiki::Text - Wiki text handler for Labyrinth framework.
12  
13 =head1 DESCRIPTION
14  
15 Contains all the Wiki text rendering code for Labyrinth.
16  
17 =cut
18  
19 # -------------------------------------
20 # Library Modules
21  
22 5 ▶use base qw(Labyrinth::Plugin::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 5 ▶use Labyrinth::Audit;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use Labyrinth::DBUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use Labyrinth::MLUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use Labyrinth::Support;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use Labyrinth::Users;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use Labyrinth::Variables;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # -------------------------------------
32 # Variables
33  
34 # preset with restricted pages
35 5 my %wiki_links = map {$_ => 1} qw(People Login Search RecentChanges);
36  
37 5 my ($LinkPattern,$SitePattern,$UrlPattern,$UriPattern,$MailPattern,$SendPattern);
38  
39 # HTML tag lists
40 # Single tags (that do not require a closing /tag)
41 5 my @HtmlSingle = qw(br hr);
42 # Tags that must be in <tag> ... </tag> pairs:
43 5 my @HtmlPairs = ( qw(b i p u h1 h2 h3 h4 h5 h6 code em strike strong blockquote ol ul li dt dd tr td th),
44 @HtmlSingle); # All singles can also be pairs
45  
46 # -------------------------------------
47 # Public Methods
48  
49 =head1 PUBLIC INTERFACE METHODS
50  
51 =over 4
52  
53 =item Render
54  
55 Controls the process of rendering a given page.
56  
57 =item InitLinkPatterns
58  
59 Prepares patterns used to translate wiki links into HTML links.
60  
61 =item Wiki2HTML
62  
63 Translate WikiFormat into XHTML.
64  
65 =item CommonMarkup
66  
67 Looks for and translates common WikiFormat markup into XHTML.
68  
69 =item WikiLink
70  
71 Looks for and translates WikiFormat links into XHTML.
72  
73 =item WikiHeading
74  
75 Translate WikiFormat heading into XHTML.
76  
77 =cut
78  
79 sub Render {
80 0 ▶ my $self = shift;
Subroutine Render: not called CC=3 SCAR=24.8
Pod documented
81 0 my $hash = shift;
82 0 my $title = $cgiparams{pagename};
83 0 my $content = $hash->{content};
84  
85 0 ▶ InitLinkPatterns() unless($LinkPattern);
Branch: 1 branch
BranchTrueFalseTotal
unless $LinkPattern 0 0 0
86  
87 0 $content = Wiki2HTML($content);
88  
89 # reposition top level heading
90 0 ▶ if($content =~ s!^<h1>(.*?)</h1>!!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s[^<h1>(.*?)</h1>][]) 0 0 0
91 0 $title = $1;
92 }
93  
94 0 return $title,$content;
95 }
96  
97 sub InitLinkPatterns {
98 0 ▶ my $UpperLetter = '[A-Z\xc0-\xde]';
Subroutine InitLinkPatterns: not called CC=1 SCAR=6.9
Pod documented
99 0 my $LowerLetter = '[a-z\xdf-\xff]';
100 0 my $AnyLetter = '[A-Za-z\xc0-\xff_0-9\$]';
101 0 my $AnyString = '[A-Za-z\xc0-\xff_0-9 \-\&\'~.,\?\(\)\"!\$:\/]';
102  
103 # Main link pattern: lowercase between uppercase, then anything
104 0 my $LpA = $UpperLetter . $AnyLetter . '*';
105 0 my $LpB = $AnyLetter . $AnyString . '*';
106 0 my $LpC = $AnyLetter . '*:' . $AnyString . '*';
107  
108 0 $LinkPattern = qr!\[\[($LpA|$LpC)\]\]!;
109 0 $SitePattern = qr!\[\[($LpA|$LpC)\|($LpB)\]\]!;
110  
111 0 $UrlPattern = qr!\[($settings{urlregex})\]!;
112 0 $UriPattern = qr!\[($settings{urlregex})[ \|]($LpB)\]!;
113  
114 0 $MailPattern = qr!\[(?:mailto:)?($settings{emailregex})\]!;
115 0 $SendPattern = qr!\[(?:mailto:)?($settings{emailregex})[ |]($LpB)\]!;
116 }
117  
118 sub Wiki2HTML {
119 0 ▶ my ($text) = @_;
Subroutine Wiki2HTML: not called CC=14 SCAR=53.5
Pod documented
120 0 my (@stack, $code, $oldcode, $parse);
121 0 my $depth = 0;
122 0 my $html = '';
123  
124 0 $code = 'p'; # we assume a paragraph starts
125 0 $text =~ s/\r\n?/\n/g;
126 0 for (split(/\n/, $text)) { # Process lines one-at-a-time
127 0 $_ .= "\n";
128 0 $parse = 2;
129 0 ▶ if (s/^(\*+)/<li>/) {
Branch: 5 branches
BranchTrueFalseTotal
if (s/^(\*+)/<li>/) { } 0 0 0
elsif (s/^(\#+)/<li>/) { } 0 0 0
elsif (s/^![ \t]//) { } 0 0 0
elsif (s/^([ \t]{2})//) { } 0 0 0
elsif (s/^(\" )//) { } 0 0 0
130 0 $code = "ul";
131 0 $depth = length $1;
132 } elsif (s/^(\#+)/<li>/) {
133 0 $code = "ol";
134 0 $depth = length $1;
135 } elsif (s/^![ \t]//) {
136 0 $code = "pre";
137 0 $depth = 1;
138 0 $parse = 0;
139 } elsif (s/^([ \t]{2})//) {
140 0 $code = "pre";
141 0 $depth = 1;
142 0 $parse = 1;
143 } elsif (s/^(\" )//) {
144 0 $code = "blockquote";
145 0 $depth = 1;
146 } else {
147 0 $code = "p";
148 0 $depth = 0;
149 }
150 0 ▶ while (@stack > $depth) { # Close tags as needed
Branch: 1 branch
BranchTrueFalseTotal
if (@stack > $depth) 0 0 0
151 0 $html .= '</' . pop(@stack) . ">\n";
152 }
153 0 ▶ if ($depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > 0) 0 0 0
154 # $depth = $IndentLimit if ($depth > $IndentLimit);
155 0 ▶ if (@stack) { # Non-empty stack
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 0 0 0
156 0 $oldcode = pop(@stack);
157 0 ▶ if ($oldcode ne $code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oldcode ne $code) 0 0 0
158 0 $html .= "</$oldcode><$code>\n";
159 }
160 0 push(@stack, $code);
161 }
162 0 ▶ while (@stack < $depth) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack < $depth) 0 0 0
163 0 push(@stack, $code);
164 0 $html .= "<$code>\n";
165 }
166 }
167  
168 0 ▶ if($code eq 'pre') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq 'pre') { } 0 0 0
169 0 s!^\s*$!<br />\n!; # Blank lines become new lines
170 } else {
171 0 s!^\s*$!<p>!; # Blank lines become new paragraphs
172 }
173 0 $html .= CommonMarkup($_, $parse);
174 }
175 0 ▶ while (@stack > 0) { # Clear stack
Branch: 1 branch
BranchTrueFalseTotal
if (@stack > 0) 0 0 0
176 0 $html .= '</' . pop(@stack) . ">\n";
177 }
178  
179 0 $html = process_html($html,0,1);
180  
181 # $html =~ s!<p>(.*?)\s*<(ul|ol|h[1-6]|pre|p)>!<p>$1</p>\n<$2>!gs; # close <p>'s.
182 # $html =~ s!<p>(.*?)\s*$!<p>$1</p>!gs; # close final <p>.
183 # $html =~ s!\s*</p>\s*</p>!</p>!gs; # remove extra close paragraphs
184 # $html =~ s!<p>\s*</p>!!gs; # remove black paragraphs
185 # $html =~ s/(\s|<p>)*<p>\s*/\n<p>/gs; # multiple blank lines fold into one.
186 # $html =~ s/\s*<p>\s*<(ul|ol|h[1-6]|pre)/\n<$1/gs; # remove unnecessary <p>'s.
187 # $html =~ s!([^>\s]+)\s*<p>!$1</p>\n<p>!gs; # close paragraphs.
188 # $html =~ s!\s*</p>\s*<p!</p>\n<p!gs; # separate paragraphs for readability.
189  
190 0 LogDebug("html=[$html]");
191 0 return $html;
192 }
193  
194 # 2 = Full parser
195 # 1 = Link only parsing
196 # 0 = no parsing
197  
198 sub CommonMarkup {
199 0 ▶ my ($text, $parse) = @_;
Subroutine CommonMarkup: not called CC=5 SCAR=34.0
Pod documented
200 0 local $_ = $text;
201  
202 0 ▶ if ($parse > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parse > 1) 0 0 0
203 0 s!\&lt;pre\&gt;((.|\n)*?)\&lt;\/pre\&gt;!<pre>$1</pre>!ig;
204 0 s!\&lt;code\&gt;((.|\n)*?)\&lt;\/code\&gt;!<code>$1</code>!ig;
205  
206 0 my $t;
207 0 for $t (@HtmlPairs) {
208 0 s!\&lt;$t(\s[^<>]+?)?\&gt;(.*?)\&lt;\/$t\&gt;!<$t$1>$2<\/$t>!gis;
209 }
210 0 for $t (@HtmlSingle) {
211 0 s!\&lt;$t(\s[^<>]+?)?\&gt;!<$t$1>!gi;
212 }
213  
214 # The quote markup patterns avoid overlapping tags (with 5 quotes)
215 # by matching the inner quotes for the strong pattern.
216 0 s/!!(.*?)!!/<code>$1<\/code>/g; #'
217 0 s/('*)'''(.*?)'''/$1<strong>$2<\/strong>/g; #'
218 0 s/''(.*?)''/<em>$1<\/em>/g;
219 0 s/(^|\n)\s*(\=+)\s+([^\n]+)\s+\=+/WikiHeading($1, $2, $3)/geo;
220  
221 0 s!\&lt;br\&gt;!<br />!g;
222 0 s!----+!<hr noshade="noshade" size="1" />!g;
223 0 s!====+!<hr noshade="noshade" size="2" />!g;
224 }
225  
226 0 ▶ if($parse > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parse > 0) 0 0 0
227 0 s!$SitePattern!WikiLink($1,$2)!eg;
228 0 s!$LinkPattern!WikiLink($1,$1)!eg;
229  
230 0 s!$UriPattern!<a href="$1">$2</a>!g;
231 0 s!$UrlPattern!<a href="$1">$1</a>!g;
232  
233 0 s!$SendPattern!<a href="mailto:$1">$2</a>!g;
234 0 s!$MailPattern!<a href="mailto:$1">$1</a>!g;
235 }
236  
237 0 return $_;
238 }
239  
240 sub WikiLink {
241 0 ▶ my ($page,$name) = @_;
Subroutine WikiLink: not called CC=13 SCAR=52.0
Pod documented
242  
243 0 ▶ if($page =~ /cpan:~(.*)/) {
Branch: 8 branches
BranchTrueFalseTotal
if ($page =~ /cpan:~(.*)/) { } 0 0 0
elsif ($page =~ /cpan:(.*)/) { } 0 0 0
elsif ($page =~ /perldoc:(.*)/) { } 0 0 0
elsif ($page =~ /user:(\d+|[\w ]+)/) { } 0 0 0
elsif ($page =~ /image:(\d+)/) { } 0 0 0
elsif ($page =~ /image:(.*)/) { } 0 0 0
elsif ($page =~ /media:(\d+)/) { } 0 0 0
elsif ($page =~ /media:(.*)/) { } 0 0 0
244 0 $page =~ s!cpan:~!!;
245 0 $name =~ s!cpan:~!!;
246 0 return qq!<a href="http://search.cpan.org/~$page">$name</a>!;
247 } elsif($page =~ /cpan:(.*)/) {
248 0 $page =~ s!cpan:!!;
249 0 $name =~ s!cpan:!!;
250 0 return qq!<a href="http://search.cpan.org/dist/$page">$name</a>!;
251 } elsif($page =~ /perldoc:(.*)/) {
252 0 $page =~ s!perldoc:!!;
253 0 $name =~ s!perldoc:!!;
254 0 return qq!<a href="http://perldoc.perl.org?$page">$name</a>!;
255 } elsif($page =~ /user:(\d+|[\w ]+)/) {
256 0 ▶ $name = undef if($page eq $name);
Branch: 1 branch
BranchTrueFalseTotal
if $page eq $name 0 0 0
257 0 return _mapuser($1,$name);
258 } elsif($page =~ /image:(\d+)/) {
259 0 return _mapimage(id => $1);
260 } elsif($page =~ /image:(.*)/) {
261 0 return _mapimage(name => $1);
262 } elsif($page =~ /media:(\d+)/) {
263 0 return _mapmedia(id => $1);
264 } elsif($page =~ /media:(.*)/) {
265 0 return _mapmedia(name => $1);
266 }
267  
268 0 ▶ $wiki_links{$page} ||= do {
Condition0%0/3
$wiki_links{$page} ||= do {
l!l&&r!l&&!r
000
MC/DC0%0/2
$wiki_links{$page} ||= do { my(@rows) = $Labyrinth::Plugin::Wiki::Text::dbi->GetQuery("hash", "CheckWikiPage", $page); @rows ? 1 : 0 }
$wiki_links{$page} do { my(@rows) = $Labyrinth::Plugin::Wiki::Text::dbi->GetQuery("hash", "CheckWikiPage", $page); @rows ? 1 : 0 }
Truth table0%0/3
A ||= B
A= $wiki_links{$page}B= do { my(@rows) = $Labyrinth::Plugin::Wiki::Text::dbi->GetQuery("hash", "CheckWikiPage", $page); @rows ? 1 : 0 }
ABresult
1X1
011
000
269 0 my @rows = $dbi->GetQuery('hash','CheckWikiPage',$page);
270 0 ▶ @rows ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@rows ? : 0 0 0
271 };
272  
273 0 ▶ if($wiki_links{$page}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wiki_links{$page}) 0 0 0
274 0 return qq!<a href="/wiki/$page">$name</a>!
275 }
276  
277 0 return qq!<a href="/wiki/$page">$name</a>?!
278 }
279  
280 sub WikiHeading {
281 0 ▶ my ($pre, $depth, $text) = @_;
Subroutine WikiHeading: not called CC=2 SCAR=17.9
Pod documented
282  
283 0 $depth = length($depth) - 1;
284 0 ▶ $depth = 6 if ($depth > 6);
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 6 0 0 0
285 0 return $pre . "<h$depth>$text</h$depth>\n";
286 }
287  
288 # -------------------------------------
289 # Private Methods
290  
291 sub _mapuser {
292 0 ▶ my $id = shift;
Subroutine _mapuser: not called CC=2 SCAR=17.9
293 0 my $nm = $id;
294  
295 0 ▶ if($id =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /^\d+$/) { } 0 0 0
296 0 $nm = UserName($id);
297 } else {
298 0 $id = UserID($id);
299 }
300  
301 0 return qq!<a href="/user/$id" title="User:$nm">$nm</a>!;
302 }
303  
304 sub _mapimage {
305 0 ▶ my %hash = @_;
Subroutine _mapimage: not called CC=3 SCAR=24.8
306 0 my @rows;
307  
308 0 ▶ if($hash{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{'id'}) { } 0 0 0
309 0 @rows = $dbi->GetQuery('hash','GetImageByID',$hash{id});
310 } else {
311 0 @rows = $dbi->GetQuery('hash','GetImageByName',$hash{id});
312 }
313  
314 0 ▶ return unless(@rows);
Branch: 1 branch
BranchTrueFalseTotal
unless @rows 0 0 0
315 0 return qq!<img src="$tvars{webpath}/images/$rows[0]->{link}" alt="$rows[0]->{tag}" />!;
316 }
317  
318 sub _mapmedia {
319 0 ▶ my %hash = @_;
Subroutine _mapmedia: not called CC=3 SCAR=24.8
320 0 my @rows;
321  
322 0 ▶ if($hash{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{'id'}) { } 0 0 0
323 0 @rows = $dbi->GetQuery('hash','GetImageByID',$hash{id});
324 } else {
325 0 @rows = $dbi->GetQuery('hash','GetImageByName',$hash{id});
326 }
327  
328 0 ▶ return unless(@rows);
Branch: 1 branch
BranchTrueFalseTotal
unless @rows 0 0 0
329 0 return qq!<a href="$tvars{webpath}/images/$rows[0]->{link}" title="$rows[0]->{tag}">$rows[0]->{tag}</a>!;
330 }
331  
332 5 1;
333  
334 __END__
335  
336 =back
337  
338 =head1 SEE ALSO
339  
340 L<Labyrinth>
341  
342 =head1 AUTHOR
343  
344 Barbie, <barbie@missbarbell.co.uk> for
345 Miss Barbell Productions, L<http://www.missbarbell.co.uk/>
346  
347 =head1 COPYRIGHT & LICENSE
348  
349 Copyright (C) 2008-2014 Barbie for Miss Barbell Productions
350 All Rights Reserved.
351  
352 This module is free software; you can redistribute it and/or
353 modify it under the Artistic License 2.0.
354  
355 =cut
↑ Summary Wiki.pm »