html.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 html import escape
  7. from io import StringIO
  8. from incremental import Version
  9. from twisted.python import log
  10. from twisted.python.deprecate import deprecated
  11. @deprecated(Version("Twisted", 15, 3, 0), replacement="twisted.web.template")
  12. def PRE(text):
  13. "Wrap <pre> tags around some text and HTML-escape it."
  14. return "<pre>" + escape(text) + "</pre>"
  15. @deprecated(Version("Twisted", 15, 3, 0), replacement="twisted.web.template")
  16. def UL(lst):
  17. io = StringIO()
  18. io.write("<ul>\n")
  19. for el in lst:
  20. io.write("<li> %s</li>\n" % el)
  21. io.write("</ul>")
  22. return io.getvalue()
  23. @deprecated(Version("Twisted", 15, 3, 0), replacement="twisted.web.template")
  24. def linkList(lst):
  25. io = StringIO()
  26. io.write("<ul>\n")
  27. for hr, el in lst:
  28. io.write(f'<li> <a href="{hr}">{el}</a></li>\n')
  29. io.write("</ul>")
  30. return io.getvalue()
  31. @deprecated(Version("Twisted", 15, 3, 0), replacement="twisted.web.template")
  32. def output(func, *args, **kw):
  33. """output(func, *args, **kw) -> html string
  34. Either return the result of a function (which presumably returns an
  35. HTML-legal string) or a sparse HTMLized error message and a message
  36. in the server log.
  37. """
  38. try:
  39. return func(*args, **kw)
  40. except BaseException:
  41. log.msg(f"Error calling {func!r}:")
  42. log.err()
  43. return PRE("An error occurred.")