Skip to content

Commit

Permalink
Item8521:
Browse files Browse the repository at this point in the history
   * be more careful applying the monkey-patch to the Func API
   * parsing RENDERZONE properly but finally inserting the zone at the end of the rendering pipeline
   * moved docu to separate Var topics



git-svn-id: http://svn.foswiki.org/trunk@6332 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelDaum authored and MichaelDaum committed Feb 15, 2010
1 parent 6910e7b commit 95b7783
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 58 deletions.
28 changes: 28 additions & 0 deletions ZonePlugin/data/System/VarADDTOZONE.txt
@@ -0,0 +1,28 @@
%META:TOPICINFO{author="ProjectContributor" comment="save topic" date="1266224965" format="1.1" reprev="1.1" version="1.1"}%
%META:TOPICPARENT{name="ZonePlugin"}%
#VarADDTOZONE
---+++ ADDTOZONE

<verbatim class="tml">
%ADDTOZONE{
"zone"
...
}%
</verbatim>

* zone: optional, comma-separated list of zones the content should be added to,
special zones are =body= and =head=, defaults to =head=
* tag: optional, identificator for the material to be added, to be used in =requires=
* requires: comma-separated list of tags that must occur before the current material, constraints the order of all material in a zone
* text: optional, text to be added to the named zone, mutually exclusive with =topic=
* topic: optional, full qualified web-topic name that contains the text to be added, mutually exclusive with =text=
* section: optional, section of the =topic= to be added, defaults to the default section between STARTINCLUDE and
STOPINCLUDE

Note, that using =topic= and =section= is actually a short form of
<verbatim class="tml">
%ADDTOZONE{
"myzone"
text="$percntINCLUDE{\"topic\" section=\"section\" warn=\"off\"}$percnt"
}%
</verbatim>
22 changes: 22 additions & 0 deletions ZonePlugin/data/System/VarRENDERZONE.txt
@@ -0,0 +1,22 @@
%META:TOPICINFO{author="ProjectContributor" date="1266225040" format="1.1" version="1.1"}%
%META:TOPICPARENT{name="ZonePlugin"}%
#VarRENDERZONE
---+++ RENDERZONE

<verbatim class="tml">
%RENDERZONE{"zone" ...}%
</verbatim>

(Note, this macro can't be spread over multiple lines.)

* zone: required, name of the zone to be expanded
* header: optional, prefix format string
* format: optional, format string for each item added to the zone, defaults to =$item=, any standard escapes like =$percnt=, =$dollar= etc can be used to delay evaluation order of the format parameter
* chomp: removed leading and trailing whitespace in formatted items, useful to pretty-print them one per line
* footer: optional, suffix format string
* separator: optional, put between each item of a zone

Note, that you can create as many zones as you like. The plugin does not restrict you to use it only for =body= and =head=. Interesting use cases in wiki applications:

* create a =sidebar= zone to add widgets
* create a =toolbar= zone to add buttons icons
50 changes: 5 additions & 45 deletions ZonePlugin/data/System/ZonePlugin.txt
@@ -1,4 +1,4 @@
%META:TOPICINFO{author="ProjectContributor" comment="reprev" date="1265976902" format="1.1" reprev="1.2" version="1.2"}%
%META:TOPICINFO{author="ProjectContributor" comment="reprev" date="1266225075" format="1.1" reprev="1.3" version="1.3"}%
---+!! %TOPIC%
<!--
* Set SHORTDESCRIPTION = %$SHORTDESCRIPTION%
Expand Down Expand Up @@ -26,50 +26,8 @@ This plugin allows authors of extensions and wiki applications to make use of th

---++ Syntax

---+++ ADDTOZONE

<verbatim class="tml">
%ADDTOZONE{
"zone"
...
}%
</verbatim>

* zone: optional, comma-separated list of zones the content should be added to,
special zones are =body= and =head=, defaults to =head=
* tag: optional, identificator for the material to be added, to be used in =requires=
* requires: comma-separated list of tags that must occur before the current material, constraints the order of all material in a zone
* text: optional, text to be added to the named zone, mutually exclusive with =topic=
* topic: optional, full qualified web-topic name that contains the text to be added, mutually exclusive with =text=
* section: optional, section of the =topic= to be added, defaults to the default section between STARTINCLUDE and
STOPINCLUDE

Note, that using =topic= and =section= is actually a short form of
<verbatim class="tml">
%ADDTOZONE{
"myzone"
text="$percntINCLUDE{\"topic\" section=\"section\" warn=\"off\"}$percnt"
}%
</verbatim>

---+++ RENDERZONE

<verbatim class="tml">
%RENDERZONE{"zone" ...}%
</verbatim>

(Note, this macro can't be spread over multiple lines.)

* zone: required, name of the zone to be expanded
* header: optional, prefix format string
* format: optional, format string for each item added to the zone, defaults to =$item=, any standard escapes like =$percnt=, =$dollar= etc can be used to delay evaluation order of the format parameter
* footer: optional, suffix format string
* separator: optional, put between each item of a zone

Note, that you can create as many zones as you like. The plugin does not restrict you to use it only for =body= and =head=. Interesting use cases in wiki applications:

* create a =sidebar= zone to add widgets
* create a =toolbar= zone to add buttons icons
%INCLUDE{"VarADDTOZONE"}%
%INCLUDE{"VarRENDERZONE"}%

---++ Perl API

Expand Down Expand Up @@ -98,6 +56,8 @@ Many thanks to the following sponsors for supporting this work:
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| 15 Feb 2010 | be more careful applying the monkey-patch to the Func API; \
parsing RENDERZONE properly but finally inserting the zone at the end of the rendering pipeline |
| 12 Feb 2010 | initial release |
| Dependencies: | %$DEPENDENCIES% |
| Home page: | Foswiki:Extensions/ZonePlugin |
Expand Down
64 changes: 51 additions & 13 deletions ZonePlugin/lib/Foswiki/Plugins/ZonePlugin.pm
Expand Up @@ -19,38 +19,49 @@ use Foswiki::Func ();
use Foswiki::Plugins ();

our $VERSION = '$Rev$';
our $RELEASE = '1.0';
our $RELEASE = '1.1';
our $SHORTDESCRIPTION = 'Gather content of a page in named zones while rendering it';
our $NO_PREFS_IN_TOPIC = 1;

# hash of all content posted to the named zone using ADDTOZONE
our %ZONES;

# hash tracking occurences of RENDERZONE, that are replaced with a token during
# first pass, then expanded showing all gathered content in completePageHandler
our %RENDERZONE;

# token used to generate the RENDERZONE placeholder while parsing
our $translationToken = "\03";

# switch on to WARN on deprecated calls to ADDZOHEAD or Foswiki::Func::addToHEAD
use constant DOWARN => 0;

# monkey-patch API ###########################################################
BEGIN {
no warnings 'redefine';
*Foswiki::Func::addToZone = \&Foswiki::Plugins::ZonePlugin::addToZone;
*Foswiki::Func::addToHEAD = \&Foswiki::Plugins::ZonePlugin::addToHead;
if ($Foswiki::cfg{Plugins}{ZonePlugin}{Enabled}) {
no warnings 'redefine';
*Foswiki::Func::addToZone = \&Foswiki::Plugins::ZonePlugin::addToZone;
*Foswiki::Func::addToHEAD = \&Foswiki::Plugins::ZonePlugin::addToHead;
}
}

##############################################################################
sub initPlugin {

Foswiki::Func::registerTagHandler('ADDTOZONE', \&ADDTOZONE);
Foswiki::Func::registerTagHandler('RENDERZONE', \&RENDERZONE);

# redefine
Foswiki::Func::registerTagHandler('ADDTOHEAD', \&ADDTOHEAD);

# RENDERZONE is handled in completePageHandler

return 1;
}

##############################################################################
sub completePageHandler {
# my ($text, $hdr)

$_[0] =~ s/%RENDERZONE{(.*?)}%/RENDERZONE($1)/ge;
$_[0] =~ s/${translationToken}RENDERZONE{(.*?)}${translationToken}/renderZoneById($1)/ge;

# get the head zone ones again and insert it at </head>
my $content = renderZone('head', {chomp=>"on", format=>'$item$n'});
Expand All @@ -65,6 +76,8 @@ sub completePageHandler {

# finally forget it
%ZONES = ();
%RENDERZONE = ();

}

##############################################################################
Expand Down Expand Up @@ -177,18 +190,43 @@ sub addToZone {
}

##############################################################################
# captures all RENDERZONE macros and inserts a token to finally insert the
# one's content at the end of the rendering pipeline
sub RENDERZONE {
my $attrs = shift;
my ($sessions, $params, $topic, $web) = @_;

my $id = scalar(keys %RENDERZONE);

$RENDERZONE{$id} = {
params => $params,
topic => $topic,
web => $web,
};

return $translationToken."RENDERZONE{$id}".$translationToken;
}

##############################################################################
sub renderZoneById {
my $id = shift;

return '' unless defined $id;

my $renderZone = $RENDERZONE{$id};

return '' unless defined $renderZone;

my $params = Foswiki::Attrs->new($attrs);
my $web = $renderZone->{web};
my $topic = $renderZone->{topic};
my $params = $renderZone->{params};
my $zone = $params->{_DEFAULT} || $params->{zone};

return renderZone($zone, $params);
return renderZone($zone, $params, $web, $topic);
}

##############################################################################
sub renderZone {
my ($zone, $params) = @_;
my ($zone, $params, $web, $topic) = @_;

return '' unless $zone && $ZONES{$zone};

Expand Down Expand Up @@ -234,8 +272,8 @@ sub renderZone {
$params->{separator} = Foswiki::Func::decodeFormatTokens($params->{separator});

my $result = $params->{header} . join($params->{separator}, @result) . $params->{footer};
$result = Foswiki::Func::expandCommonVariables($result);
$result = Foswiki::Func::renderText($result);
$result = Foswiki::Func::expandCommonVariables($result, $topic, $web);
$result = Foswiki::Func::renderText($result, $web, $topic);

return $result;
}
Expand Down
2 changes: 2 additions & 0 deletions ZonePlugin/lib/Foswiki/Plugins/ZonePlugin/MANIFEST
@@ -1,2 +1,4 @@
data/System/VarADDTOZONE.txt 0644
data/System/VarRENDERZONE.txt 0644
data/System/ZonePlugin.txt 0644
lib/Foswiki/Plugins/ZonePlugin.pm 0644

0 comments on commit 95b7783

Please sign in to comment.