Skip to content

Commit

Permalink
feat: added custom sidebar menu, configurable via SIDEBAR_CUSTOM_MENU
Browse files Browse the repository at this point in the history
The menu can be configured both via the setting SIDEBAR_CUSTOM_MENU,
where the config is stored as json list of lists, e.g.

    [["Example",""], ["Pagetitle","Subfolder/Pagetitle"], ["External
    Link", "https://example.com""]]

In the admin preferences the Menu can be configured in a convenient way. For
adding wiki pages a dropdown select box with all pages is available. The
order can be sorted manually via drag and drop.

This was discussed in #125.
  • Loading branch information
redimp committed Sep 10, 2024
1 parent d2a617e commit be3c4a5
Show file tree
Hide file tree
Showing 14 changed files with 3,591 additions and 29 deletions.
20 changes: 19 additions & 1 deletion otterwiki/preferences.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python

import re
import json
from otterwiki.util import is_valid_email
from flask import (
redirect,
Expand All @@ -12,7 +13,8 @@
current_user,
)
from otterwiki.server import app, db, update_app_config, Preferences
from otterwiki.helper import toast, send_mail
from otterwiki.sidebar import SidebarPageIndex, SidebarMenu
from otterwiki.helper import toast, send_mail, get_pagename
from otterwiki.util import empty, is_valid_email
from flask_login import current_user
from otterwiki.auth import (
Expand Down Expand Up @@ -92,6 +94,17 @@ def handle_mail_preferences(form):


def handle_sidebar_preferences(form):
custom_menu_js = json.dumps(
[ x
for x in list(zip(form.getlist("title"), form.getlist("link")))
if x[0].strip() or x[1].strip()
]
)

_update_preference(
"SIDEBAR_CUSTOM_MENU", custom_menu_js
)

for checkbox in [
"sidebar_shortcut_home",
"sidebar_shortcut_page_index",
Expand Down Expand Up @@ -337,10 +350,15 @@ def permissions_and_registration_form():
def sidebar_preferences_form():
if not has_permission("ADMIN"):
abort(403)
# we re-use SidebarPageIndex to generate a list of all pages
sn = SidebarPageIndex("", mode="*")
pages = [get_pagename(fh[0], full=True, header=fh[1]) for fh in sn.filenames_and_header]
# render form
return render_template(
"admin/sidebar_preferences.html",
title="Sidebar Preferences",
pages=pages,
custom_menu=SidebarMenu().config,
)


Expand Down
1 change: 1 addition & 0 deletions otterwiki/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
MINIFY_HTML=True,
SIDEBAR_MENUTREE_MODE="SORTED",
SIDEBAR_MENUTREE_MAXDEPTH="",
SIDEBAR_CUSTOM_MENU="",
COMMIT_MESSAGE="REQUIRED", # OPTIONAL
GIT_WEB_SERVER=False,
SIDEBAR_SHORTCUT_HOME=True,
Expand Down
51 changes: 48 additions & 3 deletions otterwiki/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,78 @@

import os
import re
import json
from collections import OrderedDict

from flask import url_for
from otterwiki.server import storage, app
from otterwiki.util import (
split_path,
join_path,
empty,
)
from otterwiki.helper import (
get_pagename,
)


class SidebarNavigation:
class SidebarMenu:
URI_SIMPLE = re.compile(
r"^(((https?)\:\/\/)|(mailto:))\S+"
)
def __init__(self):
self.menu = []
self.config = []
if not app.config.get("SIDEBAR_CUSTOM_MENU", None):
return
try:
raw_config = json.loads(app.config.get("SIDEBAR_CUSTOM_MENU",""))
except (ValueError, IndexError) as e:
app.logger.error(
f"Error decoding SIDEBAR_CUSTOM_MENU={app.config.get('SIDEBAR_CUSTOM_MENU','')}: {e}"
)
raw_config = []
for entry in raw_config:
if len(entry) != 2: continue # FIXME: print warning
if empty(entry[0]) and empty(entry[1]): continue
self.config.append(entry)
for entry in self.config:
title, link = entry
if empty(link):
if empty(title): continue
self.menu.append([title, url_for("view", path=title)])
elif self.URI_SIMPLE.match(link):
if empty(title): title = link
self.menu.append([title, link])
else:
if empty(title): title = link
self.menu.append([title, url_for("view", path=link)])

def query(self):
return self.menu

class SidebarPageIndex:
AXT_HEADING = re.compile(
r' {0,3}(#{1,6})(?!#+)(?: *\n+|' r'\s+([^\n]*?)(?:\n+|\s+?#+\s*\n+))'
)
SETEX_HEADING = re.compile(r'([^\n]+)\n *(=|-){2,}[ \t]*\n+')

def __init__(self, path: str = "/"):
def __init__(self, path: str = "/", mode: str = ""):
self.path = path if app.config["RETAIN_PAGE_NAME_CASE"] else path.lower()
self.path_depth = len(split_path(self.path))
try:
self.max_depth = int(app.config["SIDEBAR_MENUTREE_MAXDEPTH"])
except ValueError:
self.max_depth = None
self.mode = app.config["SIDEBAR_MENUTREE_MODE"]
# overwrite mode if argument is given
if mode: self.mode = mode

# TODO load configs (yaml header in page?) (sidebar.yaml?)

# TODO check for cached pages

self.filenames_and_header = []

# load pages
if self.mode == "":
self.tree = None
Expand Down Expand Up @@ -122,6 +166,7 @@ def load(self):
if entry.endswith(".md"):
header = self.read_header(entry)
entry = entry[:-3]
self.filenames_and_header.append((entry, header))
parts = split_path(entry)
self.add_node(self.tree, [], parts, header)

Expand Down
4 changes: 4 additions & 0 deletions otterwiki/static/css/otterwiki.css
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ pre,
padding-right: 0;
}

.btn-handle {
cursor: move;
}

.extra-nav-attachment {
padding: 1rem;
}
Expand Down
Loading

0 comments on commit be3c4a5

Please sign in to comment.