recently i've tried to modify our trac setup a bit and started playing
with genshi. however, even after quite a bit of investigation i
couldn't figure out how to simply add an additional attribute to an
existing tag using `py:match` and `py:attrs`: simply setting
`py:attrs` to a dict removes the already existing attributes from the
tag, and trying to find information about how to manipulate the stream
object provided by "select('@*')" didn't lead anywhere either.
any help on this would be greatly appreciated! :)
best regards,
andi
--
zeidler it consulting - http://zitc.de/ - in...@zitc.de
friedelstraße 31 - 12047 berlin - telefon +49 30 25563779
pgp key at http://zitc.de/pgp - http://wwwkeys.de.pgp.net/
plone 3.2.2 released! -- http://plone.org/products/plone/
what i'm trying to do is add an accesskey attribute to an existing link:
<a class="next" href="/changeset/25997" title="Changeset
25997">Next Changeset</a>
so that it becomes:
<a class="next" href="/changeset/25997" title="Changeset 25997"
accesskey="n">Next Changeset</a>
trac uses a genshi template that is applied to every rendered page.
currently mine looks like:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/" py:strip="">
<a py:match="a[@class='next']" py:attrs="select('@*')">$
{select('*|text()')}</a>
</html>
this matches the link but otherwise effectively does nothing (since
all existing attributes are replaced with all existing
attributes :)). like i said, a `py:attrs="{'accesskey': 'n'}"`
replaces _all_ attributes, i.e. ending up in:
<a accesskey="n">Next Changeset</a>
what i'd like to know is how to add the extra attribute here (without
using extra python code — since i don't think i can wrt the way trac
is set up).
On May 7, 2009, at 11:22 AM, kindy wrote:what i'm trying to do is add an accesskey attribute to an existing link:
could you give some sample code?
<a class="next" href="/changeset/25997" title="Changeset 25997">Next Changeset</a>
so that it becomes:
<a class="next" href="/changeset/25997" title="Changeset 25997" accesskey="n">Next Changeset</a>
trac uses a genshi template that is applied to every rendered page. currently mine looks like:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/" py:strip="">
<a py:match="a[@class='next']" py:attrs="select('@*')">${select('*|text()')}</a>
</html>
this matches the link but otherwise effectively does nothing (since all existing attributes are replaced with all existing attributes :)). like i said, a `py:attrs="{'accesskey': 'n'}"` replaces _all_ attributes, i.e. ending up in:
<a accesskey="n">Next Changeset</a>
what i'd like to know is how to add the extra attribute here (without using extra python code — since i don't think i can wrt the way trac is set up).
best regards,
andi
--
zeidler it consulting - http://zitc.de/ - in...@zitc.de
friedelstraße 31 - 12047 berlin - telefon +49 30 25563779
pgp key at http://zitc.de/pgp - http://wwwkeys.de.pgp.net/
plone 3.2.2 released! -- http://plone.org/products/plone/
On Thu, May 7, 2009 at 6:46 PM, Andreas Zeidler <a...@zitc.de> wrote:On May 7, 2009, at 11:22 AM, kindy wrote:what i'm trying to do is add an accesskey attribute to an existing link:
could you give some sample code?
<a class="next" href="/changeset/25997" title="Changeset 25997">Next Changeset</a>
so that it becomes:
<a class="next" href="/changeset/25997" title="Changeset 25997" accesskey="n">Next Changeset</a>
trac uses a genshi template that is applied to every rendered page. currently mine looks like:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/" py:strip="">
<a py:match="a[@class='next']" py:attrs="select('@*')">${select('*|text()')}</a><a py:match="a[@class='next']" accesskey="n" py:attrs="select('@*')">${select('*|text()')}</a>will this OK?
> On May 7, 2009, at 6:09 PM, kindy wrote:
> > On Thu, May 7, 2009 at 6:46 PM, Andreas Zeidler <a...@zitc.de> wrote:
> > On May 7, 2009, at 11:22 AM, kindy wrote:
> > could you give some sample code?
> >
> > what i'm trying to do is add an accesskey attribute to an existing
> > link:
> >
> > <a class="next" href="/changeset/25997" title="Changeset
> > 25997">Next Changeset</a>
> >
> > so that it becomes:
> >
> > <a class="next" href="/changeset/25997" title="Changeset 25997"
> > accesskey="n">Next Changeset</a>
> >
> > trac uses a genshi template that is applied to every rendered
> > page. currently mine looks like:
> >
> > <html xmlns="http://www.w3.org/1999/xhtml"
> > xmlns:py="http://genshi.edgewall.org/" py:strip="">
> > <a py:match="a[@class='next']" py:attrs="select('@*')">$
> > {select('*|text()')}</a>
> >
> > <a py:match="a[@class='next']" accesskey="n"
> > py:attrs="select('@*')"> ${select('*|text()')}</a>
> >
> > will this OK?
>
> no, this doesn't work. the `py:attrs` still replaces all attributes
> including the one given here...
>
My first reaction was that py:attrs accepts any expression, so just
write an expression that merges the manually-added attribute with the
ones from the matched element. However, this won't be straightforward at
all because select() returns a Stream: it would seem to require you to
manually insert stream events into it.
If there were a way to get the attributes from the matched element in a
more useful structure (like a dict, which you can update and dump right
back into py:attrs) then this would be trivial, but so far I can't find
anything that does this out of the box...
An analogue of xsl:attribute would make this trivial as well, but I
doubt that can be done with the stream-based design of Genshi.
-Kyle
exactly. i made it to this point and was expecting the API of the
stream object to be of help, actually. having figured out that the
stream iterator returns triplets similar to what i knew from
elementtree, i decided not to try insert a few more inside a single
expression. at this point i was kinda assuming there must be a far
easier way which i had simply failed to see so far...
> If there were a way to get the attributes from the matched element
> in a
> more useful structure (like a dict, which you can update and dump
> right
> back into py:attrs) then this would be trivial, but so far I can't
> find
> anything that does this out of the box...
that would indeed be more like what one would expect. however, i
couldn't find any other examples either.
> An analogue of xsl:attribute would make this trivial as well, but I
> doubt that can be done with the stream-based design of Genshi.
not sure, but perhaps someone else knows the missing piece here... :)
thanks for looking into this, though!
best regards,
On May 8, 2009, at 8:46 PM, Kyle Schaffrick wrote:exactly. i made it to this point and was expecting the API of the stream object to be of help, actually. having figured out that the stream iterator returns triplets similar to what i knew from elementtree, i decided not to try insert a few more inside a single expression. at this point i was kinda assuming there must be a far easier way which i had simply failed to see so far...
My first reaction was that py:attrs accepts any expression, so just
write an expression that merges the manually-added attribute with the
ones from the matched element. However, this won't be straightforward at
all because select() returns a Stream: it would seem to require you to
manually insert stream events into it.
that would indeed be more like what one would expect. however, i couldn't find any other examples either.
If there were a way to get the attributes from the matched element in a
more useful structure (like a dict, which you can update and dump right
back into py:attrs) then this would be trivial, but so far I can't find
anything that does this out of the box...
not sure, but perhaps someone else knows the missing piece here... :) thanks for looking into this, though!
An analogue of xsl:attribute would make this trivial as well, but I
doubt that can be done with the stream-based design of Genshi.
best regards,
andi
--
zeidler it consulting - http://zitc.de/ - in...@zitc.de
friedelstraße 31 - 12047 berlin - telefon +49 30 25563779
pgp key at http://zitc.de/pgp - http://wwwkeys.de.pgp.net/
plone 3.2.2 released! -- http://plone.org/products/plone/
many thanks for your help! both of your versions:
<a py:match="a[@class='next']"
py:attrs="dict(list(list(select('@*'))[0]) + [('foo', 'p')])">...</a>
<a py:match="a[@class='next']" py:attrs="dict([i for a in
select('@*') for i in a] + [('foo', 'n')])">...</a>
do work indeed. however, after wondering for a bit why the accesskey
attribute still didn't show up, it turned out that it somehow gets
filtered: adding an attribute "foo" works fine, but as soon as i name
it "accesskey" it gets dropped again. i have no idea why that is, but
suppose it's specific to trac...
but anyway, thanks again for looking into this!
regards,
andi
On May 9, 2009, at 3:01 AM, Nicholas Dudfield wrote:
> Here's a horrible hack (which does work) I did for rewriting relative
> image paths to absolutes
>
> <py:match path="img">
> <img py:attrs="dict((k, '/%s'%v if k == 'src' else v) for a in
> select('@*') for (k,v) in a)" />
> </py:match
>
> Based on the above I'm guessing something like this could work
> (untested) :
>
> dict([kv for a in select('@*') for kv in a] + [(newk, newv)])
>
> Good luck
On May 10, 2009, at 3:59 PM, kindy wrote:
> <a py:match="a[@class='next']" py:attrs="dict(list(list(select('@*'))
> [0]) + [('accesskey', 'n')])">
>
> attrs = select('@*') # is a stream
> attrs = list(attrs) # convert to a list
> attrs = attrs[0] # the first one is the attributes (Attr -<
> Tuple)
> attrs = list(attrs) # convert the tuple to list, because
> # we need to add our custom attr
> attrs += [('accesskey', 'n')] # add out attr
>
> attrs = dict(attrs)
>
> i hope this can do some help.
> :)
--
hi nicholas, hi kindy!many thanks for your help! both of your versions:
<a py:match="a[@class='next']"
py:attrs="dict(list(list(select('@*'))[0]) + [('foo', 'p')])">...</a>
<a py:match="a[@class='next']" py:attrs="dict([i for a in
select('@*') for i in a] + [('foo', 'n')])">...</a>do work indeed. however, after wondering for a bit why the accesskey
attribute still didn't show up, it turned out that it somehow gets
filtered: adding an attribute "foo" works fine, but as soon as i name
it "accesskey" it gets dropped again. i have no idea why that is, but
suppose it's specific to trac...