Piki is a minimal wiki
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

page.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from django.conf import settings
  2. # TODO: PRIO: Add wildcards for subpages filter
  3. # TODO: Add whoosh and search
  4. import fstools
  5. from pages import messages, url_page
  6. import mycreole
  7. import os
  8. class creol_page(object):
  9. SPLITCHAR = ":"
  10. FOLDER_ATTACHMENTS = "attachments"
  11. FOLDER_CONTENT = 'content'
  12. FILE_NAME = 'page'
  13. def __init__(self, request, rel_path) -> None:
  14. self._rel_path = rel_path
  15. self._request = request
  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):
  50. if self.is_available():
  51. return self.render_text(self._request, self.raw_page_src)
  52. else:
  53. messages.unavailable_msg_page(self._request, self._rel_path)
  54. return ""
  55. def render_text(self, request, txt):
  56. macros = {
  57. "subpages": self.macro_subpages,
  58. "allpages": self.macro_allpages,
  59. }
  60. return mycreole.render(request, txt, self.attachment_path, macros=macros)
  61. def macro_allpages(self, *args, **kwargs):
  62. kwargs["allpages"] = True
  63. return self.macro_subpages(*args, **kwargs)
  64. def macro_subpages(self, *args, **kwargs):
  65. allpages = kwargs.pop("allpages", False)
  66. #
  67. def parse_depth(s: str):
  68. try:
  69. return int(s)
  70. except ValueError:
  71. pass
  72. params = kwargs.get('', '')
  73. startname = ''
  74. depth = parse_depth(params)
  75. if depth is None:
  76. params = params.split(",")
  77. depth = parse_depth(params[0])
  78. if len(params) == 2:
  79. startname = params[1]
  80. elif depth is None:
  81. startname = params[0]
  82. if depth is None:
  83. depth = 9999
  84. #
  85. rv = ""
  86. # create a rel_path list
  87. pathlist = [self.__folder_content_filter__(os.path.basename(path)) for path in fstools.dirlist(settings.PAGES_ROOT, rekursive=False)]
  88. # sort basename
  89. pathlist.sort(key=os.path.basename)
  90. last_char = None
  91. for contentname in pathlist:
  92. #
  93. if (contentname.startswith(self._rel_path) or allpages) and contentname != self._rel_path:
  94. if allpages:
  95. name = contentname
  96. else:
  97. name = contentname[len(self._rel_path)+1:]
  98. if name.count('/') < depth and name.startswith(startname):
  99. if last_char != os.path.basename(name)[0].upper():
  100. last_char = os.path.basename(name)[0].upper()
  101. if last_char is not None:
  102. rv += "</ul>\n"
  103. rv += f'<h3>{last_char}</h3>\n<ul>\n'
  104. rv += f' <li><a href="{url_page(self._request, contentname)}">{name}</a></li>\n'
  105. if len(rv) > 0:
  106. rv += "</ul>\n"
  107. return rv