kid questions.

6 views
Skip to first unread message

Fred C

unread,
Jun 11, 2006, 8:59:05 AM6/11/06
to turbo...@googlegroups.com

Hello,

In my kid template I have this following line: <div
class="reviewitem" py:content="XML(review.Content)" />.
My problem is when the XML() argument is not a very clean html
string, XML fail miserably.
How can I manage to roughly clean that string (in the template) or at
least return the string not formated, instead of having an exception
thrown.

Thanks

-fred-


Mark Ramm

unread,
Jun 11, 2006, 10:54:30 AM6/11/06
to turbo...@googlegroups.com
You could put an ajax call on your page, retrieve that string through
an seprate request and then render your string using innerHTML from
javascript.

But if the HTML isn't valid, you may have trouble rendering it no
matter what you do.


--
Mark Ramm-Christensen
email: mark at compoundthinking dot com
blog: www.compoundthinking.com/blog

Sven H.

unread,
Jun 11, 2006, 3:29:05 PM6/11/06
to turbo...@googlegroups.com
Mark Ramm schrieb:

> You could put an ajax call on your page, retrieve that string through
> an seprate request and then render your string using innerHTML from
> javascript.
>
um.. that sounds like a terribly bad idea.
a) it produces a webpage where the content is only availble with
javascript and a AJAX-enabled browser.
b) What sense does it make to use kid and then try to circumvent one of
kid's main features, ensuring correctness of output?

-- Sven

Sven H.

unread,
Jun 11, 2006, 3:39:18 PM6/11/06
to turbo...@googlegroups.com
Fred C schrieb:
I think the only way apart from horribly circumventing kid is to have
clean (X)HTML markup.
I'm currently using a validator like this to validate XHTML markup..

class XHTMLValidator(validators.FancyValidator):
def _to_python(self, value, state = None):
try:
template = kid.Template(
"""<html xmlns:py="http://purl.org/kid/ns#">%s</html>"""
% value)
except Exception, e:
raise validators.Invalid( e.args[0], value, state)

s = template.serialize( output = "xml")
return s[s.index("<html>")+6:-7]


It uses a temporary template to check the XHTML code for correctness,
feeds errors back as
validation errors and extracts the cleaned-up XHTML markup from the
serialized output.

-- Sven

Fred C

unread,
Jun 11, 2006, 4:13:44 PM6/11/06
to turbo...@googlegroups.com

Sorry, I can't use AJAX. I know AJAX is very fashionable right now
but it is not an option for this page. Also I think I would have the
same problem.

Mark Ramm

unread,
Jun 11, 2006, 4:55:43 PM6/11/06
to turbo...@googlegroups.com
On 6/11/06, Fred C <bsd...@gmail.com> wrote:
> Sorry, I can't use AJAX. I know AJAX is very fashionable right now
> but it is not an option for this page. Also I think I would have the
> same problem.

My suggestion was a bit tongue in cheek ;) It's the only way I know
of to 100% avoid KID while using KID. And while you can get around
Kid's validation that way, I don't particularly think it's a good idea
either. But I thought I'd throw it out there in case you were
looking for an ugly hack.

What I should have mentioned is that an easier way to get around KID
in a situation like this is not to use KID at all. You can use
Cheetah instead, and get around KID's validity requirement. Cheetah
shouldn't care a bit about how broken your HTML is.

Automatic HTML repair sounds like it would be nice, and perhaps it's
possible for some subset of HTML errors, but I doubt it's really
feasible unless your HTML is already almost right, and is broken an
easy to detect way. It seems like your best choice is to go to the
source, and see if you can fix that.

--Mark

Mark Ramm

unread,
Jun 11, 2006, 4:59:09 PM6/11/06
to turbo...@googlegroups.com
> um.. that sounds like a terribly bad idea.

I agree. I never said it was good, and I even said that it probably
wouldn't work! But it's the only ugly hack I could think of to get
done what Fred was asking for.

The right answer is exactly what you say. Don't use KID when you want
to have invalid output. The right thing to do is to create valid
output, but if that's not possible the second best thing is to use
Cheetah, or one of the other available templates with TurboGears
plugins that doesn't care about validity.

Jorge Vargas

unread,
Jun 11, 2006, 5:37:08 PM6/11/06
to turbo...@googlegroups.com
On 6/11/06, Fred C <bsd...@gmail.com> wrote:


Hello,

In my kid template I have this following line:  <div
class="reviewitem" py:content="XML(review.Content)" />.
My problem is when the XML() argument is not a very clean html
string, XML fail miserably.

why not make that string valid xhtml?  XML is evil inside kid and should be avoid

How can I manage to roughly clean that string (in the template) or at
least return the string not formated, instead of having an exception
thrown.

is that data coming from the db or a user? why not clean it up at insertion time?

Thanks

-fred-



Fred C

unread,
Jun 11, 2006, 8:15:47 PM6/11/06
to turbo...@googlegroups.com

I think you are right and I am going to use Cheetah instead of kid. I
like kid and I like the fact that it forces you to write clean code,
but it can be a pain when you get part of your html content from a
third party with a lot of small errors.

I have try to cleanup this code with ElementTidy, but this tool is
not design to clean up a single string. It did not gave me the result
expected. For fixing the source this is impossible, the only thing I
can do with the source is read it. And I don't think the people who
are producing that source gonna start closing the </p> , </li> or
other tags who are not really affecting the presentation on their
browsers.

Thanks for you answer.
-fred-


Fred C

unread,
Jun 11, 2006, 8:18:03 PM6/11/06
to turbo...@googlegroups.com
Just because this data is coming from a database from a third party and they don't feel the need to produce a clean html since it is displayed well in their browsers.

-fred-

Michael P. Soulier

unread,
Jun 11, 2006, 8:24:15 PM6/11/06
to turbo...@googlegroups.com
On 11/06/06 Fred C said:

> I think you are right and I am going to use Cheetah instead of kid. I
> like kid and I like the fact that it forces you to write clean code,
> but it can be a pain when you get part of your html content from a
> third party with a lot of small errors.

Not to mention when it mangles your code by putting spaces between images when
you don't want them. How does one change templates? I think I'm more
interested in Myghty.

Mike
--
Michael P. Soulier <msou...@digitaltorque.ca>
"Any intelligent fool can make things bigger and more complex... It
takes a touch of genius - and a lot of courage to move in the opposite
direction." --Albert Einstein

Ed Singleton

unread,
Jun 12, 2006, 4:32:08 AM6/12/06
to turbo...@googlegroups.com
A possible solution is to use BeautifulSoup.

It's quite good at producing valid markup out of invalid markup (in
terms of matching tags and stuff).

It's not very quick, but it can handle very bad markup.

Ed

On 6/11/06, Fred C <bsd...@gmail.com> wrote:
>
>

Reply all
Reply to author
Forward
0 new messages