1 rizwank 1.1 # Plugin for TWiki Collaboration Platform, http://TWiki.org/
2 #
3 # Copyright (C) 2001-2004 Peter Thoeny, peter@thoeny.com
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details, published at
14 # http://www.gnu.org/copyleft/gpl.html
15 #
16 # =========================
17 #
18 # Each plugin is a package that may contain these functions: VERSION:
19 #
20 # earlyInitPlugin ( ) 1.020
21 # initPlugin ( $topic, $web, $user, $installWeb ) 1.000
22 rizwank 1.1 # initializeUserHandler ( $loginName, $url, $pathInfo ) 1.010
23 # registrationHandler ( $web, $wikiName, $loginName ) 1.010
24 # commonTagsHandler ( $text, $topic, $web ) 1.000
25 # startRenderingHandler ( $text, $web ) 1.000
26 # outsidePREHandler ( $text ) 1.000
27 # insidePREHandler ( $text ) 1.000
28 # endRenderingHandler ( $text ) 1.000
29 # beforeEditHandler ( $text, $topic, $web ) 1.010
30 # afterEditHandler ( $text, $topic, $web ) 1.010
31 # beforeSaveHandler ( $text, $topic, $web ) 1.010
32 # writeHeaderHandler ( $query ) 1.010 Use only in one Plugin
33 # redirectCgiQueryHandler ( $query, $url ) 1.010 Use only in one Plugin
34 # getSessionValueHandler ( $key ) 1.010 Use only in one Plugin
35 # setSessionValueHandler ( $key, $value ) 1.010 Use only in one Plugin
36 #
37 # initPlugin is required, all other are optional.
38 # For increased performance, all handlers except initPlugin are
39 # disabled. To enable a handler remove the leading DISABLE_ from
40 # the function name. Remove disabled handlers you do not need.
41 #
42 # NOTE: To interact with TWiki use the official TWiki functions
43 rizwank 1.1 # in the TWiki::Func module. Do not reference any functions or
44 # variables elsewhere in TWiki!!
45
46
47 # =========================
48 package TWiki::Plugins::RenderListPlugin; # change the package name and $pluginName!!!
49
50 # =========================
51 use vars qw(
52 $web $topic $user $installWeb $VERSION $pluginName
53 $debug
54 );
55
56 $VERSION = '1.031';
57 $pluginName = 'RenderListPlugin'; # Name of this Plugin
58
59 # =========================
60 sub initPlugin
61 {
62 ( $topic, $web, $user, $installWeb ) = @_;
63
64 rizwank 1.1 # check for Plugins.pm versions
65 if( $TWiki::Plugins::VERSION < 1 ) {
66 TWiki::Func::writeWarning( "Version mismatch between $pluginName and Plugins.pm" );
67 return 0;
68 }
69
70 # Get plugin debug flag
71 $debug = TWiki::Func::getPreferencesFlag( "\U$pluginName\E_DEBUG" );
72
73 # Plugin correctly initialized
74 TWiki::Func::writeDebug( "- TWiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK" ) if $debug;
75 return 1;
76 }
77
78 # =========================
79 sub DISABLE_commonTagsHandler
80 {
81 ### my ( $text, $topic, $web ) = @_; # do not uncomment, use $_[0], $_[1]... instead
82
83 TWiki::Func::writeDebug( "- ${pluginName}::commonTagsHandler( $_[2].$_[1] )" ) if $debug;
84
85 rizwank 1.1 # $_[0] =~ s/%RENDERLIST{(.*?)}%(([\n\r]+[^\t]{1}[^\n\r]*)*?)(([\n\r]+\t[^\n\r]*)+)/&handleRenderList($1, $2, $4)/ges;
86 }
87
88 # =========================
89 sub startRenderingHandler
90 {
91 ### my ( $text, $web ) = @_; # do not uncomment, use $_[0], $_[1] instead
92
93 TWiki::Func::writeDebug( "- ${pluginName}::startRenderingHandler( $_[1] )" ) if $debug;
94
95 # This handler is called by getRenderedVersion just before the line loop
96
97 # Render here, not in commonTagsHandler so that lists produced by
98 # Plugins, TOC and SEARCH can be rendered
99 $_[0] =~ s/%RENDERLIST{(.*?)}%(([\n\r]+[^\t]{1}[^\n\r]*)*?)(([\n\r]+\t[^\n\r]*)+)/&handleRenderList($1, $2, $4)/ges;
100 }
101
102 # =========================
103 sub handleRenderList
104 {
105 my ( $theAttr, $thePre, $theList ) = @_;
106 rizwank 1.1
107 my $focus = &TWiki::Func::extractNameValuePair( $theAttr, "focus" );
108 my $depth = &TWiki::Func::extractNameValuePair( $theAttr, "depth" );
109 my $theme = &TWiki::Func::extractNameValuePair( $theAttr, "theme" ) ||
110 &TWiki::Func::extractNameValuePair( $theAttr );
111 $theme = "RENDERLISTPLUGIN_" . uc( $theme ) . "_THEME";
112 $theme = &TWiki::Func::getPreferencesValue( $theme ) || "unrecognized theme type";
113 my ( $type, $params ) = split( /, */, $theme, 2 );
114 $type = lc( $type );
115
116 if( $type eq "tree" || $type eq "icon" ) {
117 return $thePre . renderIconList( $type, $params, $focus, $depth, $theList );
118 } else {
119 return "$thePre$theList";
120 }
121 }
122
123 # =========================
124 sub renderIconList
125 {
126 my ( $theType, $theParams, $theFocus, $theDepth, $theText ) = @_;
127 rizwank 1.1
128 $theText =~ s/^[\n\r]*//os;
129 my @tree = ();
130 my $level = 0;
131 my $type = "";
132 my $text = "";
133 my $focusIndex = -1;
134 foreach( split ( /[\n\r]+/, $theText ) ) {
135 m/^(\t+)(.) *(.*)/;
136 $level = length( $1 );
137 $type = $2;
138 $text = $3;
139 if( ( $theFocus ) && ( $focusIndex < 0 ) && ( $text =~ /$theFocus/ ) ) {
140 $focusIndex = scalar( @tree );
141 }
142 push( @tree, { level => $level, type => $type, text => $text } );
143 }
144
145 # reduce tree to relatives around focus
146 if( $focusIndex >= 0 ) {
147 # splice tree into before, current node and after parts
148 rizwank 1.1 my @after = splice( @tree, $focusIndex + 1 );
149 my $nref = pop( @tree );
150
151 # highlight node with focus and remove links
152 $text = $nref->{'text'};
153 $text =~ s/^([^\-]*)\[\[.*?\]\[(.*?)\]\]/$1$2/o; # remove [[...][...]] link
154 $text =~ s/^([^\-]*)\[\[(.*?)\]\]/$1$2/o; # remove [[...]] link
155 $text = "<b> $text </b>"; # bold focus text
156 $nref->{'text'} = $text;
157
158 # remove uncles and siblings below current node
159 $level = $nref->{'level'};
160 for( my $i = 0; $i < scalar( @after ); $i++ ) {
161 if( ( $after[$i]->{'level'} < $level )
162 || ( $after[$i]->{'level'} <= $level && $after[$i]->{'type'} ne " " ) ) {
163 splice( @after, $i );
164 last;
165 }
166 }
167
168 # remove uncles and siblings above current node
169 rizwank 1.1 my @before = ();
170 for( my $i = scalar( @tree ) - 1; $i >= 0; $i-- ) {
171 if( $tree[$i]->{'level'} < $level ) {
172 push( @before, $tree[$i] );
173 $level = $tree[$i]->{'level'};
174 }
175 }
176 @tree = reverse( @before );
177 $focusIndex = scalar( @tree );
178 push( @tree, $nref );
179 push( @tree, @after );
180 }
181
182 # limit depth of tree
183 my $depth = $theDepth;
184 unless( $depth =~ s/.*?([0-9]+).*/$1/o ) {
185 $depth = 0;
186 }
187 if( $theFocus ) {
188 if( $theDepth eq "" ) {
189 $depth = $focusIndex + 3;
190 rizwank 1.1 } else {
191 $depth += $focusIndex + 1;
192 }
193 }
194 if( $depth > 0 ) {
195 my @tmp = ();
196 foreach my $ref ( @tree ) {
197 push( @tmp, $ref ) if( $ref->{'level'} <= $depth );
198 }
199 @tree = @tmp;
200 }
201
202 my $attachUrl = TWiki::Func::getUrlHost() . TWiki::Func::getPubUrlPath();
203 $theParams =~ s/%PUBURL%/$attachUrl/go;
204 $attachUrl .= "/$installWeb/$pluginName";
205 $theParams =~ s/%ATTACHURL%/$attachUrl/go;
206 $theParams =~ s/%WEB%/$installWeb/go;
207 $theParams =~ s/%MAINWEB%/TWiki::Func::getMainWebname()/geo;
208 $theParams =~ s/%TWIKIWEB%/TWiki::Func::getTwikiWebname()/geo;
209 my ( $showLead, $width, $height, $iconSp, $iconT, $iconI, $iconL, $iconImg )
210 = split( /, */, $theParams );
211 rizwank 1.1 $width = 16 unless( $width );
212 $height = 16 unless( $height );
213 $iconSp = "$attachUrl/empty.gif" unless( $iconSp );
214 $iconSp = fixImageTag( $iconSp, $width, $height );
215 $iconT = "$attachUrl/dot_udr.gif" unless( $iconT );
216 $iconT = fixImageTag( $iconT, $width, $height );
217 $iconI = "$attachUrl/dot_ud.gif" unless( $iconI );
218 $iconI = fixImageTag( $iconI, $width, $height );
219 $iconL = "$attachUrl/dot_ur.gif" unless( $iconL );
220 $iconL = fixImageTag( $iconL, $width, $height );
221 $iconImg = "$attachUrl/home.gif" unless( $iconImg );
222 $iconImg = fixImageTag( $iconImg, $width, $height );
223
224 $text = "";
225 my $start = 0;
226 $start = 1 unless( $showLead );
227 my @listIcon = ();
228 for( my $i = 0; $i < scalar( @tree ); $i++ ) {
229 $text .= '<table border="0" cellspacing="0" cellpadding="0"><tr>' . "\n";
230 $level = $tree[$i]->{'level'};
231 for( my $l = $start; $l < $level; $l++ ) {
232 rizwank 1.1 if( $l == $level - 1 ) {
233 $listIcon[$l] = $iconSp;
234 for( my $x = $i + 1; $x < scalar( @tree ); $x++ ) {
235 last if( $tree[$x]->{'level'} < $level );
236 if( $tree[$x]->{'level'} <= $level && $tree[$x]->{'type'} ne " " ) {
237 $listIcon[$l] = $iconI;
238 last;
239 }
240 }
241 if( $tree[$i]->{'type'} eq " " ) {
242 $text .= "<td valign=\"top\">$listIcon[$l]</td>\n";
243 } elsif( $listIcon[$l] eq $iconSp ) {
244 $text .= "<td valign=\"top\">$iconL</td>\n";
245 } else {
246 $text .= "<td valign=\"top\">$iconT</td>\n";
247 }
248 } else {
249 $text .= "<td valign=\"top\">$listIcon[$l]</td>\n";
250 }
251 }
252 if( $theType eq "icon" ) {
253 rizwank 1.1 # icon theme type
254 if( $tree[$i]->{'type'} eq " " ) {
255 # continuation line
256 $text .= "<td valign=\"top\">$iconSp</td>\n";
257 } elsif( $tree[$i]->{'text'} =~ /^\s*(<b>)?\s*icon:([^\s]+)\s*(.*)/ ) {
258 # specific icon
259 $tree[$i]->{'text'} = $3;
260 $tree[$i]->{'text'} = "$1 $3" if( $1 );
261 my $icon = fixImageTag( "$attachUrl/$2.gif", $width, $height );
262 $text .= "<td valign=\"top\">$icon</td>\n";
263 } else {
264 # default icon
265 $text .= "<td valign=\"top\">$iconImg</td>\n";
266 }
267 $text .= "<td valign=\"top\"><nobr> $tree[$i]->{'text'} </nobr></td>\n";
268
269 } else {
270 # tree theme type
271 if( $tree[$i]->{'text'} =~ /^\s*(<b>)?\s*icon:([^\s]+)\s*(.*)/ ) {
272 # specific icon
273 $tree[$i]->{'text'} = $3;
274 rizwank 1.1 $tree[$i]->{'text'} = "$1 $3" if( $1 );
275 my $icon = fixImageTag( "$attachUrl/$2.gif", $width, $height );
276 $text .= "<td valign=\"top\">$icon</td>\n";
277 $text .= "<td valign=\"top\"><nobr> $tree[$i]->{'text'} </nobr></td>\n";
278 } else {
279 $text .= "<td valign=\"top\"><nobr> $tree[$i]->{'text'} </nobr></td>\n";
280 }
281 }
282 $text .= '</tr></table>' . "\n";
283 }
284 return $text;
285 }
286
287 # =========================
288 sub fixImageTag
289 {
290 my ( $theIcon, $theWidth, $theHeight ) = @_;
291
292 if( $theIcon =~ /\.(png|gif|jpeg|jpg)/i && $theIcon !~ /<img/i ) {
293 $theIcon = "<img src=\"$theIcon\" width=\"$theWidth\" height=\"$theHeight\""
294 . " alt=\"\" border=\"0\" />";
295 rizwank 1.1 }
296 return $theIcon;
297 }
298
299 # =========================
300
301 1;
|