2018-09-15 06:17:04 +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-09-15 06:17:04 +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_settings_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"\{relative\|(?P<link_type>.*?)\|(?P<key>.*?)\}")
|
2018-09-15 06:17:04 +02:00
|
|
|
|
|
|
|
gear_info = {
|
|
|
|
# The pattern is key: [name, link]
|
|
|
|
# key is from REGEXP: `{relative|gear|key}`
|
|
|
|
# name is what the item is called in the gear menu: `Select **name**.`
|
|
|
|
# link is used for relative links: `Select [name](link).`
|
2021-02-12 08:20:45 +01:00
|
|
|
"manage-streams": ["Manage streams", "/#streams/subscribed"],
|
2021-07-01 09:45:39 +02:00
|
|
|
"settings": ["Personal Settings", "/#settings/profile"],
|
2023-01-26 06:44:17 +01:00
|
|
|
"organization-settings": ["Organization settings", "/#organization/organization-profile"],
|
2023-03-23 03:47:14 +01:00
|
|
|
"integrations": ["Integrations", "/integrations/"],
|
2021-05-12 16:56:51 +02:00
|
|
|
"stats": ["Usage statistics", "/stats"],
|
2023-03-23 03:53:14 +01:00
|
|
|
"plans": ["Plans and pricing", "/plans/"],
|
2023-03-23 04:28:36 +01:00
|
|
|
"billing": ["Billing", "/billing/"],
|
2021-02-12 08:20:45 +01:00
|
|
|
"invite": ["Invite users", "/#invite"],
|
2022-04-12 12:22:04 +02:00
|
|
|
"about-zulip": ["About Zulip", "/#about-zulip"],
|
2018-09-15 06:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gear_instructions = """
|
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-09-15 06:17:04 +02:00
|
|
|
|
2020-07-10 01:57:43 +02:00
|
|
|
1. Select {item}.
|
2018-09-15 06:17:04 +02:00
|
|
|
"""
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-09-15 06:17:04 +02:00
|
|
|
def gear_handle_match(key: str) -> str:
|
|
|
|
if relative_help_links:
|
2021-02-12 08:20:45 +01:00
|
|
|
item = f"[{gear_info[key][0]}]({gear_info[key][1]})"
|
2018-09-15 06:17:04 +02:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
item = f"**{gear_info[key][0]}**"
|
2020-07-10 01:57:43 +02:00
|
|
|
return gear_instructions.format(item=item)
|
2018-09-15 06:17:04 +02:00
|
|
|
|
2018-09-16 04:29:56 +02:00
|
|
|
|
|
|
|
stream_info = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"all": ["All streams", "/#streams/all"],
|
2022-03-28 17:54:29 +02:00
|
|
|
"subscribed": ["Subscribed streams", "/#streams/subscribed"],
|
2018-09-16 04:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stream_instructions_no_link = """
|
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-09-16 04:29:56 +02:00
|
|
|
|
|
|
|
1. Click **Manage streams**.
|
|
|
|
"""
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-09-16 04:29:56 +02:00
|
|
|
def stream_handle_match(key: str) -> str:
|
|
|
|
if relative_help_links:
|
2020-06-10 06:41:04 +02:00
|
|
|
return f"1. Go to [{stream_info[key][0]}]({stream_info[key][1]})."
|
2021-02-12 08:20:45 +01:00
|
|
|
if key == "all":
|
2018-09-16 04:29:56 +02:00
|
|
|
return stream_instructions_no_link + "\n\n1. Click **All streams** in the upper left."
|
|
|
|
return stream_instructions_no_link
|
|
|
|
|
|
|
|
|
2018-09-15 06:17:04 +02:00
|
|
|
LINK_TYPE_HANDLERS = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"gear": gear_handle_match,
|
|
|
|
"stream": stream_handle_match,
|
2018-09-15 06:17:04 +02:00
|
|
|
}
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-10-19 06:37:43 +02:00
|
|
|
class RelativeLinksHelpExtension(Extension):
|
|
|
|
def extendMarkdown(self, md: Markdown) -> None:
|
2021-05-08 02:36:30 +02:00
|
|
|
"""Add RelativeLinksHelpExtension to the Markdown instance."""
|
2018-09-15 06:17:04 +02:00
|
|
|
md.registerExtension(self)
|
2021-09-17 19:01:36 +02:00
|
|
|
md.preprocessors.register(
|
|
|
|
RelativeLinks(), "help_relative_links", PREPROCESSOR_PRIORITES["help_relative_links"]
|
|
|
|
)
|
2018-09-15 06:17:04 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-27 04:23:43 +02:00
|
|
|
relative_help_links: bool = False
|
2018-09-15 06:17:04 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-09-15 06:17:04 +02:00
|
|
|
def set_relative_help_links(value: bool) -> None:
|
|
|
|
global relative_help_links
|
|
|
|
relative_help_links = value
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-09-15 06:17:04 +02:00
|
|
|
class RelativeLinks(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-09-15 06:17:04 +02:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
done = True
|
|
|
|
return lines
|
|
|
|
|
|
|
|
def handleMatch(self, match: Match[str]) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
return LINK_TYPE_HANDLERS[match.group("link_type")](match.group("key"))
|
2018-09-15 06:17:04 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-09-15 06:17:04 +02:00
|
|
|
def makeExtension(*args: Any, **kwargs: Any) -> RelativeLinksHelpExtension:
|
|
|
|
return RelativeLinksHelpExtension(*args, **kwargs)
|