1 rizwank 1.1 # This is a stopgap measure to let the CommentPlugin read templates from
2 # webs as well as the templates dir.
3 # Templates are loaded by choice from the templates dir, but if the template
4 # isn't found, webs are searched. If a web is specified, then that web is searched;
5 # otherwise the template is assumed to be in the TwikiWeb.
6 # The stopgap can be removed when TWiki supports reading templates from webs.
7
8 { package CommentPlugin::Templates;
9
10 # It would have been really good if we could have simply overridden this method in
11 # a template handing class. But we can't.
12 sub _loadFile {
13 my( $theName, $theSkin ) = @_;
14 my $text = TWiki::Store::_readTemplateFile( $theName, $theSkin,
15 $TWiki::webName );
16 return $text unless ( $text eq "" );
17 # wasn't a matched template, try topic
18 my $theTopic = $theName;
19 my $theWeb = TWiki::Func::getTwikiWebname();
20 if ( $theName =~ /^(\w+)\.(\w+)$/ ) {
21 $theWeb = $1;
22 rizwank 1.1 $theTopic = $2;
23 }
24 if ( TWiki::Func::topicExists( $theWeb, $theTopic )) {
25 my $meta;
26 ( $meta, $text ) = TWiki::Func::readTopic( $theWeb, $theTopic );
27 }
28 return $text;
29 }
30
31 sub readTemplate {
32 my( $theName, $theSkin ) = @_;
33
34 if( ! defined($theSkin) ) {
35 $theSkin = TWiki::Func::getSkin();
36 }
37
38 # recursively read template file(s)
39 my $text = _loadFile( $theName, $theSkin );
40 while( $text =~ /%TMPL\:INCLUDE{[\s\"]*(.*?)[\"\s]*}%/s ) {
41 $text =~ s/%TMPL\:INCLUDE{[\s\"]*(.*?)[\"\s]*}%/&_loadFile( $1, $theSkin )/geo;
42 }
43 rizwank 1.1
44 # or even if this function had been split here, and file reading separated from
45 # template processing
46 if( ! ( $text =~ /%TMPL\:/s ) ) {
47 # no template processing
48 $text =~ s|^(( {3})+)|"\t" x (length($1)/3)|geom; # leading spaces to tabs
49 return $text;
50 }
51
52 my $result = "";
53 my $key = "";
54 my $val = "";
55 my $delim = "";
56 foreach( split( /(%TMPL\:)/, $text ) ) {
57 if( /^(%TMPL\:)$/ ) {
58 $delim = $1;
59 } elsif( ( /^DEF{[\s\"]*(.*?)[\"\s]*}%[\n\r]*(.*)/s ) && ( $1 ) ) {
60 # handle %TMPL:DEF{"key"}%
61 if( $key ) {
62 $TWiki::Store::templateVars{ $key } = $val;
63 }
64 rizwank 1.1 $key = $1;
65 $val = $2 || "";
66
67 } elsif( /^END%[\n\r]*(.*)/s ) {
68 # handle %TMPL:END%
69 $TWiki::Store::templateVars{ $key } = $val;
70 $key = "";
71 $val = "";
72 $result .= $1 || "";
73
74 } elsif( $key ) {
75 $val .= "$delim$_";
76
77 } else {
78 $result .= "$delim$_";
79 }
80 }
81
82 # handle %TMPL:P{"..."}% recursively
83 $result =~ s/%TMPL\:P{[\s\"]*(.*?)[\"\s]*}%/&TWiki::Store::handleTmplP($1)/geo;
84 $result =~ s|^(( {3})+)|"\t" x (length($1)/3)|geom; # leading spaces to tabs
85 rizwank 1.1
86 return $result;
87 }
88 }
89
90 1;
|