11"""An extension to capture amsmath latex environments."""
2+ from __future__ import annotations
3+
24import re
3- from typing import Callable , Optional
5+ from typing import TYPE_CHECKING , Callable , Optional , Sequence
46
57from markdown_it import MarkdownIt
68from markdown_it .common .utils import escapeHtml
79from markdown_it .rules_block import StateBlock
810
911from mdit_py_plugins .utils import is_code_block
1012
13+ if TYPE_CHECKING :
14+ from markdown_it .renderer import RendererProtocol
15+ from markdown_it .token import Token
16+ from markdown_it .utils import EnvType , OptionsDict
17+
1118# Taken from amsmath version 2.1
1219# http://anorien.csc.warwick.ac.uk/mirrors/CTAN/macros/latex/required/amsmath/amsldoc.pdf
1320ENVIRONMENTS = [
4956RE_OPEN = re .compile (r"\\begin\{(" + "|" .join (ENVIRONMENTS ) + r")([\*]?)\}" )
5057
5158
52- def amsmath_plugin (md : MarkdownIt , * , renderer : Optional [Callable [[str ], str ]] = None ):
59+ def amsmath_plugin (
60+ md : MarkdownIt , * , renderer : Optional [Callable [[str ], str ]] = None
61+ ) -> None :
5362 """Parses TeX math equations, without any surrounding delimiters,
5463 only for top-level `amsmath <https://ctan.org/pkg/amsmath>`__ environments:
5564
@@ -72,14 +81,20 @@ def amsmath_plugin(md: MarkdownIt, *, renderer: Optional[Callable[[str], str]] =
7281
7382 _renderer = (lambda content : escapeHtml (content )) if renderer is None else renderer
7483
75- def render_amsmath_block (self , tokens , idx , options , env ):
84+ def render_amsmath_block (
85+ self : RendererProtocol ,
86+ tokens : Sequence [Token ],
87+ idx : int ,
88+ options : OptionsDict ,
89+ env : EnvType ,
90+ ) -> str :
7691 content = _renderer (str (tokens [idx ].content ))
7792 return f'<div class="math amsmath">\n { content } \n </div>\n '
7893
7994 md .add_render_rule ("amsmath" , render_amsmath_block )
8095
8196
82- def match_environment (string ) :
97+ def match_environment (string : str ) -> None | tuple [ str , str , int ] :
8398 match_open = RE_OPEN .match (string )
8499 if not match_open :
85100 return None
@@ -93,7 +108,9 @@ def match_environment(string):
93108 return (environment , numbered , match_close .end ())
94109
95110
96- def amsmath_block (state : StateBlock , startLine : int , endLine : int , silent : bool ):
111+ def amsmath_block (
112+ state : StateBlock , startLine : int , endLine : int , silent : bool
113+ ) -> bool :
97114 if is_code_block (state , startLine ):
98115 return False
99116
0 commit comments