common/uilib.py
changeset 2234 1fbcf202882d
parent 2208 1c73148ed912
child 2312 af4d8f75c5db
equal deleted inserted replaced
2209:2b91abd9f5a4 2234:1fbcf202882d
    90         return u''
    90         return u''
    91     return REF_PROG.sub(lambda obj, view=view:_subst_rql(view, obj), text)
    91     return REF_PROG.sub(lambda obj, view=view:_subst_rql(view, obj), text)
    92 
    92 
    93 # fallback implementation, nicer one defined below if lxml is available
    93 # fallback implementation, nicer one defined below if lxml is available
    94 def soup2xhtml(data, encoding):
    94 def soup2xhtml(data, encoding):
    95     return data
    95     # normalize line break
       
    96     # see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1
       
    97     return u'\n'.join(data.splitlines())
    96 
    98 
    97 # fallback implementation, nicer one defined below if lxml> 2.0 is available
    99 # fallback implementation, nicer one defined below if lxml> 2.0 is available
    98 def safe_cut(text, length):
   100 def safe_cut(text, length):
    99     """returns a string of length <length> based on <text>, removing any html
   101     """returns a string of length <length> based on <text>, removing any html
   100     tags from given text if cut is necessary."""
   102     tags from given text if cut is necessary."""
   121     def soup2xhtml(data, encoding):
   123     def soup2xhtml(data, encoding):
   122         """tidy (at least try) html soup and return the result
   124         """tidy (at least try) html soup and return the result
   123         Note: the function considers a string with no surrounding tag as valid
   125         Note: the function considers a string with no surrounding tag as valid
   124               if <div>`data`</div> can be parsed by an XML parser
   126               if <div>`data`</div> can be parsed by an XML parser
   125         """
   127         """
       
   128         # normalize line break
       
   129         # see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1
       
   130         data = u'\n'.join(data.splitlines())
       
   131         # XXX lxml 1.1 support still needed ?
   126         xmltree = etree.HTML('<div>%s</div>' % data)
   132         xmltree = etree.HTML('<div>%s</div>' % data)
   127         # NOTE: lxml 1.1 (etch platforms) doesn't recognize
   133         # NOTE: lxml 1.1 (etch platforms) doesn't recognize
   128         #       the encoding=unicode parameter (lxml 2.0 does), this is
   134         #       the encoding=unicode parameter (lxml 2.0 does), this is
   129         #       why we specify an encoding and re-decode to unicode later
   135         #       why we specify an encoding and re-decode to unicode later
   130         body = etree.tostring(xmltree[0], encoding=encoding)
   136         body = etree.tostring(xmltree[0], encoding=encoding)