Skip to content

Commit cf3f363

Browse files
committed
Add render rules to renderers as bound methods
This allows for rules to also be inherited
1 parent 7edd0d5 commit cf3f363

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

markdown_it/extensions/footnote/index.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,8 @@ def render_footnote_caption(self, tokens, idx, options, env):
366366

367367

368368
def render_footnote_ref(self, tokens, idx, options, env):
369-
ident = self.rules["footnote_anchor_name"](self, tokens, idx, options, env)
370-
caption = self.rules["footnote_caption"](self, tokens, idx, options, env)
369+
ident = self.rules["footnote_anchor_name"](tokens, idx, options, env)
370+
caption = self.rules["footnote_caption"](tokens, idx, options, env)
371371
refid = ident
372372

373373
if tokens[idx].meta.get("subId", -1) > 0:
@@ -401,7 +401,7 @@ def render_footnote_block_close(self, tokens, idx, options, env):
401401

402402

403403
def render_footnote_open(self, tokens, idx, options, env):
404-
ident = self.rules["footnote_anchor_name"](self, tokens, idx, options, env)
404+
ident = self.rules["footnote_anchor_name"](tokens, idx, options, env)
405405

406406
if tokens[idx].meta.get("subId", -1) > 0:
407407
ident += ":" + tokens[idx].meta["subId"]
@@ -414,7 +414,7 @@ def render_footnote_close(self, tokens, idx, options, env):
414414

415415

416416
def render_footnote_anchor(self, tokens, idx, options, env):
417-
ident = self.rules["footnote_anchor_name"](self, tokens, idx, options, env)
417+
ident = self.rules["footnote_anchor_name"](tokens, idx, options, env)
418418

419419
if tokens[idx].meta["subId"] > 0:
420420
ident += ":" + str(tokens[idx].meta["subId"])

markdown_it/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def add_render_rule(self, name: str, function: Callable, fmt="html"):
187187
Only applied when ``renderer.__output__ == fmt``
188188
"""
189189
if self.renderer.__output__ == fmt:
190-
self.renderer.rules[name] = function
190+
self.renderer.rules[name] = function.__get__(self.renderer)
191191

192192
def use(self, plugin: Callable, *params) -> "MarkdownIt":
193193
"""Load specified plugin with given params into current parser instance. (chainable)

markdown_it/renderer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Renderer
55
copy of rules. Those can be rewritten with ease. Also, you can add new
66
rules if you create plugin and adds new token types.
77
"""
8-
8+
import inspect
99
from typing import List
1010

1111
from .common.utils import unescapeAll, escapeHtml
@@ -49,7 +49,7 @@ def strong_close(self, tokens, idx, options, env):
4949
def __init__(self):
5050
self.rules = {
5151
k: v
52-
for k, v in self.__class__.__dict__.items()
52+
for k, v in inspect.getmembers(self, predicate=inspect.ismethod)
5353
if not (k.startswith("render") or k.startswith("_"))
5454
}
5555

@@ -68,7 +68,7 @@ def render(self, tokens: List[Token], options, env) -> str:
6868
if token.type == "inline":
6969
result += self.renderInline(token.children, options, env)
7070
elif token.type in self.rules:
71-
result += self.rules[token.type](self, tokens, i, options, env)
71+
result += self.rules[token.type](tokens, i, options, env)
7272
else:
7373
result += self.renderToken(tokens, i, options, env)
7474

@@ -85,7 +85,7 @@ def renderInline(self, tokens: List[Token], options, env) -> str:
8585

8686
for i, token in enumerate(tokens):
8787
if token.type in self.rules:
88-
result += self.rules[token.type](self, tokens, i, options, env)
88+
result += self.rules[token.type](tokens, i, options, env)
8989
else:
9090
result += self.renderToken(tokens, i, options, env)
9191

0 commit comments

Comments
 (0)