2018-09-07 00:09:51 +02:00
|
|
|
import re
|
2024-07-12 02:30:25 +02:00
|
|
|
from collections.abc import Mapping
|
|
|
|
from typing import Any
|
2018-09-07 00:09:51 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
import markdown
|
2018-09-07 00:09:51 +02:00
|
|
|
from markdown.extensions import Extension
|
|
|
|
from markdown.preprocessors import Preprocessor
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2018-09-07 00:09:51 +02:00
|
|
|
|
2024-05-20 22:09:35 +02:00
|
|
|
from zerver.lib.markdown.priorities import PREPROCESSOR_PRIORITIES
|
2021-09-17 19:01:36 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
START_TABBED_SECTION_REGEX = re.compile(r"^\{start_tabs\}$")
|
|
|
|
END_TABBED_SECTION_REGEX = re.compile(r"^\{end_tabs\}$")
|
2023-09-07 23:06:59 +02:00
|
|
|
TAB_CONTENT_REGEX = re.compile(r"^\{tab\|([^}]+)\}$")
|
2018-09-07 00:09:51 +02:00
|
|
|
|
2023-08-30 00:58:01 +02:00
|
|
|
TABBED_SECTION_TEMPLATE = """
|
|
|
|
<div class="tabbed-section {tab_class}" markdown="1">
|
2018-09-07 00:09:51 +02:00
|
|
|
{nav_bar}
|
|
|
|
<div class="blocks">
|
|
|
|
{blocks}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
NAV_BAR_TEMPLATE = """
|
|
|
|
<ul class="nav">
|
|
|
|
{tabs}
|
|
|
|
</ul>
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
NAV_LIST_ITEM_TEMPLATE = """
|
2023-06-22 18:17:50 +02:00
|
|
|
<li data-tab-key="{data_tab_key}" tabindex="0">{label}</li>
|
2018-09-07 00:09:51 +02:00
|
|
|
""".strip()
|
|
|
|
|
|
|
|
DIV_TAB_CONTENT_TEMPLATE = """
|
2024-08-01 06:59:15 +02:00
|
|
|
<div class="tab-content" data-tab-key="{data_tab_key}" markdown="1">
|
2018-09-07 00:09:51 +02:00
|
|
|
{content}
|
|
|
|
</div>
|
|
|
|
""".strip()
|
|
|
|
|
2018-09-15 21:09:35 +02:00
|
|
|
# If adding new entries here, also check if you need to update
|
|
|
|
# tabbed-instructions.js
|
2021-10-01 23:16:47 +02:00
|
|
|
TAB_SECTION_LABELS = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"desktop-web": "Desktop/Web",
|
|
|
|
"ios": "iOS",
|
|
|
|
"android": "Android",
|
|
|
|
"mac": "macOS",
|
|
|
|
"windows": "Windows",
|
|
|
|
"linux": "Linux",
|
2024-02-21 03:13:37 +01:00
|
|
|
"most-systems": "Most systems",
|
|
|
|
"linux-with-apt": "Linux with APT",
|
2021-02-12 08:20:45 +01:00
|
|
|
"python": "Python",
|
|
|
|
"js": "JavaScript",
|
|
|
|
"curl": "curl",
|
|
|
|
"zulip-send": "zulip-send",
|
|
|
|
"web": "Web",
|
|
|
|
"desktop": "Desktop",
|
|
|
|
"mobile": "Mobile",
|
|
|
|
"mm-default": "Default installation",
|
2022-11-22 07:23:52 +01:00
|
|
|
"mm-cloud": "Cloud instance",
|
2021-02-12 08:20:45 +01:00
|
|
|
"mm-docker": "Docker",
|
|
|
|
"mm-gitlab-omnibus": "GitLab Omnibus",
|
2022-11-22 07:23:52 +01:00
|
|
|
"mm-self-hosting-cloud-export": "Self hosting (cloud export)",
|
2021-09-22 10:09:30 +02:00
|
|
|
"require-invitations": "Require invitations",
|
2021-02-12 08:20:45 +01:00
|
|
|
"allow-anyone-to-join": "Allow anyone to join",
|
|
|
|
"restrict-by-email-domain": "Restrict by email domain",
|
|
|
|
"zoom": "Zoom",
|
|
|
|
"jitsi-meet": "Jitsi Meet",
|
2021-07-06 00:23:51 +02:00
|
|
|
"bigbluebutton": "BigBlueButton",
|
2021-02-12 08:20:45 +01:00
|
|
|
"disable": "Disabled",
|
|
|
|
"chrome": "Chrome",
|
|
|
|
"firefox": "Firefox",
|
|
|
|
"desktop-app": "Desktop app",
|
|
|
|
"system-proxy-settings": "System proxy settings",
|
|
|
|
"custom-proxy-settings": "Custom proxy settings",
|
2021-09-14 02:14:40 +02:00
|
|
|
"stream": "From a stream view",
|
|
|
|
"not-stream": "From other views",
|
2022-10-12 19:54:34 +02:00
|
|
|
"via-recent-conversations": "Via recent conversations",
|
2023-09-30 02:21:16 +02:00
|
|
|
"via-inbox-view": "Via inbox view",
|
2021-09-14 02:14:40 +02:00
|
|
|
"via-left-sidebar": "Via left sidebar",
|
2024-02-29 02:55:11 +01:00
|
|
|
"via-right-sidebar": "Via right sidebar",
|
2021-10-01 23:29:43 +02:00
|
|
|
"instructions-for-all-platforms": "Instructions for all platforms",
|
2024-05-06 12:45:31 +02:00
|
|
|
"public-channels": "Public channels",
|
|
|
|
"private-channels": "Private channels",
|
|
|
|
"web-public-channels": "Web-public channels",
|
2022-11-14 20:03:31 +01:00
|
|
|
"via-user-card": "Via user card",
|
2023-09-12 01:48:30 +02:00
|
|
|
"via-user-profile": "Via user profile",
|
2022-03-15 15:30:16 +01:00
|
|
|
"via-organization-settings": "Via organization settings",
|
2022-10-16 04:27:03 +02:00
|
|
|
"via-personal-settings": "Via personal settings",
|
2024-05-06 12:45:31 +02:00
|
|
|
"via-channel-settings": "Via channel settings",
|
2022-08-24 01:19:37 +02:00
|
|
|
"default-subdomain": "Default subdomain",
|
|
|
|
"custom-subdomain": "Custom subdomain",
|
2023-11-29 08:30:55 +01:00
|
|
|
"zulip-cloud-standard": "Zulip Cloud Standard",
|
|
|
|
"zulip-cloud-plus": "Zulip Cloud Plus",
|
|
|
|
"request-sponsorship": "Request sponsorship",
|
|
|
|
"request-education-pricing": "Request education pricing",
|
2022-08-24 01:19:37 +02:00
|
|
|
"zulip-cloud": "Zulip Cloud",
|
|
|
|
"self-hosting": "Self hosting",
|
2022-08-08 19:25:38 +02:00
|
|
|
"okta": "Okta",
|
|
|
|
"onelogin": "OneLogin",
|
|
|
|
"azuread": "AzureAD",
|
2024-05-01 17:56:48 +02:00
|
|
|
"entraid": "Microsoft Entra ID",
|
2022-08-08 19:25:38 +02:00
|
|
|
"keycloak": "Keycloak",
|
2023-03-30 00:43:04 +02:00
|
|
|
"auth0": "Auth0",
|
2022-10-25 17:51:35 +02:00
|
|
|
"logged-in": "If you are logged in",
|
|
|
|
"logged-out": "If you are logged out",
|
2023-01-14 03:05:24 +01:00
|
|
|
"user": "User",
|
|
|
|
"bot": "Bot",
|
2023-02-22 23:27:29 +01:00
|
|
|
"on-sign-up": "On sign-up",
|
2023-10-28 01:20:44 +02:00
|
|
|
"via-paste": "Via paste",
|
2023-11-04 00:36:01 +01:00
|
|
|
"via-drag-and-drop": "Via drag-and-drop",
|
2023-03-10 05:54:05 +01:00
|
|
|
"via-markdown": "Via Markdown",
|
2024-04-22 23:33:55 +02:00
|
|
|
"via-compose-box-buttons": "Via compose box button",
|
2024-05-06 12:45:31 +02:00
|
|
|
"channel-compose": "Compose to a channel",
|
2023-05-10 06:55:17 +02:00
|
|
|
"dm-compose": "Compose a DM",
|
2023-11-30 08:04:39 +01:00
|
|
|
"v8": "Zulip Server 8.0+",
|
2023-05-29 07:50:27 +02:00
|
|
|
"v6": "Zulip Server 6.0+",
|
|
|
|
"v4": "Zulip Server 4.0+",
|
2024-01-25 19:50:45 +01:00
|
|
|
"all-versions": "All versions",
|
2024-06-06 22:12:30 +02:00
|
|
|
"by-card": "Pay by credit card",
|
|
|
|
"by-invoice": "Pay by invoice",
|
2024-02-16 01:25:09 +01:00
|
|
|
"for-a-bot": "For a bot",
|
|
|
|
"for-yourself": "For yourself",
|
2024-08-30 20:44:20 +02:00
|
|
|
"new-organizations": "New organizations",
|
|
|
|
"imported-organizations": "Imported organizations",
|
2018-09-07 00:09:51 +02:00
|
|
|
}
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-09-07 00:09:51 +02:00
|
|
|
class TabbedSectionsGenerator(Extension):
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2020-10-20 01:28:13 +02:00
|
|
|
def extendMarkdown(self, md: markdown.Markdown) -> None:
|
|
|
|
md.preprocessors.register(
|
2021-09-17 19:01:36 +02:00
|
|
|
TabbedSectionsPreprocessor(md, self.getConfigs()),
|
|
|
|
"tabbed_sections",
|
2024-05-20 22:09:35 +02:00
|
|
|
PREPROCESSOR_PRIORITIES["tabbed_sections"],
|
2020-10-20 01:28:13 +02:00
|
|
|
)
|
2018-09-07 00:09:51 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-09-07 00:09:51 +02:00
|
|
|
class TabbedSectionsPreprocessor(Preprocessor):
|
2020-10-20 02:49:02 +02:00
|
|
|
def __init__(self, md: markdown.Markdown, config: Mapping[str, Any]) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
super().__init__(md)
|
2018-09-07 00:09:51 +02:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2024-07-12 02:30:17 +02:00
|
|
|
def run(self, lines: list[str]) -> list[str]:
|
2018-09-07 00:09:51 +02:00
|
|
|
tab_section = self.parse_tabs(lines)
|
|
|
|
while tab_section:
|
2021-02-12 08:20:45 +01:00
|
|
|
if "tabs" in tab_section:
|
|
|
|
tab_class = "has-tabs"
|
2019-02-08 04:55:50 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
tab_class = "no-tabs"
|
|
|
|
tab_section["tabs"] = [
|
2021-10-01 23:29:43 +02:00
|
|
|
{
|
2023-06-22 18:17:50 +02:00
|
|
|
"tab_key": "instructions-for-all-platforms",
|
2021-10-01 23:29:43 +02:00
|
|
|
"start": tab_section["start_tabs_index"],
|
|
|
|
}
|
2021-02-12 08:19:30 +01:00
|
|
|
]
|
2018-09-07 00:09:51 +02:00
|
|
|
nav_bar = self.generate_nav_bar(tab_section)
|
|
|
|
content_blocks = self.generate_content_blocks(tab_section, lines)
|
2023-08-30 00:58:01 +02:00
|
|
|
rendered_tabs = TABBED_SECTION_TEMPLATE.format(
|
2021-02-12 08:19:30 +01:00
|
|
|
tab_class=tab_class, nav_bar=nav_bar, blocks=content_blocks
|
|
|
|
)
|
2018-09-07 00:09:51 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
start = tab_section["start_tabs_index"]
|
|
|
|
end = tab_section["end_tabs_index"] + 1
|
2020-09-02 06:59:07 +02:00
|
|
|
lines = [*lines[:start], rendered_tabs, *lines[end:]]
|
2018-09-07 00:09:51 +02:00
|
|
|
tab_section = self.parse_tabs(lines)
|
|
|
|
return lines
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def generate_content_blocks(self, tab_section: dict[str, Any], lines: list[str]) -> str:
|
2018-09-07 00:09:51 +02:00
|
|
|
tab_content_blocks = []
|
2021-02-12 08:20:45 +01:00
|
|
|
for index, tab in enumerate(tab_section["tabs"]):
|
|
|
|
start_index = tab["start"] + 1
|
2018-09-07 00:09:51 +02:00
|
|
|
try:
|
|
|
|
# If there are more tabs, we can use the starting index
|
|
|
|
# of the next tab as the ending index of the previous one
|
2021-02-12 08:20:45 +01:00
|
|
|
end_index = tab_section["tabs"][index + 1]["start"]
|
2018-09-07 00:09:51 +02:00
|
|
|
except IndexError:
|
|
|
|
# Otherwise, just use the end of the entire section
|
2021-02-12 08:20:45 +01:00
|
|
|
end_index = tab_section["end_tabs_index"]
|
2018-09-07 00:09:51 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
content = "\n".join(lines[start_index:end_index]).strip()
|
2018-09-07 00:09:51 +02:00
|
|
|
tab_content_block = DIV_TAB_CONTENT_TEMPLATE.format(
|
2023-06-22 18:17:50 +02:00
|
|
|
data_tab_key=tab["tab_key"],
|
2018-09-07 00:09:51 +02:00
|
|
|
# Wrapping the content in two newlines is necessary here.
|
|
|
|
# If we don't do this, the inner Markdown does not get
|
|
|
|
# rendered properly.
|
2021-02-12 08:20:45 +01:00
|
|
|
content=f"\n{content}\n",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-09-07 00:09:51 +02:00
|
|
|
tab_content_blocks.append(tab_content_block)
|
2021-02-12 08:20:45 +01:00
|
|
|
return "\n".join(tab_content_blocks)
|
2018-09-07 00:09:51 +02:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def generate_nav_bar(self, tab_section: dict[str, Any]) -> str:
|
2018-09-07 00:09:51 +02:00
|
|
|
li_elements = []
|
2021-02-12 08:20:45 +01:00
|
|
|
for tab in tab_section["tabs"]:
|
2023-06-22 18:17:50 +02:00
|
|
|
tab_key = tab.get("tab_key")
|
|
|
|
tab_label = TAB_SECTION_LABELS.get(tab_key)
|
2021-09-27 23:15:24 +02:00
|
|
|
if tab_label is None:
|
|
|
|
raise ValueError(
|
2023-06-22 18:17:50 +02:00
|
|
|
f"Tab '{tab_key}' is not present in TAB_SECTION_LABELS in zerver/lib/markdown/tabbed_sections.py"
|
2021-09-27 23:15:24 +02:00
|
|
|
)
|
|
|
|
|
2023-06-22 18:17:50 +02:00
|
|
|
li = NAV_LIST_ITEM_TEMPLATE.format(data_tab_key=tab_key, label=tab_label)
|
2018-09-07 00:09:51 +02:00
|
|
|
li_elements.append(li)
|
2021-09-27 23:15:24 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
return NAV_BAR_TEMPLATE.format(tabs="\n".join(li_elements))
|
2018-09-07 00:09:51 +02:00
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
def parse_tabs(self, lines: list[str]) -> dict[str, Any] | None:
|
2024-07-12 02:30:17 +02:00
|
|
|
block: dict[str, Any] = {}
|
2018-09-07 00:09:51 +02:00
|
|
|
for index, line in enumerate(lines):
|
|
|
|
start_match = START_TABBED_SECTION_REGEX.search(line)
|
|
|
|
if start_match:
|
2021-02-12 08:20:45 +01:00
|
|
|
block["start_tabs_index"] = index
|
2018-09-07 00:09:51 +02:00
|
|
|
|
|
|
|
tab_content_match = TAB_CONTENT_REGEX.search(line)
|
|
|
|
if tab_content_match:
|
2021-02-12 08:20:45 +01:00
|
|
|
block.setdefault("tabs", [])
|
2023-06-22 18:17:50 +02:00
|
|
|
tab = {"start": index, "tab_key": tab_content_match.group(1)}
|
2021-02-12 08:20:45 +01:00
|
|
|
block["tabs"].append(tab)
|
2018-09-07 00:09:51 +02:00
|
|
|
|
|
|
|
end_match = END_TABBED_SECTION_REGEX.search(line)
|
|
|
|
if end_match:
|
2021-02-12 08:20:45 +01:00
|
|
|
block["end_tabs_index"] = index
|
2018-09-07 00:09:51 +02:00
|
|
|
break
|
|
|
|
return block
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-09-07 00:09:51 +02:00
|
|
|
def makeExtension(*args: Any, **kwargs: str) -> TabbedSectionsGenerator:
|
2018-12-20 08:28:40 +01:00
|
|
|
return TabbedSectionsGenerator(**kwargs)
|