Piki is a minimal wiki
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

page.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from django.conf import settings
  2. import fstools
  3. from pages import messages
  4. import mycreole
  5. import os
  6. class creol_page(object):
  7. SPLITCHAR = ":"
  8. FOLDER_ATTACHMENTS = "attachments"
  9. FOLDER_CONTENT = 'content'
  10. FILE_NAME = 'page'
  11. def __init__(self, rel_path) -> None:
  12. self._rel_path = rel_path
  13. def rel_path_is_valid(self):
  14. return not self.SPLITCHAR in self._rel_path
  15. def is_available(self):
  16. return os.path.isfile(self.content_file_name)
  17. @property
  18. def title(self):
  19. return os.path.basename(self._rel_path)
  20. @property
  21. def attachment_path(self):
  22. return os.path.join(self.content_folder_name, self.FOLDER_ATTACHMENTS)
  23. def __content_folder_filter__(self, folder):
  24. return folder.replace('/', '::')
  25. def __folder_content_filter__(self, folder):
  26. return folder.replace('::', '/')
  27. @property
  28. def content_folder_name(self):
  29. return self.__content_folder_filter__(self._rel_path)
  30. @property
  31. def content_file_name(self):
  32. return os.path.join(settings.PAGES_ROOT, self.content_folder_name, self.FOLDER_CONTENT, self.FILE_NAME)
  33. @property
  34. def raw_page_src(self):
  35. try:
  36. with open(self.content_file_name, 'r') as fh:
  37. return fh.read()
  38. except FileNotFoundError:
  39. return ""
  40. def update_page(self, page_txt):
  41. folder = os.path.dirname(self.content_file_name)
  42. if not os.path.exists(folder):
  43. fstools.mkdir(folder)
  44. with open(self.content_file_name, 'w') as fh:
  45. fh.write(page_txt)
  46. def render_to_html(self, request):
  47. if self.is_available():
  48. return self.render_text(request, self.raw_page_src)
  49. else:
  50. messages.unavailable_msg_page(request, self._rel_path)
  51. return ""
  52. def render_text(self, request, txt):
  53. macros = {
  54. "subpages": self.macro_subpages
  55. }
  56. return mycreole.render(request, txt, self.attachment_path, "next_anchor", macros=macros)
  57. def macro_subpages(self, *args, **kwargs):
  58. def parse_depth(s: str):
  59. try:
  60. return int(s)
  61. except ValueError:
  62. pass
  63. params = kwargs.get('').split(",")
  64. depth = parse_depth(params[0])
  65. if len(params) == 2:
  66. startname = params[1]
  67. elif depth is None:
  68. startname = params[0]
  69. if depth is None:
  70. depth = 9999
  71. #
  72. rv = ""
  73. pathlist = fstools.dirlist(settings.PAGES_ROOT, rekursive=False)
  74. pathlist.sort()
  75. for path in pathlist:
  76. dirname = os.path.basename(path)
  77. contentname = self.__folder_content_filter__(dirname)
  78. #
  79. my_dirname = self.__content_folder_filter__(self._rel_path)
  80. #
  81. if dirname.startswith(my_dirname) and dirname != my_dirname:
  82. name = contentname[len(self._rel_path)+1:]
  83. if name.count('/') <= depth and name.startswith(startname):
  84. rv += f' <li><a href="{contentname}">{name}</a></li>\n'
  85. if len(rv) > 0:
  86. rv = "<ul>\n" + rv + "</ul>\n"
  87. return rv