Edgewall Software

Ticket #202: Element-__html__-r826+t211.diff

File Element-__html__-r826+t211.diff, 3.3 KB (added by cboos, 7 years ago)

Previous patch (fixed) + added Element.__html__ method. Applies on ticket:211:mapping-style-__mod__-only-and-no-Markup_new-r826.diff Download

  • genshi/_speedups.c

    diff --git a/genshi/_speedups.c b/genshi/_speedups.c
    a b  
    5454    if (PyObject_TypeCheck(text, &MarkupType)) { 
    5555        Py_INCREF(text); 
    5656        return text; 
     57    } 
     58    if (PyObject_HasAttrString(text, "__html__")) { 
     59        ret = PyObject_CallMethod(text, "__html__", NULL); 
     60        args = PyTuple_New(1); 
     61        if (args == NULL) { 
     62            Py_DECREF(ret); 
     63            return NULL; 
     64        } 
     65        PyTuple_SET_ITEM(args, 0, ret); 
     66        ret = MarkupType.tp_new(&MarkupType, args, NULL); 
     67        Py_DECREF(args); 
     68        return ret; 
    5769    } 
    5870    in = (PyUnicodeObject *) PyObject_Unicode(text); 
    5971    if (in == NULL) { 
     
    183195        return text; 
    184196    } 
    185197    return escape(text, quotes); 
     198} 
     199 
     200static PyObject * 
     201Markup_html(PyObject *self) 
     202{ 
     203    Py_INCREF(self); 
     204    return self; 
    186205} 
    187206 
    188207PyDoc_STRVAR(join__doc__, 
     
    513532} MarkupObject; 
    514533 
    515534static PyMethodDef Markup_methods[] = { 
     535    {"__html__", (PyCFunction) Markup_html, METH_NOARGS, NULL}, 
    516536    {"escape", (PyCFunction) Markup_escape, 
    517537     METH_VARARGS|METH_CLASS|METH_KEYWORDS, escape__doc__}, 
    518538    {"join", (PyCFunction)Markup_join, METH_VARARGS|METH_KEYWORDS, join__doc__}, 
  • genshi/builder.py

    diff --git a/genshi/builder.py b/genshi/builder.py
    a b  
    6868Hello, <em>world</em>! 
    6969""" 
    7070 
    71 from genshi.core import Attrs, Namespace, QName, Stream, START, END, TEXT 
     71from genshi.core import Attrs, Namespace, QName, Stream, START, END, TEXT, \ 
     72                        Markup 
    7273 
    7374__all__ = ['Fragment', 'Element', 'ElementFactory', 'tag'] 
    7475__docformat__ = 'restructuredtext en' 
     
    105106        return str(self.generate()) 
    106107 
    107108    def __unicode__(self): 
     109        return unicode(self.generate()) 
     110 
     111    def __html__(self): 
    108112        return unicode(self.generate()) 
    109113 
    110114    def append(self, node): 
  • genshi/core.py

    diff --git a/genshi/core.py b/genshi/core.py
    a b  
    442442    def __repr__(self): 
    443443        return '<%s %r>' % (self.__class__.__name__, unicode(self)) 
    444444 
     445    def __html__(self): 
     446        return self 
     447 
    445448    def join(self, seq, escape_quotes=True): 
    446449        """Return a `Markup` object which is the concatenation of the strings 
    447450        in the given sequence, where this `Markup` object is the separator 
     
    484487            return cls() 
    485488        if type(text) is cls: 
    486489            return text 
     490        if hasattr(text, '__html__'): 
     491            return Markup(text.__html__()) 
     492 
    487493        text = unicode(text).replace('&', '&amp;') \ 
    488494                            .replace('<', '&lt;') \ 
    489495                            .replace('>', '&gt;') 
  • genshi/tests/builder.py

    diff --git a/genshi/tests/builder.py b/genshi/tests/builder.py
    a b  
    5151        self.assertEqual((Stream.END, 'b'), xml[3][:2]) 
    5252        self.assertEqual((Stream.END, 'span'), xml[4][:2]) 
    5353 
     54    def test_Markup_escape(self): 
     55        from genshi.core import Markup 
     56        m = Markup("See %s") % tag.a("genshi", href='http://genshi.edgwall.org') 
     57        self.assertEqual(m, Markup('See <a href="http://genshi.edgwall.org">' 
     58                                   'genshi</a>')) 
    5459 
    5560def suite(): 
    5661    suite = unittest.TestSuite()