2024-10-05 11:55:55 +02:00
|
|
|
from django.conf import settings
|
|
|
|
|
2024-10-05 16:22:40 +02:00
|
|
|
import fstools
|
2024-10-07 16:47:15 +02:00
|
|
|
from pages import messages, url_page
|
2024-10-05 11:55:55 +02:00
|
|
|
import mycreole
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
2024-10-05 16:22:40 +02:00
|
|
|
class creol_page(object):
|
2024-10-05 11:55:55 +02:00
|
|
|
SPLITCHAR = ":"
|
|
|
|
FOLDER_ATTACHMENTS = "attachments"
|
|
|
|
FOLDER_CONTENT = 'content'
|
|
|
|
FILE_NAME = 'page'
|
|
|
|
|
2024-10-07 16:47:15 +02:00
|
|
|
def __init__(self, request, rel_path) -> None:
|
2024-10-05 11:55:55 +02:00
|
|
|
self._rel_path = rel_path
|
2024-10-07 16:47:15 +02:00
|
|
|
self._request = request
|
2024-10-05 11:55:55 +02:00
|
|
|
|
|
|
|
def rel_path_is_valid(self):
|
|
|
|
return not self.SPLITCHAR in self._rel_path
|
|
|
|
|
|
|
|
def is_available(self):
|
|
|
|
return os.path.isfile(self.content_file_name)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def title(self):
|
|
|
|
return os.path.basename(self._rel_path)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def attachment_path(self):
|
|
|
|
return os.path.join(self.content_folder_name, self.FOLDER_ATTACHMENTS)
|
|
|
|
|
2024-10-05 20:20:28 +02:00
|
|
|
def __content_folder_filter__(self, folder):
|
|
|
|
return folder.replace('/', '::')
|
|
|
|
|
|
|
|
def __folder_content_filter__(self, folder):
|
|
|
|
return folder.replace('::', '/')
|
|
|
|
|
2024-10-05 11:55:55 +02:00
|
|
|
@property
|
|
|
|
def content_folder_name(self):
|
2024-10-05 20:20:28 +02:00
|
|
|
return self.__content_folder_filter__(self._rel_path)
|
2024-10-05 11:55:55 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def content_file_name(self):
|
|
|
|
return os.path.join(settings.PAGES_ROOT, self.content_folder_name, self.FOLDER_CONTENT, self.FILE_NAME)
|
|
|
|
|
2024-10-05 16:22:40 +02:00
|
|
|
@property
|
|
|
|
def raw_page_src(self):
|
|
|
|
try:
|
2024-10-05 11:55:55 +02:00
|
|
|
with open(self.content_file_name, 'r') as fh:
|
|
|
|
return fh.read()
|
2024-10-05 16:22:40 +02:00
|
|
|
except FileNotFoundError:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def update_page(self, page_txt):
|
|
|
|
folder = os.path.dirname(self.content_file_name)
|
|
|
|
if not os.path.exists(folder):
|
|
|
|
fstools.mkdir(folder)
|
|
|
|
with open(self.content_file_name, 'w') as fh:
|
|
|
|
fh.write(page_txt)
|
2024-10-05 11:55:55 +02:00
|
|
|
|
2024-10-07 16:47:15 +02:00
|
|
|
def render_to_html(self):
|
2024-10-05 16:22:40 +02:00
|
|
|
if self.is_available():
|
2024-10-07 16:47:15 +02:00
|
|
|
return self.render_text(self._request, self.raw_page_src)
|
2024-10-05 16:22:40 +02:00
|
|
|
else:
|
2024-10-07 16:47:15 +02:00
|
|
|
messages.unavailable_msg_page(self._request, self._rel_path)
|
2024-10-05 16:22:40 +02:00
|
|
|
return ""
|
2024-10-05 20:20:28 +02:00
|
|
|
|
|
|
|
def render_text(self, request, txt):
|
|
|
|
macros = {
|
2024-10-07 17:19:29 +02:00
|
|
|
"subpages": self.macro_subpages,
|
|
|
|
"allpages": self.macro_allpages,
|
2024-10-05 20:20:28 +02:00
|
|
|
}
|
2024-10-07 00:44:22 +02:00
|
|
|
return mycreole.render(request, txt, self.attachment_path, macros=macros)
|
2024-10-05 20:20:28 +02:00
|
|
|
|
2024-10-07 17:19:29 +02:00
|
|
|
def macro_allpages(self, *args, **kwargs):
|
|
|
|
kwargs["allpages"] = True
|
|
|
|
return self.macro_subpages(*args, **kwargs)
|
|
|
|
|
2024-10-05 20:20:28 +02:00
|
|
|
def macro_subpages(self, *args, **kwargs):
|
2024-10-07 17:19:29 +02:00
|
|
|
allpages = kwargs.pop("allpages", False)
|
|
|
|
#
|
|
|
|
|
2024-10-05 20:20:28 +02:00
|
|
|
def parse_depth(s: str):
|
|
|
|
try:
|
|
|
|
return int(s)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2024-10-07 17:19:29 +02:00
|
|
|
params = kwargs.get('', '')
|
|
|
|
startname = ''
|
|
|
|
depth = parse_depth(params)
|
2024-10-05 20:20:28 +02:00
|
|
|
if depth is None:
|
2024-10-07 17:19:29 +02:00
|
|
|
params = params.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
|
2024-10-05 20:20:28 +02:00
|
|
|
#
|
|
|
|
rv = ""
|
2024-10-07 21:21:47 +02:00
|
|
|
# create a rel_path list
|
|
|
|
pathlist = [self.__folder_content_filter__(os.path.basename(path)) for path in fstools.dirlist(settings.PAGES_ROOT, rekursive=False)]
|
|
|
|
# sort basename
|
|
|
|
pathlist.sort(key=os.path.basename)
|
|
|
|
|
|
|
|
last_char = None
|
|
|
|
for contentname in pathlist:
|
2024-10-05 20:20:28 +02:00
|
|
|
#
|
2024-10-07 17:19:29 +02:00
|
|
|
if (contentname.startswith(self._rel_path) or allpages) and contentname != self._rel_path:
|
|
|
|
if allpages:
|
|
|
|
name = contentname
|
|
|
|
else:
|
|
|
|
name = contentname[len(self._rel_path)+1:]
|
|
|
|
if name.count('/') < depth and name.startswith(startname):
|
2024-10-07 21:21:47 +02:00
|
|
|
if last_char != os.path.basename(name)[0].upper():
|
|
|
|
last_char = os.path.basename(name)[0].upper()
|
|
|
|
if last_char is not None:
|
|
|
|
rv += "</ul>\n"
|
|
|
|
rv += f'<h3>{last_char}</h3>\n<ul>\n'
|
2024-10-07 16:47:15 +02:00
|
|
|
rv += f' <li><a href="{url_page(self._request, contentname)}">{name}</a></li>\n'
|
2024-10-05 20:20:28 +02:00
|
|
|
if len(rv) > 0:
|
2024-10-07 21:21:47 +02:00
|
|
|
rv += "</ul>\n"
|
2024-10-05 20:20:28 +02:00
|
|
|
return rv
|