zulip/zerver/lib/markdown/help_emoticon_translations_...

76 lines
2.0 KiB
Python
Raw Normal View History

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\}")
templates: Fix more low-hanging HTML validation problems. "http://localhost:9981/help/enable-emoticon-translations":323.13-324.31: error: The “align” attribute on the “th” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":324.45-325.31: error: The “align” attribute on the “th” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":329.13-330.23: error: The “align” attribute on the “td” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":330.44-331.23: error: The “align” attribute on the “td” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":337.6-337.9: error: Stray end tag “p”. "http://localhost:9981/help/enable-emoticon-translations":337.6-337.9: error: Cannot recover after last error. Any further errors will be ignored. "http://localhost:9981/api/incoming-webhooks-walkthrough":381.4-381.77: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. "http://localhost:9981/apps/":192.21-192.34: error: Bad value “” for attribute “src” on element “img”: Must be non-empty. "http://localhost:9981/apps/":192.21-192.34: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. "http://localhost:9981/features/":225.9-225.82: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. "http://localhost:9981/features/":230.72-232.8: error: Text not allowed in element “svg” in this context. "http://localhost:9981/features/":259.9-259.91: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2019-04-23 05:50:41 +02:00
TABLE_HTML = """\
<table>
<thead>
<tr>
templates: Fix more low-hanging HTML validation problems. "http://localhost:9981/help/enable-emoticon-translations":323.13-324.31: error: The “align” attribute on the “th” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":324.45-325.31: error: The “align” attribute on the “th” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":329.13-330.23: error: The “align” attribute on the “td” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":330.44-331.23: error: The “align” attribute on the “td” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":337.6-337.9: error: Stray end tag “p”. "http://localhost:9981/help/enable-emoticon-translations":337.6-337.9: error: Cannot recover after last error. Any further errors will be ignored. "http://localhost:9981/api/incoming-webhooks-walkthrough":381.4-381.77: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. "http://localhost:9981/apps/":192.21-192.34: error: Bad value “” for attribute “src” on element “img”: Must be non-empty. "http://localhost:9981/apps/":192.21-192.34: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. "http://localhost:9981/features/":225.9-225.82: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. "http://localhost:9981/features/":230.72-232.8: error: Text not allowed in element “svg” in this context. "http://localhost:9981/features/":259.9-259.91: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2019-04-23 05:50:41 +02:00
<th>Emoticon</th>
<th>Emoji</th>
</tr>
</thead>
<tbody>
{body}
</tbody>
</table>
"""
templates: Fix more low-hanging HTML validation problems. "http://localhost:9981/help/enable-emoticon-translations":323.13-324.31: error: The “align” attribute on the “th” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":324.45-325.31: error: The “align” attribute on the “th” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":329.13-330.23: error: The “align” attribute on the “td” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":330.44-331.23: error: The “align” attribute on the “td” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":337.6-337.9: error: Stray end tag “p”. "http://localhost:9981/help/enable-emoticon-translations":337.6-337.9: error: Cannot recover after last error. Any further errors will be ignored. "http://localhost:9981/api/incoming-webhooks-walkthrough":381.4-381.77: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. "http://localhost:9981/apps/":192.21-192.34: error: Bad value “” for attribute “src” on element “img”: Must be non-empty. "http://localhost:9981/apps/":192.21-192.34: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. "http://localhost:9981/features/":225.9-225.82: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. "http://localhost:9981/features/":230.72-232.8: error: Text not allowed in element “svg” in this context. "http://localhost:9981/features/":259.9-259.91: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2019-04-23 05:50:41 +02:00
ROW_HTML = """\
<tr>
templates: Fix more low-hanging HTML validation problems. "http://localhost:9981/help/enable-emoticon-translations":323.13-324.31: error: The “align” attribute on the “th” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":324.45-325.31: error: The “align” attribute on the “th” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":329.13-330.23: error: The “align” attribute on the “td” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":330.44-331.23: error: The “align” attribute on the “td” element is obsolete. Use CSS instead. "http://localhost:9981/help/enable-emoticon-translations":337.6-337.9: error: Stray end tag “p”. "http://localhost:9981/help/enable-emoticon-translations":337.6-337.9: error: Cannot recover after last error. Any further errors will be ignored. "http://localhost:9981/api/incoming-webhooks-walkthrough":381.4-381.77: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. "http://localhost:9981/apps/":192.21-192.34: error: Bad value “” for attribute “src” on element “img”: Must be non-empty. "http://localhost:9981/apps/":192.21-192.34: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. "http://localhost:9981/features/":225.9-225.82: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. "http://localhost:9981/features/":230.72-232.8: error: Text not allowed in element “svg” in this context. "http://localhost:9981/features/":259.9-259.91: error: An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2019-04-23 05:50:41 +02:00
<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)