html.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # -*- test-case-name: twisted.web.test.test_html -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """I hold HTML generation helpers.
  5. """
  6. from twisted.python import log
  7. from twisted.python.compat import NativeStringIO as StringIO, escape
  8. from twisted.python.deprecate import deprecated
  9. from incremental import Version
  10. @deprecated(Version('Twisted', 15, 3, 0), replacement='twisted.web.template')
  11. def PRE(text):
  12. "Wrap <pre> tags around some text and HTML-escape it."
  13. return "<pre>"+escape(text)+"</pre>"
  14. @deprecated(Version('Twisted', 15, 3, 0), replacement='twisted.web.template')
  15. def UL(lst):
  16. io = StringIO()
  17. io.write("<ul>\n")
  18. for el in lst:
  19. io.write("<li> %s</li>\n" % el)
  20. io.write("</ul>")
  21. return io.getvalue()
  22. @deprecated(Version('Twisted', 15, 3, 0), replacement='twisted.web.template')
  23. def linkList(lst):
  24. io = StringIO()
  25. io.write("<ul>\n")
  26. for hr, el in lst:
  27. io.write('<li> <a href="%s">%s</a></li>\n' % (hr, el))
  28. io.write("</ul>")
  29. return io.getvalue()
  30. @deprecated(Version('Twisted', 15, 3, 0), replacement='twisted.web.template')
  31. def output(func, *args, **kw):
  32. """output(func, *args, **kw) -> html string
  33. Either return the result of a function (which presumably returns an
  34. HTML-legal string) or a sparse HTMLized error message and a message
  35. in the server log.
  36. """
  37. try:
  38. return func(*args, **kw)
  39. except:
  40. log.msg("Error calling %r:" % (func,))
  41. log.err()
  42. return PRE("An error occurred.")