mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 21:43:21 +00:00
All of our custom Markdown extensions have priorities that govern the order in which the preprocessors will be run. It is more convenient to have these all in one file so that you can easily discern the order at first glance. Thanks to Alya Abbott for reporting the bug that led to this refactoring!
76 lines
2.0 KiB
Python
76 lines
2.0 KiB
Python
import re
|
|
from typing import Any, List, Match
|
|
|
|
from markdown import Markdown
|
|
from markdown.extensions import Extension
|
|
from markdown.preprocessors import Preprocessor
|
|
|
|
from zerver.lib.emoji import EMOTICON_CONVERSIONS, name_to_codepoint
|
|
from zerver.lib.markdown.preprocessor_priorities import PREPROCESSOR_PRIORITES
|
|
|
|
REGEXP = re.compile(r"\{emoticon_translations\}")
|
|
|
|
TABLE_HTML = """\
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th>Emoticon</th>
|
|
<th>Emoji</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{body}
|
|
</tbody>
|
|
</table>
|
|
"""
|
|
|
|
ROW_HTML = """\
|
|
<tr>
|
|
<td><code>{emoticon}</code></td>
|
|
<td>
|
|
<img
|
|
src="/static/generated/emoji/images-google-64/{codepoint}.png"
|
|
alt="{name}"
|
|
class="emoji-big">
|
|
</td>
|
|
</tr>
|
|
"""
|
|
|
|
|
|
class EmoticonTranslationsHelpExtension(Extension):
|
|
def extendMarkdown(self, md: Markdown) -> None:
|
|
"""Add SettingHelpExtension to the Markdown instance."""
|
|
md.registerExtension(self)
|
|
md.preprocessors.register(
|
|
EmoticonTranslation(),
|
|
"emoticon_translations",
|
|
PREPROCESSOR_PRIORITES["emoticon_translations"],
|
|
)
|
|
|
|
|
|
class EmoticonTranslation(Preprocessor):
|
|
def run(self, lines: List[str]) -> List[str]:
|
|
for loc, line in enumerate(lines):
|
|
match = REGEXP.search(line)
|
|
if match:
|
|
text = self.handleMatch(match)
|
|
lines = lines[:loc] + text + lines[loc + 1 :]
|
|
break
|
|
return lines
|
|
|
|
def handleMatch(self, match: Match[str]) -> List[str]:
|
|
rows = [
|
|
ROW_HTML.format(
|
|
emoticon=emoticon,
|
|
name=name.strip(":"),
|
|
codepoint=name_to_codepoint[name.strip(":")],
|
|
)
|
|
for emoticon, name in EMOTICON_CONVERSIONS.items()
|
|
]
|
|
body = "".join(rows).strip()
|
|
return TABLE_HTML.format(body=body).strip().splitlines()
|
|
|
|
|
|
def makeExtension(*args: Any, **kwargs: Any) -> EmoticonTranslationsHelpExtension:
|
|
return EmoticonTranslationsHelpExtension(*args, **kwargs)
|