diff --git a/md2gost/handlers/__init__.py b/md2gost/handlers/__init__.py index 724d20a..9a5a88b 100644 --- a/md2gost/handlers/__init__.py +++ b/md2gost/handlers/__init__.py @@ -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", diff --git a/md2gost/handlers/inline.py b/md2gost/handlers/inline.py index 47bf02d..a7bea8a 100644 --- a/md2gost/handlers/inline.py +++ b/md2gost/handlers/inline.py @@ -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) diff --git a/md2gost/handlers/math.py b/md2gost/handlers/math.py new file mode 100644 index 0000000..09d8d3a --- /dev/null +++ b/md2gost/handlers/math.py @@ -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\\]" diff --git a/md2gost/parser.py b/md2gost/parser.py index b9b001a..8e77f33 100644 --- a/md2gost/parser.py +++ b/md2gost/parser.py @@ -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() diff --git a/md2gost/render.py b/md2gost/render.py index f0c15fc..0097f4b 100644 --- a/md2gost/render.py +++ b/md2gost/render.py @@ -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]] = {