mirror of
https://git.sr.ht/~tsileo/microblog.pub
synced 2024-12-22 05:04:27 +00:00
Tweak code highlight
This commit is contained in:
parent
5d1ae0c9cd
commit
7b784e3011
2 changed files with 24 additions and 19 deletions
|
@ -3,12 +3,12 @@ import typing
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from mistletoe import Document # type: ignore
|
from mistletoe import Document # type: ignore
|
||||||
|
from mistletoe.block_token import CodeFence # type: ignore
|
||||||
from mistletoe.html_renderer import HTMLRenderer # type: ignore
|
from mistletoe.html_renderer import HTMLRenderer # type: ignore
|
||||||
from mistletoe.span_token import SpanToken # type: ignore
|
from mistletoe.span_token import SpanToken # type: ignore
|
||||||
from pygments import highlight # type: ignore
|
|
||||||
from pygments.formatters import HtmlFormatter # type: ignore
|
from pygments.formatters import HtmlFormatter # type: ignore
|
||||||
from pygments.lexers import get_lexer_by_name as get_lexer # type: ignore
|
from pygments.lexers import get_lexer_by_name as get_lexer # type: ignore
|
||||||
from pygments.lexers import guess_lexer # type: ignore
|
from pygments.util import ClassNotFound # type: ignore
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
|
|
||||||
from app import webfinger
|
from app import webfinger
|
||||||
|
@ -104,10 +104,16 @@ class CustomRenderer(HTMLRenderer):
|
||||||
)
|
)
|
||||||
return link
|
return link
|
||||||
|
|
||||||
def render_block_code(self, token: typing.Any) -> str:
|
def render_block_code(self, token: CodeFence) -> str:
|
||||||
|
lexer_attr = ""
|
||||||
|
try:
|
||||||
|
lexer = get_lexer(token.language)
|
||||||
|
lexer_attr = f' data-microblogpub-lexer="{lexer.aliases[0]}"'
|
||||||
|
except ClassNotFound:
|
||||||
|
pass
|
||||||
|
|
||||||
code = token.children[0].content
|
code = token.children[0].content
|
||||||
lexer = get_lexer(token.language) if token.language else guess_lexer(code)
|
return f"<pre><code{lexer_attr}>\n{code}\n</code></pre>"
|
||||||
return highlight(code, lexer, _FORMATTER)
|
|
||||||
|
|
||||||
|
|
||||||
async def _prefetch_mentioned_actors(
|
async def _prefetch_mentioned_actors(
|
||||||
|
|
|
@ -32,15 +32,11 @@ def highlight(html: str) -> str:
|
||||||
|
|
||||||
# If this comes from a microblog.pub instance we may have the language
|
# If this comes from a microblog.pub instance we may have the language
|
||||||
# in the class name
|
# in the class name
|
||||||
if "class" in code.attrs and code.attrs["class"][0].startswith("language-"):
|
if "data-microblogpub-lexer" in code.attrs:
|
||||||
try:
|
try:
|
||||||
lexer = get_lexer_by_name(
|
lexer = get_lexer_by_name(code.attrs["data-microblogpub-lexer"])
|
||||||
code.attrs["class"][0].removeprefix("language-")
|
|
||||||
)
|
|
||||||
except Exception:
|
except Exception:
|
||||||
lexer = guess_lexer(code_content)
|
lexer = guess_lexer(code_content)
|
||||||
else:
|
|
||||||
lexer = guess_lexer(code_content)
|
|
||||||
|
|
||||||
# Replace the code with Pygment output
|
# Replace the code with Pygment output
|
||||||
# XXX: the HTML escaping causes issue with Python type annotations
|
# XXX: the HTML escaping causes issue with Python type annotations
|
||||||
|
@ -50,5 +46,8 @@ def highlight(html: str) -> str:
|
||||||
phighlight(code_content, lexer, _FORMATTER), "html5lib"
|
phighlight(code_content, lexer, _FORMATTER), "html5lib"
|
||||||
).body.next
|
).body.next
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
code.name = "div"
|
||||||
|
code["class"] = code.get("class", []) + ["highlight"]
|
||||||
|
|
||||||
return soup.body.encode_contents().decode()
|
return soup.body.encode_contents().decode()
|
||||||
|
|
Loading…
Reference in a new issue