url.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """
  2. Sometimes it feels like 80% of the battle with creating a
  3. static website is getting all the URLs correct.
  4. These are some helpers.
  5. Here are some naming conventions for URL pieces:
  6. zulip_url: https://example.zulip.com
  7. site_url: https://example.zulip-archive.com
  8. html_root: archive
  9. And then URLs use Zulip stream/topics, which are sometimes
  10. "sanitized" to guarantee uniqueness and not have special characters:
  11. stream_id: 599
  12. stream_name: general
  13. topic_name: lunch
  14. sanitized_stream_name : 599-general
  15. sanitized_topic_name: lunch
  16. """
  17. import urllib.parse
  18. def zulip_post_url(zulip_url, stream_id, stream_name, topic_name, post_id):
  19. """
  20. https://example.zulipchat.com/#narrow/stream/213222-general/topic/hello/near/179892604
  21. """
  22. sanitized = urllib.parse.quote(
  23. "{0}-{1}/topic/{2}/near/{3}".format(stream_id, stream_name, topic_name, post_id)
  24. )
  25. return zulip_url + "#narrow/stream/" + sanitized
  26. def archive_stream_url(site_url, html_root, sanitized_stream_name):
  27. """
  28. http://127.0.0.1:4000/archive/stream/213222-general/index.html
  29. """
  30. base_url = urllib.parse.urljoin(site_url, html_root)
  31. return f"{base_url}/stream/{sanitized_stream_name}/index.html"
  32. def archive_topic_url(site_url, html_root, sanitized_stream_name, sanitized_topic_name):
  33. """
  34. http://127.0.0.1:4000/archive/stream/213222-general/topic/newstreams.html
  35. """
  36. base_url = urllib.parse.urljoin(site_url, html_root)
  37. return (
  38. f"{base_url}/stream/{sanitized_stream_name}/topic/{sanitized_topic_name}.html"
  39. )
  40. def archive_message_url(
  41. site_url, html_root, sanitized_stream_name, sanitized_topic_name, msg_id
  42. ):
  43. """
  44. http://127.0.0.1:4000/archive/stream/213222-general/topic/newstreams.html#1234567
  45. """
  46. topic_url = archive_topic_url(
  47. site_url, html_root, sanitized_stream_name, sanitized_topic_name
  48. )
  49. return f"{topic_url}#{msg_id}"
  50. ## String cleaning functions
  51. def sanitize(s):
  52. """
  53. Sanitize the string to a safe string that can be used in URLs
  54. This is copied from Zulip's core code:
  55. https://github.com/zulip/zulip/blob/de31114d700561f32139a63a0e5f33d5c30039b3/zerver/lib/url_encoding.py#L8
  56. """
  57. return urllib.parse.quote(s, safe=b"").replace(".", "%2E").replace("%", ".")
  58. # create a unique sanitized identifier for a stream
  59. def sanitize_stream(stream_name, stream_id):
  60. """
  61. Encode streams for urls as something like 99-Foo-bar.
  62. This is copied from Zulip's core code:
  63. https://github.com/zulip/zulip/blob/de31114d700561f32139a63a0e5f33d5c30039b3/zerver/lib/url_encoding.py#L15
  64. """
  65. stream_name = stream_name.replace(" ", "-")
  66. return str(stream_id) + "-" + sanitize(stream_name)