Hi asm,
I am also using TinyMCE and needed to deal with the same thing. What
I did was make a customized TinyMCE widget (sub-class) that, amongst
other things, tells TinyMCE to only use XML escapes and leave the rest
as Unicode. The way I see it, HTML escapes are only really applicable
if you are not using a Unicode encoding (like utf-8), so I let them
through, but still escape the dangerous ones.
I also use a custom validator that makes sure what I get from the
widget is valid XML. It also requires that there be at least one
paragraph tag, but that is an application-specific requirement.
It's very likely not the most efficient code and might not even be the
best way to do it, but works for me.
Anyway, I have posted the code below in case it helps. Any
suggestions for improvements is always welcome. :)
Someday I'll finally get around to releasing a bunch more of of my
TurboGears code... :(
Enjoy!
-- Custom widget --
class TinyMCE(RealTinyMCE):
default = u'<p></p>'
new_options = dict(
# Custom styles
content_css='/static/css/tinymce.css',
popups_css='/static/css/editor_popup.css',
editor_css='/static/css/editor_ui.css',
theme_advanced_disable='styleselect',
relative_urls=False,
# These are the only character entities that are valid in
# both XML and HTML. Only these are used for maximum
# compatibilty while still providing safe escaping.
entities='34,quot,38,amp,60,lt,62,gt')
def __init__(self, not_empty=False, return_tree=False,
extra_check=None, *args, **kwargs):
if 'validator' not in kwargs:
kwargs['validator'] = UnicodeXML(not_empty=not_empty,
return_tree=return_tree, extra_check=extra_check)
super(TinyMCE, self).__init__(*args, **kwargs)
-- custom validator --
from elementtree.ElementTree import XML
from xml.parsers.expat import ExpatError
class UnicodeXML(UnicodeString):
return_tree = False
extra_check = None
def _to_python(self, value, state):
value = super(UnicodeXML, self)._to_python(value, state)
try:
tree = XML((u'<root>%s</root>' % value).encode('utf-8'))
except ExpatError:
raise Invalid(u'Not valid XML.', value, state)
if callable(self.extra_check):
self.extra_check(tree, value, state)
if self.return_tree:
return tree
return value
-- end --
P.S. Thanks Florent. :)
On May 13, 4:52 am, asm <[EMAIL PROTECTED]> wrote:
> Thank you again. It took a little while but it is all working now. To
> find the final bits: I went to TurboBlog to see what you were doing.
>
> I had not replied here yet because I am writing it up to contribute to
> 'roughdocs'.
>
> > Unicode support in python can be tough to understand in the beginning,
> > but once you grab the meaning, it is becomes really easy and sooo
> > logical.
>
> I was a bit surprised because I was trying to follow the Unicode
> everywhere approach in the hope there would be no problems.
>
> > The python Unicode support has been despised by many in the forums but
> > don't get fooled by these whiners :) they just didn't get it and then
> > tried to convince the world that is was python's fault :p
>
> Am really not knowledgeable on Unicode, nor am I prepared to comment
> on the Python implementation. I would expect that it is good and that
> the Py3K implementation might be better(!).
>
> > Krys Wilken who is also a TurboGears user, blogged sometime ago on his
> > personal blog about the Unicode support in Python. I liked his point of
> > view:
>
> >http://krys.ca/entry/58/
>
> > (the site seems down right now but I'm sure it will come back on-line)
>
> > Fredrik Lundh (ElementTree author to say the least) also posted some
> > interesting observations on Unicode usage with python on his web site:
>
> >http://effbot.org/zone/unicode-objects.htm
>
> > I am sure you'll find a lot of other interesting readings all over the
> > world wide web :)
>
> Thank you again for your help and these references which I will draw
> upon in writing a HOWTO for TurboTinyMCE.
>
> Most of the issues I hit against were the impedence between HTML in
> ISO8859-1 and XHTML (XML) in Unicode. It was interesting to read that
> someone has done a patch for TinyMCE so that it edits XHTML. I am not
> sure how thorough it would be in handling replacements for HTML
> entities. It might be beneficial for a future TurboGears widget to
> work with a patched TinyMCE and XHTML. Perhaps ToscaWidgets has
> already managed this.
>
> Thanks again
> A
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---