Skip to content

Commit be3c4a5

Browse files
committed
feat: added custom sidebar menu, configurable via SIDEBAR_CUSTOM_MENU
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.
1 parent d2a617e commit be3c4a5

14 files changed

+3591
-29
lines changed

otterwiki/preferences.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22

33
import re
4+
import json
45
from otterwiki.util import is_valid_email
56
from flask import (
67
redirect,
@@ -12,7 +13,8 @@
1213
current_user,
1314
)
1415
from otterwiki.server import app, db, update_app_config, Preferences
15-
from otterwiki.helper import toast, send_mail
16+
from otterwiki.sidebar import SidebarPageIndex, SidebarMenu
17+
from otterwiki.helper import toast, send_mail, get_pagename
1618
from otterwiki.util import empty, is_valid_email
1719
from flask_login import current_user
1820
from otterwiki.auth import (
@@ -92,6 +94,17 @@ def handle_mail_preferences(form):
9294

9395

9496
def handle_sidebar_preferences(form):
97+
custom_menu_js = json.dumps(
98+
[ x
99+
for x in list(zip(form.getlist("title"), form.getlist("link")))
100+
if x[0].strip() or x[1].strip()
101+
]
102+
)
103+
104+
_update_preference(
105+
"SIDEBAR_CUSTOM_MENU", custom_menu_js
106+
)
107+
95108
for checkbox in [
96109
"sidebar_shortcut_home",
97110
"sidebar_shortcut_page_index",
@@ -337,10 +350,15 @@ def permissions_and_registration_form():
337350
def sidebar_preferences_form():
338351
if not has_permission("ADMIN"):
339352
abort(403)
353+
# we re-use SidebarPageIndex to generate a list of all pages
354+
sn = SidebarPageIndex("", mode="*")
355+
pages = [get_pagename(fh[0], full=True, header=fh[1]) for fh in sn.filenames_and_header]
340356
# render form
341357
return render_template(
342358
"admin/sidebar_preferences.html",
343359
title="Sidebar Preferences",
360+
pages=pages,
361+
custom_menu=SidebarMenu().config,
344362
)
345363

346364

otterwiki/server.py

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
MINIFY_HTML=True,
4848
SIDEBAR_MENUTREE_MODE="SORTED",
4949
SIDEBAR_MENUTREE_MAXDEPTH="",
50+
SIDEBAR_CUSTOM_MENU="",
5051
COMMIT_MESSAGE="REQUIRED", # OPTIONAL
5152
GIT_WEB_SERVER=False,
5253
SIDEBAR_SHORTCUT_HOME=True,

otterwiki/sidebar.py

+48-3
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,78 @@
22

33
import os
44
import re
5+
import json
56
from collections import OrderedDict
6-
7+
from flask import url_for
78
from otterwiki.server import storage, app
89
from otterwiki.util import (
910
split_path,
1011
join_path,
12+
empty,
1113
)
1214
from otterwiki.helper import (
1315
get_pagename,
1416
)
1517

1618

17-
class SidebarNavigation:
19+
class SidebarMenu:
20+
URI_SIMPLE = re.compile(
21+
r"^(((https?)\:\/\/)|(mailto:))\S+"
22+
)
23+
def __init__(self):
24+
self.menu = []
25+
self.config = []
26+
if not app.config.get("SIDEBAR_CUSTOM_MENU", None):
27+
return
28+
try:
29+
raw_config = json.loads(app.config.get("SIDEBAR_CUSTOM_MENU",""))
30+
except (ValueError, IndexError) as e:
31+
app.logger.error(
32+
f"Error decoding SIDEBAR_CUSTOM_MENU={app.config.get('SIDEBAR_CUSTOM_MENU','')}: {e}"
33+
)
34+
raw_config = []
35+
for entry in raw_config:
36+
if len(entry) != 2: continue # FIXME: print warning
37+
if empty(entry[0]) and empty(entry[1]): continue
38+
self.config.append(entry)
39+
for entry in self.config:
40+
title, link = entry
41+
if empty(link):
42+
if empty(title): continue
43+
self.menu.append([title, url_for("view", path=title)])
44+
elif self.URI_SIMPLE.match(link):
45+
if empty(title): title = link
46+
self.menu.append([title, link])
47+
else:
48+
if empty(title): title = link
49+
self.menu.append([title, url_for("view", path=link)])
50+
51+
def query(self):
52+
return self.menu
53+
54+
class SidebarPageIndex:
1855
AXT_HEADING = re.compile(
1956
r' {0,3}(#{1,6})(?!#+)(?: *\n+|' r'\s+([^\n]*?)(?:\n+|\s+?#+\s*\n+))'
2057
)
2158
SETEX_HEADING = re.compile(r'([^\n]+)\n *(=|-){2,}[ \t]*\n+')
2259

23-
def __init__(self, path: str = "/"):
60+
def __init__(self, path: str = "/", mode: str = ""):
2461
self.path = path if app.config["RETAIN_PAGE_NAME_CASE"] else path.lower()
2562
self.path_depth = len(split_path(self.path))
2663
try:
2764
self.max_depth = int(app.config["SIDEBAR_MENUTREE_MAXDEPTH"])
2865
except ValueError:
2966
self.max_depth = None
3067
self.mode = app.config["SIDEBAR_MENUTREE_MODE"]
68+
# overwrite mode if argument is given
69+
if mode: self.mode = mode
70+
3171
# TODO load configs (yaml header in page?) (sidebar.yaml?)
72+
3273
# TODO check for cached pages
74+
75+
self.filenames_and_header = []
76+
3377
# load pages
3478
if self.mode == "":
3579
self.tree = None
@@ -122,6 +166,7 @@ def load(self):
122166
if entry.endswith(".md"):
123167
header = self.read_header(entry)
124168
entry = entry[:-3]
169+
self.filenames_and_header.append((entry, header))
125170
parts = split_path(entry)
126171
self.add_node(self.tree, [], parts, header)
127172

otterwiki/static/css/otterwiki.css

+4
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ pre,
218218
padding-right: 0;
219219
}
220220

221+
.btn-handle {
222+
cursor: move;
223+
}
224+
221225
.extra-nav-attachment {
222226
padding: 1rem;
223227
}

0 commit comments

Comments
 (0)