Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ REFACTOR: Move internal Rule/Delimiter to dataclass #211

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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