From 7f357e9a3af77705c4e1502f2f7e6492e68e94cb Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Sat, 5 Oct 2024 20:20:28 +0200 Subject: [PATCH] Added the macro subpages --- mycreole | 2 +- pages/creole.py | 27 -------------------------- pages/help.py | 7 ++++++- pages/page.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++-- pages/views.py | 5 ++++- piki/settings.py | 3 --- 6 files changed, 58 insertions(+), 35 deletions(-) delete mode 100644 pages/creole.py diff --git a/mycreole b/mycreole index 8a2da2b..2abea3c 160000 --- a/mycreole +++ b/mycreole @@ -1 +1 @@ -Subproject commit 8a2da2b84379df346d4f24c8fc5c1e6fe62c75e9 +Subproject commit 2abea3c4aec4716a2609828faddcd6b6e4c7728e diff --git a/pages/creole.py b/pages/creole.py deleted file mode 100644 index f0eb164..0000000 --- a/pages/creole.py +++ /dev/null @@ -1,27 +0,0 @@ -from django.urls.base import reverse - -# TODO: Add a filter to show all subpages <> and add it to settings and help - - -""" -def page_link_filter(text): - render_txt = '' - while len(text) > 0: - try: - pos = text.index('[[page:') - except ValueError: - pos = len(text) - print(pos) - render_txt += text[:pos] - text = text[pos + 7:] - if len(text): - pos = text.index(']]') - try: - rel_path = int(text[:pos]) - except ValueError: - render_txt += "[[page:" + text[:pos + 2] - else: - render_txt += '[[%s|%s]]' % (reverse('pages-pages', kwargs={'rel_path': rel_path}), rel_path) - text = text[pos + 2:] - return render_txt -""" diff --git a/pages/help.py b/pages/help.py index d9584c3..e67b0ec 100644 --- a/pages/help.py +++ b/pages/help.py @@ -21,7 +21,12 @@ MAIN = mycreole.render_simple(_( CREOLE = mycreole.mycreole_help_pagecontent() CREOLE += mycreole.render_simple(""" = Piki Markup -{{{[[rel_path_to_page|Name]]}}} will result in a Link to the given wiki page. +| {{{[[rel_path_to_page|Name]]}}} | will result in a Link to the given wiki page. | +| {{{<>}}} | will result in a list of all subpages below the current page. | +| {{{<>}}} | will result in a list of subpages below the current page.\ + N will reduce the depth of the subpages to N. \ + startswith will reduce the hits to all pages starting with the given string. + You can give one or both Parameters. | """) ACCESS = mycreole.render_simple(_(""" diff --git a/pages/page.py b/pages/page.py index b33adba..03ac605 100644 --- a/pages/page.py +++ b/pages/page.py @@ -29,9 +29,15 @@ class creol_page(object): def attachment_path(self): return os.path.join(self.content_folder_name, self.FOLDER_ATTACHMENTS) + def __content_folder_filter__(self, folder): + return folder.replace('/', '::') + + def __folder_content_filter__(self, folder): + return folder.replace('::', '/') + @property def content_folder_name(self): - return self._rel_path.replace('/', '::') + return self.__content_folder_filter__(self._rel_path) @property def content_file_name(self): @@ -54,7 +60,46 @@ class creol_page(object): def render_to_html(self, request): if self.is_available(): - return mycreole.render(request, self.raw_page_src, self.attachment_path, "next_anchor") + return self.render_text(request, self.raw_page_src) else: messages.unavailable_msg_page(request, self._rel_path) return "" + + def render_text(self, request, txt): + macros = { + "subpages": self.macro_subpages + } + return mycreole.render(request, txt, self.attachment_path, "next_anchor", macros=macros) + + def macro_subpages(self, *args, **kwargs): + def parse_depth(s: str): + try: + return int(s) + except ValueError: + pass + + params = kwargs.get('').split(",") + depth = parse_depth(params[0]) + if len(params) == 2: + startname = params[1] + elif depth is None: + startname = params[0] + if depth is None: + depth = 9999 + # + rv = "" + pathlist = fstools.dirlist(settings.PAGES_ROOT, rekursive=False) + pathlist.sort() + for path in pathlist: + dirname = os.path.basename(path) + contentname = self.__folder_content_filter__(dirname) + # + my_dirname = self.__content_folder_filter__(self._rel_path) + # + if dirname.startswith(my_dirname) and dirname != my_dirname: + name = contentname[len(self._rel_path)+1:] + if name.count('/') <= depth and name.startswith(startname): + rv += f'
  • {name}
  • \n' + if len(rv) > 0: + rv = "
      \n" + rv + "
    \n" + return rv diff --git a/pages/views.py b/pages/views.py index c13f921..e3dace6 100644 --- a/pages/views.py +++ b/pages/views.py @@ -17,6 +17,9 @@ from themes import Context logger = logging.getLogger(__name__) +# TODO: /!\ Deactivate self registration +# TODO: /!\ Remove config and add config_example with data from mm_tmux /!\ + def root(request): return HttpResponseRedirect(url_page(request, config.STARTPAGE)) @@ -79,7 +82,7 @@ def edit(request, rel_path): # TODO: Add translation title=_("Edit page %s") % repr(p.title), upload_path=p.attachment_path, - page_content=mycreole.render(request, page_txt, p.attachment_path, 'next-anchor') + page_content=p.render_text(request, page_txt) ) return render(request, 'pages/page_form.html', context=context) else: diff --git a/piki/settings.py b/piki/settings.py index f53121c..5477021 100644 --- a/piki/settings.py +++ b/piki/settings.py @@ -169,9 +169,6 @@ MYCREOLE_BAR = { 'navibar': 'pages.context.navigationbar', 'menubar': 'pages.context.menubar', } -MYCREOLE_EXT_FILTERS = [ - # 'pages.creole.page_link_filter', -] # Default primary key field type # https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field