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

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