Skip to content

Commit

Permalink
Tech: permet d’ajouter des classes dans les listes markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnix committed Apr 21, 2021
1 parent 6af51b2 commit 780b9a5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
31 changes: 28 additions & 3 deletions build.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import fnmatch
import os
import re
from html.parser import HTMLParser
from http import HTTPStatus
from pathlib import Path
Expand All @@ -21,17 +22,41 @@
jinja_env = JinjaEnv(loader=FileSystemLoader(str(SRC_DIR)), undefined=StrictUndefined)


class FrenchTypographyRenderer(mistune.HTMLRenderer):

class FrenchTypographyMixin:
def text(self, text_):
return typographie(super().text(text_))

def block_html(self, html):
return typographie(super().block_html(html))


class CSSMixin:
RE_CLASS = re.compile(
r"""^
(?P<before>.*?)
(?:\s*\{\.(?P<class>[\w\-]+?)\}\s*)
(?P<after>.*)
$
""",
re.MULTILINE | re.VERBOSE,
)

def list_item(self, text, level):
mo = self.RE_CLASS.match(text)
if mo is not None:
class_ = mo.group("class")
text = " ".join(filter(None, [mo.group("before"), mo.group("after")]))
return f'<li class="{class_}">{text}</li>\n'
return super().list_item(text, level)


class CustomHTMLRenderer(FrenchTypographyMixin, CSSMixin, mistune.HTMLRenderer):
pass


markdown = mistune.create_markdown(
escape=False, renderer=FrenchTypographyRenderer(escape=False)
escape=False,
renderer=CustomHTMLRenderer(escape=False),
)


Expand Down
25 changes: 25 additions & 0 deletions test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@ def test_liste_avec_saut_de_ligne():
)


def test_liste_avec_classes():
from build import markdown

assert (
markdown(
dedent(
"""\
- {.classe-au-debut} a
- b {.classe-a-la-fin}
- foo {.classe-au-milieu} bar
"""
)
)
== dedent(
"""\
<ul>
<li class="classe-au-debut">a</li>
<li class="classe-a-la-fin">b</li>
<li class="classe-au-milieu">foo bar</li>
</ul>
"""
)
)


def test_block_html():
from build import markdown

Expand Down

0 comments on commit 780b9a5

Please sign in to comment.