files.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """
  2. There are two major phases of this system:
  3. 1. Build JSON files from Zulip data.
  4. 2. Build static website from JSON.
  5. Both phases write to directories with some directories.
  6. Here is an example structure for the JSON piece:
  7. <json_root>
  8. stream_index.json
  9. 213222general
  10. 47413hello.json
  11. 48863swimmingturtles.json
  12. 51687topicdemonstration.json
  13. 74282newstreams.json
  14. 213224python
  15. 47413hello.json
  16. 95106streamevents.json
  17. And then here is what your website output might
  18. look like:
  19. <md_root>
  20. index.html
  21. style.css
  22. stream
  23. 213222general
  24. index.html
  25. 47413hello.html
  26. 48863swimmingturtles.html
  27. 51687topicdemonstration.html
  28. 74282newstreams.html
  29. 213224python
  30. index.html
  31. 47413hello.html
  32. 95106streamevents.html
  33. In the examples above we have two streams:
  34. general:
  35. hello
  36. swimming turtles
  37. topic demonstration
  38. new streams
  39. python:
  40. hello
  41. stream events
  42. We "sanitize" the directory names to avoid escaping issues
  43. with spaces (hence the number prefix). FWIW the number prefix
  44. for streams corresponds to the Zulip stream id, whereas the topic
  45. prefix is a random hash. All that really matters is that they are
  46. unique.
  47. """
  48. import json
  49. from pathlib import Path
  50. from .common import open_outfile
  51. def read_zulip_stream_info(json_root):
  52. """
  53. stream_index.json
  54. This JSON goes two levels deep, showing every stream, and
  55. then within each stream, a bit of info for every topic in
  56. the stream. To get actual messages within a topic, you go
  57. to other files deeper in the directory structure.
  58. """
  59. f = (json_root / Path("stream_index.json")).open("r", encoding="utf-8")
  60. stream_info = json.load(f)
  61. f.close()
  62. return stream_info
  63. def read_zulip_messages_for_topic(
  64. json_root, sanitized_stream_name, sanitized_topic_name
  65. ):
  66. """
  67. <stream>/<topic>.json
  68. This JSON has info for all the messags in a topic.
  69. """
  70. json_path = (
  71. json_root / Path(sanitized_stream_name) / Path(sanitized_topic_name + ".json")
  72. )
  73. f = json_path.open("r", encoding="utf-8")
  74. messages = json.load(f)
  75. f.close()
  76. return messages
  77. def open_main_page(md_root):
  78. outfile = open_outfile(md_root, Path("index.html"), "w+")
  79. return outfile
  80. def open_stream_topics_page(md_root, sanitized_stream_name):
  81. directory = md_root / Path("stream/" + sanitized_stream_name)
  82. outfile = open_outfile(directory, Path("index.html"), "w+")
  83. return outfile
  84. def open_topic_messages_page(md_root, sanitized_stream_name, sanitized_topic_name):
  85. directory = md_root / Path("stream/" + sanitized_stream_name + "/topic")
  86. outfile = open_outfile(directory, Path(sanitized_topic_name + ".html"), "w+")
  87. return outfile