Skip to content

Commit cbca541

Browse files
committed
‼️ Remove deorecated NestedTokens and nest_tokens
Already replaced by `SyntaxTreeNode`
1 parent ead951d commit cbca541

File tree

2 files changed

+1
-95
lines changed

2 files changed

+1
-95
lines changed

markdown_it/token.py

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -157,67 +157,3 @@ def from_dict(cls, dct: MutableMapping[str, Any]) -> Token:
157157
if token.children:
158158
token.children = [cls.from_dict(c) for c in token.children] # type: ignore[arg-type]
159159
return token
160-
161-
162-
@attr.s(slots=True)
163-
class NestedTokens:
164-
"""A class that closely resembles a Token,
165-
but for a an opening/closing Token pair, and their containing children.
166-
"""
167-
168-
opening: Token = attr.ib()
169-
closing: Token = attr.ib()
170-
children: list[Token | NestedTokens] = attr.ib(factory=list)
171-
172-
def __getattr__(self, name):
173-
return getattr(self.opening, name)
174-
175-
def attrGet(self, name: str) -> None | str | int | float:
176-
"""Get the value of attribute `name`, or null if it does not exist."""
177-
return self.opening.attrGet(name)
178-
179-
180-
def nest_tokens(tokens: list[Token]) -> list[Token | NestedTokens]:
181-
"""Convert the token stream to a list of tokens and nested tokens.
182-
183-
``NestedTokens`` contain the open and close tokens and a list of children
184-
of all tokens in between (recursively nested)
185-
"""
186-
warnings.warn(
187-
"`markdown_it.token.nest_tokens` and `markdown_it.token.NestedTokens`"
188-
" are deprecated. Please migrate to `markdown_it.tree.SyntaxTreeNode`",
189-
DeprecationWarning,
190-
)
191-
192-
output: list[Token | NestedTokens] = []
193-
194-
tokens = list(reversed(tokens))
195-
while tokens:
196-
token = tokens.pop()
197-
198-
if token.nesting == 0:
199-
token = token.copy()
200-
output.append(token)
201-
if token.children:
202-
# Ignore type checkers because `nest_tokens` doesn't respect
203-
# typing of `Token.children`. We add `NestedTokens` into a
204-
# `list[Token]` here.
205-
token.children = nest_tokens(token.children) # type: ignore
206-
continue
207-
208-
assert token.nesting == 1, token.nesting
209-
210-
nested_tokens = [token]
211-
nesting = 1
212-
while tokens and nesting != 0:
213-
token = tokens.pop()
214-
nested_tokens.append(token)
215-
nesting += token.nesting
216-
if nesting != 0:
217-
raise ValueError(f"unclosed tokens starting {nested_tokens[0]}")
218-
219-
child = NestedTokens(nested_tokens[0], nested_tokens[-1])
220-
output.append(child)
221-
child.children = nest_tokens(nested_tokens[1:-1])
222-
223-
return output

tests/test_api/test_token.py

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import warnings
22

3-
from markdown_it.token import NestedTokens, Token, nest_tokens
3+
from markdown_it.token import Token
44

55

66
def test_token():
@@ -36,33 +36,3 @@ def test_token():
3636
def test_serialization():
3737
token = Token("name", "tag", 0, children=[Token("other", "tag2", 0)])
3838
assert token == Token.from_dict(token.as_dict())
39-
40-
41-
def test_nest_tokens():
42-
tokens = nest_tokens(
43-
[
44-
Token("start", "", 0),
45-
Token("open", "", 1),
46-
Token("open_inner", "", 1),
47-
Token("inner", "", 0),
48-
Token("close_inner", "", -1),
49-
Token("close", "", -1),
50-
Token("end", "", 0),
51-
]
52-
)
53-
assert [t.type for t in tokens] == ["start", "open", "end"]
54-
assert isinstance(tokens[0], Token)
55-
assert isinstance(tokens[1], NestedTokens)
56-
assert isinstance(tokens[2], Token)
57-
58-
nested = tokens[1]
59-
assert nested.opening.type == "open"
60-
assert nested.closing.type == "close"
61-
assert len(nested.children) == 1
62-
assert nested.children[0].type == "open_inner"
63-
64-
nested2 = nested.children[0]
65-
assert nested2.opening.type == "open_inner"
66-
assert nested2.closing.type == "close_inner"
67-
assert len(nested2.children) == 1
68-
assert nested2.children[0].type == "inner"

0 commit comments

Comments
 (0)