[Petal] Multiple Conditions?

jhiver at mkdoc.com jhiver at mkdoc.com
Sun Mar 9 19:47:54 GMT 2003


> Perhaps the website has more?  I've seen several mentions of it on this
> list, but I've only looked at the docs on CPAN.  Come to think of it,
> where is the website?  I've never noticed it in many Google searches.
> (Hmm, trying to find a Petal homepage is currently crashing Google!)

Unfortunately the website has much less, and is very new :-(

I need to put more stuff on it, but you know how things are... everybody
always agrees that more docs are needed except that nobody wants to
do them :-)

Anyway the site is at the address http://www.petal.mkdoc.org/

In the (happy) event where you'd like to put some example on the site
just gimme a should on the ML and I'll create an editor account for you.


> template only wants to list a chapter if the chapter contains one of a
> set of elements.  The template has the ability to ask for a list of a
> type of element in a chapter, so what I'd like do to is something like:
>
> <section petal:repeat="chapter $book/chapters">

Does this work at all? Using the $ sign is for inline stuff... Petal must
be pretty forgiving :-)

For example:

<span petal:replace="hello/world">Blargh</span>

Is equivalent to

$hello/world


> <chapter_section petal:condition="true:$chapter/descendants 'Figure'

Another remark, when passing arguments to methods if your argument
value hasn't got any characters you can use the GNU style double dash.

So in this case, the expression "true:chapter/descendants --Figure"
should do.


To come back to your question, Petal allows you to write your own
modifiers. So the workaround would be to write a or: modifier...

Basically at the initialization of your program you could have
the following piece of code...

(warning - this code is totally untested and has been typed in
outlook express, so you might want to make sure it really does
what it needs to do...)

$MODIFIERS->{'bool_or:'} = sub {
    my $hash  = shift;
    my $argument = shift;

    # let's split by any '|' which is not preceeded
    # by a backslash
    my @expressions = split /(?<!\\)\|/, $argument;

    while (scalar @expressions) {
        my $expr = shift (@expressions);
        next unless (defined $expr and $expr !~ /^\s*$/);
        return 1 if ($hash->{"true: $expr"});
    }

    return;
}

This - hopefully - would let you do:

<chapter_section
    petal:condition="bool_or: chapter/descendants --Figure |
                                           chapter/descendants --CodeListing
|
                                           chapter/descendants --Whatever">

do some stuff...
</chapter_section>


Another way of doing it, as you suggested, would be to add a method to your
'chapter' object, i.e. has_any (@list) that would let you write:

<chapter_section

   petal:condition="true:chapter/has_any --Figure --CodeListing --Whatever">
do some stuff...
</chapter_section>


this could look like: (evil code ahead)

sub has_any
{
    my $self = shift;
    scalar @_ and $self->descendant (shift()) or $self->has_any (@_);
}


Tell us how it goes,
Jean-Michel.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.email-lists.org/pipermail/petal/attachments/20030309/1bfb73e2/attachment.htm


More information about the Petal mailing list