Insert link on page.

31 views
Skip to first unread message

Jurgens de Bruin

unread,
Jul 27, 2011, 5:07:27 AM7/27/11
to Genshi
I am currently developing a TG webpage and would like to add a link to
a webpage using Genshi.

Currently my code is as follows:

@property
def imagelink(self):
href = 'http://www.google.co.za/search?tbm=isch&q=%s+%s'%
(self.genus,self.species)
link = "<a href=\"%s\" target=\"_blank\">%s</a>"%(href,self.genus)
return MarkupTemplate(link).generate()

Upon which I get the following error:

return MarkupTemplate(link.strip()).generate()
File "build/bdist.macosx-10.6-universal/egg/genshi/template/
markup.py", line 67, in __init__
allow_exec=allow_exec)
File "build/bdist.macosx-10.6-universal/egg/genshi/template/
base.py", line 408, in __init__
raise TemplateSyntaxError(e.msg, self.filepath, e.lineno,
e.offset)
TemplateSyntaxError: not well-formed (invalid token): line 1, column
51 (<string>, line 1)

I do not understand why this is happening. Any help please??

Kyle Alan Hale

unread,
Jul 27, 2011, 10:54:58 AM7/27/11
to gen...@googlegroups.com
MarkupTemplate expects well-formed XML; use entities when appropriate.  In this case, you have an ampersand in your URL that needs to be replaced with "&amp;".

fviktor

unread,
Jul 27, 2011, 8:28:32 PM7/27/11
to Genshi
(Copying my response here, since replying to the e-mail half day ago
did not work.)

The problem is that you put an & character without escaping into your
template, which renders your markup invalid.

Try this:

import cgi

...

link = "<a href=\"%s\" target=\"_blank\">%s</a>" % (
cgi.escape(href), cgi.escape(self.genus))

Please note, that cgi.escape is not perfect for escaping attribute
values, you should use xml.sax.saxutils.escape for that instead.

BTW, why do you substitute your variables into the template instead of
passing that to Genshi? Genshi would escape it automatically for you:

link_template = MarkupTemplate(
'<a href="${href}" target="_blank">${text}</a>')

link_stream = link_template.generate(href=href, text=self.genus)
link_html = link_stream.render()

Viktor Ferenczi

unread,
Jul 27, 2011, 6:12:23 AM7/27/11
to gen...@googlegroups.com
Hi,

The problem is that you put an & character without escaping into your
template, which renders your markup invalid.

Try this:

import cgi

...

link = "<a href=\"%s\" target=\"_blank\">%s</a>" % (
cgi.escape(href), cgi.escape(self.genus))

Please note, that cgi.escape is not perfect for escaping attribute
values, you should use xml.sax.saxutils.escape for that instead.

BTW, why do you substitute your variables into the template instead of
passing that to Genshi? Genshi would escape it automatically for you:

link_template = MarkupTemplate(
'<a href="${href}" target="_blank">${text}</a>')

link_stream = link_template.generate(href=href, text=self.genus)
link_html = link_stream.render()

Hope it helps.

Viktor

Reply all
Reply to author
Forward
0 new messages