Skip to content

Commit

Permalink
Merge branch 'master' into catch-up
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Dec 29, 2020
2 parents 5126e02 + 8290faa commit 0ba72e6
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 19 deletions.
2 changes: 1 addition & 1 deletion markdown_it/common/normalize_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def normalizeLink(url):
# return quote(urlunparse(parsed))


def unescape_unquote(x):
def unescape_unquote(x: str) -> str:
return unquote(unescape_string(x))


Expand Down
6 changes: 3 additions & 3 deletions markdown_it/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def __init__(
self.renderer = renderer_cls(self)

self.utils = utils
self.helpers = helpers
self.options: Dict[str, Any] = {}
self.helpers: Any = helpers
self.options = AttrDict()
self.configure(config)

self.linkify = linkify_it.LinkifyIt() if linkify_it else None
Expand All @@ -59,7 +59,7 @@ def __getitem__(self, name):
"renderer": self.renderer,
}[name]

def set(self, options):
def set(self, options: AttrDict) -> None:
"""Set parser options (in the same format as in constructor).
Probably, you will never need it, but you can change options after constructor call.
Expand Down
27 changes: 25 additions & 2 deletions markdown_it/ruler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,39 @@ class Ruler
rules control use [[MarkdownIt.disable]], [[MarkdownIt.enable]] and
[[MarkdownIt.use]].
"""
from typing import Callable, Dict, Iterable, List, Optional, Union
from typing import (
Callable,
Dict,
Iterable,
List,
Optional,
TYPE_CHECKING,
Union,
)
import attr

from markdown_it.utils import AttrDict

if TYPE_CHECKING:
from markdown_it import MarkdownIt


class StateBase:
def __init__(self, src: str, md, env):
def __init__(self, src: str, md: "MarkdownIt", env: AttrDict):
self.srcCharCode: List[int] = []
self.src = src
self.env = env
self.md = md

@property
def src(self):
return self._src

@src.setter
def src(self, value):
self._src = value
self.srcCharCode = [ord(c) for c in self.src] if self.src is not None else []


# The first positional arg is always a subtype of `StateBase`. Other
# arguments may or may not exist, based on the rule's type (block,
Expand Down
4 changes: 2 additions & 2 deletions markdown_it/rules_block/state_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def __init__(
srcCharCode: Optional[List[int]] = None,
):

self.src = src
if srcCharCode is not None:
self._src = src
self.srcCharCode = srcCharCode
else:
self.srcCharCode = [ord(c) for c in src] if src is not None else []
self.src = src

# link to parser instance
self.md = md
Expand Down
2 changes: 2 additions & 0 deletions markdown_it/rules_core/inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ def inline(state: StateCore) -> None:
"""Parse inlines"""
for token in state.tokens:
if token.type == "inline":
if token.children is None:
token.children = []
state.md.inline.parse(token.content, state.md, state.env, token.children)
15 changes: 12 additions & 3 deletions markdown_it/rules_core/state_core.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from typing import List
from typing import List, Optional, TYPE_CHECKING

from ..utils import AttrDict
from ..token import Token
from ..ruler import StateBase

if TYPE_CHECKING:
from markdown_it import MarkdownIt


class StateCore(StateBase):
def __init__(self, src: str, md, env, tokens=None):
def __init__(
self,
src: str,
md: "MarkdownIt",
env: AttrDict,
tokens: Optional[List[Token]] = None,
):
self.src = src
self.srcCharCode = [ord(c) for c in src] if src is not None else []
self.md = md # link to parser instance
self.env = env
self.tokens: List[Token] = tokens or []
Expand Down
3 changes: 2 additions & 1 deletion markdown_it/rules_inline/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

def image(state: StateInline, silent: bool):

tokens: List[Token] = []
label = None
href = ""
oldPos = state.pos
Expand Down Expand Up @@ -134,11 +133,13 @@ def image(state: StateInline, silent: bool):
if not silent:
content = state.src[labelStart:labelEnd]

tokens: List[Token] = []
state.md.inline.parse(content, state.md, state.env, tokens)

token = state.push("image", "img", 0)
token.attrs = [["src", href], ["alt", ""]]
token.children = tokens or None
token.content = content

if title:
token.attrs.append(["title", title])
Expand Down
11 changes: 8 additions & 3 deletions markdown_it/rules_inline/state_inline.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from collections import namedtuple
from typing import Dict, List, Optional
from typing import Dict, List, Optional, TYPE_CHECKING

import attr

from ..utils import AttrDict
from ..token import Token
from ..ruler import StateBase
from ..common.utils import isWhiteSpace, isPunctChar, isMdAsciiPunct

if TYPE_CHECKING:
from markdown_it import MarkdownIt


@attr.s(slots=True)
class Delimiter:
Expand Down Expand Up @@ -43,9 +47,10 @@ class Delimiter:


class StateInline(StateBase):
def __init__(self, src: str, md, env, outTokens: List[Token]):
def __init__(
self, src: str, md: "MarkdownIt", env: AttrDict, outTokens: List[Token]
):
self.src = src
self.srcCharCode = [ord(c) for c in src] if src is not None else []
self.env = env
self.md = md
self.tokens = outTokens
Expand Down
2 changes: 1 addition & 1 deletion markdown_it/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def attrJoin(self, name, value):
else:
self.attrs[idx][1] = self.attrs[idx][1] + " " + value

def copy(self):
def copy(self) -> "Token":
"""Return a shallow copy of the instance."""
return attr.evolve(self)

Expand Down
4 changes: 2 additions & 2 deletions markdown_it/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, List, Union


if TYPE_CHECKING:
Expand All @@ -17,7 +17,7 @@ def __init__(self, *args, **kwargs):
self[key] = AttrDict(item)


def read_fixture_file(path):
def read_fixture_file(path: Union[str, Path]) -> List[list]:
text = Path(path).read_text(encoding="utf-8")
tests = []
section = 0
Expand Down
2 changes: 1 addition & 1 deletion tests/test_port/test_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def test_store_labels():
hidden=False,
)
],
content="",
content="a",
markup="",
info="",
meta={"label": "A"},
Expand Down

0 comments on commit 0ba72e6

Please sign in to comment.