2018-08-15 04:55:36 +02:00
|
|
|
import re
|
2020-10-20 01:28:13 +02:00
|
|
|
from typing import Any, List
|
2018-08-15 04:55:36 +02:00
|
|
|
from typing.re import 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-08-15 04:55:36 +02:00
|
|
|
from markdown.preprocessors import Preprocessor
|
|
|
|
|
|
|
|
from zerver.lib.emoji import EMOTICON_CONVERSIONS, name_to_codepoint
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
REGEXP = re.compile(r"\{emoticon_translations\}")
|
2018-08-15 04:55:36 +02:00
|
|
|
|
2019-04-23 05:50:41 +02:00
|
|
|
TABLE_HTML = """\
|
2018-08-15 04:55:36 +02:00
|
|
|
<table>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
2019-04-23 05:50:41 +02:00
|
|
|
<th>Emoticon</th>
|
|
|
|
<th>Emoji</th>
|
2018-08-15 04:55:36 +02:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{body}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
"""
|
|
|
|
|
2019-04-23 05:50:41 +02:00
|
|
|
ROW_HTML = """\
|
2018-08-15 04:55:36 +02:00
|
|
|
<tr>
|
2019-04-23 05:50:41 +02:00
|
|
|
<td><code>{emoticon}</code></td>
|
|
|
|
<td>
|
2018-08-15 04:55:36 +02:00
|
|
|
<img
|
|
|
|
src="/static/generated/emoji/images-google-64/{codepoint}.png"
|
|
|
|
alt="{name}"
|
2018-08-16 23:38:15 +02:00
|
|
|
class="emoji-big">
|
2018-08-15 04:55:36 +02:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
"""
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-10-19 06:37:43 +02:00
|
|
|
class EmoticonTranslationsHelpExtension(Extension):
|
|
|
|
def extendMarkdown(self, md: Markdown) -> None:
|
2018-08-15 04:55:36 +02:00
|
|
|
""" Add SettingHelpExtension to the Markdown instance. """
|
|
|
|
md.registerExtension(self)
|
2021-02-12 08:20:45 +01:00
|
|
|
md.preprocessors.register(EmoticonTranslation(), "emoticon_translations", -505)
|
2018-08-15 04:55:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2021-02-12 08:19:30 +01:00
|
|
|
lines = lines[:loc] + text + lines[loc + 1 :]
|
2018-08-15 04:55:36 +02:00
|
|
|
break
|
|
|
|
return lines
|
|
|
|
|
|
|
|
def handleMatch(self, match: Match[str]) -> List[str]:
|
|
|
|
rows = [
|
2021-02-12 08:19:30 +01:00
|
|
|
ROW_HTML.format(
|
|
|
|
emoticon=emoticon,
|
2021-02-12 08:20:45 +01:00
|
|
|
name=name.strip(":"),
|
|
|
|
codepoint=name_to_codepoint[name.strip(":")],
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-08-15 04:55:36 +02:00
|
|
|
for emoticon, name in EMOTICON_CONVERSIONS.items()
|
|
|
|
]
|
2021-02-12 08:20:45 +01:00
|
|
|
body = "".join(rows).strip()
|
2018-08-15 04:55:36 +02:00
|
|
|
return TABLE_HTML.format(body=body).strip().splitlines()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-08-15 04:55:36 +02:00
|
|
|
def makeExtension(*args: Any, **kwargs: Any) -> EmoticonTranslationsHelpExtension:
|
|
|
|
return EmoticonTranslationsHelpExtension(*args, **kwargs)
|