Replace markdown-it-py with small function in common/ (#32664)

* Replace markdown-it-py with small function in common/

* simple test

* unused

* lock

* linting fixes
old-commit-hash: 5265d13fe0
This commit is contained in:
Adeeb Shihadeh 2024-06-08 15:39:26 -07:00 committed by GitHub
parent 8472df65ea
commit 92ad30cbd6
5 changed files with 63 additions and 4 deletions

45
common/markdown.py Normal file
View File

@ -0,0 +1,45 @@
HTML_REPLACEMENTS = [
(r'&', r'&'),
(r'"', r'"'),
]
def parse_markdown(text: str, tab_length: int = 2) -> str:
lines = text.split("\n")
output: list[str] = []
list_level = 0
def end_outstanding_lists(level: int, end_level: int) -> int:
while level > end_level:
level -= 1
output.append("</ul>")
if level > 0:
output.append("</li>")
return end_level
for i, line in enumerate(lines):
if i + 1 < len(lines) and lines[i + 1].startswith("==="): # heading
output.append(f"<h1>{line}</h1>")
elif line.startswith("==="):
pass
elif line.lstrip().startswith("* "): # list
line_level = 1 + line.count(" " * tab_length, 0, line.index("*"))
if list_level >= line_level:
list_level = end_outstanding_lists(list_level, line_level)
else:
list_level += 1
if list_level > 1:
output[-1] = output[-1].replace("</li>", "")
output.append("<ul>")
output.append(f"<li>{line.replace('*', '', 1).lstrip()}</li>")
else:
list_level = end_outstanding_lists(list_level, 0)
if len(line) > 0:
output.append(line)
end_outstanding_lists(list_level, 0)
output_str = "\n".join(output) + "\n"
for (fr, to) in HTML_REPLACEMENTS:
output_str = output_str.replace(fr, to)
return output_str

View File

@ -0,0 +1,15 @@
import os
from openpilot.common.basedir import BASEDIR
from openpilot.common.markdown import parse_markdown
class TestMarkdown:
def test_all_release_notes(self):
with open(os.path.join(BASEDIR, "RELEASES.md")) as f:
release_notes = f.read().split("\n\n")
assert len(release_notes) > 10
for rn in release_notes:
md = parse_markdown(rn)
assert len(md) > 0

2
poetry.lock generated
View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:84ac396fe236ffaf09f9a865fa3bae19ac961db726bdc19b79e52c2f45db6abe
oid sha256:c803c02c1a5b9bb67b70a847c862464f49481e0edf44226ec1aeb3f107c5d790
size 618064

View File

@ -121,7 +121,6 @@ casadi = "*"
future-fstrings = "*"
# these should be removed
markdown-it-py = "*"
timezonefinder = "*"
setproctitle = "*"

View File

@ -11,11 +11,11 @@ import time
import threading
from collections import defaultdict
from pathlib import Path
from markdown_it import MarkdownIt
from openpilot.common.basedir import BASEDIR
from openpilot.common.params import Params
from openpilot.common.time import system_time_valid
from openpilot.common.markdown import parse_markdown
from openpilot.common.swaglog import cloudlog
from openpilot.selfdrive.controls.lib.alertmanager import set_offroad_alert
from openpilot.system.hardware import AGNOS, HARDWARE
@ -89,7 +89,7 @@ def parse_release_notes(basedir: str) -> bytes:
with open(os.path.join(basedir, "RELEASES.md"), "rb") as f:
r = f.read().split(b'\n\n', 1)[0] # Slice latest release notes
try:
return bytes(MarkdownIt().render(r.decode("utf-8")), encoding="utf-8")
return bytes(parse_markdown(r.decode("utf-8")), encoding="utf-8")
except Exception:
return r + b"\n"
except FileNotFoundError: