Skip to content

Commit

Permalink
♻️ REFACTOR: Move internal Rule/Delimiter to dataclass
Browse files Browse the repository at this point in the history
In order to remove attr dependency
  • Loading branch information
chrisjsewell committed Apr 14, 2022
1 parent 063268b commit e82d06e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
13 changes: 6 additions & 7 deletions markdown_it/ruler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ class Ruler
from __future__ import annotations

from collections.abc import Callable, Iterable, MutableMapping
from dataclasses import dataclass, field
from typing import TYPE_CHECKING

import attr

if TYPE_CHECKING:
from markdown_it import MarkdownIt

Expand Down Expand Up @@ -51,12 +50,12 @@ def src(self, value: str) -> None:
RuleFunc = Callable


@attr.s(slots=True)
@dataclass()
class Rule:
name: str = attr.ib()
enabled: bool = attr.ib()
fn: RuleFunc = attr.ib(repr=False)
alt: list[str] = attr.ib()
name: str
enabled: bool
fn: RuleFunc = field(repr=False)
alt: list[str]


class Ruler:
Expand Down
21 changes: 10 additions & 11 deletions markdown_it/rules_inline/state_inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

from collections import namedtuple
from collections.abc import MutableMapping
from dataclasses import dataclass
from typing import TYPE_CHECKING

import attr

from ..common.utils import isMdAsciiPunct, isPunctChar, isWhiteSpace
from ..ruler import StateBase
from ..token import Token
Expand All @@ -14,35 +13,35 @@
from markdown_it import MarkdownIt


@attr.s(slots=True)
@dataclass()
class Delimiter:
# Char code of the starting marker (number).
marker: int = attr.ib()
marker: int

# Total length of these series of delimiters.
length: int = attr.ib()
length: int

# An amount of characters before this one that's equivalent to
# current one. In plain English: if this delimiter does not open
# an emphasis, neither do previous `jump` characters.
#
# Used to skip sequences like "*****" in one step, for 1st asterisk
# value will be 0, for 2nd it's 1 and so on.
jump: int = attr.ib()
jump: int

# A position of the token this delimiter corresponds to.
token: int = attr.ib()
token: int

# If this delimiter is matched as a valid opener, `end` will be
# equal to its position, otherwise it's `-1`.
end: int = attr.ib()
end: int

# Boolean flags that determine if this delimiter could open or close
# an emphasis.
open: bool = attr.ib()
close: bool = attr.ib()
open: bool
close: bool

level: bool = attr.ib(default=None)
level: bool | None = None


Scanned = namedtuple("Scanned", ["can_open", "can_close", "length"])
Expand Down

0 comments on commit e82d06e

Please sign in to comment.