comparison genshi/core.py @ 737:ca72e3dc443d trunk

Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
author cmlenz
date Thu, 05 Jun 2008 17:00:15 +0000
parents 4bc6741b2811
children 1fd97b75cdc9 25b8ffa7fe65 5d8ca3f527af
comparison
equal deleted inserted replaced
736:a1e8b24bc1bb 737:ca72e3dc443d
243 return self.render() 243 return self.render()
244 244
245 def __unicode__(self): 245 def __unicode__(self):
246 return self.render(encoding=None) 246 return self.render(encoding=None)
247 247
248 def __html__(self):
249 return self
250
248 251
249 START = Stream.START 252 START = Stream.START
250 END = Stream.END 253 END = Stream.END
251 TEXT = Stream.TEXT 254 TEXT = Stream.TEXT
252 XML_DECL = Stream.XML_DECL 255 XML_DECL = Stream.XML_DECL
483 """ 486 """
484 if not text: 487 if not text:
485 return cls() 488 return cls()
486 if type(text) is cls: 489 if type(text) is cls:
487 return text 490 return text
491 if hasattr(text, '__html__'):
492 return Markup(text.__html__())
493
488 text = unicode(text).replace('&', '&') \ 494 text = unicode(text).replace('&', '&') \
489 .replace('<', '&lt;') \ 495 .replace('<', '&lt;') \
490 .replace('>', '&gt;') 496 .replace('>', '&gt;')
491 if quotes: 497 if quotes:
492 text = text.replace('"', '&#34;') 498 text = text.replace('"', '&#34;')
Copyright (C) 2012-2017 Edgewall Software