[ADD] поддержка LaTeX-математики: dollarmath plugin, math_inline, math_block
This commit is contained in:
@@ -3,6 +3,7 @@ from md2gost.handlers.heading import heading
|
||||
from md2gost.handlers.hr import hr
|
||||
from md2gost.handlers.image import image
|
||||
from md2gost.handlers.lists import bullet_list, ordered_list
|
||||
from md2gost.handlers.math import math_block
|
||||
from md2gost.handlers.table import table
|
||||
from md2gost.handlers.text import paragraph
|
||||
|
||||
@@ -12,6 +13,7 @@ __all__ = [
|
||||
"heading",
|
||||
"image",
|
||||
"bullet_list",
|
||||
"math_block",
|
||||
"ordered_list",
|
||||
"table",
|
||||
"paragraph",
|
||||
|
||||
@@ -50,6 +50,8 @@ def render_inline_tokens(tokens: list[Token]) -> str:
|
||||
parts.append("}")
|
||||
elif tok.type in ("softbreak", "hardbreak"):
|
||||
parts.append("~\\\\")
|
||||
elif tok.type == "math_inline":
|
||||
parts.append(f"${tok.content}$")
|
||||
else:
|
||||
parts.append(escape_latex(tok.content) if tok.content else "")
|
||||
return "".join(parts)
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from markdown_it.token import Token
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from md2gost.render import Renderer
|
||||
|
||||
|
||||
def math_block(r: Renderer, tok: Token | None = None) -> str:
|
||||
tok = r.tokens[r.pos]
|
||||
content = tok.content.strip()
|
||||
r.pos += 1
|
||||
return f"\\[\n{content}\n\\]"
|
||||
@@ -1,4 +1,5 @@
|
||||
from markdown_it import MarkdownIt
|
||||
from mdit_py_plugins.dollarmath import dollarmath_plugin
|
||||
from mdit_py_plugins.front_matter import front_matter_plugin
|
||||
|
||||
|
||||
@@ -9,6 +10,7 @@ class Parser:
|
||||
MarkdownIt("commonmark", {"breaks": True, "html": True})
|
||||
.enable("table")
|
||||
.use(front_matter_plugin)
|
||||
.use(dollarmath_plugin)
|
||||
)
|
||||
self.tokens = self.md.parse(md_text)
|
||||
self.front_matter = self._extract_front_matter()
|
||||
|
||||
@@ -8,6 +8,7 @@ from md2gost.handlers import (
|
||||
heading,
|
||||
hr,
|
||||
image,
|
||||
math_block,
|
||||
ordered_list,
|
||||
paragraph,
|
||||
table,
|
||||
@@ -24,6 +25,7 @@ _BLOCK: dict[str, Callable[..., str]] = {
|
||||
"ordered_list_open": ordered_list,
|
||||
"table_open": table,
|
||||
"hr": hr,
|
||||
"math_block": math_block,
|
||||
}
|
||||
|
||||
_INLINE: dict[str, Callable[..., str]] = {
|
||||
|
||||
Reference in New Issue
Block a user