Piki is a minimal wiki
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

page.py 3.3KB

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