|
2 | 2 |
|
3 | 3 | import os
|
4 | 4 | import re
|
| 5 | +import json |
5 | 6 | from collections import OrderedDict
|
6 |
| - |
| 7 | +from flask import url_for |
7 | 8 | from otterwiki.server import storage, app
|
8 | 9 | from otterwiki.util import (
|
9 | 10 | split_path,
|
10 | 11 | join_path,
|
| 12 | + empty, |
11 | 13 | )
|
12 | 14 | from otterwiki.helper import (
|
13 | 15 | get_pagename,
|
14 | 16 | )
|
15 | 17 |
|
16 | 18 |
|
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: |
18 | 55 | AXT_HEADING = re.compile(
|
19 | 56 | r' {0,3}(#{1,6})(?!#+)(?: *\n+|' r'\s+([^\n]*?)(?:\n+|\s+?#+\s*\n+))'
|
20 | 57 | )
|
21 | 58 | SETEX_HEADING = re.compile(r'([^\n]+)\n *(=|-){2,}[ \t]*\n+')
|
22 | 59 |
|
23 |
| - def __init__(self, path: str = "/"): |
| 60 | + def __init__(self, path: str = "/", mode: str = ""): |
24 | 61 | self.path = path if app.config["RETAIN_PAGE_NAME_CASE"] else path.lower()
|
25 | 62 | self.path_depth = len(split_path(self.path))
|
26 | 63 | try:
|
27 | 64 | self.max_depth = int(app.config["SIDEBAR_MENUTREE_MAXDEPTH"])
|
28 | 65 | except ValueError:
|
29 | 66 | self.max_depth = None
|
30 | 67 | self.mode = app.config["SIDEBAR_MENUTREE_MODE"]
|
| 68 | + # overwrite mode if argument is given |
| 69 | + if mode: self.mode = mode |
| 70 | + |
31 | 71 | # TODO load configs (yaml header in page?) (sidebar.yaml?)
|
| 72 | + |
32 | 73 | # TODO check for cached pages
|
| 74 | + |
| 75 | + self.filenames_and_header = [] |
| 76 | + |
33 | 77 | # load pages
|
34 | 78 | if self.mode == "":
|
35 | 79 | self.tree = None
|
@@ -122,6 +166,7 @@ def load(self):
|
122 | 166 | if entry.endswith(".md"):
|
123 | 167 | header = self.read_header(entry)
|
124 | 168 | entry = entry[:-3]
|
| 169 | + self.filenames_and_header.append((entry, header)) |
125 | 170 | parts = split_path(entry)
|
126 | 171 | self.add_node(self.tree, [], parts, header)
|
127 | 172 |
|
|
0 commit comments