Change pre code css in markdown
This commit is contained in:
parent
d75a498d18
commit
aef795b40c
24 changed files with 200 additions and 397 deletions
|
@ -1,44 +1,13 @@
|
|||
from django.utils.html import escape, mark_safe
|
||||
from judge.markdown import markdown
|
||||
|
||||
__all__ = ["highlight_code"]
|
||||
|
||||
|
||||
def _make_pre_code(code):
|
||||
return mark_safe("<pre>" + escape(code) + "</pre>")
|
||||
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}}}"
|
||||
|
||||
|
||||
try:
|
||||
import pygments
|
||||
import pygments.lexers
|
||||
import pygments.formatters
|
||||
import pygments.util
|
||||
except ImportError:
|
||||
|
||||
def highlight_code(code, language, cssclass=None):
|
||||
return _make_pre_code(code)
|
||||
|
||||
else:
|
||||
|
||||
def highlight_code(code, language, cssclass="codehilite", linenos=True):
|
||||
try:
|
||||
lexer = pygments.lexers.get_lexer_by_name(language)
|
||||
except pygments.util.ClassNotFound:
|
||||
return _make_pre_code(code)
|
||||
|
||||
if linenos:
|
||||
return mark_safe(
|
||||
pygments.highlight(
|
||||
code,
|
||||
lexer,
|
||||
pygments.formatters.HtmlFormatter(
|
||||
cssclass=cssclass, linenos="table", wrapcode=True
|
||||
),
|
||||
)
|
||||
)
|
||||
return mark_safe(
|
||||
pygments.highlight(
|
||||
code,
|
||||
lexer,
|
||||
pygments.formatters.HtmlFormatter(cssclass=cssclass, wrapcode=True),
|
||||
)
|
||||
)
|
||||
value = f"```{options}\n{code}\n```\n"
|
||||
return mark_safe(markdown(value))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue