2018-04-18 04:31:57 +02:00
|
|
|
import re
|
2022-04-27 04:23:43 +02:00
|
|
|
from typing import Any, List, Match
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-10-19 06:37:43 +02:00
|
|
|
from markdown import Markdown
|
|
|
|
from markdown.extensions import Extension
|
2018-04-18 04:31:57 +02:00
|
|
|
from markdown.preprocessors import Preprocessor
|
|
|
|
|
2022-06-26 02:35:20 +02:00
|
|
|
from zerver.lib.markdown.priorities import PREPROCESSOR_PRIORITES
|
2021-09-17 19:01:36 +02:00
|
|
|
|
2018-09-15 06:17:04 +02:00
|
|
|
# There is a lot of duplicated code between this file and
|
|
|
|
# help_relative_links.py. So if you're making a change here consider making
|
|
|
|
# it there as well.
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
REGEXP = re.compile(r"\{settings_tab\|(?P<setting_identifier>.*?)\}")
|
2018-04-18 04:31:57 +02:00
|
|
|
|
|
|
|
link_mapping = {
|
|
|
|
# a mapping from the setting identifier that is the same as the final URL
|
|
|
|
# breadcrumb to that setting to the name of its setting type, the setting
|
|
|
|
# name as it appears in the user interface, and a relative link that can
|
|
|
|
# be used to get to that setting
|
2021-07-01 09:45:39 +02:00
|
|
|
"profile": ["Personal settings", "Profile", "/#settings/profile"],
|
|
|
|
"account-and-privacy": [
|
|
|
|
"Personal settings",
|
|
|
|
"Account & privacy",
|
|
|
|
"/#settings/account-and-privacy",
|
|
|
|
],
|
2023-06-29 15:28:38 +02:00
|
|
|
"preferences": ["Personal settings", "Preferences", "/#settings/preferences"],
|
2021-07-01 09:45:39 +02:00
|
|
|
"notifications": ["Personal settings", "Notifications", "/#settings/notifications"],
|
2023-01-21 04:00:00 +01:00
|
|
|
"your-bots": ["Personal settings", "Bots", "/#settings/your-bots"],
|
2021-07-01 09:45:39 +02:00
|
|
|
"alert-words": ["Personal settings", "Alert words", "/#settings/alert-words"],
|
|
|
|
"uploaded-files": ["Personal settings", "Uploaded files", "/#settings/uploaded-files"],
|
2023-04-20 22:48:10 +02:00
|
|
|
"topics": ["Personal settings", "Topics", "/#settings/topics"],
|
2021-07-01 09:45:39 +02:00
|
|
|
"muted-users": ["Personal settings", "Muted users", "/#settings/muted-users"],
|
2021-02-12 08:20:45 +01:00
|
|
|
"organization-profile": [
|
2023-01-26 06:44:17 +01:00
|
|
|
"Organization settings",
|
2021-02-12 08:20:45 +01:00
|
|
|
"Organization profile",
|
|
|
|
"/#organization/organization-profile",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
2021-02-12 08:20:45 +01:00
|
|
|
"organization-settings": [
|
2023-01-26 06:44:17 +01:00
|
|
|
"Organization settings",
|
2021-02-12 08:20:45 +01:00
|
|
|
"Organization settings",
|
|
|
|
"/#organization/organization-settings",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
2021-02-12 08:20:45 +01:00
|
|
|
"organization-permissions": [
|
2023-01-26 06:44:17 +01:00
|
|
|
"Organization settings",
|
2021-02-12 08:20:45 +01:00
|
|
|
"Organization permissions",
|
|
|
|
"/#organization/organization-permissions",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
2021-09-20 20:51:38 +02:00
|
|
|
"default-user-settings": [
|
2023-01-26 06:44:17 +01:00
|
|
|
"Organization settings",
|
2021-09-20 20:51:38 +02:00
|
|
|
"Default user settings",
|
|
|
|
"/#organization/organization-level-user-defaults",
|
|
|
|
],
|
2023-01-26 06:44:17 +01:00
|
|
|
"emoji-settings": ["Organization settings", "Custom emoji", "/#organization/emoji-settings"],
|
2021-02-12 08:20:45 +01:00
|
|
|
"auth-methods": [
|
2023-01-26 06:44:17 +01:00
|
|
|
"Organization settings",
|
2021-02-12 08:20:45 +01:00
|
|
|
"Authentication methods",
|
|
|
|
"/#organization/auth-methods",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
2023-01-26 06:44:17 +01:00
|
|
|
"user-groups-admin": [
|
|
|
|
"Organization settings",
|
|
|
|
"User groups",
|
|
|
|
"/#organization/user-groups-admin",
|
|
|
|
],
|
|
|
|
"user-list-admin": ["Organization settings", "Users", "/#organization/user-list-admin"],
|
2021-02-12 08:20:45 +01:00
|
|
|
"deactivated-users-admin": [
|
2023-01-26 06:44:17 +01:00
|
|
|
"Organization settings",
|
2021-02-12 08:20:45 +01:00
|
|
|
"Deactivated users",
|
|
|
|
"/#organization/deactivated-users-admin",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
2022-10-16 04:27:03 +02:00
|
|
|
"bot-list-admin": [
|
2023-01-26 06:44:17 +01:00
|
|
|
"Organization settings",
|
2023-01-21 04:00:00 +01:00
|
|
|
"Bots",
|
2022-10-16 04:27:03 +02:00
|
|
|
"/#organization/bot-list-admin",
|
|
|
|
],
|
2021-02-12 08:20:45 +01:00
|
|
|
"default-streams-list": [
|
2023-01-26 06:44:17 +01:00
|
|
|
"Organization settings",
|
2021-02-12 08:20:45 +01:00
|
|
|
"Default streams",
|
|
|
|
"/#organization/default-streams-list",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
2021-03-13 18:15:14 +01:00
|
|
|
"linkifier-settings": [
|
2023-01-26 06:44:17 +01:00
|
|
|
"Organization settings",
|
2021-03-13 18:15:14 +01:00
|
|
|
"Linkifiers",
|
|
|
|
"/#organization/linkifier-settings",
|
|
|
|
],
|
2021-04-18 14:49:02 +02:00
|
|
|
"playground-settings": [
|
2023-01-26 06:44:17 +01:00
|
|
|
"Organization settings",
|
2021-05-05 02:05:47 +02:00
|
|
|
"Code playgrounds",
|
2021-04-18 14:49:02 +02:00
|
|
|
"/#organization/playground-settings",
|
|
|
|
],
|
2021-02-12 08:20:45 +01:00
|
|
|
"profile-field-settings": [
|
2023-01-26 06:44:17 +01:00
|
|
|
"Organization settings",
|
2021-02-12 08:20:45 +01:00
|
|
|
"Custom profile fields",
|
|
|
|
"/#organization/profile-field-settings",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
2021-02-12 08:20:45 +01:00
|
|
|
"invites-list-admin": [
|
2023-01-26 06:44:17 +01:00
|
|
|
"Organization settings",
|
2021-02-12 08:20:45 +01:00
|
|
|
"Invitations",
|
|
|
|
"/#organization/invites-list-admin",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
2021-02-12 08:20:45 +01:00
|
|
|
"data-exports-admin": [
|
2023-01-26 06:44:17 +01:00
|
|
|
"Organization settings",
|
2021-02-12 08:20:45 +01:00
|
|
|
"Data exports",
|
|
|
|
"/#organization/data-exports-admin",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
2018-04-18 04:31:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
settings_markdown = """
|
2021-08-17 10:08:33 +02:00
|
|
|
1. Click on the **gear** (<i class="fa fa-cog"></i>) icon in the upper
|
|
|
|
right corner of the web or desktop app.
|
2018-04-18 04:31:57 +02:00
|
|
|
|
2020-07-10 01:57:43 +02:00
|
|
|
1. Select **{setting_type_name}**.
|
2018-04-18 04:31:57 +02:00
|
|
|
|
2020-07-10 01:57:43 +02:00
|
|
|
1. On the left, click {setting_reference}.
|
2018-04-18 04:31:57 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2023-01-21 01:17:53 +01:00
|
|
|
def getMarkdown(setting_type_name: str, setting_name: str, setting_link: str) -> str:
|
|
|
|
if relative_settings_links:
|
2023-01-21 04:00:00 +01:00
|
|
|
relative_link = f"[{setting_name}]({setting_link})"
|
|
|
|
# The "Bots" label appears in both Personal and Organization settings
|
|
|
|
# in the user interface so we need special text for this setting.
|
|
|
|
if setting_name == "Bots":
|
|
|
|
return f"1. Navigate to the {relative_link} \
|
|
|
|
tab of the **{setting_type_name}** menu."
|
|
|
|
return f"1. Go to {relative_link}."
|
2023-01-21 01:17:53 +01:00
|
|
|
return settings_markdown.format(
|
|
|
|
setting_type_name=setting_type_name,
|
|
|
|
setting_reference=f"**{setting_name}**",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-10-19 06:37:43 +02:00
|
|
|
class SettingHelpExtension(Extension):
|
|
|
|
def extendMarkdown(self, md: Markdown) -> None:
|
2021-05-08 02:36:30 +02:00
|
|
|
"""Add SettingHelpExtension to the Markdown instance."""
|
2018-04-18 04:31:57 +02:00
|
|
|
md.registerExtension(self)
|
2021-09-17 19:01:36 +02:00
|
|
|
md.preprocessors.register(Setting(), "setting", PREPROCESSOR_PRIORITES["setting"])
|
2018-04-18 04:31:57 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-27 04:23:43 +02:00
|
|
|
relative_settings_links: bool = False
|
2018-04-18 04:31:57 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-04-26 00:56:00 +02:00
|
|
|
def set_relative_settings_links(value: bool) -> None:
|
2018-04-18 04:31:57 +02:00
|
|
|
global relative_settings_links
|
|
|
|
relative_settings_links = value
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-04-18 04:31:57 +02:00
|
|
|
class Setting(Preprocessor):
|
|
|
|
def run(self, lines: List[str]) -> List[str]:
|
|
|
|
done = False
|
|
|
|
while not done:
|
|
|
|
for line in lines:
|
|
|
|
loc = lines.index(line)
|
|
|
|
match = REGEXP.search(line)
|
|
|
|
|
|
|
|
if match:
|
|
|
|
text = [self.handleMatch(match)]
|
|
|
|
# The line that contains the directive to include the macro
|
|
|
|
# may be preceded or followed by text or tags, in that case
|
|
|
|
# we need to make sure that any preceding or following text
|
|
|
|
# stays the same.
|
|
|
|
line_split = REGEXP.split(line, maxsplit=0)
|
|
|
|
preceding = line_split[0]
|
|
|
|
following = line_split[-1]
|
2020-09-02 06:59:07 +02:00
|
|
|
text = [preceding, *text, following]
|
2021-02-12 08:19:30 +01:00
|
|
|
lines = lines[:loc] + text + lines[loc + 1 :]
|
2018-04-18 04:31:57 +02:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
done = True
|
|
|
|
return lines
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def handleMatch(self, match: Match[str]) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
setting_identifier = match.group("setting_identifier")
|
2023-01-21 01:17:53 +01:00
|
|
|
return getMarkdown(*link_mapping[setting_identifier])
|
2018-04-18 04:31:57 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-04-18 04:31:57 +02:00
|
|
|
def makeExtension(*args: Any, **kwargs: Any) -> SettingHelpExtension:
|
|
|
|
return SettingHelpExtension(*args, **kwargs)
|