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.

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