forked from DGNum/gestiojeux
c01ed7cb47
Python: - black - isort (black profile) - ruff Nix: - statix - nixfmt-rfc-style - deadnix
29 lines
893 B
Python
29 lines
893 B
Python
import re
|
|
|
|
import markdown
|
|
|
|
|
|
class NbspPreprocessor(markdown.preprocessors.Preprocessor):
|
|
"""Replace regular spaces with non-breaking spaces within a text around relevant
|
|
symbols"""
|
|
|
|
NBSP_BEFORE = [":", "!", "?", "»", ":", ";", "—", "€"]
|
|
NBSP_AFTER = ["«", "—"]
|
|
|
|
def run(self, lines):
|
|
text = "\n".join(lines)
|
|
re_before = re.compile("(?: *\n *| +)([{}])".format("".join(self.NBSP_BEFORE)))
|
|
re_after = re.compile("([{}])(?: +| *\n *)".format("".join(self.NBSP_AFTER)))
|
|
|
|
text = re_before.sub(r" \1", text)
|
|
text = re_after.sub(r"\1 ", text)
|
|
return text.split("\n")
|
|
|
|
|
|
class MainSiteMarkdownExtension(markdown.extensions.Extension):
|
|
def extendMarkdown(self, md):
|
|
md.preprocessors.register(NbspPreprocessor(md), "nbsp", 10)
|
|
|
|
|
|
def makeExtension(**kwargs):
|
|
return MainSiteMarkdownExtension(**kwargs)
|