NDOJ/judge/highlight_code.py

14 lines
435 B
Python
Raw Permalink Normal View History

2020-01-21 06:35:58 +00:00
from django.utils.html import escape, mark_safe
2024-01-19 01:46:41 +00:00
from judge.markdown import markdown
2020-01-21 06:35:58 +00:00
2022-05-14 17:57:27 +00:00
__all__ = ["highlight_code"]
2020-01-21 06:35:58 +00:00
2024-01-19 01:46:41 +00:00
def highlight_code(code, language, linenos=True, title=None):
linenos_option = 'linenums="1"' if linenos else ""
title_option = f'title="{title}"' if title else ""
options = f"{{.{language} {linenos_option} {title_option}}}"
2020-01-21 06:35:58 +00:00
2024-01-19 01:46:41 +00:00
value = f"```{options}\n{code}\n```\n"
return mark_safe(markdown(value))